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 _throwtj() { \
63 printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
64 bailout() \
65 }
66 #define _tj(f) { if ((f) == -1) _throwtj(); }
67 #define _throw(m) { printf("ERROR: %s\n", m); bailout() }
68 #define _throwmd5(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) _throwtj();
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 _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 _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf, dstSize,
398 jpegQual, flags));
399 } else {
400 printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
401 jpegQual);
402 _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 _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) _throwtj();
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 _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth, pad,
453 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 _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 _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) _throwtj();
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 _throwtj();
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
bufSizeTest(void)555 void bufSizeTest(void)
556 {
557 int w, h, i, subsamp;
558 unsigned char *srcBuf = NULL, *dstBuf = NULL;
559 tjhandle handle = NULL;
560 unsigned long dstSize = 0;
561
562 if ((handle = tjInitCompress()) == NULL) _throwtj();
563
564 printf("Buffer size regression test\n");
565 for (subsamp = 0; subsamp < TJ_NUMSAMP; subsamp++) {
566 for (w = 1; w < 48; w++) {
567 int maxh = (w == 1) ? 2048 : 48;
568
569 for (h = 1; h < maxh; h++) {
570 if (h % 100 == 0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
571 if ((srcBuf = (unsigned char *)malloc(w * h * 4)) == NULL)
572 _throw("Memory allocation failure");
573 if (!alloc || doYUV) {
574 if (doYUV) dstSize = tjBufSizeYUV2(w, pad, h, subsamp);
575 else dstSize = tjBufSize(w, h, subsamp);
576 if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
577 _throw("Memory allocation failure");
578 }
579
580 for (i = 0; i < w * h * 4; i++) {
581 if (random() < RAND_MAX / 2) srcBuf[i] = 0;
582 else srcBuf[i] = 255;
583 }
584
585 if (doYUV) {
586 _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
587 subsamp, 0));
588 } else {
589 _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
590 &dstSize, subsamp, 100,
591 alloc ? 0 : TJFLAG_NOREALLOC));
592 }
593 free(srcBuf); srcBuf = NULL;
594 if (!alloc || doYUV) {
595 tjFree(dstBuf); dstBuf = NULL;
596 }
597
598 if ((srcBuf = (unsigned char *)malloc(h * w * 4)) == NULL)
599 _throw("Memory allocation failure");
600 if (!alloc || doYUV) {
601 if (doYUV) dstSize = tjBufSizeYUV2(h, pad, w, subsamp);
602 else dstSize = tjBufSize(h, w, subsamp);
603 if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
604 _throw("Memory allocation failure");
605 }
606
607 for (i = 0; i < h * w * 4; i++) {
608 if (random() < RAND_MAX / 2) srcBuf[i] = 0;
609 else srcBuf[i] = 255;
610 }
611
612 if (doYUV) {
613 _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
614 subsamp, 0));
615 } else {
616 _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
617 &dstSize, subsamp, 100,
618 alloc ? 0 : TJFLAG_NOREALLOC));
619 }
620 free(srcBuf); srcBuf = NULL;
621 if (!alloc || doYUV) {
622 tjFree(dstBuf); dstBuf = NULL;
623 }
624 }
625 }
626 }
627 printf("Done. \n");
628
629 bailout:
630 if (srcBuf) free(srcBuf);
631 if (dstBuf) tjFree(dstBuf);
632 if (handle) tjDestroy(handle);
633 }
634
635
initBitmap(unsigned char * buf,int width,int pitch,int height,int pf,int flags)636 void initBitmap(unsigned char *buf, int width, int pitch, int height, int pf,
637 int flags)
638 {
639 int roffset = tjRedOffset[pf];
640 int goffset = tjGreenOffset[pf];
641 int boffset = tjBlueOffset[pf];
642 int ps = tjPixelSize[pf];
643 int i, j;
644
645 for (j = 0; j < height; j++) {
646 int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j;
647
648 for (i = 0; i < width; i++) {
649 unsigned char r = (i * 256 / width) % 256;
650 unsigned char g = (j * 256 / height) % 256;
651 unsigned char b = (j * 256 / height + i * 256 / width) % 256;
652
653 memset(&buf[row * pitch + i * ps], 0, ps);
654 if (pf == TJPF_GRAY) buf[row * pitch + i * ps] = b;
655 else if (pf == TJPF_CMYK)
656 rgb_to_cmyk(r, g, b, &buf[row * pitch + i * ps + 0],
657 &buf[row * pitch + i * ps + 1],
658 &buf[row * pitch + i * ps + 2],
659 &buf[row * pitch + i * ps + 3]);
660 else {
661 buf[row * pitch + i * ps + roffset] = r;
662 buf[row * pitch + i * ps + goffset] = g;
663 buf[row * pitch + i * ps + boffset] = b;
664 }
665 }
666 }
667 }
668
669
cmpBitmap(unsigned char * buf,int width,int pitch,int height,int pf,int flags,int gray2rgb)670 int cmpBitmap(unsigned char *buf, int width, int pitch, int height, int pf,
671 int flags, int gray2rgb)
672 {
673 int roffset = tjRedOffset[pf];
674 int goffset = tjGreenOffset[pf];
675 int boffset = tjBlueOffset[pf];
676 int aoffset = tjAlphaOffset[pf];
677 int ps = tjPixelSize[pf];
678 int i, j;
679
680 for (j = 0; j < height; j++) {
681 int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j;
682
683 for (i = 0; i < width; i++) {
684 unsigned char r = (i * 256 / width) % 256;
685 unsigned char g = (j * 256 / height) % 256;
686 unsigned char b = (j * 256 / height + i * 256 / width) % 256;
687
688 if (pf == TJPF_GRAY) {
689 if (buf[row * pitch + i * ps] != b)
690 return 0;
691 } else if (pf == TJPF_CMYK) {
692 unsigned char rf, gf, bf;
693
694 cmyk_to_rgb(buf[row * pitch + i * ps + 0],
695 buf[row * pitch + i * ps + 1],
696 buf[row * pitch + i * ps + 2],
697 buf[row * pitch + i * ps + 3], &rf, &gf, &bf);
698 if (gray2rgb) {
699 if (rf != b || gf != b || bf != b)
700 return 0;
701 } else if (rf != r || gf != g || bf != b) return 0;
702 } else {
703 if (gray2rgb) {
704 if (buf[row * pitch + i * ps + roffset] != b ||
705 buf[row * pitch + i * ps + goffset] != b ||
706 buf[row * pitch + i * ps + boffset] != b)
707 return 0;
708 } else if (buf[row * pitch + i * ps + roffset] != r ||
709 buf[row * pitch + i * ps + goffset] != g ||
710 buf[row * pitch + i * ps + boffset] != b)
711 return 0;
712 if (aoffset >= 0 && buf[row * pitch + i * ps + aoffset] != 0xFF)
713 return 0;
714 }
715 }
716 }
717 return 1;
718 }
719
720
doBmpTest(const char * ext,int width,int align,int height,int pf,int flags)721 int doBmpTest(const char *ext, int width, int align, int height, int pf,
722 int flags)
723 {
724 char filename[80], *md5sum, md5buf[65];
725 int ps = tjPixelSize[pf], pitch = PAD(width * ps, align), loadWidth = 0,
726 loadHeight = 0, retval = 0, pixelFormat = pf;
727 unsigned char *buf = NULL;
728 char *md5ref;
729
730 if (pf == TJPF_GRAY) {
731 md5ref = !strcasecmp(ext, "ppm") ? "112c682e82ce5de1cca089e20d60000b" :
732 "51976530acf75f02beddf5d21149101d";
733 } else {
734 md5ref = !strcasecmp(ext, "ppm") ? "c0c9f772b464d1896326883a5c79c545" :
735 "6d659071b9bfcdee2def22cb58ddadca";
736 }
737
738 if ((buf = (unsigned char *)tjAlloc(pitch * height)) == NULL)
739 _throw("Could not allocate memory");
740 initBitmap(buf, width, pitch, height, pf, flags);
741
742 snprintf(filename, 80, "test_bmp_%s_%d_%s.%s", pixFormatStr[pf], align,
743 (flags & TJFLAG_BOTTOMUP) ? "bu" : "td", ext);
744 _tj(tjSaveImage(filename, buf, width, pitch, height, pf, flags));
745 md5sum = MD5File(filename, md5buf);
746 if (strcasecmp(md5sum, md5ref))
747 _throwmd5(filename, md5sum, md5ref);
748
749 tjFree(buf); buf = NULL;
750 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
751 flags)) == NULL)
752 _throwtj();
753 if (width != loadWidth || height != loadHeight) {
754 printf("\n Image dimensions of %s are bogus\n", filename);
755 retval = -1; goto bailout;
756 }
757 if (!cmpBitmap(buf, width, pitch, height, pf, flags, 0)) {
758 printf("\n Pixel data in %s is bogus\n", filename);
759 retval = -1; goto bailout;
760 }
761 if (pf == TJPF_GRAY) {
762 tjFree(buf); buf = NULL;
763 pf = TJPF_XBGR;
764 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
765 flags)) == NULL)
766 _throwtj();
767 pitch = PAD(width * tjPixelSize[pf], align);
768 if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
769 printf("\n Converting %s to RGB failed\n", filename);
770 retval = -1; goto bailout;
771 }
772
773 tjFree(buf); buf = NULL;
774 pf = TJPF_CMYK;
775 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
776 flags)) == NULL)
777 _throwtj();
778 pitch = PAD(width * tjPixelSize[pf], align);
779 if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
780 printf("\n Converting %s to CMYK failed\n", filename);
781 retval = -1; goto bailout;
782 }
783 }
784 /* Verify that tjLoadImage() returns the proper "preferred" pixel format for
785 the file type. */
786 tjFree(buf); buf = NULL;
787 pf = pixelFormat;
788 pixelFormat = TJPF_UNKNOWN;
789 if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight,
790 &pixelFormat, flags)) == NULL)
791 _throwtj();
792 if ((pf == TJPF_GRAY && pixelFormat != TJPF_GRAY) ||
793 (pf != TJPF_GRAY && !strcasecmp(ext, "bmp") &&
794 pixelFormat != TJPF_BGR) ||
795 (pf != TJPF_GRAY && !strcasecmp(ext, "ppm") &&
796 pixelFormat != TJPF_RGB)) {
797 printf("\n tjLoadImage() returned unexpected pixel format: %s\n",
798 pixFormatStr[pixelFormat]);
799 retval = -1;
800 }
801 unlink(filename);
802
803 bailout:
804 if (buf) tjFree(buf);
805 if (exitStatus < 0) return exitStatus;
806 return retval;
807 }
808
809
bmpTest(void)810 int bmpTest(void)
811 {
812 int align, width = 35, height = 39, format;
813
814 for (align = 1; align <= 8; align *= 2) {
815 for (format = 0; format < TJ_NUMPF; format++) {
816 printf("%s Top-Down BMP (row alignment = %d bytes) ... ",
817 pixFormatStr[format], align);
818 if (doBmpTest("bmp", width, align, height, format, 0) == -1)
819 return -1;
820 printf("OK.\n");
821
822 printf("%s Top-Down PPM (row alignment = %d bytes) ... ",
823 pixFormatStr[format], align);
824 if (doBmpTest("ppm", width, align, height, format,
825 TJFLAG_BOTTOMUP) == -1)
826 return -1;
827 printf("OK.\n");
828
829 printf("%s Bottom-Up BMP (row alignment = %d bytes) ... ",
830 pixFormatStr[format], align);
831 if (doBmpTest("bmp", width, align, height, format, 0) == -1)
832 return -1;
833 printf("OK.\n");
834
835 printf("%s Bottom-Up PPM (row alignment = %d bytes) ... ",
836 pixFormatStr[format], align);
837 if (doBmpTest("ppm", width, align, height, format,
838 TJFLAG_BOTTOMUP) == -1)
839 return -1;
840 printf("OK.\n");
841 }
842 }
843
844 return 0;
845 }
846
847
main(int argc,char * argv[])848 int main(int argc, char *argv[])
849 {
850 int i, num4bf = 5;
851
852 #ifdef _WIN32
853 srand((unsigned int)time(NULL));
854 #endif
855 if (argc > 1) {
856 for (i = 1; i < argc; i++) {
857 if (!strcasecmp(argv[i], "-yuv")) doYUV = 1;
858 else if (!strcasecmp(argv[i], "-noyuvpad")) pad = 1;
859 else if (!strcasecmp(argv[i], "-alloc")) alloc = 1;
860 else if (!strcasecmp(argv[i], "-bmp")) return bmpTest();
861 else usage(argv[0]);
862 }
863 }
864 if (alloc) printf("Testing automatic buffer allocation\n");
865 if (doYUV) num4bf = 4;
866 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
867 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
868 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
869 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
870 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
871 doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
872 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
873 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
874 doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
875 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
876 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
877 doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
878 doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
879 bufSizeTest();
880 if (doYUV) {
881 printf("\n--------------------\n\n");
882 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
883 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
884 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
885 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
886 doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
887 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
888 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
889 }
890
891 return exitStatus;
892 }
893