]> granicus.if.org Git - libjpeg-turbo/commitdiff
Add TJBUFSIZEYUV() convenience function
authorDRC <dcommander@users.sourceforge.net>
Tue, 22 Feb 2011 00:16:14 +0000 (00:16 +0000)
committerDRC <dcommander@users.sourceforge.net>
Tue, 22 Feb 2011 00:16:14 +0000 (00:16 +0000)
git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@419 632fc199-4ca6-4c93-a231-07263d6284db

jpegut.c
turbojpeg.h
turbojpegl.c

index 31db1d06843492c4f2d021ba6be11786e3ad1e6d..10b5966a338aaae294096136f6cc2dee36210357 100644 (file)
--- a/jpegut.c
+++ b/jpegut.c
@@ -410,12 +410,8 @@ void _gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize,
 {
        unsigned char *bmpbuf=NULL;
        const char *pixformat;  int _hdrw=0, _hdrh=0, _hdrsubsamp=-1;  double t;
-       unsigned long size=0;
-       int hsf=_hsf[subsamp], vsf=_vsf[subsamp];
-       int pw=PAD(w, hsf), ph=PAD(h, vsf);
        int _w=(w+scalefactor-1)/scalefactor, _h=(h+scalefactor-1)/scalefactor;
-       int cw=pw/hsf, ch=ph/vsf;
-       int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4);
+       unsigned long size=0;
 
        if(yuv==YUVDECODE) flags|=TJ_YUV;
        else if(yuv==YUVENCODE) return;
@@ -449,7 +445,7 @@ void _gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize,
        }
 
        if(yuv==YUVDECODE)
-               size=ypitch*ph + (subsamp==TJ_GRAYSCALE? 0:uvpitch*ch*2);
+               size=TJBUFSIZEYUV(w, h, subsamp);
        else
                size=_w*_h*ps;
        if((bmpbuf=(unsigned char *)malloc(size+1))==NULL)
@@ -465,7 +461,7 @@ void _gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize,
 
        if(yuv==YUVDECODE)
        {
-               if(checkbufyuv(bmpbuf, size, pw, ph, subsamp))
+               if(checkbufyuv(bmpbuf, size, w, h, subsamp))
                        printf("Passed.");
                else {printf("FAILED!");  exitstatus=-1;}
        }
index 48d8e862c5ca4e3dfcf29473a2926fe8b092cdad..32952daed449e17d73b3fda143986f69ad2de053 100644 (file)
@@ -153,8 +153,28 @@ DLLEXPORT int DLLCALL tjCompress(tjhandle j,
        unsigned char *dstbuf, unsigned long *size,
        int jpegsubsamp, int jpegqual, int flags);
 
+/*
+  unsigned long TJBUFSIZE(int width, int height)
+
+  Convenience function which returns the maximum size of the buffer required to
+  hold a JPEG image with the given width and height
+
+  RETURNS: -1 if arguments are out of bounds
+*/
 DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height);
 
+/*
+  unsigned long TJBUFSIZEYUV(int width, int height, int subsamp)
+
+  Convenience function which returns the size of the buffer required to
+  hold a YUV planar image with the given width, height, and level of
+  chrominance subsampling
+
+  RETURNS: -1 if arguments are out of bounds
+*/
+DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
+  int subsamp);
+
 /*
   tjhandle tjInitDecompress(void)
 
index 1e48234fdc97ced999ccc0e752a30d46bce8989e..8ba7fccca94a6da95b24f279d18bc89e4629b650 100644 (file)
@@ -114,8 +114,33 @@ DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
 
 DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
 {
-       // This allows enough room in case the image doesn't compress
-       return ((width+15)&(~15)) * ((height+15)&(~15)) * 6 + 2048;
+       unsigned long retval=0;
+       if(width<1 || height<1)
+               _throw("Invalid argument in TJBUFSIZE()");
+
+       // This allows for rare corner cases in which a JPEG image can actually be
+       // larger than the uncompressed input (we wouldn't mention it if it hadn't
+       // happened.)
+       retval=((width+15)&(~15)) * ((height+15)&(~15)) * 6 + 2048;
+
+       bailout:
+       return retval;
+}
+
+DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
+       int subsamp)
+{
+       unsigned long retval=0;
+       int pw, ph, cw, ch;
+       if(width<1 || height<1 || subsamp<0 || subsamp>=NUMSUBOPT)
+               _throw("Invalid argument in TJBUFSIZEYUV()");
+       pw=PAD(width, hsampfactor[subsamp]);
+       ph=PAD(height, vsampfactor[subsamp]);
+       cw=pw/hsampfactor[subsamp];  ch=ph/vsampfactor[subsamp];
+       retval=PAD(pw, 4)*ph + (subsamp==TJ_GRAYSCALE? 0:PAD(cw, 4)*ch*2);
+
+       bailout:
+       return retval;
 }
 
 DLLEXPORT int DLLCALL tjCompress(tjhandle h,