1 /*
2 * Copyright (C)2009-2014, 2017-2018 D. R. Commander. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * This program tests the various code paths in the TurboJPEG C Wrapper
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include "tjutil.h"
38 #include "turbojpeg.h"
39 #include "md5/md5.h"
40 #include "cmyk.h"
41 #ifdef _WIN32
42 #include <time.h>
43 #define random() rand()
44 #else
45 #include <unistd.h>
46 #endif
47
48
usage(char * progName)49 void usage(char *progName)
50 {
51 printf("\nUSAGE: %s [options]\n\n", progName);
52 printf("Options:\n");
53 printf("-yuv = test YUV encoding/decoding support\n");
54 printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n");
55 printf(" 4-byte boundary\n");
56 printf("-alloc = test automatic buffer allocation\n");
57 printf("-bmp = tjLoadImage()/tjSaveImage() unit test\n\n");
58 exit(1);
59 }
60
61
62 #define THROW_TJ() { \
63 printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
64 BAILOUT() \
65 }
66 #define TRY_TJ(f) { if ((f) == -1) THROW_TJ(); }
67 #define THROW(m) { printf("ERROR: %s\n", m); BAILOUT() }
68 #define THROW_MD5(filename, md5sum, ref) { \
69 printf("\n%s has an MD5 sum of %s.\n Should be %s.\n", filename, md5sum, \
70 ref); \
71 BAILOUT() \
72 }
73
74 const char *subNameLong[TJ_NUMSAMP] = {
75 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
76 };
77 const char *subName[TJ_NUMSAMP] = {
78 "444", "422", "420", "GRAY", "440", "411"
79 };
80
81 const char *pixFormatStr[TJ_NUMPF] = {
82 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale",
83 "RGBA", "BGRA", "ABGR", "ARGB", "CMYK"
84 };
85
86 const int _3byteFormats[] = { TJPF_RGB, TJPF_BGR };
87 const int _4byteFormats[] = {
88 TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, TJPF_CMYK
89 };
90 const int _onlyGray[] = { TJPF_GRAY };
91 const int _onlyRGB[] = { TJPF_RGB };
92
93 int doYUV = 0, alloc = 0, pad = 4;
94
95 int exitStatus = 0;
96 #define BAILOUT() { exitStatus = -1; goto bailout; }
97
98
initBuf(unsigned char * buf,int w,int h,int pf,int flags)99 void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
100 {
101 int roffset = tjRedOffset[pf];
102 int goffset = tjGreenOffset[pf];
103 int boffset = tjBlueOffset[pf];
104 int ps = tjPixelSize[pf];
105 int index, row, col, halfway = 16;
106
107 if (pf == TJPF_GRAY) {
108 memset(buf, 0, w * h * ps);
109 for (row = 0; row < h; row++) {
110 for (col = 0; col < w; col++) {
111 if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
112 else index = row * w + col;
113 if (((row / 8) + (col / 8)) % 2 == 0)
114 buf[index] = (row < halfway) ? 255 : 0;
115 else buf[index] = (row < halfway) ? 76 : 226;
116 }
117 }
118 } else if (pf == TJPF_CMYK) {
119 memset(buf, 255, w * h * ps);
120 for (row = 0; row < h; row++) {
121 for (col = 0; col < w; col++) {
122 if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
123 else index = row * w + col;
124 if (((row / 8) + (col / 8)) % 2 == 0) {
125 if (row >= halfway) buf[index * ps + 3] = 0;
126 } else {
127 buf[index * ps + 2] = 0;
128 if (row < halfway) buf[index * ps + 1] = 0;
129 }
130 }
131 }
132 } else {
133 memset(buf, 0, w * h * ps);
134 for (row = 0; row < h; row++) {
135 for (col = 0; col < w; col++) {
136 if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
137 else index = row * w + col;
138 if (((row / 8) + (col / 8)) % 2 == 0) {
139 if (row < halfway) {
140 buf[index * ps + roffset] = 255;
141 buf[index * ps + goffset] = 255;
142 buf[index * ps + boffset] = 255;
143 }
144 } else {
145 buf[index * ps + roffset] = 255;
146 if (row >= halfway) buf[index * ps + goffset] = 255;
147 }
148 }
149 }
150 }
151 }
152
153
154 #define CHECKVAL(v, cv) { \
155 if (v < cv - 1 || v > cv + 1) { \
156 printf("\nComp. %s at %d,%d should be %d, not %d\n", #v, row, col, cv, \
157 v); \
158 retval = 0; exitStatus = -1; goto bailout; \
159 } \
160 }
161
162 #define CHECKVAL0(v) { \
163 if (v > 1) { \
164 printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
165 retval = 0; exitStatus = -1; goto bailout; \
166 } \
167 }
168
169 #define CHECKVAL255(v) { \
170 if (v < 254) { \
171 printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
172 retval = 0; exitStatus = -1; goto bailout; \
173 } \
174 }
175
176
checkBuf(unsigned char * buf,int w,int h,int pf,int subsamp,tjscalingfactor sf,int flags)177 int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
178 tjscalingfactor sf, int flags)
179 {
180 int roffset = tjRedOffset[pf];
181 int goffset = tjGreenOffset[pf];
182 int boffset = tjBlueOffset[pf];
183 int aoffset = tjAlphaOffset[pf];
184 int ps = tjPixelSize[pf];
185 int index, row, col, retval = 1;
186 int halfway = 16 * sf.num / sf.denom;
187 int blocksize = 8 * sf.num / sf.denom;
188
189 if (pf == TJPF_GRAY) roffset = goffset = boffset = 0;
190
191 if (pf == TJPF_CMYK) {
192 for (row = 0; row < h; row++) {
193 for (col = 0; col < w; col++) {
194 unsigned char c, m, y, k;
195
196 if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
197 else index = row * w + col;
198 c = buf[index * ps];
199 m = buf[index * ps + 1];
200 y = buf[index * ps + 2];
201 k = buf[index * ps + 3];
202 if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
203 CHECKVAL255(c); CHECKVAL255(m); CHECKVAL255(y);
204 if (row < halfway) CHECKVAL255(k)
205 else CHECKVAL0(k)
206 } else {
207 CHECKVAL255(c); CHECKVAL0(y); CHECKVAL255(k);
208 if (row < halfway) CHECKVAL0(m)
209 else CHECKVAL255(m)
210 }
211 }
212 }
213 return 1;
214 }
215
216 for (row = 0; row < h; row++) {
217 for (col = 0; col < w; col++) {
218 unsigned char r, g, b, a;
219
220 if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
221 else index = row * w + col;
222 r = buf[index * ps + roffset];
223 g = buf[index * ps + goffset];
224 b = buf[index * ps + boffset];
225 a = aoffset >= 0 ? buf[index * ps + aoffset] : 0xFF;
226 if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
227 if (row < halfway) {
228 CHECKVAL255(r); CHECKVAL255(g); CHECKVAL255(b);
229 } else {
230 CHECKVAL0(r); CHECKVAL0(g); CHECKVAL0(b);
231 }
232 } else {
233 if (subsamp == TJSAMP_GRAY) {
234 if (row < halfway) {
235 CHECKVAL(r, 76); CHECKVAL(g, 76); CHECKVAL(b, 76);
236 } else {
237 CHECKVAL(r, 226); CHECKVAL(g, 226); CHECKVAL(b, 226);
238 }
239 } else {
240 if (row < halfway) {
241 CHECKVAL255(r); CHECKVAL0(g); CHECKVAL0(b);
242 } else {
243 CHECKVAL255(r); CHECKVAL255(g); CHECKVAL0(b);
244 }
245 }
246 }
247 CHECKVAL255(a);
248 }
249 }
250
251 bailout:
252 if (retval == 0) {
253 for (row = 0; row < h; row++) {
254 for (col = 0; col < w; col++) {
255 if (pf == TJPF_CMYK)
256 printf("%.3d/%.3d/%.3d/%.3d ", buf[(row * w + col) * ps],
257 buf[(row * w + col) * ps + 1], buf[(row * w + col) * ps + 2],
258 buf[(row * w + col) * ps + 3]);
259 else
260 printf("%.3d/%.3d/%.3d ", buf[(row * w + col) * ps + roffset],
261 buf[(row * w + col) * ps + goffset],
262 buf[(row * w + col) * ps + boffset]);
263 }
264 printf("\n");
265 }
266 }
267 return retval;
268 }
269
270
271 #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1)))
272
checkBufYUV(unsigned char * buf,int w,int h,int subsamp,tjscalingfactor sf)273 int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
274 tjscalingfactor sf)
275 {
276 int row, col;
277 int hsf = tjMCUWidth[subsamp] / 8, vsf = tjMCUHeight[subsamp] / 8;
278 int pw = PAD(w, hsf), ph = PAD(h, vsf);
279 int cw = pw / hsf, ch = ph / vsf;
280 int ypitch = PAD(pw, pad), uvpitch = PAD(cw, pad);
281 int retval = 1;
282 int halfway = 16 * sf.num / sf.denom;
283 int blocksize = 8 * sf.num / sf.denom;
284
285 for (row = 0; row < ph; row++) {
286 for (col = 0; col < pw; col++) {
287 unsigned char y = buf[ypitch * row + col];
288
289 if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
290 if (row < halfway) CHECKVAL255(y)
291 else CHECKVAL0(y);
292 } else {
293 if (row < halfway) CHECKVAL(y, 76)
294 else CHECKVAL(y, 226);
295 }
296 }
297 }
298 if (subsamp != TJSAMP_GRAY) {
299 int halfway = 16 / vsf * sf.num / sf.denom;
300
301 for (row = 0; row < ch; row++) {
302 for (col = 0; col < cw; col++) {
303 unsigned char u = buf[ypitch * ph + (uvpitch * row + col)],
304 v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)];
305
306 if (((row * vsf / blocksize) + (col * hsf / blocksize)) % 2 == 0) {
307 CHECKVAL(u, 128); CHECKVAL(v, 128);
308 } else {
309 if (row < halfway) {
310 CHECKVAL(u, 85); CHECKVAL255(v);
311 } else {
312 CHECKVAL0(u); CHECKVAL(v, 149);
313 }
314 }
315 }
316 }
317 }
318
319 bailout:
320 if (retval == 0) {
321 for (row = 0; row < ph; row++) {
322 for (col = 0; col < pw; col++)
323 printf("%.3d ", buf[ypitch * row + col]);
324 printf("\n");
325 }
326 printf("\n");
327 for (row = 0; row < ch; row++) {
328 for (col = 0; col < cw; col++)
329 printf("%.3d ", buf[ypitch * ph + (uvpitch * row + col)]);
330 printf("\n");
331 }
332 printf("\n");
333 for (row = 0; row < ch; row++) {
334 for (col = 0; col < cw; col++)
335 printf("%.3d ",
336 buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)]);
337 printf("\n");
338 }
339 }
340
341 return retval;
342 }
343
344
writeJPEG(unsigned char * jpegBuf,unsigned long jpegSize,char * filename)345 void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
346 {
347 FILE *file = fopen(filename, "wb");
348
349 if (!file || fwrite(jpegBuf, jpegSize, 1, file) != 1) {
350 printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
351 BAILOUT()
352 }
353
354 bailout:
355 if (file) fclose(file);
356 }
357
358
compTest(tjhandle handle,unsigned char ** dstBuf,unsigned long * dstSize,int w,int h,int pf,char * basename,int subsamp,int jpegQual,int flags)359 void compTest(tjhandle handle, unsigned char **dstBuf, unsigned long *dstSize,
360 int w, int h, int pf, char *basename, int subsamp, int jpegQual,
361 int flags)
362 {
363 char tempStr[1024];
364 unsigned char *srcBuf = NULL, *yuvBuf = NULL;
365 const char *pfStr = pixFormatStr[pf];
366 const char *buStrLong =
367 (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down ";
368 const char *buStr = (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD";
369
370 if ((srcBuf = (unsigned char *)malloc(w * h * tjPixelSize[pf])) == NULL)
371 THROW("Memory allocation failure");
372 initBuf(srcBuf, w, h, pf, flags);
373
374 if (*dstBuf && *dstSize > 0) memset(*dstBuf, 0, *dstSize);
375
376 if (!alloc) flags |= TJFLAG_NOREALLOC;
377 if (doYUV) {
378 unsigned long yuvSize = tjBufSizeYUV2(w, pad, h, subsamp);
379 tjscalingfactor sf = { 1, 1 };
380 tjhandle handle2 = tjInitCompress();
381
382 if (!handle2) THROW_TJ();
383
384 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
385 THROW("Memory allocation failure");
386 memset(yuvBuf, 0, yuvSize);
387
388 printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]);
389 TRY_TJ(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp,
390 flags));
391 tjDestroy(handle2);
392 if (checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n");
393 else printf("FAILED!\n");
394
395 printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
396 jpegQual);
397 TRY_TJ(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf,
398 dstSize, jpegQual, flags));
399 } else {
400 printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
401 jpegQual);
402 TRY_TJ(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
403 jpegQual, flags));
404 }
405
406 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
407 subName[subsamp], jpegQual);
408 writeJPEG(*dstBuf, *dstSize, tempStr);
409 printf("Done.\n Result in %s\n", tempStr);
410
411 bailout:
412 if (yuvBuf) free(yuvBuf);
413 if (srcBuf) free(srcBuf);
414 }
415
416
_decompTest(tjhandle handle,unsigned char * jpegBuf,unsigned long jpegSize,int w,int h,int pf,char * basename,int subsamp,int flags,tjscalingfactor sf)417 void _decompTest(tjhandle handle, unsigned char *jpegBuf,
418 unsigned long jpegSize, int w, int h, int pf, char *basename,
419 int subsamp, int flags, tjscalingfactor sf)
420 {
421 unsigned char *dstBuf = NULL, *yuvBuf = NULL;
422 int _hdrw = 0, _hdrh = 0, _hdrsubsamp = -1;
423 int scaledWidth = TJSCALED(w, sf);
424 int scaledHeight = TJSCALED(h, sf);
425 unsigned long dstSize = 0;
426
427 TRY_TJ(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
428 &_hdrsubsamp));
429 if (_hdrw != w || _hdrh != h || _hdrsubsamp != subsamp)
430 THROW("Incorrect JPEG header");
431
432 dstSize = scaledWidth * scaledHeight * tjPixelSize[pf];
433 if ((dstBuf = (unsigned char *)malloc(dstSize)) == NULL)
434 THROW("Memory allocation failure");
435 memset(dstBuf, 0, dstSize);
436
437 if (doYUV) {
438 unsigned long yuvSize = tjBufSizeYUV2(scaledWidth, pad, scaledHeight,
439 subsamp);
440 tjhandle handle2 = tjInitDecompress();
441
442 if (!handle2) THROW_TJ();
443
444 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
445 THROW("Memory allocation failure");
446 memset(yuvBuf, 0, yuvSize);
447
448 printf("JPEG -> YUV %s ", subNameLong[subsamp]);
449 if (sf.num != 1 || sf.denom != 1)
450 printf("%d/%d ... ", sf.num, sf.denom);
451 else printf("... ");
452 TRY_TJ(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth,
453 pad, scaledHeight, flags));
454 if (checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf))
455 printf("Passed.\n");
456 else printf("FAILED!\n");
457
458 printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf],
459 (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down ");
460 TRY_TJ(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0,
461 scaledHeight, pf, flags));
462 tjDestroy(handle2);
463 } else {
464 printf("JPEG -> %s %s ", pixFormatStr[pf],
465 (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down ");
466 if (sf.num != 1 || sf.denom != 1)
467 printf("%d/%d ... ", sf.num, sf.denom);
468 else printf("... ");
469 TRY_TJ(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
470 scaledHeight, pf, flags));
471 }
472
473 if (checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
474 printf("Passed.");
475 else printf("FAILED!");
476 printf("\n");
477
478 bailout:
479 if (yuvBuf) free(yuvBuf);
480 if (dstBuf) free(dstBuf);
481 }
482
483
decompTest(tjhandle handle,unsigned char * jpegBuf,unsigned long jpegSize,int w,int h,int pf,char * basename,int subsamp,int flags)484 void decompTest(tjhandle handle, unsigned char *jpegBuf,
485 unsigned long jpegSize, int w, int h, int pf, char *basename,
486 int subsamp, int flags)
487 {
488 int i, n = 0;
489 tjscalingfactor *sf = tjGetScalingFactors(&n);
490
491 if (!sf || !n) THROW_TJ();
492
493 for (i = 0; i < n; i++) {
494 if (subsamp == TJSAMP_444 || subsamp == TJSAMP_GRAY ||
495 (subsamp == TJSAMP_411 && sf[i].num == 1 &&
496 (sf[i].denom == 2 || sf[i].denom == 1)) ||
497 (subsamp != TJSAMP_411 && sf[i].num == 1 &&
498 (sf[i].denom == 4 || sf[i].denom == 2 || sf[i].denom == 1)))
499 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
500 flags, sf[i]);
501 }
502
503 bailout:
504 return;
505 }
506
507
doTest(int w,int h,const int * formats,int nformats,int subsamp,char * basename)508 void doTest(int w, int h, const int *formats, int nformats, int subsamp,
509 char *basename)
510 {
511 tjhandle chandle = NULL, dhandle = NULL;
512 unsigned char *dstBuf = NULL;
513 unsigned long size = 0;
514 int pfi, pf, i;
515
516 if (!alloc)
517 size = tjBufSize(w, h, subsamp);
518 if (size != 0)
519 if ((dstBuf = (unsigned char *)tjAlloc(size)) == NULL)
520 THROW("Memory allocation failure.");
521
522 if ((chandle = tjInitCompress()) == NULL ||
523 (dhandle = tjInitDecompress()) == NULL)
524 THROW_TJ();
525
526 for (pfi = 0; pfi < nformats; pfi++) {
527 for (i = 0; i < 2; i++) {
528 int flags = 0;
529
530 if (subsamp == TJSAMP_422 || subsamp == TJSAMP_420 ||
531 subsamp == TJSAMP_440 || subsamp == TJSAMP_411)
532 flags |= TJFLAG_FASTUPSAMPLE;
533 if (i == 1) flags |= TJFLAG_BOTTOMUP;
534 pf = formats[pfi];
535 compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
536 flags);
537 decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp, flags);
538 if (pf >= TJPF_RGBX && pf <= TJPF_XRGB) {
539 printf("\n");
540 decompTest(dhandle, dstBuf, size, w, h, pf + (TJPF_RGBA - TJPF_RGBX),
541 basename, subsamp, flags);
542 }
543 printf("\n");
544 }
545 }
546 printf("--------------------\n\n");
547
548 bailout:
549 if (chandle) tjDestroy(chandle);
550 if (dhandle) tjDestroy(dhandle);
551 if (dstBuf) tjFree(dstBuf);
552 }
553
554
555 #if SIZEOF_SIZE_T == 8
556 #define CHECKSIZE(function) { \
557 if ((unsigned long long)size < (unsigned long long)0xFFFFFFFF) \
558 THROW(#function " overflow"); \
559 }
560 #else
561 #define CHECKSIZE(function) { \
562 if (size != (unsigned long)(-1) || \
563 !strcmp(tjGetErrorStr2(NULL), "No error")) \
564 THROW(#function " overflow"); \
565 }
566 #endif
567
overflowTest(void)568 static void overflowTest(void)
569 {
570 /* Ensure that the various buffer size functions don't overflow */
571 unsigned long size;
572
573 size = tjBufSize(26755, 26755, TJSAMP_444);
574 CHECKSIZE(tjBufSize());
575 size = TJBUFSIZE(26755, 26755);
576 CHECKSIZE(TJBUFSIZE());
577 size = tjBufSizeYUV2(37838, 1, 37838, TJSAMP_444);
578 CHECKSIZE(tjBufSizeYUV2());
579 size = TJBUFSIZEYUV(37838, 37838, TJSAMP_444);
580 CHECKSIZE(TJBUFSIZEYUV());
581 size = tjBufSizeYUV(37838, 37838, TJSAMP_444);
582 CHECKSIZE(tjBufSizeYUV());
583 size = tjPlaneSizeYUV(0, 65536, 0, 65536, TJSAMP_444);
584 CHECKSIZE(tjPlaneSizeYUV());
585
586 bailout:
587 return;
588 }
589
590
bufSizeTest(void)591 static void bufSizeTest(void)
592 {
593 int w, h, i, subsamp;
594 unsigned char *srcBuf = NULL, *dstBuf = NULL;
595 tjhandle handle = NULL;
596 unsigned long dstSize = 0;
597
598 if ((handle = tjInitCompress()) == NULL) THROW_TJ();
599
600 printf("Buffer size regression test\n");
601 for (subsamp = 0; subsamp < TJ_NUMSAMP; subsamp++) {
602 for (w = 1; w < 48; w++) {
603 int maxh = (w == 1) ? 2048 : 48;
604
605 for (h = 1; h < maxh; h++) {
606 if (h % 100 == 0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
607 if ((srcBuf = (unsigned char *)malloc(w * h * 4)) == NULL)
608 THROW("Memory allocation failure");
609 if (!alloc || doYUV) {
610 if (doYUV) dstSize = tjBufSizeYUV2(w, pad, h, subsamp);
611 else dstSize = tjBufSize(w, h, subsamp);
612 if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
613 THROW("Memory allocation failure");
614 }
615
616 for (i = 0; i < w * h * 4; i++) {
617 if (random() < RAND_MAX / 2) srcBuf[i] = 0;
618 else srcBuf[i] = 255;
619 }
620
621 if (doYUV) {
622 TRY_TJ(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
623 subsamp, 0));
624 } else {
625 TRY_TJ(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
626 &dstSize, subsamp, 100,
627 alloc ? 0 : TJFLAG_NOREALLOC));
628 }
629 free(srcBuf); srcBuf = NULL;
630 if (!alloc || doYUV) {
631 tjFree(dstBuf); dstBuf = NULL;
632 }
633
634 if ((srcBuf = (unsigned char *)malloc(h * w * 4)) == NULL)
635 THROW("Memory allocation failure");
636 if (!alloc || doYUV) {
637 if (doYUV) dstSize = tjBufSizeYUV2(h, pad, w, subsamp);
638 else dstSize = tjBufSize(h, w, subsamp);
639 if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
640 THROW("Memory allocation failure");
641 }
642
643 for (i = 0; i < h * w * 4; i++) {
644 if (random() < RAND_MAX / 2) srcBuf[i] = 0;
645 else srcBuf[i] = 255;
646 }
647
648 if (doYUV) {
649 TRY_TJ(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
650 subsamp, 0));
651 } else {
652 TRY_TJ(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
653 &dstSize, subsamp, 100,
654 alloc ? 0 : TJFLAG_NOREALLOC));
655 }
656 free(srcBuf); srcBuf = NULL;
657 if (!alloc || doYUV) {
658 tjFree(dstBuf); dstBuf = NULL;
659 }
660 }
661 }
662 }
663 printf("Done. \n");
664
665 bailout:
666 if (srcBuf) free(srcBuf);
667 if (dstBuf) tjFree(dstBuf);
668 if (handle) tjDestroy(handle);
669 }
670
671
initBitmap(unsigned char * buf,int width,int pitch,int height,int pf,int flags)672 void initBitmap(unsigned char *buf, int width, int pitch, int height, int pf,
673 int flags)
674 {
675 int roffset = tjRedOffset[pf];
676 int goffset = tjGreenOffset[pf];
677 int boffset = tjBlueOffset[pf];
678 int ps = tjPixelSize[pf];
679 int i, j;
680
681 for (j = 0; j < height; j++) {
682 int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j;
683
684 for (i = 0; i < width; i++) {
685 unsigned char r = (i * 256 / width) % 256;
686 unsigned char g = (j * 256 / height) % 256;
687 unsigned char b = (j * 256 / height + i * 256 / width) % 256;
688
689 memset(&buf[row * pitch + i * ps], 0, ps);
690 if (pf == TJPF_GRAY) buf[row * pitch + i * ps] = b;
691 else if (pf == TJPF_CMYK)
692 rgb_to_cmyk(r, g, b, &buf[row * pitch + i * ps + 0],
693 &buf[row * pitch + i * ps + 1],
694 &buf[row * pitch + i * ps + 2],
695 &buf[row * pitch + i * ps + 3]);
696 else {
697 buf[row * pitch + i * ps + roffset] = r;
698 buf[row * pitch + i * ps + goffset] = g;
699 buf[row * pitch + i * ps + boffset] = b;
700 }
701 }
702 }
703 }
704
705
cmpBitmap(unsigned char * buf,int width,int pitch,int height,int pf,int flags,int gray2rgb)706 int cmpBitmap(unsigned char *buf, int width, int pitch, int height, int pf,
707 int flags, int gray2rgb)
708 {
709 int roffset = tjRedOffset[pf];
710 int goffset = tjGreenOffset[pf];
711 int boffset = tjBlueOffset[pf];
712 int aoffset = tjAlphaOffset[pf];
713 int ps = tjPixelSize[pf];
714 int i, j;
715
716 for (j = 0; j < height; j++) {
717 int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j;
718
719 for (i = 0; i < width; i++) {
720 unsigned char r = (i * 256 / width) % 256;
721 unsigned char g = (j * 256 / height) % 256;
722 unsigned char b = (j * 256 / height + i * 256 / width) % 256;
723
724 if (pf == TJPF_GRAY) {
725 if (buf[row * pitch + i * ps] != b)
726 return 0;
727 } else if (pf == TJPF_CMYK) {
728 unsigned char rf, gf, bf;
729
730 cmyk_to_rgb(buf[row * pitch + i * ps + 0],
731 buf[row * pitch + i * ps + 1],
732 buf[row * pitch + i * ps + 2],
733 buf[row * pitch + i * ps + 3], &rf, &gf, &bf);
734 if (gray2rgb) {
735 if (rf != b || gf != b || bf != b)
736 return 0;
737 } else if (rf != r || gf != g || bf != b) return 0;
738 } else {
739 if (gray2rgb) {
740 if (buf[row * pitch + i * ps + roffset] != b ||
741 buf[row * pitch + i * ps + goffset] != b ||
742 buf[row * pitch + i * ps + boffset] != b)
743 return 0;
744 } else if (buf[row * pitch + i * ps + roffset] != r ||
745 buf[row * pitch + i * ps + goffset] != g ||
746 buf[row * pitch + i * ps + boffset] != b)
747 return 0;
748 if (aoffset >= 0 && buf[row * pitch + i * ps + aoffset] != 0xFF)
749 return 0;
750 }
751 }
752 }
753 return 1;
754 }
755
756
doBmpTest(const char * ext,int width,int align,int height,int pf,int flags)757 int doBmpTest(const char *ext, int width, int align, int height, int pf,
758 int flags)
759 {
760 char filename[80], *md5sum, md5buf[65];
761 int ps = tjPixelSize[pf], pitch = PAD(width * ps, align), loadWidth = 0,
762 loadHeight = 0, retval = 0, pixelFormat = pf;
763 unsigned char *buf = NULL;
764 char *md5ref;
765
766 if (pf == TJPF_GRAY) {
767 md5ref = !strcasecmp(ext, "ppm") ? "112c682e82ce5de1cca089e20d60000b" :
768 "51976530acf75f02beddf5d21149101d";
769 } else {
770 md5ref = !strcasecmp(ext, "ppm") ? "c0c9f772b464d1896326883a5c79c545" :
771 "6d659071b9bfcdee2def22cb58ddadca";
772 }
773
774 if ((buf = (unsigned char *)tjAlloc(pitch * height)) == NULL)
775 THROW("Could not allocate memory");
776 initBitmap(buf, width, pitch, height, pf, flags);
777
778 snprintf(filename, 80, "test_bmp_%s_%d_%s.%s", pixFormatStr[pf], align,
779 (flags & TJFLAG_BOTTOMUP) ? "bu" : "td", ext);
780 TRY_TJ(tjSaveImage(filename, buf, width, pitch, height, pf, flags));
781 md5sum = MD5File(filename, md5buf);
782 if (strcasecmp(md5sum, md5ref))
783 THROW_MD5(filename, md5sum, md5ref);
784
785 tjFree(buf); buf = NULL;
786 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
787 flags)) == NULL)
788 THROW_TJ();
789 if (width != loadWidth || height != loadHeight) {
790 printf("\n Image dimensions of %s are bogus\n", filename);
791 retval = -1; goto bailout;
792 }
793 if (!cmpBitmap(buf, width, pitch, height, pf, flags, 0)) {
794 printf("\n Pixel data in %s is bogus\n", filename);
795 retval = -1; goto bailout;
796 }
797 if (pf == TJPF_GRAY) {
798 tjFree(buf); buf = NULL;
799 pf = TJPF_XBGR;
800 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
801 flags)) == NULL)
802 THROW_TJ();
803 pitch = PAD(width * tjPixelSize[pf], align);
804 if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
805 printf("\n Converting %s to RGB failed\n", filename);
806 retval = -1; goto bailout;
807 }
808
809 tjFree(buf); buf = NULL;
810 pf = TJPF_CMYK;
811 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
812 flags)) == NULL)
813 THROW_TJ();
814 pitch = PAD(width * tjPixelSize[pf], align);
815 if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
816 printf("\n Converting %s to CMYK failed\n", filename);
817 retval = -1; goto bailout;
818 }
819 }
820 /* Verify that tjLoadImage() returns the proper "preferred" pixel format for
821 the file type. */
822 tjFree(buf); buf = NULL;
823 pf = pixelFormat;
824 pixelFormat = TJPF_UNKNOWN;
825 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight,
826 &pixelFormat, flags)) == NULL)
827 THROW_TJ();
828 if ((pf == TJPF_GRAY && pixelFormat != TJPF_GRAY) ||
829 (pf != TJPF_GRAY && !strcasecmp(ext, "bmp") &&
830 pixelFormat != TJPF_BGR) ||
831 (pf != TJPF_GRAY && !strcasecmp(ext, "ppm") &&
832 pixelFormat != TJPF_RGB)) {
833 printf("\n tjLoadImage() returned unexpected pixel format: %s\n",
834 pixFormatStr[pixelFormat]);
835 retval = -1;
836 }
837 unlink(filename);
838
839 bailout:
840 if (buf) tjFree(buf);
841 if (exitStatus < 0) return exitStatus;
842 return retval;
843 }
844
845
bmpTest(void)846 int bmpTest(void)
847 {
848 int align, width = 35, height = 39, format;
849
850 for (align = 1; align <= 8; align *= 2) {
851 for (format = 0; format < TJ_NUMPF; format++) {
852 printf("%s Top-Down BMP (row alignment = %d bytes) ... ",
853 pixFormatStr[format], align);
854 if (doBmpTest("bmp", width, align, height, format, 0) == -1)
855 return -1;
856 printf("OK.\n");
857
858 printf("%s Top-Down PPM (row alignment = %d bytes) ... ",
859 pixFormatStr[format], align);
860 if (doBmpTest("ppm", width, align, height, format,
861 TJFLAG_BOTTOMUP) == -1)
862 return -1;
863 printf("OK.\n");
864
865 printf("%s Bottom-Up BMP (row alignment = %d bytes) ... ",
866 pixFormatStr[format], align);
867 if (doBmpTest("bmp", width, align, height, format, 0) == -1)
868 return -1;
869 printf("OK.\n");
870
871 printf("%s Bottom-Up PPM (row alignment = %d bytes) ... ",
872 pixFormatStr[format], align);
873 if (doBmpTest("ppm", width, align, height, format,
874 TJFLAG_BOTTOMUP) == -1)
875 return -1;
876 printf("OK.\n");
877 }
878 }
879
880 return 0;
881 }
882
883
main(int argc,char * argv[])884 int main(int argc, char *argv[])
885 {
886 int i, num4bf = 5;
887
888 #ifdef _WIN32
889 srand((unsigned int)time(NULL));
890 #endif
891 if (argc > 1) {
892 for (i = 1; i < argc; i++) {
893 if (!strcasecmp(argv[i], "-yuv")) doYUV = 1;
894 else if (!strcasecmp(argv[i], "-noyuvpad")) pad = 1;
895 else if (!strcasecmp(argv[i], "-alloc")) alloc = 1;
896 else if (!strcasecmp(argv[i], "-bmp")) return bmpTest();
897 else usage(argv[0]);
898 }
899 }
900 if (alloc) printf("Testing automatic buffer allocation\n");
901 if (doYUV) num4bf = 4;
902 overflowTest();
903 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
904 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
905 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
906 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
907 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
908 doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
909 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
910 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
911 doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
912 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
913 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
914 doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
915 doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
916 bufSizeTest();
917 if (doYUV) {
918 printf("\n--------------------\n\n");
919 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
920 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
921 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
922 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
923 doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
924 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
925 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
926 }
927
928 return exitStatus;
929 }
930