kaleidoscope 1.4.0
 
Loading...
Searching...
No Matches
jpeg-utils.c File Reference
#include "jpeg-utils/jpeg-utils.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for jpeg-utils.c:

Go to the source code of this file.

Functions

int readImage (const char *path, ImageData *img)
 Get image data from an input file.
 
int saveImage (const char *path, ImageData *img, enum TJPF pixelFormat, enum TJSAMP samplingFormat, int jpegQuality)
 Save image data to an output file.
 
int initImageData (ImageData *img, int width, int height, int nComponents)
 Allocates memory for image.
 
void deInitImageData (ImageData *img)
 Free memory allocated by read image.
 

Function Documentation

◆ deInitImageData()

void deInitImageData ( ImageData * img)

Free memory allocated by read image.

Parameters
[in]imgImage data

Definition at line 122 of file jpeg-utils.c.

123{
124 if (img)
125 {
126 free(img->data);
127 img->data = NULL;
128 }
129}
unsigned char * data
Definition jpeg-utils.h:14
Here is the caller graph for this function:

◆ initImageData()

int initImageData ( ImageData * img,
int width,
int height,
int nComponents )

Allocates memory for image.

Parameters
[in]imgImage data
[in]widthWidth of image
[in]heightHeight of image
[in]nComponentsNumber of components
Returns
int Returns 0 on success

Definition at line 110 of file jpeg-utils.c.

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}
unsigned char nComponents
Definition jpeg-utils.h:13
Here is the caller graph for this function:

◆ readImage()

int readImage ( const char * path,
ImageData * img )

Get image data from an input file.

Parameters
[in]pathPath to input image file
[out]imgImage data
Returns
int Returns 0 on success

Definition at line 7 of file jpeg-utils.c.

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}
Here is the caller graph for this function:

◆ saveImage()

int saveImage ( const char * path,
ImageData * img,
enum TJPF pixelFormat,
enum TJSAMP samplingFormat,
int jpegQuality )

Save image data to an output file.

Parameters
[in]pathPath to output image file
[in]imgImage data
[in]pixelFormatPixel format
[in]samplingFormatSampling format
[in]jpegQualityQuality of the output image
Returns
int Returns 0 on success

Definition at line 67 of file jpeg-utils.c.

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}
Here is the caller graph for this function: