"jpegut"="tjunittest" & "jpgtest"="tjbench"


git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@644 632fc199-4ca6-4c93-a231-07263d6284db
diff --git a/tjbench.c b/tjbench.c
new file mode 100644
index 0000000..c58afa9
--- /dev/null
+++ b/tjbench.c
@@ -0,0 +1,873 @@
+/*
+ * Copyright (C)2009-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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <errno.h>
+#include <cdjpeg.h>
+#include "./bmp.h"
+#include "./tjutil.h"
+#include "./turbojpeg.h"
+
+
+#define _throw(op, err) {  \
+	printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err);  \
+  retval=-1;  goto bailout;}
+#define _throwunix(m) _throw(m, strerror(errno))
+#define _throwtj(m) _throw(m, tjGetErrorStr())
+#define _throwbmp(m) _throw(m, bmpgeterr())
+
+enum {YUVENCODE=1, YUVDECODE};
+int flags=TJFLAG_NOREALLOC, decomponly=0, yuv=0, quiet=0, dotile=0,
+	pf=TJPF_BGR;
+char *ext="ppm";
+const char *pixFormatStr[TJ_NUMPF]=
+{
+	"RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY"
+};
+const char *subNameLong[TJ_NUMSAMP]=
+{
+	"4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0"
+};
+const char *subName[NUMSUBOPT]={"444", "422", "420", "GRAY", "440"};
+tjscalingfactor *scalingfactors=NULL, sf={1, 1};  int nsf=0;
+int xformop=TJXOP_NONE, xformopt=0;
+double benchtime=5.0;
+
+
+char *sigfig(double val, int figs, char *buf, int len)
+{
+	char format[80];
+	int digitsafterdecimal=figs-(int)ceil(log10(fabs(val)));
+	if(digitsafterdecimal<1) snprintf(format, 80, "%%.0f");
+	else snprintf(format, 80, "%%.%df", digitsafterdecimal);
+	snprintf(buf, len, format, val);
+	return buf;
+}
+
+
+/* Decompression test */
+int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf,
+	unsigned long *jpegsize, unsigned char *dstbuf, int w, int h,
+	int subsamp, int jpegqual, char *filename, int tilew, int tileh)
+{
+	char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr;
+	FILE *file=NULL;  tjhandle handle=NULL;
+	int row, col, i, dstbufalloc=0, retval=0;
+	double start, elapsed;
+	int ps=tjPixelSize[pf];
+	int yuvsize=TJBUFSIZEYUV(w, h, subsamp), bufsize;
+	int scaledw=(yuv==YUVDECODE)? w : TJSCALED(w, sf);
+	int scaledh=(yuv==YUVDECODE)? h : TJSCALED(h, sf);
+	int pitch=scaledw*ps;
+	int ntilesw=(w+tilew-1)/tilew, ntilesh=(h+tileh-1)/tileh;
+	unsigned char *dstptr, *dstptr2;
+
+	if(jpegqual>0)
+	{
+		snprintf(qualstr, 6, "_Q%d", jpegqual);
+		qualstr[5]=0;
+	}
+
+	if((handle=tjInitDecompress())==NULL)
+		_throwtj("executing tjInitDecompress()");
+
+	bufsize=(yuv==YUVDECODE? yuvsize:pitch*h);
+	if(dstbuf==NULL)
+	{
+		if((dstbuf=(unsigned char *)malloc(bufsize)) == NULL)
+			_throwunix("allocating image buffer");
+		dstbufalloc=1;
+	}
+	/* Set the destination buffer to gray so we know whether the decompressor
+	   attempted to write to it */
+	memset(dstbuf, 127, bufsize);
+
+	/* Execute once to preload cache */
+	if(yuv==YUVDECODE)
+	{
+		if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1)
+			_throwtj("executing tjDecompressToYUV()");
+	}
+	else if(tjDecompress2(handle, jpegbuf[0], jpegsize[0], dstbuf, scaledw,
+		pitch, scaledh, pf, flags)==-1)
+		_throwtj("executing tjDecompress2()");
+
+	/* Benchmark */
+	for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
+	{
+		int tile=0;
+		if(yuv==YUVDECODE)
+		{
+			if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1)
+			_throwtj("executing tjDecompressToYUV()");
+		}
+		else for(row=0, dstptr=dstbuf; row<ntilesh; row++, dstptr+=pitch*tileh)
+		{
+			for(col=0, dstptr2=dstptr; col<ntilesw; col++, tile++, dstptr2+=ps*tilew)
+			{
+				int width=dotile? min(tilew, w-col*tilew):scaledw;
+				int height=dotile? min(tileh, h-row*tileh):scaledh;
+				if(tjDecompress2(handle, jpegbuf[tile], jpegsize[tile], dstptr2, width,
+					pitch, height, pf, flags)==-1)
+					_throwtj("executing tjDecompress2()");
+			}
+		}
+	}
+
+	if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
+	handle=NULL;
+
+	if(quiet)
+	{
+		printf("%s\n",
+			sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024));
+	}
+	else
+	{
+		printf("D--> Frame rate:           %f fps\n", (double)i/elapsed);
+		printf("     Dest. throughput:     %f Megapixels/sec\n",
+			(double)(w*h)/1000000.*(double)i/elapsed);
+	}
+	if(yuv==YUVDECODE)
+	{
+		snprintf(tempstr, 1024, "%s_%s%s.yuv", filename, subName[subsamp],
+			qualstr);
+		if((file=fopen(tempstr, "wb"))==NULL)
+			_throwunix("opening YUV image for output");
+		if(fwrite(dstbuf, yuvsize, 1, file)!=1)
+			_throwunix("writing YUV image");
+		fclose(file);  file=NULL;
+	}
+	else
+	{
+		if(sf.num!=1 || sf.denom!=1)
+			snprintf(sizestr, 20, "%d_%d", sf.num, sf.denom);
+		else if(tilew!=w || tileh!=h)
+			snprintf(sizestr, 20, "%dx%d", tilew, tileh);
+		else snprintf(sizestr, 20, "full");
+		if(decomponly)
+			snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr, ext);
+		else
+			snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename, subName[subsamp],
+				qualstr, sizestr, ext);
+		if(savebmp(tempstr, dstbuf, scaledw, scaledh, pf,
+			(flags&TJFLAG_BOTTOMUP)!=0)==-1)
+			_throwbmp("saving bitmap");
+		ptr=strrchr(tempstr, '.');
+		snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", ext);
+		if(srcbuf && sf.num==1 && sf.denom==1)
+		{
+			if(!quiet) printf("Compression error written to %s.\n", tempstr);
+			if(subsamp==TJ_GRAYSCALE)
+			{
+				int index, index2;
+				for(row=0, index=0; row<h; row++, index+=pitch)
+				{
+					for(col=0, index2=index; col<w; col++, index2+=ps)
+					{
+						int rindex=index2+tjRedOffset[pf];
+						int gindex=index2+tjGreenOffset[pf];
+						int bindex=index2+tjBlueOffset[pf];
+						int y=(int)((double)srcbuf[rindex]*0.299
+							+ (double)srcbuf[gindex]*0.587
+							+ (double)srcbuf[bindex]*0.114 + 0.5);
+						if(y>255) y=255;  if(y<0) y=0;
+						dstbuf[rindex]=abs(dstbuf[rindex]-y);
+						dstbuf[gindex]=abs(dstbuf[gindex]-y);
+						dstbuf[bindex]=abs(dstbuf[bindex]-y);
+					}
+				}
+			}		
+			else
+			{
+				for(row=0; row<h; row++)
+					for(col=0; col<w*ps; col++)
+						dstbuf[pitch*row+col]
+							=abs(dstbuf[pitch*row+col]-srcbuf[pitch*row+col]);
+			}
+			if(savebmp(tempstr, dstbuf, w, h, pf,
+				(flags&TJFLAG_BOTTOMUP)!=0)==-1)
+				_throwbmp("saving bitmap");
+		}
+	}
+
+	bailout:
+	if(file) {fclose(file);  file=NULL;}
+	if(handle) {tjDestroy(handle);  handle=NULL;}
+	if(dstbuf && dstbufalloc) {free(dstbuf);  dstbuf=NULL;}
+	return retval;
+}
+
+
+void dotestyuv(unsigned char *srcbuf, int w, int h, int subsamp,
+	char *filename)
+{
+	char tempstr[1024], tempstr2[80];
+	FILE *file=NULL;  tjhandle handle=NULL;
+	unsigned char *dstbuf=NULL;
+	double start, elapsed;
+	int i, retval=0, ps=tjPixelSize[pf];
+	int yuvsize=0;
+
+	yuvsize=TJBUFSIZEYUV(w, h, subsamp);
+	if((dstbuf=(unsigned char *)malloc(yuvsize)) == NULL)
+		_throwunix("allocating image buffer");
+
+	if(!quiet)
+		printf(">>>>>  %s (%s) <--> YUV %s  <<<<<\n", pixFormatStr[pf],
+			(flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp]);
+
+	if(quiet==1)
+		printf("%s\t%s\t%s\tN/A\t", pixFormatStr[pf],
+			(flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]);
+
+	if((handle=tjInitCompress())==NULL)
+		_throwtj("executing tjInitCompress()");
+
+	/* Execute once to preload cache */
+	if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1)
+		_throwtj("executing tjEncodeYUV2()");
+
+	/* Benchmark */
+	for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
+	{
+		if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1)
+			_throwtj("executing tjEncodeYUV2()");
+	}
+
+	if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
+	handle=NULL;
+
+	if(quiet==1) printf("%-4d  %-4d\t", w, h);
+	if(quiet)
+	{
+		printf("%s%c%s%c",
+			sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024),
+			quiet==2? '\n':'\t',
+			sigfig((double)(w*h*ps)/(double)yuvsize, 4, tempstr2, 80),
+			quiet==2? '\n':'\t');
+	}
+	else
+	{
+		printf("\n%s size: %d x %d\n", "Image", w, h);
+		printf("C--> Frame rate:           %f fps\n", (double)i/elapsed);
+		printf("     Output image size:    %d bytes\n", yuvsize);
+		printf("     Compression ratio:    %f:1\n",
+			(double)(w*h*ps)/(double)yuvsize);
+		printf("     Source throughput:    %f Megapixels/sec\n",
+			(double)(w*h)/1000000.*(double)i/elapsed);
+		printf("     Output bit stream:    %f Megabits/sec\n",
+			(double)yuvsize*8./1000000.*(double)i/elapsed);
+	}
+	snprintf(tempstr, 1024, "%s_%s.yuv", filename, subName[subsamp]);
+	if((file=fopen(tempstr, "wb"))==NULL)
+		_throwunix("opening reference image");
+	if(fwrite(dstbuf, yuvsize, 1, file)!=1)
+		_throwunix("writing reference image");
+	fclose(file);  file=NULL;
+	if(!quiet) printf("Reference image written to %s\n", tempstr);
+
+	bailout:
+	if(file) {fclose(file);  file=NULL;}
+	if(dstbuf) {free(dstbuf);  dstbuf=NULL;}
+	if(handle) {tjDestroy(handle);  handle=NULL;}
+	return;
+}
+
+
+void dotest(unsigned char *srcbuf, int w, int h, int subsamp, int jpegqual,
+	char *filename)
+{
+	char tempstr[1024], tempstr2[80];
+	FILE *file=NULL;  tjhandle handle=NULL;
+	unsigned char **jpegbuf=NULL, *tmpbuf=NULL, *srcptr, *srcptr2;
+	double start, elapsed;
+	int totaljpegsize=0, row, col, i, tilew=w, tileh=h, retval=0;
+	unsigned long *jpegsize=NULL;
+	int ps=tjPixelSize[pf], ntilesw=1, ntilesh=1, pitch=w*ps;
+
+	if(yuv==YUVENCODE) {dotestyuv(srcbuf, w, h, subsamp, filename);  return;}
+
+	if((tmpbuf=(unsigned char *)malloc(pitch*h)) == NULL)
+		_throwunix("allocating temporary image buffer");
+
+	if(!quiet)
+		printf(">>>>>  %s (%s) <--> JPEG %s Q%d  <<<<<\n", pixFormatStr[pf],
+			(flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp],
+			jpegqual);
+
+	for(tilew=dotile? 8:w, tileh=dotile? 8:h; ; tilew*=2, tileh*=2)
+	{
+		if(tilew>w) tilew=w;  if(tileh>h) tileh=h;
+		ntilesw=(w+tilew-1)/tilew;  ntilesh=(h+tileh-1)/tileh;
+
+		if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
+			*ntilesw*ntilesh))==NULL)
+			_throwunix("allocating JPEG tile array");
+		memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
+		if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
+			*ntilesw*ntilesh))==NULL)
+			_throwunix("allocating JPEG size array");
+		memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
+
+		if((flags&TJFLAG_NOREALLOC)!=0)
+			for(i=0; i<ntilesw*ntilesh; i++)
+			{
+				if((jpegbuf[i]=(unsigned char *)malloc(TJBUFSIZE(tilew, tileh)))==NULL)
+					_throwunix("allocating JPEG tiles");
+			}
+
+		/* Compression test */
+		if(quiet==1)
+			printf("%s\t%s\t%s\t%d\t", pixFormatStr[pf],
+				(flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp], jpegqual);
+		for(i=0; i<h; i++)
+			memcpy(&tmpbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
+		if((handle=tjInitCompress())==NULL)
+			_throwtj("executing tjInitCompress()");
+
+		/* Execute once to preload cache */
+		if(tjCompress2(handle, srcbuf, tilew, pitch, tileh, pf, &jpegbuf[0],
+			&jpegsize[0], subsamp, jpegqual, flags)==-1)
+			_throwtj("executing tjCompress2()");
+
+		/* Benchmark */
+		for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
+		{
+			int tile=0;
+			totaljpegsize=0;
+			for(row=0, srcptr=srcbuf; row<ntilesh; row++, srcptr+=pitch*tileh)
+			{
+				for(col=0, srcptr2=srcptr; col<ntilesw; col++, tile++,
+					srcptr2+=ps*tilew)
+				{
+					int width=min(tilew, w-col*tilew);
+					int height=min(tileh, h-row*tileh);
+					if(tjCompress2(handle, srcptr2, width, pitch, height, pf,
+						&jpegbuf[tile], &jpegsize[tile], subsamp, jpegqual, flags)==-1)
+						_throwtj("executing tjCompress()2");
+					totaljpegsize+=jpegsize[tile];
+				}
+			}
+		}
+
+		if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
+		handle=NULL;
+
+		if(quiet==1) printf("%-4d  %-4d\t", tilew, tileh);
+		if(quiet)
+		{
+			printf("%s%c%s%c",
+				sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024),
+				quiet==2? '\n':'\t',
+				sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
+				quiet==2? '\n':'\t');
+		}
+		else
+		{
+			printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilew,
+				tileh);
+			printf("C--> Frame rate:           %f fps\n", (double)i/elapsed);
+			printf("     Output image size:    %d bytes\n", totaljpegsize);
+			printf("     Compression ratio:    %f:1\n",
+				(double)(w*h*ps)/(double)totaljpegsize);
+			printf("     Source throughput:    %f Megapixels/sec\n",
+				(double)(w*h)/1000000.*(double)i/elapsed);
+			printf("     Output bit stream:    %f Megabits/sec\n",
+				(double)totaljpegsize*8./1000000.*(double)i/elapsed);
+		}
+		if(tilew==w && tileh==h)
+		{
+			snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, subName[subsamp],
+				jpegqual);
+			if((file=fopen(tempstr, "wb"))==NULL)
+				_throwunix("opening reference image");
+			if(fwrite(jpegbuf[0], jpegsize[0], 1, file)!=1)
+				_throwunix("writing reference image");
+			fclose(file);  file=NULL;
+			if(!quiet) printf("Reference image written to %s\n", tempstr);
+		}
+
+		/* Decompression test */
+		if(decomptest(srcbuf, jpegbuf, jpegsize, tmpbuf, w, h, subsamp, jpegqual,
+			filename, tilew, tileh)==-1)
+			goto bailout;
+
+		for(i=0; i<ntilesw*ntilesh; i++)
+		{
+			if(jpegbuf[i]) free(jpegbuf[i]);  jpegbuf[i]=NULL;
+		}
+		free(jpegbuf);  jpegbuf=NULL;
+		free(jpegsize);  jpegsize=NULL;
+
+		if(tilew==w && tileh==h) break;
+	}
+
+	bailout:
+	if(file) {fclose(file);  file=NULL;}
+	if(jpegbuf)
+	{
+		for(i=0; i<ntilesw*ntilesh; i++)
+		{
+			if(jpegbuf[i]) free(jpegbuf[i]);  jpegbuf[i]=NULL;
+		}
+		free(jpegbuf);  jpegbuf=NULL;
+	}
+	if(jpegsize) {free(jpegsize);  jpegsize=NULL;}
+	if(tmpbuf) {free(tmpbuf);  tmpbuf=NULL;}
+	if(handle) {tjDestroy(handle);  handle=NULL;}
+	return;
+}
+
+
+void dodecomptest(char *filename)
+{
+	FILE *file=NULL;  tjhandle handle=NULL;
+	unsigned char **jpegbuf=NULL, *srcbuf=NULL;
+	unsigned long *jpegsize=NULL, srcsize, totaljpegsize;
+	tjtransform *t=NULL;
+	int w=0, h=0, subsamp=-1, _w, _h, _tilew, _tileh,
+		_ntilesw, _ntilesh, _subsamp;
+	char *temp=NULL, tempstr[80], tempstr2[80];
+	int row, col, i, tilew, tileh, ntilesw, ntilesh, retval=0;
+	double start, elapsed;
+	int ps=tjPixelSize[pf], tile;
+
+	if((file=fopen(filename, "rb"))==NULL)
+		_throwunix("opening file");
+	if(fseek(file, 0, SEEK_END)<0 || (srcsize=ftell(file))<0)
+		_throwunix("determining file size");
+	if((srcbuf=(unsigned char *)malloc(srcsize))==NULL)
+		_throwunix("allocating memory");
+	if(fseek(file, 0, SEEK_SET)<0)
+		_throwunix("setting file position");
+	if(fread(srcbuf, srcsize, 1, file)<1)
+		_throwunix("reading JPEG data");
+	fclose(file);  file=NULL;
+
+	temp=strrchr(filename, '.');
+	if(temp!=NULL) *temp='\0';
+
+	if((handle=tjInitTransform())==NULL)
+		_throwtj("executing tjInitTransform()");
+	if(tjDecompressHeader2(handle, srcbuf, srcsize, &w, &h, &subsamp)==-1)
+		_throwtj("executing tjDecompressHeader2()");
+
+	if(quiet==1)
+	{
+		printf("All performance values in Mpixels/sec\n\n");
+		printf("Bitmap\tBitmap\tJPEG\t%s %s \tXform\tComp\tDecomp\n",
+			dotile? "Tile ":"Image", dotile? "Tile ":"Image");
+		printf("Format\tOrder\tSubsamp\tWidth Height\tPerf \tRatio\tPerf\n\n");
+	}
+	else if(!quiet)
+	{
+		printf(">>>>>  JPEG %s --> %s (%s)  <<<<<\n", subNameLong[subsamp],
+			pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down");
+	}
+
+	for(tilew=dotile? 16:w, tileh=dotile? 16:h; ; tilew*=2, tileh*=2)
+	{
+		if(tilew>w) tilew=w;  if(tileh>h) tileh=h;
+		ntilesw=(w+tilew-1)/tilew;  ntilesh=(h+tileh-1)/tileh;
+
+		if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
+			*ntilesw*ntilesh))==NULL)
+			_throwunix("allocating JPEG tile array");
+		memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
+		if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
+			*ntilesw*ntilesh))==NULL)
+			_throwunix("allocating JPEG size array");
+		memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
+
+		if((flags&TJFLAG_NOREALLOC)!=0)
+			for(i=0; i<ntilesw*ntilesh; i++)
+			{
+				if((jpegbuf[i]=(unsigned char *)malloc(TJBUFSIZE(tilew, tileh)))==NULL)
+					_throwunix("allocating JPEG tiles");
+			}
+
+		_w=w;  _h=h;  _tilew=tilew;  _tileh=tileh;
+		if(!quiet)
+		{
+			printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilew,
+				_tileh);
+			if(sf.num!=1 || sf.denom!=1)
+				printf(" --> %d x %d", TJSCALED(_w, sf), TJSCALED(_h, sf));
+			printf("\n");
+		}
+		else if(quiet==1)
+		{
+			printf("%s\t%s\t%s\t", pixFormatStr[pf],
+				(flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]);
+			printf("%-4d  %-4d\t", tilew, tileh);
+		}
+
+		_subsamp=subsamp;
+		if(dotile || xformop!=TJXOP_NONE || xformopt!=0)
+		{
+			if((t=(tjtransform *)malloc(sizeof(tjtransform)*ntilesw*ntilesh))
+				==NULL)
+				_throwunix("allocating image transform array");
+
+			if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
+				|| xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
+			{
+				_w=h;  _h=w;  _tilew=tileh;  _tileh=tilew;
+			}
+
+			if(xformopt&TJXOPT_GRAY) _subsamp=TJ_GRAYSCALE;
+			if(xformop==TJXOP_HFLIP || xformop==TJXOP_ROT180)
+				_w=_w-(_w%tjMCUWidth[_subsamp]);
+			if(xformop==TJXOP_VFLIP || xformop==TJXOP_ROT180)
+				_h=_h-(_h%tjMCUHeight[_subsamp]);
+			if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT90)
+				_w=_w-(_w%tjMCUHeight[_subsamp]);
+			if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT270)
+				_h=_h-(_h%tjMCUWidth[_subsamp]);
+			_ntilesw=(_w+_tilew-1)/_tilew;
+			_ntilesh=(_h+_tileh-1)/_tileh;
+
+			for(row=0, tile=0; row<_ntilesh; row++)
+			{
+				for(col=0; col<_ntilesw; col++, tile++)
+				{
+					t[tile].r.w=min(_tilew, _w-col*_tilew);
+					t[tile].r.h=min(_tileh, _h-row*_tileh);
+					t[tile].r.x=col*_tilew;
+					t[tile].r.y=row*_tileh;
+					t[tile].op=xformop;
+					t[tile].options=xformopt|TJXOPT_TRIM;
+				}
+			}
+
+			start=gettime();
+			if(tjTransform(handle, srcbuf, srcsize, _ntilesw*_ntilesh, jpegbuf,
+				jpegsize, t, flags)==-1)
+				_throwtj("executing tjTransform()");
+			elapsed=gettime()-start;
+
+			free(t);  t=NULL;
+
+			for(tile=0, totaljpegsize=0; tile<_ntilesw*_ntilesh; tile++)
+				totaljpegsize+=jpegsize[tile];
+
+			if(quiet)
+			{
+				printf("%s%c%s%c",
+					sigfig((double)(w*h)/1000000./elapsed, 4, tempstr, 80),
+					quiet==2? '\n':'\t',
+					sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
+					quiet==2? '\n':'\t');
+			}
+			else if(!quiet)
+			{
+				printf("X--> Frame rate:           %f fps\n", 1.0/elapsed);
+				printf("     Output image size:    %lu bytes\n", totaljpegsize);
+				printf("     Compression ratio:    %f:1\n",
+					(double)(w*h*ps)/(double)totaljpegsize);
+				printf("     Source throughput:    %f Megapixels/sec\n",
+					(double)(w*h)/1000000./elapsed);
+				printf("     Output bit stream:    %f Megabits/sec\n",
+					(double)totaljpegsize*8./1000000./elapsed);
+			}
+		}
+		else
+		{
+			if(quiet==1) printf("N/A\tN/A\t");
+			jpegsize[0]=srcsize;
+			memcpy(jpegbuf[0], srcbuf, srcsize);
+		}
+
+		if(w==tilew) _tilew=_w;
+		if(h==tileh) _tileh=_h;
+		if(decomptest(NULL, jpegbuf, jpegsize, NULL, _w, _h, _subsamp, 0,
+			filename, _tilew, _tileh)==-1)
+			goto bailout;
+
+		for(i=0; i<ntilesw*ntilesh; i++)
+		{
+			free(jpegbuf[i]);  jpegbuf[i]=NULL;
+		}
+		free(jpegbuf);  jpegbuf=NULL;
+		if(jpegsize) {free(jpegsize);  jpegsize=NULL;}
+
+		if(tilew==w && tileh==h) break;
+	}
+
+	bailout:
+	if(file) {fclose(file);  file=NULL;}
+	if(jpegbuf)
+	{
+		for(i=0; i<ntilesw*ntilesh; i++)
+		{
+			if(jpegbuf[i]) free(jpegbuf[i]);  jpegbuf[i]=NULL;
+		}
+		free(jpegbuf);  jpegbuf=NULL;
+	}
+	if(jpegsize) {free(jpegsize);  jpegsize=NULL;}
+	if(srcbuf) {free(srcbuf);  srcbuf=NULL;}
+	if(t) {free(t);  t=NULL;}
+	if(handle) {tjDestroy(handle);  handle=NULL;}
+	return;
+}
+
+
+void usage(char *progname)
+{
+	int i;
+	printf("USAGE: %s\n", progname);
+	printf("       <Inputfile (BMP|PPM)> <%% Quality> [options]\n\n");
+	printf("       %s\n", progname);
+	printf("       <Inputfile (JPG)> [options]\n\n");
+	printf("Options:\n\n");
+	printf("-alloc = Dynamically allocate JPEG image buffers\n");
+	printf("-bmp = Generate output images in Windows Bitmap format (default=PPM)\n");
+	printf("-bottomup = Test bottom-up compression/decompression\n");
+	printf("-tile = Test performance of the codec when the image is encoded as separate\n");
+	printf("     tiles of varying sizes.\n");
+	printf("-forcemmx, -forcesse, -forcesse2, -forcesse3 =\n");
+	printf("     Force MMX, SSE, SSE2, or SSE3 code paths in the underlying codec\n");
+	printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
+	printf("     Test the specified color conversion path in the codec (default: BGR)\n");
+	printf("-fastupsample = Use fast, inaccurate upsampling code to perform 4:2:2 and 4:2:0\n");
+	printf("     YUV decoding in libjpeg decompressor\n");
+	printf("-quiet = Output results in tabular rather than verbose format\n");
+	printf("-yuvencode = Encode RGB input as planar YUV rather than compressing as JPEG\n");
+	printf("-yuvdecode = Decode JPEG image to planar YUV rather than RGB\n");
+	printf("-scale M/N = scale down the width/height of the decompressed JPEG image by a\n");
+	printf("     factor of M/N (M/N = ");
+	for(i=0; i<nsf; i++)
+	{
+		printf("%d/%d", scalingfactors[i].num, scalingfactors[i].denom);
+		if(nsf==2 && i!=nsf-1) printf(" or ");
+		else if(nsf>2)
+		{
+			if(i!=nsf-1) printf(", ");
+			if(i==nsf-2) printf("or ");
+		}
+	}
+	printf(")\n");
+	printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
+	printf("     Perform the corresponding lossless transform prior to\n");
+	printf("     decompression (these options are mutually exclusive)\n");
+	printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
+	printf("     test (can be combined with the other transforms above)\n");
+	printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n\n");
+	printf("NOTE:  If the quality is specified as a range (e.g. 90-100), a separate\n");
+	printf("test will be performed for all quality values in the range.\n\n");
+	exit(1);
+}
+
+
+int main(int argc, char *argv[])
+{
+	unsigned char *srcbuf=NULL;  int w, h, i, j;
+	int minqual=-1, maxqual=-1;  char *temp;
+	int minarg=2;  int retval=0;
+
+	if((scalingfactors=tjGetScalingFactors(&nsf))==NULL || nsf==0)
+		_throwtj("executing tjGetScalingFactors()");
+
+	if(argc<minarg) usage(argv[0]);
+
+	temp=strrchr(argv[1], '.');
+	if(temp!=NULL)
+	{
+		if(!strcasecmp(temp, ".bmp")) ext="bmp";
+		if(!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg")) decomponly=1;
+	}
+
+	printf("\n");
+
+	if(argc>minarg)
+	{
+		for(i=minarg; i<argc; i++)
+		{
+			if(!strcasecmp(argv[i], "-yuvencode"))
+			{
+				printf("Testing YUV planar encoding\n\n");
+				yuv=YUVENCODE;  maxqual=minqual=100;
+			}
+			if(!strcasecmp(argv[i], "-yuvdecode"))
+			{
+				printf("Testing YUV planar decoding\n\n");
+				yuv=YUVDECODE;
+			}
+		}
+	}
+
+	if(!decomponly && yuv!=YUVENCODE)
+	{
+		minarg=3;
+		if(argc<minarg) usage(argv[0]);
+		if((minqual=atoi(argv[2]))<1 || minqual>100)
+		{
+			puts("ERROR: Quality must be between 1 and 100.");
+			exit(1);
+		}
+		if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
+			&& sscanf(&temp[1], "%d", &maxqual)==1 && maxqual>minqual && maxqual>=1
+			&& maxqual<=100) {}
+		else maxqual=minqual;
+	}
+
+	if(argc>minarg)
+	{
+		for(i=minarg; i<argc; i++)
+		{
+			if(!strcasecmp(argv[i], "-tile"))
+			{
+				dotile=1;  xformopt|=TJXOPT_CROP;
+			}
+			if(!strcasecmp(argv[i], "-forcesse3"))
+			{
+				printf("Forcing SSE3 code\n\n");
+				flags|=TJFLAG_FORCESSE3;
+			}
+			if(!strcasecmp(argv[i], "-forcesse2"))
+			{
+				printf("Forcing SSE2 code\n\n");
+				flags|=TJFLAG_FORCESSE2;
+			}
+			if(!strcasecmp(argv[i], "-forcesse"))
+			{
+				printf("Forcing SSE code\n\n");
+				flags|=TJFLAG_FORCESSE;
+			}
+			if(!strcasecmp(argv[i], "-forcemmx"))
+			{
+				printf("Forcing MMX code\n\n");
+				flags|=TJFLAG_FORCEMMX;
+			}
+			if(!strcasecmp(argv[i], "-fastupsample"))
+			{
+				printf("Using fast upsampling code\n\n");
+				flags|=TJFLAG_FASTUPSAMPLE;
+			}
+			if(!strcasecmp(argv[i], "-rgb")) pf=TJPF_RGB;
+			if(!strcasecmp(argv[i], "-rgbx")) pf=TJPF_RGBX;
+			if(!strcasecmp(argv[i], "-bgr")) pf=TJPF_BGR;
+			if(!strcasecmp(argv[i], "-bgrx")) pf=TJPF_BGRX;
+			if(!strcasecmp(argv[i], "-xbgr")) pf=TJPF_XBGR;
+			if(!strcasecmp(argv[i], "-xrgb")) pf=TJPF_XRGB;
+			if(!strcasecmp(argv[i], "-bottomup")) flags|=TJFLAG_BOTTOMUP;
+			if(!strcasecmp(argv[i], "-quiet")) quiet=1;
+			if(!strcasecmp(argv[i], "-qq")) quiet=2;
+			if(!strcasecmp(argv[i], "-scale") && i<argc-1)
+			{
+				int temp1=0, temp2=0, match=0;
+				if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2)
+				{
+					for(j=0; j<nsf; j++)
+					{
+						if(temp1==scalingfactors[j].num && temp2==scalingfactors[j].denom)
+						{
+							sf=scalingfactors[j];
+							match=1;  break;
+						}
+					}
+					if(!match) usage(argv[0]);
+				}
+				else usage(argv[0]);
+			}
+			if(!strcasecmp(argv[i], "-hflip")) xformop=TJXOP_HFLIP;
+			if(!strcasecmp(argv[i], "-vflip")) xformop=TJXOP_VFLIP;
+			if(!strcasecmp(argv[i], "-transpose")) xformop=TJXOP_TRANSPOSE;
+			if(!strcasecmp(argv[i], "-transverse")) xformop=TJXOP_TRANSVERSE;
+			if(!strcasecmp(argv[i], "-rot90")) xformop=TJXOP_ROT90;
+			if(!strcasecmp(argv[i], "-rot180")) xformop=TJXOP_ROT180;
+			if(!strcasecmp(argv[i], "-rot270")) xformop=TJXOP_ROT270;
+			if(!strcasecmp(argv[i], "-grayscale")) xformopt|=TJXOPT_GRAY;
+			if(!strcasecmp(argv[i], "-benchtime") && i<argc-1)
+			{
+				double temp=atof(argv[++i]);
+				if(temp>0.0) benchtime=temp;
+				else usage(argv[0]);
+			}
+			if(!strcmp(argv[i], "-?")) usage(argv[0]);
+			if(!strcasecmp(argv[i], "-alloc")) flags&=(~TJFLAG_NOREALLOC);
+		}
+	}
+
+	if((sf.num!=1 || sf.denom!=1) && dotile)
+	{
+		printf("Disabling tiled compression/decompression tests, because those tests do not\n");
+		printf("work when scaled decompression is enabled.\n");
+		dotile=0;
+	}
+
+	if(yuv && dotile)
+	{
+		printf("Disabling tiled compression/decompression tests, because those tests do not\n");
+		printf("work when YUV encoding or decoding is enabled.\n\n");
+		dotile=0;
+	}
+
+	if(!decomponly)
+	{
+		if(loadbmp(argv[1], &srcbuf, &w, &h, pf, (flags&TJFLAG_BOTTOMUP)!=0)==-1)
+			_throwbmp("loading bitmap");
+		temp=strrchr(argv[1], '.');
+		if(temp!=NULL) *temp='\0';
+	}
+
+	if(quiet==1 && !decomponly)
+	{
+		printf("All performance values in Mpixels/sec\n\n");
+		printf("Bitmap\tBitmap\tJPEG\tJPEG\t%s %s \tComp\tComp\tDecomp\n",
+			dotile? "Tile ":"Image", dotile? "Tile ":"Image");
+		printf("Format\tOrder\tSubsamp\tQual\tWidth Height\tPerf \tRatio\tPerf\n\n");
+	}
+
+	if(decomponly)
+	{
+		dodecomptest(argv[1]);
+		printf("\n");
+		goto bailout;
+	}
+	for(i=maxqual; i>=minqual; i--)
+		dotest(srcbuf, w, h, TJ_GRAYSCALE, i, argv[1]);
+	printf("\n");
+	for(i=maxqual; i>=minqual; i--)
+		dotest(srcbuf, w, h, TJ_420, i, argv[1]);
+	printf("\n");
+	for(i=maxqual; i>=minqual; i--)
+		dotest(srcbuf, w, h, TJ_422, i, argv[1]);
+	printf("\n");
+	for(i=maxqual; i>=minqual; i--)
+		dotest(srcbuf, w, h, TJ_444, i, argv[1]);
+	printf("\n");
+
+	bailout:
+	if(srcbuf) free(srcbuf);
+	return retval;
+}
diff --git a/tjunittest.c b/tjunittest.c
new file mode 100644
index 0000000..c7a6611
--- /dev/null
+++ b/tjunittest.c
@@ -0,0 +1,647 @@
+/*
+ * Copyright (C)2009-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.
+ */
+
+/*
+ * This program tests the various code paths in the TurboJPEG JNI Wrapper
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "./tjutil.h"
+#include "./turbojpeg.h"
+
+
+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"};
+
+const char *pixFormatStr[TJ_NUMPF]=
+{
+	"RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale"
+};
+
+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, alloc=0;
+
+int exitStatus=0;
+#define bailout() {exitStatus=-1;  goto bailout;}
+
+int pixels[9][3]=
+{
+	{0, 255, 0},
+	{255, 0, 255},
+	{0, 255, 255},
+	{255, 0, 0},
+	{255, 255, 0},
+	{0, 0, 255},
+	{255, 255, 255},
+	{0, 0, 0},
+	{0, 0, 255}
+};
+
+
+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;
+
+	memset(buf, 0, w*h*ps);
+	if(pf==TJPF_GRAY)
+	{
+		for(row=0; row<h; row++)
+		{
+			for(col=0; col<w; col++)
+			{
+				if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
+				else index=row*w+col;
+				if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0;
+				else buf[index]=(row<halfway)? 76:226;
+			}
+		}
+	}
+	else
+	{
+		for(row=0; row<h; row++)
+		{
+			for(col=0; col<w; col++)
+			{
+				if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
+				else index=row*w+col;
+				if(((row/8)+(col/8))%2==0)
+				{
+					if(row<halfway)
+					{
+						buf[index*ps+roffset]=255;
+						buf[index*ps+goffset]=255;
+						buf[index*ps+boffset]=255;
+					}
+				}
+				else
+				{
+					buf[index*ps+roffset]=255;
+					if(row>=halfway) buf[index*ps+goffset]=255;
+				}
+			}
+		}
+	}
+}
+
+
+#define checkval(v, cv) { \
+	if(v<cv-1 || v>cv+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 ps=tjPixelSize[pf];
+	int index, row, col, retval=1;
+	int halfway=16*sf.num/sf.denom;
+	int blocksize=8*sf.num/sf.denom;
+
+	for(row=0; row<h; row++)
+	{
+		for(col=0; col<w; col++)
+		{
+			unsigned char r, g, b;
+			if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
+			else index=row*w+col;
+			r=buf[index*ps+roffset];
+			g=buf[index*ps+goffset];
+			b=buf[index*ps+boffset];
+			if(((row/blocksize)+(col/blocksize))%2==0)
+			{
+				if(row<halfway)
+				{
+					checkval255(r);  checkval255(g);  checkval255(b);
+				}
+				else
+				{
+					checkval0(r);  checkval0(g);  checkval0(b);
+				}
+			}
+			else
+			{
+				if(subsamp==TJSAMP_GRAY)
+				{
+					if(row<halfway)
+					{
+						checkval(r, 76);  checkval(g, 76);  checkval(b, 76);
+					}
+					else
+					{
+						checkval(r, 226);  checkval(g, 226);  checkval(b, 226);
+					}
+				}
+				else
+				{
+					if(row<halfway)
+					{
+						checkval255(r);  checkval0(g);  checkval0(b);
+					}
+					else
+					{
+						checkval255(r);  checkval255(g);  checkval0(b);
+					}
+				}
+			}
+		}
+	}
+
+	bailout:
+	if(retval==0)
+	{
+		printf("\n");
+		for(row=0; row<h; row++)
+		{
+			for(col=0; col<w; col++)
+			{
+				printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset],
+					buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]);
+			}
+			printf("\n");
+		}
+	}
+	return retval;
+}
+
+
+#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
+
+int checkBufYUV(unsigned char *buf, int w, int h, int subsamp)
+{
+	int row, col;
+	int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
+	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 retval=1;
+
+	for(row=0; row<16; row++)
+	{
+		for(col=0; col<pw; col++)
+		{
+			unsigned char y=buf[ypitch*row+col];
+			if(((row/8)+(col/8))%2==0) checkval255(y)
+			else checkval(y, 76)
+		}
+	}
+	for(row=16; row<ph; row++)
+	{
+		for(col=0; col<pw; col++)
+		{
+			unsigned char y=buf[ypitch*row+col];
+			if(((row/8)+(col/8))%2==0) checkval0(y)
+			else checkval(y, 226)
+		}
+	}
+	if(subsamp!=TJSAMP_GRAY)
+	{
+		for(row=0; row<16/vsf; row++)
+		{
+			for(col=0; col<cw; col++)
+			{
+				unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
+					v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
+				if(((row*vsf/8)+(col*hsf/8))%2==0)
+				{
+					checkval(u, 128);  checkval(v, 128);
+				}
+				else
+				{
+					checkval(u, 85);  checkval255(v);
+				}
+			}
+		}
+		for(row=16/vsf; row<ch; row++)
+		{
+			for(col=0; col<cw; col++)
+			{
+				unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
+					v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
+				if(((row*vsf/8)+(col*hsf/8))%2==0)
+				{
+					checkval(u, 128);  checkval(v, 128);
+				}
+				else
+				{
+					checkval0(u);  checkval(v, 149);
+				}
+			}
+		}
+	}
+
+	bailout:
+	if(retval==0)
+	{
+		for(row=0; row<ph; row++)
+		{
+			for(col=0; col<pw; col++)
+				printf("%.3d ", buf[ypitch*row+col]);
+			printf("\n");
+		}
+		printf("\n");
+		for(row=0; row<ch; row++)
+		{
+			for(col=0; col<cw; col++)
+				printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]);
+			printf("\n");
+		}
+		printf("\n");
+		for(row=0; row<ch; row++)
+		{
+			for(col=0; col<cw; col++)
+				printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]);
+			printf("\n");
+		}
+		printf("\n");
+	}
+
+	return retval;
+}
+
+
+void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
+{
+	FILE *file=fopen(filename, "wb");
+	if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1)
+	{
+		printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
+		bailout();
+	}
+
+	bailout:
+	if(file) fclose(file);
+}
+
+
+void compTest(tjhandle handle, unsigned char **dstBuf,
+	unsigned long *dstSize, int w, int h, int pf, char *basename,
+	int subsamp, int jpegQual, int flags)
+{
+	char tempStr[1024];  unsigned char *srcBuf=NULL;
+	double t;
+
+	if(yuv==YUVENCODE)
+		printf("%s %s -> %s YUV ... ", pixFormatStr[pf],
+			(flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp]);
+	else
+		printf("%s %s -> %s Q%d ... ", pixFormatStr[pf],
+			(flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp],
+			jpegQual);
+
+	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=gettime();
+	if(yuv==YUVENCODE)
+	{
+		_tj(tjEncodeYUV2(handle, srcBuf, w, 0, h, pf, *dstBuf, subsamp, flags));
+	}
+	else
+	{
+		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=gettime()-t;
+
+	if(yuv==YUVENCODE)
+		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", basename,
+			pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp],
+			jpegQual);
+	writeJPEG(*dstBuf, *dstSize, tempStr);
+	if(yuv==YUVENCODE)
+	{
+		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);
+
+	bailout:
+	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;
+	int _hdrw=0, _hdrh=0, _hdrsubsamp=-1;  double t;
+	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 ... ", subName[subsamp]);
+	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(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
+		&_hdrsubsamp));
+	if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
+		_throw("Incorrect JPEG header");
+
+	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=gettime();
+	if(yuv==YUVDECODE)
+	{
+		_tj(tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags));
+	}
+	else
+	{
+		_tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
+			scaledHeight, pf, flags));
+	}
+	t=gettime()-t;
+
+	if(yuv==YUVDECODE)
+	{
+		if(checkBufYUV(dstBuf, w, h, subsamp)) printf("Passed.");
+		else printf("FAILED!");
+	}
+	else
+	{
+		if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
+			printf("Passed.");
+		else printf("FAILED!");
+	}
+	printf("  %f ms\n", t*1000.);
+
+	bailout:
+	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), sf1={1, 1};
+	if(!sf || !n) _throwtj();
+
+	if((subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY) && !yuv)
+	{
+		for(i=0; i<n; i++)
+			_decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
+				flags, sf[i]);
+	}
+	else
+		_decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, flags,
+			sf1);
+
+	bailout:
+	printf("\n");
+}
+
+
+void doTest(int w, int h, const int *formats, int nformats, int subsamp,
+	char *basename)
+{
+	tjhandle chandle=NULL, dhandle=NULL;
+	unsigned char *dstBuf=NULL;
+	unsigned long size=0;  int pfi, pf, i;
+
+	if(!alloc)
+	{
+		size=(yuv==YUVENCODE? TJBUFSIZEYUV(w, h, subsamp):TJBUFSIZE(w, h));
+		if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
+			_throw("Memory allocation failure.");
+	}
+
+	if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
+		_throwtj();
+
+	for(pfi=0; pfi<nformats; pfi++)
+	{
+		for(i=0; i<2; i++)
+		{
+			int flags=0;
+			if(i==1)
+			{
+				if(yuv==YUVDECODE) goto bailout;
+				else flags|=TJFLAG_BOTTOMUP;
+			}
+			pf=formats[pfi];
+			compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
+				flags);
+			decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp,
+				flags);
+		}
+	}
+
+	bailout:
+	if(chandle) tjDestroy(chandle);
+	if(dhandle) tjDestroy(dhandle);
+
+	if(dstBuf) tjFree(dstBuf);
+}
+
+
+void doTest1(void)
+{
+	int w, h, i;
+	unsigned char *srcBuf=NULL, *jpegBuf=NULL;
+	tjhandle handle=NULL;
+	unsigned long jpegSize=0;
+
+	if((handle=tjInitCompress())==NULL) _throwtj();
+
+	printf("Buffer size regression test\n");
+	for(w=1; w<48; w++)
+	{
+		int maxh=(w==1)? 2048:48;
+		for(h=1; h<maxh; h++)
+		{
+			if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
+			if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
+				_throw("Memory allocation failure");
+			if(!alloc)
+			{
+				if((jpegBuf=(unsigned char *)tjAlloc(TJBUFSIZE(w, h)))==NULL)
+					_throw("Memory allocation failure");
+				jpegSize=TJBUFSIZE(w, h);
+			}
+			memset(srcBuf, 0, w*h*4);
+
+			for(i=0; i<w*h; i++) memcpy(srcBuf, &pixels[i%9], 3);
+
+			_tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf, &jpegSize,
+				TJSAMP_444, 100, alloc? 0:TJFLAG_NOREALLOC));
+			free(srcBuf);  srcBuf=NULL;
+			tjFree(jpegBuf);  jpegBuf=NULL;
+
+			if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
+				_throw("Memory allocation failure");
+			if(!alloc)
+			{
+				if((jpegBuf=(unsigned char *)tjAlloc(TJBUFSIZE(h, w)))==NULL)
+					_throw("Memory allocation failure");
+				jpegSize=TJBUFSIZE(h, w);
+			}
+
+			for(i=0; i<h*w; i++)
+			{
+				if(i%2==0) srcBuf[i*4]=srcBuf[i*4+1]=srcBuf[i*4+2]=0xFF;
+				else srcBuf[i*4]=srcBuf[i*4+1]=srcBuf[i*4+2]=0;
+			}
+			_tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf, &jpegSize,
+				TJSAMP_444, 100, alloc? 0:TJFLAG_NOREALLOC));
+			free(srcBuf);  srcBuf=NULL;
+			tjFree(jpegBuf);  jpegBuf=NULL;
+		}
+	}
+	printf("Done.      \n");
+
+	bailout:
+	if(srcBuf) free(srcBuf);
+	if(jpegBuf) free(jpegBuf);
+	if(handle) tjDestroy(handle);
+}
+
+
+int main(int argc, char *argv[])
+{
+	int doyuv=0, i;
+	if(argc>1)
+	{
+		for(i=1; i<argc; i++)
+		{
+			if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
+			if(!strcasecmp(argv[i], "-alloc")) alloc=1;
+			if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
+				usage(argv[0]);
+		}
+	}
+	if(alloc) printf("Testing automatic buffer allocation\n");
+	if(doyuv) {yuv=YUVENCODE;  alloc=0;}
+	doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
+	doTest(39, 41, _4byteFormats, 4, TJSAMP_444, "test");
+	if(doyuv)
+	{
+		doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
+		doTest(35, 39, _4byteFormats, 4, TJSAMP_422, "test");
+		doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
+		doTest(41, 35, _4byteFormats, 4, TJSAMP_420, "test");
+		doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
+		doTest(39, 41, _4byteFormats, 4, TJSAMP_440, "test");
+	}
+	doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test");
+	doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test");
+	doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test");
+	if(!doyuv) doTest1();
+	if(doyuv)
+	{
+		yuv=YUVDECODE;
+		doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
+		doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1");
+		doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
+		doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1");
+		doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
+		doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1");
+		doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
+		doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1");
+		doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
+		doTest(35, 39, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1");
+		doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
+		doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1");
+	}
+
+	return exitStatus;
+}