/* Copyright (C)2004 Landmark Graphics Corporation * Copyright (C)2005 Sun Microsystems, Inc. * Copyright (C)2009-2011 D. R. Commander * * This library is free software and may be redistributed and/or modified under * the terms of the wxWindows Library License, Version 3.1 or (at your option) * any later version. The full license is in the LICENSE.txt file included * with this distribution. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * wxWindows Library License for more details. */ #include #include #include #include "./rrtimer.h" #include "./rrutil.h" #include "./turbojpeg.h" #define _catch(f) {if((f)==-1) {printf("TJPEG: %s\n", tjGetErrorStr()); bailout();}} const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0"}; const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY", "440"}; #define NUMPF 7 enum {RGB=0, BGR, RGBX, BGRX, XBGR, XRGB, GRAY}; const int _ps[NUMPF]={3, 3, 4, 4, 4, 4, 1}; const int _roffset[NUMPF]={0, 2, 0, 2, 3, 1, 0}; const int _goffset[NUMPF]={1, 1, 1, 1, 2, 2, 0}; const int _boffset[NUMPF]={2, 0, 2, 0, 1, 3, 0}; const int _flags[NUMPF]={0, TJ_BGR, 0, TJ_BGR, TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST, 0}; const char *_pfstr[NUMPF]={"RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale"}; const int _3byteformats[]={RGB, BGR}; const int _4byteformats[]={RGBX, BGRX, XBGR, XRGB}; const int _onlygray[]={GRAY}; const int _onlyrgb[]={RGB}; enum {YUVENCODE=1, YUVDECODE}; int yuv=0; int exitstatus=0; #define bailout() {exitstatus=-1; goto bailout;} int pixels[9][3]= { {0, 255, 0}, {255, 0, 255}, {255, 255, 0}, {0, 0, 255}, {0, 255, 255}, {255, 0, 0}, {255, 255, 255}, {0, 0, 0}, {255, 0, 0} }; void initbuf(unsigned char *buf, int w, int h, int pf, int flags) { int ps=_ps[pf], i, _i, j; int roffset=_roffset[pf], goffset=_goffset[pf], boffset=_boffset[pf]; memset(buf, 0, w*h*ps); if(pf==GRAY) { for(_i=0; _i<16; _i++) { if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; for(j=0; jcv+1) { \ printf("\nComp. %s at %d,%d should be %d, not %d\n", #v, i, j, cv, v); \ retval=0; goto bailout; \ }} #define checkval0(v) { \ if(v>1) { \ printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, i, j, v); \ retval=0; goto bailout; \ }} #define checkval255(v) { \ if(v<254) { \ printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, i, j, v); \ retval=0; goto bailout; \ }} int checkbuf(unsigned char *buf, int w, int h, int pf, int subsamp, int scale_num, int scale_denom, int flags) { int ps=_ps[pf]; int roffset=_roffset[pf], goffset=_goffset[pf], boffset=_boffset[pf]; int i, _i, j, retval=1; int halfway=16*scale_num/scale_denom, blocksize=8*scale_num/scale_denom; for(_i=0; _i %s YUV ... ", _pfstr[pf], (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ", _subnamel[subsamp]); else printf("%s %s -> %s Q%d ... ", _pfstr[pf], (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ", _subnamel[subsamp], qual); if((bmpbuf=(unsigned char *)malloc(w*h*ps+1))==NULL) { printf("ERROR: Could not allocate buffer\n"); bailout(); } initbuf(bmpbuf, w, h, pf, flags); memset(jpegbuf, 0, yuv==YUVENCODE? TJBUFSIZEYUV(w, h, subsamp):TJBUFSIZE(w, h)); t=rrtime(); if(yuv==YUVENCODE) { _catch(tjEncodeYUV(hnd, bmpbuf, w, 0, h, ps, jpegbuf, subsamp, flags|_flags[pf])); *size=TJBUFSIZEYUV(w, h, subsamp); } else { _catch(tjCompress(hnd, bmpbuf, w, 0, h, ps, jpegbuf, size, subsamp, qual, flags|_flags[pf])); } t=rrtime()-t; if(yuv==YUVENCODE) snprintf(tempstr, 1024, "%s_enc_%s_%s_%s.yuv", basefilename, _pfstr[pf], (flags&TJ_BOTTOMUP)? "BU":"TD", _subnames[subsamp]); else snprintf(tempstr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basefilename, _pfstr[pf], (flags&TJ_BOTTOMUP)? "BU":"TD", _subnames[subsamp], qual); writejpeg(jpegbuf, *size, tempstr); if(yuv==YUVENCODE) { if(checkbufyuv(jpegbuf, w, h, subsamp)) printf("Passed."); else {printf("FAILED!"); exitstatus=-1;} } else printf("Done."); printf(" %f ms\n Result in %s\n", t*1000., tempstr); bailout: if(bmpbuf) free(bmpbuf); } void _gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize, int w, int h, int pf, char *basefilename, int subsamp, int flags, int scale_num, int scale_denom) { unsigned char *bmpbuf=NULL; int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; double t; int scaledw=(w*scale_num+scale_denom-1)/scale_denom; int scaledh=(h*scale_num+scale_denom-1)/scale_denom; int ps=_ps[pf]; unsigned long size=0; if(yuv==YUVENCODE) return; if(yuv==YUVDECODE) printf("JPEG -> YUV %s ... ", _subnames[subsamp]); else { printf("JPEG -> %s %s ", _pfstr[pf], (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down "); if(scale_num!=1 || scale_denom!=1) printf("%d/%d ... ", scale_num, scale_denom); else printf("... "); } _catch(tjDecompressHeader2(hnd, jpegbuf, jpegsize, &_hdrw, &_hdrh, &_hdrsubsamp)); if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp) { printf("Incorrect JPEG header\n"); bailout(); } if(yuv==YUVDECODE) size=TJBUFSIZEYUV(w, h, subsamp); else size=scaledw*scaledh*ps+1; if((bmpbuf=(unsigned char *)malloc(size))==NULL) { printf("ERROR: Could not allocate buffer\n"); bailout(); } memset(bmpbuf, 0, size); t=rrtime(); if(yuv==YUVDECODE) { _catch(tjDecompressToYUV(hnd, jpegbuf, jpegsize, bmpbuf, flags|_flags[pf])); } else { _catch(tjDecompress(hnd, jpegbuf, jpegsize, bmpbuf, scaledw, 0, scaledh, ps, flags|_flags[pf])); } t=rrtime()-t; if(yuv==YUVDECODE) { if(checkbufyuv(bmpbuf, w, h, subsamp)) printf("Passed."); else {printf("FAILED!"); exitstatus=-1;} } else { if(checkbuf(bmpbuf, scaledw, scaledh, pf, subsamp, scale_num, scale_denom, flags)) printf("Passed."); else {printf("FAILED!"); exitstatus=-1;} } printf(" %f ms\n", t*1000.); bailout: if(bmpbuf) free(bmpbuf); } void gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize, int w, int h, int pf, char *basefilename, int subsamp, int flags) { int i, n=0; tjscalingfactor *sf=tjGetScalingFactors(&n); if(!sf || !n) { printf("Error in tjGetScalingFactors():\n%s\n", tjGetErrorStr()); bailout(); } if((subsamp==TJ_444 || subsamp==TJ_GRAYSCALE) && !yuv) { for(i=0; i1 && !stricmp(argv[1], "-yuv")) doyuv=1; if(doyuv) yuv=YUVENCODE; dotest(35, 39, _3byteformats, 2, TJ_444, "test"); dotest(39, 41, _4byteformats, 4, TJ_444, "test"); if(doyuv) { dotest(41, 35, _3byteformats, 2, TJ_422, "test"); dotest(35, 39, _4byteformats, 4, TJ_422, "test"); dotest(39, 41, _3byteformats, 2, TJ_420, "test"); dotest(41, 35, _4byteformats, 4, TJ_420, "test"); dotest(35, 39, _3byteformats, 2, TJ_440, "test"); dotest(39, 41, _4byteformats, 4, TJ_440, "test"); } dotest(35, 39, _onlygray, 1, TJ_GRAYSCALE, "test"); dotest(39, 41, _3byteformats, 2, TJ_GRAYSCALE, "test"); dotest(41, 35, _4byteformats, 4, TJ_GRAYSCALE, "test"); if(!doyuv) dotest1(); if(doyuv) { yuv=YUVDECODE; dotest(48, 48, _onlyrgb, 1, TJ_444, "test_yuv0"); dotest(35, 39, _onlyrgb, 1, TJ_444, "test_yuv1"); dotest(48, 48, _onlyrgb, 1, TJ_422, "test_yuv0"); dotest(39, 41, _onlyrgb, 1, TJ_422, "test_yuv1"); dotest(48, 48, _onlyrgb, 1, TJ_420, "test_yuv0"); dotest(41, 35, _onlyrgb, 1, TJ_420, "test_yuv1"); dotest(48, 48, _onlyrgb, 1, TJ_440, "test_yuv0"); dotest(35, 39, _onlyrgb, 1, TJ_440, "test_yuv1"); dotest(48, 48, _onlyrgb, 1, TJ_GRAYSCALE, "test_yuv0"); dotest(35, 39, _onlyrgb, 1, TJ_GRAYSCALE, "test_yuv1"); dotest(48, 48, _onlygray, 1, TJ_GRAYSCALE, "test_yuv0"); dotest(39, 41, _onlygray, 1, TJ_GRAYSCALE, "test_yuv1"); } return exitstatus; }