2 * Copyright (C)2009-2014 D. R. Commander. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
30 * This program tests the various code paths in the TurboJPEG C Wrapper
38 #include "./turbojpeg.h"
41 #define random() rand()
45 void usage(char *progName)
47 printf("\nUSAGE: %s [options]\n", progName);
49 printf("-yuv = test YUV encoding/decoding support\n");
50 printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n");
51 printf(" 4-byte boundary\n");
52 printf("-alloc = test automatic buffer allocation\n");
57 #define _throwtj() {printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
59 #define _tj(f) {if((f)==-1) _throwtj();}
60 #define _throw(m) {printf("ERROR: %s\n", m); bailout();}
62 const char *subNameLong[TJ_NUMSAMP]=
64 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
66 const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"};
68 const char *pixFormatStr[TJ_NUMPF]=
70 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale",
71 "RGBA", "BGRA", "ABGR", "ARGB", "CMYK"
74 const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1};
76 const int _3byteFormats[]={TJPF_RGB, TJPF_BGR};
77 const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB,
79 const int _onlyGray[]={TJPF_GRAY};
80 const int _onlyRGB[]={TJPF_RGB};
82 int doyuv=0, alloc=0, pad=4;
85 #define bailout() {exitStatus=-1; goto bailout;}
88 void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
90 int roffset=tjRedOffset[pf];
91 int goffset=tjGreenOffset[pf];
92 int boffset=tjBlueOffset[pf];
93 int ps=tjPixelSize[pf];
94 int index, row, col, halfway=16;
98 memset(buf, 0, w*h*ps);
99 for(row=0; row<h; row++)
101 for(col=0; col<w; col++)
103 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
104 else index=row*w+col;
105 if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0;
106 else buf[index]=(row<halfway)? 76:226;
110 else if(pf==TJPF_CMYK)
112 memset(buf, 255, w*h*ps);
113 for(row=0; row<h; row++)
115 for(col=0; col<w; col++)
117 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
118 else index=row*w+col;
119 if(((row/8)+(col/8))%2==0)
121 if(row>=halfway) buf[index*ps+3]=0;
126 if(row<halfway) buf[index*ps+1]=0;
133 memset(buf, 0, w*h*ps);
134 for(row=0; row<h; row++)
136 for(col=0; col<w; col++)
138 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
139 else index=row*w+col;
140 if(((row/8)+(col/8))%2==0)
144 buf[index*ps+roffset]=255;
145 buf[index*ps+goffset]=255;
146 buf[index*ps+boffset]=255;
151 buf[index*ps+roffset]=255;
152 if(row>=halfway) buf[index*ps+goffset]=255;
160 #define checkval(v, cv) { \
161 if(v<cv-1 || v>cv+1) { \
162 printf("\nComp. %s at %d,%d should be %d, not %d\n", \
163 #v, row, col, cv, v); \
164 retval=0; exitStatus=-1; goto bailout; \
167 #define checkval0(v) { \
169 printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
170 retval=0; exitStatus=-1; goto bailout; \
173 #define checkval255(v) { \
175 printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
176 retval=0; exitStatus=-1; goto bailout; \
180 int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
181 tjscalingfactor sf, int flags)
183 int roffset=tjRedOffset[pf];
184 int goffset=tjGreenOffset[pf];
185 int boffset=tjBlueOffset[pf];
186 int aoffset=alphaOffset[pf];
187 int ps=tjPixelSize[pf];
188 int index, row, col, retval=1;
189 int halfway=16*sf.num/sf.denom;
190 int blocksize=8*sf.num/sf.denom;
194 for(row=0; row<h; row++)
196 for(col=0; col<w; col++)
198 unsigned char c, m, y, k;
199 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
200 else index=row*w+col;
205 if(((row/blocksize)+(col/blocksize))%2==0)
207 checkval255(c); checkval255(m); checkval255(y);
208 if(row<halfway) checkval255(k)
213 checkval255(c); checkval0(y); checkval255(k);
214 if(row<halfway) checkval0(m)
222 for(row=0; row<h; row++)
224 for(col=0; col<w; col++)
226 unsigned char r, g, b, a;
227 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
228 else index=row*w+col;
229 r=buf[index*ps+roffset];
230 g=buf[index*ps+goffset];
231 b=buf[index*ps+boffset];
232 a=aoffset>=0? buf[index*ps+aoffset]:0xFF;
233 if(((row/blocksize)+(col/blocksize))%2==0)
237 checkval255(r); checkval255(g); checkval255(b);
241 checkval0(r); checkval0(g); checkval0(b);
246 if(subsamp==TJSAMP_GRAY)
250 checkval(r, 76); checkval(g, 76); checkval(b, 76);
254 checkval(r, 226); checkval(g, 226); checkval(b, 226);
261 checkval255(r); checkval0(g); checkval0(b);
265 checkval255(r); checkval255(g); checkval0(b);
276 for(row=0; row<h; row++)
278 for(col=0; col<w; col++)
281 printf("%.3d/%.3d/%.3d/%.3d ", buf[(row*w+col)*ps],
282 buf[(row*w+col)*ps+1], buf[(row*w+col)*ps+2],
283 buf[(row*w+col)*ps+3]);
285 printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset],
286 buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]);
295 #define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
297 int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
301 int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
302 int pw=PAD(w, hsf), ph=PAD(h, vsf);
303 int cw=pw/hsf, ch=ph/vsf;
304 int ypitch=PAD(pw, pad), uvpitch=PAD(cw, pad);
306 int halfway=16*sf.num/sf.denom;
307 int blocksize=8*sf.num/sf.denom;
309 for(row=0; row<ph; row++)
311 for(col=0; col<pw; col++)
313 unsigned char y=buf[ypitch*row+col];
314 if(((row/blocksize)+(col/blocksize))%2==0)
316 if(row<halfway) checkval255(y) else checkval0(y);
320 if(row<halfway) checkval(y, 76) else checkval(y, 226);
324 if(subsamp!=TJSAMP_GRAY)
326 int halfway=16/vsf*sf.num/sf.denom;
327 for(row=0; row<ch; row++)
329 for(col=0; col<cw; col++)
331 unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
332 v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
333 if(((row*vsf/blocksize)+(col*hsf/blocksize))%2==0)
335 checkval(u, 128); checkval(v, 128);
341 checkval(u, 85); checkval255(v);
345 checkval0(u); checkval(v, 149);
355 for(row=0; row<ph; row++)
357 for(col=0; col<pw; col++)
358 printf("%.3d ", buf[ypitch*row+col]);
362 for(row=0; row<ch; row++)
364 for(col=0; col<cw; col++)
365 printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]);
369 for(row=0; row<ch; row++)
371 for(col=0; col<cw; col++)
372 printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]);
381 void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
383 FILE *file=fopen(filename, "wb");
384 if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1)
388 strerror_r(errno, err_str, 256);
389 printf("ERROR: Could not write to %s.\n%s\n", filename, err_str);
391 printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
397 if(file) fclose(file);
401 void compTest(tjhandle handle, unsigned char **dstBuf,
402 unsigned long *dstSize, int w, int h, int pf, char *basename,
403 int subsamp, int jpegQual, int flags)
405 char tempStr[1024]; unsigned char *srcBuf=NULL, *yuvBuf=NULL;
406 const char *pfStr=pixFormatStr[pf];
407 const char *buStrLong=(flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ";
408 const char *buStr=(flags&TJFLAG_BOTTOMUP)? "BU":"TD";
410 if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL)
411 _throw("Memory allocation failure");
412 initBuf(srcBuf, w, h, pf, flags);
414 if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize);
417 if(!alloc) flags|=TJFLAG_NOREALLOC;
420 unsigned long yuvSize=tjBufSizeYUV2(w, pad, h, subsamp);
421 tjscalingfactor sf={1, 1};
422 tjhandle handle2=tjInitCompress();
423 if(!handle2) _throwtj();
425 if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL)
426 _throw("Memory allocation failure");
427 memset(yuvBuf, 0, yuvSize);
429 printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]);
430 _tj(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp,
433 if(checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n");
434 else printf("FAILED!\n");
436 printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
438 _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf,
439 dstSize, jpegQual, flags));
443 printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
445 _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
449 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
450 subName[subsamp], jpegQual);
451 writeJPEG(*dstBuf, *dstSize, tempStr);
452 printf("Done.\n Result in %s\n", tempStr);
455 if(yuvBuf) free(yuvBuf);
456 if(srcBuf) free(srcBuf);
460 void _decompTest(tjhandle handle, unsigned char *jpegBuf,
461 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
462 int flags, tjscalingfactor sf)
464 unsigned char *dstBuf=NULL, *yuvBuf=NULL;
465 int _hdrw=0, _hdrh=0, _hdrsubsamp=-1;
466 int scaledWidth=TJSCALED(w, sf);
467 int scaledHeight=TJSCALED(h, sf);
468 unsigned long dstSize=0;
470 _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
472 if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
473 _throw("Incorrect JPEG header");
475 dstSize=scaledWidth*scaledHeight*tjPixelSize[pf];
476 if((dstBuf=(unsigned char *)malloc(dstSize))==NULL)
477 _throw("Memory allocation failure");
478 memset(dstBuf, 0, dstSize);
482 unsigned long yuvSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight,
484 tjhandle handle2=tjInitDecompress();
485 if(!handle2) _throwtj();
487 if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL)
488 _throw("Memory allocation failure");
489 memset(yuvBuf, 0, yuvSize);
491 printf("JPEG -> YUV %s ", subNameLong[subsamp]);
492 if(sf.num!=1 || sf.denom!=1)
493 printf("%d/%d ... ", sf.num, sf.denom);
495 _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth,
496 pad, scaledHeight, flags));
497 if(checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf))
499 else printf("FAILED!\n");
501 printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf],
502 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
503 _tj(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0,
504 scaledHeight, pf, flags));
509 printf("JPEG -> %s %s ", pixFormatStr[pf],
510 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
511 if(sf.num!=1 || sf.denom!=1)
512 printf("%d/%d ... ", sf.num, sf.denom);
514 _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
515 scaledHeight, pf, flags));
518 if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
520 else printf("FAILED!");
524 if(yuvBuf) free(yuvBuf);
525 if(dstBuf) free(dstBuf);
529 void decompTest(tjhandle handle, unsigned char *jpegBuf,
530 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
534 tjscalingfactor *sf=tjGetScalingFactors(&n);
535 if(!sf || !n) _throwtj();
539 if(subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY ||
540 (subsamp==TJSAMP_411 && sf[i].num==1 &&
541 (sf[i].denom==2 || sf[i].denom==1)) ||
542 (subsamp!=TJSAMP_411 && sf[i].num==1 &&
543 (sf[i].denom==4 || sf[i].denom==2 || sf[i].denom==1)))
544 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
553 void doTest(int w, int h, const int *formats, int nformats, int subsamp,
556 tjhandle chandle=NULL, dhandle=NULL;
557 unsigned char *dstBuf=NULL;
558 unsigned long size=0; int pfi, pf, i;
561 size=tjBufSize(w, h, subsamp);
563 if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
564 _throw("Memory allocation failure.");
566 if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
569 for(pfi=0; pfi<nformats; pfi++)
574 if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440 ||
576 flags|=TJFLAG_FASTUPSAMPLE;
577 if(i==1) flags|=TJFLAG_BOTTOMUP;
579 compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
581 decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp,
583 if(pf>=TJPF_RGBX && pf<=TJPF_XRGB)
586 decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX),
587 basename, subsamp, flags);
592 printf("--------------------\n\n");
595 if(chandle) tjDestroy(chandle);
596 if(dhandle) tjDestroy(dhandle);
598 if(dstBuf) tjFree(dstBuf);
602 void bufSizeTest(void)
604 int w, h, i, subsamp;
605 unsigned char *srcBuf=NULL, *dstBuf=NULL;
606 tjhandle handle=NULL;
607 unsigned long dstSize=0;
609 if((handle=tjInitCompress())==NULL) _throwtj();
611 printf("Buffer size regression test\n");
612 for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
616 int maxh=(w==1)? 2048:48;
617 for(h=1; h<maxh; h++)
619 if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
620 if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
621 _throw("Memory allocation failure");
624 if(doyuv) dstSize=tjBufSizeYUV2(w, pad, h, subsamp);
625 else dstSize=tjBufSize(w, h, subsamp);
626 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
627 _throw("Memory allocation failure");
630 for(i=0; i<w*h*4; i++)
632 if(random()<RAND_MAX/2) srcBuf[i]=0;
638 _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
643 _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
644 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
646 free(srcBuf); srcBuf=NULL;
649 tjFree(dstBuf); dstBuf=NULL;
652 if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
653 _throw("Memory allocation failure");
656 if(doyuv) dstSize=tjBufSizeYUV2(h, pad, w, subsamp);
657 else dstSize=tjBufSize(h, w, subsamp);
658 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
659 _throw("Memory allocation failure");
662 for(i=0; i<h*w*4; i++)
664 if(random()<RAND_MAX/2) srcBuf[i]=0;
670 _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
675 _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
676 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
678 free(srcBuf); srcBuf=NULL;
681 tjFree(dstBuf); dstBuf=NULL;
689 if(srcBuf) free(srcBuf);
690 if(dstBuf) tjFree(dstBuf);
691 if(handle) tjDestroy(handle);
695 int main(int argc, char *argv[])
699 srand((unsigned int)time(NULL));
703 for(i=1; i<argc; i++)
705 if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
706 if(!strcasecmp(argv[i], "-noyuvpad")) pad=1;
707 if(!strcasecmp(argv[i], "-alloc")) alloc=1;
708 if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
712 if(alloc) printf("Testing automatic buffer allocation\n");
714 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
715 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
716 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
717 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
718 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
719 doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
720 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
721 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
722 doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
723 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
724 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
725 doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
726 doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
730 printf("\n--------------------\n\n");
731 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
732 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
733 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
734 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
735 doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
736 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
737 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");