Get image data from an input file.
8{
9
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
19 assert(path);
20 assert(img);
21
22
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
31 compImg = (unsigned char *)malloc(imgSize * sizeof(unsigned char));
32 if (!compImg || (fread(compImg, imgSize, 1, fptr) < 1))
33 goto cleanup;
34
35
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
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}