From 842416034561f6d5320165a4fe98825e999a4a37 Mon Sep 17 00:00:00 2001 From: DRC Date: Fri, 25 Feb 2011 02:08:23 +0000 Subject: [PATCH] Replace the TJ_YUV flag with two new API functions git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@448 632fc199-4ca6-4c93-a231-07263d6284db --- jpegut.c | 59 ++++++++++---------- jpgtest.c | 70 ++++++++++++++---------- turbojpeg-mapfile | 2 + turbojpeg-mapfile.jni | 2 + turbojpeg.h | 121 +++++++++++++++++++++++++++++++----------- turbojpegl.c | 62 ++++++++++++++-------- 6 files changed, 208 insertions(+), 108 deletions(-) diff --git a/jpegut.c b/jpegut.c index c974129..995d544 100644 --- a/jpegut.c +++ b/jpegut.c @@ -229,8 +229,7 @@ int checkbuf(unsigned char *buf, int w, int h, int ps, int subsamp, #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) -int checkbufyuv(unsigned char *buf, unsigned long size, int w, int h, - int subsamp) +int checkbufyuv(unsigned char *buf, int w, int h, int subsamp) { int i, j; int hsf=_hsf[subsamp], vsf=_vsf[subsamp]; @@ -238,13 +237,6 @@ int checkbufyuv(unsigned char *buf, unsigned long size, int w, int h, int cw=pw/hsf, ch=ph/vsf; int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4); int retval=1; - unsigned long correctsize=ypitch*ph + (subsamp==TJ_GRAYSCALE? 0:uvpitch*ch*2); - - if(size!=correctsize) - { - printf("\nIncorrect size %lu. Should be %lu\n", size, correctsize); - retval=0; goto bailout; - } for(i=0; i<16; i++) { @@ -353,8 +345,6 @@ void gentestjpeg(tjhandle hnd, unsigned char *jpegbuf, unsigned long *size, char tempstr[1024]; unsigned char *bmpbuf=NULL; const char *pixformat; double t; - if(yuv==YUVENCODE) flags|=TJ_YUV; - if(flags&TJ_BGR) { if(ps==3) pixformat="BGR"; @@ -378,11 +368,20 @@ void gentestjpeg(tjhandle hnd, unsigned char *jpegbuf, unsigned long *size, printf("ERROR: Could not allocate buffer\n"); bailout(); } initbuf(bmpbuf, w, h, ps, flags); - memset(jpegbuf, 0, TJBUFSIZE(w, h)); + memset(jpegbuf, 0, + yuv==YUVENCODE? TJBUFSIZEYUV(w, h, subsamp):TJBUFSIZE(w, h)); t=rrtime(); - _catch(tjCompress(hnd, bmpbuf, w, 0, h, ps, jpegbuf, size, subsamp, qual, - flags)); + if(yuv==YUVENCODE) + { + _catch(tjEncodeYUV(hnd, bmpbuf, w, 0, h, ps, jpegbuf, subsamp, flags)); + *size=TJBUFSIZEYUV(w, h, subsamp); + } + else + { + _catch(tjCompress(hnd, bmpbuf, w, 0, h, ps, jpegbuf, size, subsamp, qual, + flags)); + } t=rrtime()-t; if(yuv==YUVENCODE) @@ -394,7 +393,7 @@ void gentestjpeg(tjhandle hnd, unsigned char *jpegbuf, unsigned long *size, writejpeg(jpegbuf, *size, tempstr); if(yuv==YUVENCODE) { - if(checkbufyuv(jpegbuf, *size, w, h, subsamp)) printf("Passed."); + if(checkbufyuv(jpegbuf, w, h, subsamp)) printf("Passed."); else {printf("FAILED!"); exitstatus=-1;} } else printf("Done."); @@ -414,8 +413,7 @@ void _gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize, int temp1, temp2; unsigned long size=0; - if(yuv==YUVDECODE) flags|=TJ_YUV; - else if(yuv==YUVENCODE) return; + if(yuv==YUVENCODE) return; if(flags&TJ_BGR) { @@ -452,25 +450,29 @@ void _gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize, printf("Scaled size mismatch\n"); bailout(); } - if(yuv==YUVDECODE) - size=TJBUFSIZEYUV(w, h, subsamp); - else - size=scaledw*scaledh*ps; - if((bmpbuf=(unsigned char *)malloc(size+1))==NULL) + 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+1); + memset(bmpbuf, 0, size); t=rrtime(); - _catch(tjDecompress(hnd, jpegbuf, jpegsize, bmpbuf, scaledw, 0, scaledh, ps, - flags)); + if(yuv==YUVDECODE) + { + _catch(tjDecompressToYUV(hnd, jpegbuf, jpegsize, bmpbuf, flags)); + } + else + { + _catch(tjDecompress(hnd, jpegbuf, jpegsize, bmpbuf, scaledw, 0, scaledh, ps, + flags)); + } t=rrtime()-t; if(yuv==YUVDECODE) { - if(checkbufyuv(bmpbuf, size, w, h, subsamp)) - printf("Passed."); + if(checkbufyuv(bmpbuf, w, h, subsamp)) printf("Passed."); else {printf("FAILED!"); exitstatus=-1;} } else @@ -510,7 +512,8 @@ void dotest(int w, int h, int ps, int subsamp, char *basefilename) tjhandle hnd=NULL, dhnd=NULL; unsigned char *jpegbuf=NULL; unsigned long size; - if((jpegbuf=(unsigned char *)malloc(TJBUFSIZE(w, h))) == NULL) + size=(yuv==YUVENCODE? TJBUFSIZEYUV(w, h, subsamp):TJBUFSIZE(w, h)); + if((jpegbuf=(unsigned char *)malloc(size)) == NULL) { puts("ERROR: Could not allocate buffer."); bailout(); } diff --git a/jpgtest.c b/jpgtest.c index 1e79c30..1af91c7 100644 --- a/jpgtest.c +++ b/jpgtest.c @@ -44,8 +44,6 @@ const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3}; const char *_pfname[]={"RGB", "RGBX", "BGR", "BGRX", "XBGR", "XRGB"}; const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"}; const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"}; -const int _hsf[NUMSUBOPT]={1, 2, 2, 1}; -const int _vsf[NUMSUBOPT]={1, 1, 2, 1}; void printsigfig(double val, int figs) { @@ -78,13 +76,9 @@ int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf, int i, j, ITER, rgbbufalloc=0; double start, elapsed; int ps=_ps[pf]; - int hsf=_hsf[jpegsub], vsf=_vsf[jpegsub]; - int pw=PAD(w, hsf), ph=PAD(h, vsf); - int cw=pw/hsf, ch=ph/vsf; - int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4); - int yuvsize=ypitch*ph + (jpegsub==TJ_GRAYSCALE? 0:uvpitch*ch*2); - int scaledw=(flags&TJ_YUV)? w : (w+scalefactor-1)/scalefactor; - int scaledh=(flags&TJ_YUV)? h : (h+scalefactor-1)/scalefactor; + int yuvsize=TJBUFSIZEYUV(w, h, jpegsub), bufsize; + int scaledw=(yuv==YUVDECODE)? w : (w+scalefactor-1)/scalefactor; + int scaledh=(yuv==YUVDECODE)? h : (h+scalefactor-1)/scalefactor; int pitch=scaledw*ps; if(qual>0) @@ -95,21 +89,26 @@ int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf, flags |= _flags[pf]; if(bu) flags |= TJ_BOTTOMUP; - if(yuv==YUVDECODE) flags |= TJ_YUV; if((hnd=tjInitDecompress())==NULL) _throwtj("executing tjInitDecompress()"); if(rgbbuf==NULL) { - if((rgbbuf=(unsigned char *)malloc(max(yuvsize, pitch*scaledh))) == NULL) + bufsize=(yuv==YUVDECODE? yuvsize:pitch*h); + if((rgbbuf=(unsigned char *)malloc(bufsize)) == NULL) _throwunix("allocating image buffer"); rgbbufalloc=1; } // Grey image means decompressor did nothing - memset(rgbbuf, 127, max(yuvsize, pitch*scaledh)); + memset(rgbbuf, 127, bufsize); - if(tjDecompress(hnd, jpegbuf[0], comptilesize[0], rgbbuf, scaledw, pitch, - scaledh, ps, flags)==-1) + if(yuv==YUVDECODE) + { + if(tjDecompressToYUV(hnd, jpegbuf[0], comptilesize[0], rgbbuf, flags)==-1) + _throwtj("executing tjDecompressToYUV()"); + } + else if(tjDecompress(hnd, jpegbuf[0], comptilesize[0], rgbbuf, scaledw, + pitch, scaledh, ps, flags)==-1) _throwtj("executing tjDecompress()"); ITER=0; start=rrtime(); @@ -121,14 +120,20 @@ int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf, for(j=0; jdinfo, TRUE); - jpegwidth=j->dinfo.image_width; jpegheight=j->dinfo.image_height; - if(width==0) width=jpegwidth; - if(height==0) height=jpegheight; - if(widthwidth || scaledh>height) - _throw("Could not scale down to desired image dimensions"); - width=scaledw; height=scaledh; - scale_denom=i; - } - if(flags&TJ_YUV) { j_decompress_ptr dinfo=&j->dinfo; @@ -549,7 +544,7 @@ DLLEXPORT int DLLCALL tjDecompress(tjhandle h, th[i]=compptr->v_samp_factor*DCTSIZE; tmpbufsize+=iw[i]*th[i]; if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL) - _throw("Memory allocation failed in tjInitDecompress()"); + _throw("Memory allocation failed in tjDecompress()"); for(row=0; rownum_components; i++) { jpeg_component_info *compptr=&dinfo->comp_info[i]; if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL) - _throw("Memory allocation failed in tjInitDecompress()"); + _throw("Memory allocation failed in tjDecompress()"); for(row=0; rowdinfo.raw_data_out=TRUE; else { + jpegwidth=j->dinfo.image_width; jpegheight=j->dinfo.image_height; + if(width==0) width=jpegwidth; + if(height==0) height=jpegheight; + if(widthwidth || scaledh>height) + _throw("Could not scale down to desired image dimensions"); + width=scaledw; height=scaledh; + scale_denom=i; + } j->dinfo.scale_num=scale_num; j->dinfo.scale_denom=scale_denom; } @@ -664,6 +676,14 @@ DLLEXPORT int DLLCALL tjDecompress(tjhandle h, } +DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle h, + unsigned char *srcbuf, unsigned long size, + unsigned char *dstbuf, int flags) +{ + return tjDecompress(h, srcbuf, size, dstbuf, 1, 0, 1, 3, flags|TJ_YUV); +} + + // General DLLEXPORT char* DLLCALL tjGetErrorStr(void) -- 2.40.0