kaleidoscope 1.4.0
 
Loading...
Searching...
No Matches
jpeg-utils.c
Go to the documentation of this file.
2
3#include <assert.h>
4#include <stdio.h>
5#include <stdlib.h>
6
7int readImage(const char *path, ImageData *img)
8{
9 // Init variables
10 int retval = EXIT_FAILURE;
11
12 FILE *fptr = NULL;
13 int width = 0, height = 0;
14 long imgSize = 0;
15 unsigned char nComponent = 3, *compImg = NULL, *decompImg = NULL;
16 tjhandle jpegDecompressor = NULL;
17
18 // Check inputs
19 assert(path);
20 assert(img);
21
22 // Find file and get size
23 if ((fptr = fopen(path, "rb")) == NULL)
24 goto cleanup;
25 if (fseek(fptr, 0, SEEK_END) < 0 || ((imgSize = ftell(fptr)) < 0) || fseek(fptr, 0, SEEK_SET) < 0)
26 goto cleanup;
27 if (imgSize == 0)
28 goto cleanup;
29
30 // Read file
31 compImg = (unsigned char *)malloc(imgSize * sizeof(unsigned char));
32 if (!compImg || (fread(compImg, imgSize, 1, fptr) < 1))
33 goto cleanup;
34
35 // Decompress
36 jpegDecompressor = tjInitDecompress();
37 if (!jpegDecompressor)
38 goto cleanup;
39
40 retval = tjDecompressHeader(jpegDecompressor, compImg, imgSize, &width, &height);
41 if (retval < 0)
42 goto cleanup;
43 decompImg = (unsigned char *)malloc((unsigned long long)width * height * nComponent * sizeof(unsigned char));
44 if (!decompImg)
45 goto cleanup;
46 retval = tjDecompress(jpegDecompressor, compImg, imgSize, decompImg, width, 0, height, nComponent, TJFLAG_FASTDCT);
47 if (retval < 0)
48 goto cleanup;
49
50 // Set output
51 img->width = width;
52 img->height = height;
53 img->nComponents = nComponent;
54 img->data = decompImg;
55 decompImg = NULL;
56
57cleanup:
58 tjDestroy(jpegDecompressor);
59 fclose(fptr);
60
61 free(compImg);
62 free(decompImg);
63
64 return retval;
65}
66
67int saveImage(const char *path, ImageData *img, enum TJPF pixelFormat, enum TJSAMP samplingFormat, int jpegQuality)
68{
69 // Init variables
70 int retval = EXIT_FAILURE;
71
72 FILE *fptr = NULL;
73 long unsigned int outSize = 0;
74 unsigned char *compImg = NULL;
75 tjhandle jpegCompressor = NULL;
76
77 // Check inputs
78 assert(path);
79 assert(img);
80
81 // Compress
82 jpegCompressor = tjInitCompress();
83 if (!jpegCompressor)
84 goto cleanup;
85
86 retval = tjCompress2(jpegCompressor, img->data, img->width, 0, img->height, pixelFormat, &compImg, &outSize,
87 samplingFormat, jpegQuality, TJFLAG_FASTDCT);
88 if (retval < 0)
89 goto cleanup;
90
91 // Write file
92 retval = EXIT_FAILURE; // To simplify if checks
93 if ((fptr = fopen(path, "wb")) == NULL)
94 goto cleanup;
95 if (fwrite(compImg, outSize, 1, fptr) < 1)
96 goto cleanup;
97 if (fflush(fptr))
98 goto cleanup;
99
100 retval = EXIT_SUCCESS;
101
102cleanup:
103 fclose(fptr);
104 tjDestroy(jpegCompressor);
105 tjFree(compImg);
106
107 return retval;
108}
109
110int initImageData(ImageData *img, int width, int height, int nComponents)
111{
112 img->data = (unsigned char *)malloc((unsigned long long)width * height * nComponents);
113 if (!img->data)
114 return EXIT_FAILURE;
115
116 img->height = height;
117 img->nComponents = nComponents;
118 img->width = width;
119 return EXIT_SUCCESS;
120}
121
123{
124 if (img)
125 {
126 free(img->data);
127 img->data = NULL;
128 }
129}
int initImageData(ImageData *img, int width, int height, int nComponents)
Allocates memory for image.
Definition jpeg-utils.c:110
int saveImage(const char *path, ImageData *img, enum TJPF pixelFormat, enum TJSAMP samplingFormat, int jpegQuality)
Save image data to an output file.
Definition jpeg-utils.c:67
void deInitImageData(ImageData *img)
Free memory allocated by read image.
Definition jpeg-utils.c:122
int readImage(const char *path, ImageData *img)
Get image data from an input file.
Definition jpeg-utils.c:7
Data struct for images.
Definition jpeg-utils.h:10
unsigned char nComponents
Definition jpeg-utils.h:13
unsigned char * data
Definition jpeg-utils.h:14