From: DRC Date: Sun, 22 May 2011 13:55:17 +0000 (+0000) Subject: Refactored jpegut to test the new TurboJPEG API. There is no legacy code remaining... X-Git-Tag: 1.1.90~87 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a8b8b472b9fb3da2164bd01de4a05d2f8862c8c3;p=libjpeg-turbo Refactored jpegut to test the new TurboJPEG API. There is no legacy code remaining, so the refactored version of the program has been re-licensed under a BSD-style license. git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@618 632fc199-4ca6-4c93-a231-07263d6284db --- diff --git a/Makefile.am b/Makefile.am index 9237233..8ba8185 100644 --- a/Makefile.am +++ b/Makefile.am @@ -81,7 +81,7 @@ jpgtest_SOURCES = $(TSTHDRS) jpgtest.c bmp.h bmp.c jpgtest_LDADD = libturbojpeg.la -lm -jpegut_SOURCES = $(TSTHDRS) jpegut.c bmp.h bmp.c +jpegut_SOURCES = $(TSTHDRS) jpegut.c bmp.h bmp.c tjutil.h tjutil.c jpegut_LDADD = libturbojpeg.la @@ -144,6 +144,7 @@ if WITH_JAVA $(JAVA) -cp java/turbojpeg.jar -Djava.library.path=.libs TJUnitTest -yuv -bi endif ./jpegut + ./jpegut -alloc ./jpegut -yuv ./cjpeg -dct int -outfile testoutint.jpg $(srcdir)/testorig.ppm cmp $(srcdir)/testimgint.jpg testoutint.jpg diff --git a/jpegut.c b/jpegut.c index a9db8b6..a1efca4 100644 --- a/jpegut.c +++ b/jpegut.c @@ -1,192 +1,216 @@ -/* Copyright (C)2004 Landmark Graphics Corporation - * Copyright (C)2005 Sun Microsystems, Inc. - * Copyright (C)2009-2011 D. R. Commander +/* + * Copyright (C)2009-2011 D. R. Commander. All Rights Reserved. * - * 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. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * 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. + * - 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 JNI Wrapper */ #include #include #include -#include "./rrtimer.h" -#include "./rrutil.h" +#include +#include "./tjutil.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"}; +void usage(char *progName) +{ + printf("\nUSAGE: %s [options]\n", progName); + printf("Options:\n"); + printf("-yuv = test YUV encoding/decoding support\n"); + printf("-alloc = test automatic buffer allocation\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" +}; +const char *subName[TJ_NUMSAMP]={"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 char *pixFormatStr[TJ_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}; +const int _3byteFormats[]={TJPF_RGB, TJPF_BGR}; +const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB}; +const int _onlyGray[]={TJPF_GRAY}; +const int _onlyRGB[]={TJPF_RGB}; enum {YUVENCODE=1, YUVDECODE}; -int yuv=0; +int yuv=0, alloc=0; -int exitstatus=0; -#define bailout() {exitstatus=-1; goto bailout;} +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, 0}, + {0, 0, 255}, {255, 255, 255}, {0, 0, 0}, - {255, 0, 0} + {0, 0, 255} }; -void initbuf(unsigned char *buf, int w, int h, int pf, int flags) + +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]; + int roffset=tjRedOffset[pf]; + int goffset=tjGreenOffset[pf]; + int boffset=tjBlueOffset[pf]; + int ps=tjPixelSize[pf]; + int index, row, col, halfway=16; memset(buf, 0, w*h*ps); - if(pf==GRAY) + if(pf==TJPF_GRAY) { - for(_i=0; _i<16; _i++) + for(row=0; rowcv+1) { \ - printf("\nComp. %s at %d,%d should be %d, not %d\n", #v, i, j, cv, v); \ - retval=0; goto bailout; \ + 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, i, j, v); \ - retval=0; goto bailout; \ + 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, i, j, v); \ - retval=0; goto bailout; \ + 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, - 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]); + printf("%s %s -> %s YUV ... ", pixFormatStr[pf], + (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp]); else - printf("%s %s -> %s Q%d ... ", _pfstr[pf], - (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ", _subnamel[subsamp], qual); + printf("%s %s -> %s Q%d ... ", pixFormatStr[pf], + (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp], + jpegQual); - 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)); + if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL) + _throw("Memory allocation failure"); + initBuf(srcBuf, w, h, pf, flags); + if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize); - t=rrtime(); + t=gettime(); if(yuv==YUVENCODE) { - _catch(tjEncodeYUV(hnd, bmpbuf, w, 0, h, ps, jpegbuf, subsamp, - flags|_flags[pf])); - *size=TJBUFSIZEYUV(w, h, subsamp); + _tj(tjEncodeYUV2(handle, srcBuf, w, 0, h, pf, *dstBuf, subsamp, flags)); } else { - _catch(tjCompress(hnd, bmpbuf, w, 0, h, ps, jpegbuf, size, subsamp, qual, - flags|_flags[pf])); + if(!alloc) + { + flags|=TJFLAG_NOREALLOC; + *dstSize=(yuv==YUVENCODE? TJBUFSIZEYUV(w, h, subsamp):TJBUFSIZE(w, h)); + } + _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp, + jpegQual, flags)); } - t=rrtime()-t; + t=gettime()-t; if(yuv==YUVENCODE) - snprintf(tempstr, 1024, "%s_enc_%s_%s_%s.yuv", basefilename, _pfstr[pf], - (flags&TJ_BOTTOMUP)? "BU":"TD", _subnames[subsamp]); + snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pixFormatStr[pf], + (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[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); + snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, + pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp], + jpegQual); + writeJPEG(*dstBuf, *dstSize, tempStr); if(yuv==YUVENCODE) { - if(checkbufyuv(jpegbuf, w, h, subsamp)) printf("Passed."); - else {printf("FAILED!"); exitstatus=-1;} + if(checkBufYUV(*dstBuf, w, h, subsamp)) printf("Passed."); + else printf("FAILED!"); } else printf("Done."); - printf(" %f ms\n Result in %s\n", t*1000., tempstr); + printf(" %f ms\n Result in %s\n", t*1000., tempStr); bailout: - if(bmpbuf) free(bmpbuf); + if(srcBuf) free(srcBuf); } -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) + +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 *bmpbuf=NULL; + unsigned char *dstBuf=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; + int scaledWidth=TJSCALED(w, sf); + int scaledHeight=TJSCALED(h, sf); + unsigned long dstSize=0; if(yuv==YUVENCODE) return; if(yuv==YUVDECODE) - printf("JPEG -> YUV %s ... ", _subnames[subsamp]); + printf("JPEG -> YUV %s ... ", subName[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); + 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("... "); } - _catch(tjDecompressHeader2(hnd, jpegbuf, jpegsize, &_hdrw, &_hdrh, + _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh, &_hdrsubsamp)); if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp) - { - printf("Incorrect JPEG header\n"); bailout(); - } + _throw("Incorrect JPEG header"); - 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); + if(yuv==YUVDECODE) dstSize=TJBUFSIZEYUV(w, h, subsamp); + else dstSize=scaledWidth*scaledHeight*tjPixelSize[pf]; + if((dstBuf=(unsigned char *)malloc(dstSize))==NULL) + _throw("Memory allocation failure"); + memset(dstBuf, 0, dstSize); - t=rrtime(); + t=gettime(); if(yuv==YUVDECODE) { - _catch(tjDecompressToYUV(hnd, jpegbuf, jpegsize, bmpbuf, - flags|_flags[pf])); + _tj(tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags)); } else { - _catch(tjDecompress(hnd, jpegbuf, jpegsize, bmpbuf, scaledw, 0, scaledh, - ps, flags|_flags[pf])); + _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0, + scaledHeight, pf, flags)); } - t=rrtime()-t; + t=gettime()-t; if(yuv==YUVDECODE) { - if(checkbufyuv(bmpbuf, w, h, subsamp)) printf("Passed."); - else {printf("FAILED!"); exitstatus=-1;} + if(checkBufYUV(dstBuf, w, h, subsamp)) printf("Passed."); + printf("FAILED!"); } else { - if(checkbuf(bmpbuf, scaledw, scaledh, pf, subsamp, scale_num, scale_denom, - flags)) printf("Passed."); - else {printf("FAILED!"); exitstatus=-1;} + if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags)) + printf("Passed."); + else printf("FAILED!"); } printf(" %f ms\n", t*1000.); bailout: - if(bmpbuf) free(bmpbuf); + if(dstBuf) free(dstBuf); } -void gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize, - int w, int h, int pf, char *basefilename, int subsamp, int flags) + +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) - { - printf("Error in tjGetScalingFactors():\n%s\n", tjGetErrorStr()); - bailout(); - } + tjscalingfactor *sf=tjGetScalingFactors(&n), sf1={1, 1}; + if(!sf || !n) _throwtj(); - if((subsamp==TJ_444 || subsamp==TJ_GRAYSCALE) && !yuv) + if((subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY) && !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"); + int doyuv=0, i; + if(argc>1) + { + for(i=1; i + +static double getfreq(void) +{ + LARGE_INTEGER freq; + if(!QueryPerformanceFrequency(&freq)) return 0.0; + return (double)freq.QuadPart; +} + +static double f=getfreq(); + +double gettime(void) +{ + LARGE_INTEGER t; + if(f==0.0) return (double)GetTickCount()/1000.; + else + { + QueryPerformanceCounter(&t); + return (double)t.QuadPart/f; + } +} + +#else + +#include + +double gettime(void) +{ + struct timeval tv; + if(gettimeofday(&tv, NULL)<0) return 0.0; + else return (double)tv.tv_sec+((double)tv.tv_usec/1000000.); +} + +#endif diff --git a/tjutil.h b/tjutil.h new file mode 100644 index 0000000..5189caf --- /dev/null +++ b/tjutil.h @@ -0,0 +1,39 @@ +/* + * Copyright (C)2011 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. + */ + +#ifdef _WIN32 + #ifndef __MINGW32__ + #include + #define snprintf(str, n, format, ...) \ + _snprintf_s(str, n, _TRUNCATE, format, __VA_ARGS__) + #endif + #define strcasecmp stricmp + #define strncasecmp strnicmp +#endif + +extern double gettime(void);