10 int retval = EXIT_FAILURE;
13 int width = 0, height = 0;
15 unsigned char nComponent = 3, *compImg = NULL, *decompImg = NULL;
16 tjhandle jpegDecompressor = NULL;
23 if ((fptr = fopen(path,
"rb")) == NULL)
25 if (fseek(fptr, 0, SEEK_END) < 0 || ((imgSize = ftell(fptr)) < 0) || fseek(fptr, 0, SEEK_SET) < 0)
31 compImg = (
unsigned char *)malloc(imgSize *
sizeof(
unsigned char));
32 if (!compImg || (fread(compImg, imgSize, 1, fptr) < 1))
36 jpegDecompressor = tjInitDecompress();
37 if (!jpegDecompressor)
40 retval = tjDecompressHeader(jpegDecompressor, compImg, imgSize, &width, &height);
43 decompImg = (
unsigned char *)malloc((
unsigned long long)width * height * nComponent *
sizeof(
unsigned char));
46 retval = tjDecompress(jpegDecompressor, compImg, imgSize, decompImg, width, 0, height, nComponent, TJFLAG_FASTDCT);
54 img->
data = decompImg;
58 tjDestroy(jpegDecompressor);
67int saveImage(
const char *path,
ImageData *img,
enum TJPF pixelFormat,
enum TJSAMP samplingFormat,
int jpegQuality)
70 int retval = EXIT_FAILURE;
73 long unsigned int outSize = 0;
74 unsigned char *compImg = NULL;
75 tjhandle jpegCompressor = NULL;
82 jpegCompressor = tjInitCompress();
86 retval = tjCompress2(jpegCompressor, img->
data, img->
width, 0, img->
height, pixelFormat, &compImg, &outSize,
87 samplingFormat, jpegQuality, TJFLAG_FASTDCT);
92 retval = EXIT_FAILURE;
93 if ((fptr = fopen(path,
"wb")) == NULL)
95 if (fwrite(compImg, outSize, 1, fptr) < 1)
100 retval = EXIT_SUCCESS;
104 tjDestroy(jpegCompressor);
int initImageData(ImageData *img, int width, int height, int nComponents)
Allocates memory for image.
int saveImage(const char *path, ImageData *img, enum TJPF pixelFormat, enum TJSAMP samplingFormat, int jpegQuality)
Save image data to an output file.