/* * Copyright (C)2009-2014, 2017 D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the libjpeg-turbo Project nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * This program tests the various code paths in the TurboJPEG C Wrapper */ #include #include #include #include #include "./tjutil.h" #include "./turbojpeg.h" #ifdef _WIN32 #include #define random() rand() #endif void usage(char *progName) { printf("\nUSAGE: %s [options]\n\n", progName); printf("Options:\n"); printf("-yuv = test YUV encoding/decoding support\n"); printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n"); printf(" 4-byte boundary\n"); printf("-alloc = test automatic buffer allocation\n\n"); exit(1); } #define _throwtj() {printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \ bailout();} #define _tj(f) {if((f)==-1) _throwtj();} #define _throw(m) {printf("ERROR: %s\n", m); bailout();} const char *subNameLong[TJ_NUMSAMP]= { "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1" }; const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"}; const char *pixFormatStr[TJ_NUMPF]= { "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale", "RGBA", "BGRA", "ABGR", "ARGB", "CMYK" }; const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1}; const int _3byteFormats[]={TJPF_RGB, TJPF_BGR}; const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, TJPF_CMYK}; const int _onlyGray[]={TJPF_GRAY}; const int _onlyRGB[]={TJPF_RGB}; int doyuv=0, alloc=0, pad=4; int exitStatus=0; #define bailout() {exitStatus=-1; goto bailout;} void initBuf(unsigned char *buf, int w, int h, int pf, int flags) { int roffset=tjRedOffset[pf]; int goffset=tjGreenOffset[pf]; int boffset=tjBlueOffset[pf]; int ps=tjPixelSize[pf]; int index, row, col, halfway=16; if(pf==TJPF_GRAY) { memset(buf, 0, w*h*ps); for(row=0; row=halfway) buf[index*ps+3]=0; } else { buf[index*ps+2]=0; if(row=halfway) buf[index*ps+goffset]=255; } } } } } #define checkval(v, cv) { \ if(vcv+1) { \ printf("\nComp. %s at %d,%d should be %d, not %d\n", \ #v, row, col, cv, v); \ retval=0; exitStatus=-1; goto bailout; \ }} #define checkval0(v) { \ if(v>1) { \ printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \ retval=0; exitStatus=-1; goto bailout; \ }} #define checkval255(v) { \ if(v<254) { \ printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \ retval=0; exitStatus=-1; goto bailout; \ }} int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp, tjscalingfactor sf, int flags) { int roffset=tjRedOffset[pf]; int goffset=tjGreenOffset[pf]; int boffset=tjBlueOffset[pf]; int aoffset=alphaOffset[pf]; int ps=tjPixelSize[pf]; int index, row, col, retval=1; int halfway=16*sf.num/sf.denom; int blocksize=8*sf.num/sf.denom; if(pf==TJPF_CMYK) { for(row=0; row=0? buf[index*ps+aoffset]:0xFF; if(((row/blocksize)+(col/blocksize))%2==0) { if(row0) memset(*dstBuf, 0, *dstSize); if(!alloc) flags|=TJFLAG_NOREALLOC; if(doyuv) { unsigned long yuvSize=tjBufSizeYUV2(w, pad, h, subsamp); tjscalingfactor sf={1, 1}; tjhandle handle2=tjInitCompress(); if(!handle2) _throwtj(); if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL) _throw("Memory allocation failure"); memset(yuvBuf, 0, yuvSize); printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]); _tj(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp, flags)); tjDestroy(handle2); if(checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n"); else printf("FAILED!\n"); printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong, jpegQual); _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf, dstSize, jpegQual, flags)); } else { printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp], jpegQual); _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp, jpegQual, flags)); } snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr, subName[subsamp], jpegQual); writeJPEG(*dstBuf, *dstSize, tempStr); printf("Done.\n Result in %s\n", tempStr); bailout: if(yuvBuf) free(yuvBuf); if(srcBuf) free(srcBuf); } void _decompTest(tjhandle handle, unsigned char *jpegBuf, unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, int flags, tjscalingfactor sf) { unsigned char *dstBuf=NULL, *yuvBuf=NULL; int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; int scaledWidth=TJSCALED(w, sf); int scaledHeight=TJSCALED(h, sf); unsigned long dstSize=0; _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh, &_hdrsubsamp)); if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp) _throw("Incorrect JPEG header"); dstSize=scaledWidth*scaledHeight*tjPixelSize[pf]; if((dstBuf=(unsigned char *)malloc(dstSize))==NULL) _throw("Memory allocation failure"); memset(dstBuf, 0, dstSize); if(doyuv) { unsigned long yuvSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight, subsamp); tjhandle handle2=tjInitDecompress(); if(!handle2) _throwtj(); if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL) _throw("Memory allocation failure"); memset(yuvBuf, 0, yuvSize); printf("JPEG -> YUV %s ", subNameLong[subsamp]); if(sf.num!=1 || sf.denom!=1) printf("%d/%d ... ", sf.num, sf.denom); else printf("... "); _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth, pad, scaledHeight, flags)); if(checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf)) printf("Passed.\n"); else printf("FAILED!\n"); printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down "); _tj(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0, scaledHeight, pf, flags)); tjDestroy(handle2); } else { printf("JPEG -> %s %s ", pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down "); if(sf.num!=1 || sf.denom!=1) printf("%d/%d ... ", sf.num, sf.denom); else printf("... "); _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0, scaledHeight, pf, flags)); } if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags)) printf("Passed."); else printf("FAILED!"); printf("\n"); bailout: if(yuvBuf) free(yuvBuf); if(dstBuf) free(dstBuf); } void decompTest(tjhandle handle, unsigned char *jpegBuf, unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, int flags) { int i, n=0; tjscalingfactor *sf=tjGetScalingFactors(&n); if(!sf || !n) _throwtj(); for(i=0; i=TJPF_RGBX && pf<=TJPF_XRGB) { printf("\n"); decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX), basename, subsamp, flags); } printf("\n"); } } printf("--------------------\n\n"); bailout: if(chandle) tjDestroy(chandle); if(dhandle) tjDestroy(dhandle); if(dstBuf) tjFree(dstBuf); } void bufSizeTest(void) { int w, h, i, subsamp; unsigned char *srcBuf=NULL, *dstBuf=NULL; tjhandle handle=NULL; unsigned long dstSize=0; if((handle=tjInitCompress())==NULL) _throwtj(); printf("Buffer size regression test\n"); for(subsamp=0; subsamp1) { for(i=1; i