Fix an issue that prevented tjEncodeYUV3() and TJCompressor.encodeYUV() from working properly if the source image was very tiny.  Basically, jpeg_start_compress() was attempting to write the JPEG headers, which was overrunning the YUV buffer.  This patch removes the call to jpeg_start_compress() in tjEncodeYUV3() and replaces it with calls to the individual routines that are necessary to initialize the color converter and downsampler.  TJUnitTest has also been modified to test for this condition (the buffer size regression test now works in YUV mode.)


git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@1118 632fc199-4ca6-4c93-a231-07263d6284db
diff --git a/tjunittest.c b/tjunittest.c
index 519686f..8204153 100644
--- a/tjunittest.c
+++ b/tjunittest.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C)2009-2013 D. R. Commander.  All Rights Reserved.
+ * Copyright (C)2009-2014 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:
@@ -667,9 +667,9 @@
 void bufSizeTest(void)
 {
 	int w, h, i, subsamp;
-	unsigned char *srcBuf=NULL, *jpegBuf=NULL;
+	unsigned char *srcBuf=NULL, *dstBuf=NULL;
 	tjhandle handle=NULL;
-	unsigned long jpegSize=0;
+	unsigned long dstSize=0;
 
 	if((handle=tjInitCompress())==NULL) _throwtj();
 
@@ -684,12 +684,12 @@
 				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(!alloc || yuv==YUVENCODE)
 				{
-					if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, subsamp)))
-						==NULL)
+					if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(w, pad, h, subsamp);
+					else dstSize=tjBufSize(w, h, subsamp);
+					if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
 						_throw("Memory allocation failure");
-					jpegSize=tjBufSize(w, h, subsamp);
 				}
 
 				for(i=0; i<w*h*4; i++)
@@ -698,19 +698,27 @@
 					else srcBuf[i]=255;
 				}
 
-				_tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf,
-					&jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
+				if(yuv==YUVENCODE)
+				{
+					_tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
+						subsamp, 0));
+				}
+				else
+				{
+					_tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
+						&dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
+				}
 				free(srcBuf);  srcBuf=NULL;
-				tjFree(jpegBuf);  jpegBuf=NULL;
+				tjFree(dstBuf);  dstBuf=NULL;
 
 				if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
 					_throw("Memory allocation failure");
-				if(!alloc)
+				if(!alloc || yuv==YUVENCODE)
 				{
-					if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(h, w, subsamp)))
-						==NULL)
+					if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(h, pad, w, subsamp);
+					else dstSize=tjBufSize(h, w, subsamp);
+					if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
 						_throw("Memory allocation failure");
-					jpegSize=tjBufSize(h, w, subsamp);
 				}
 
 				for(i=0; i<h*w*4; i++)
@@ -719,10 +727,18 @@
 					else srcBuf[i]=255;
 				}
 
-				_tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf,
-					&jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
+				if(yuv==YUVENCODE)
+				{
+					_tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
+						subsamp, 0));
+				}
+				else
+				{
+					_tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
+						&dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
+				}
 				free(srcBuf);  srcBuf=NULL;
-				tjFree(jpegBuf);  jpegBuf=NULL;
+				tjFree(dstBuf);  dstBuf=NULL;
 			}
 		}
 	}
@@ -730,7 +746,7 @@
 
 	bailout:
 	if(srcBuf) free(srcBuf);
-	if(jpegBuf) free(jpegBuf);
+	if(dstBuf) free(dstBuf);
 	if(handle) tjDestroy(handle);
 }
 
@@ -767,9 +783,10 @@
 	doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
 	doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
 	doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
-	if(!doyuv) bufSizeTest();
+	bufSizeTest();
 	if(doyuv)
 	{
+		printf("\n--------------------\n\n");
 		yuv=YUVDECODE;
 		doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
 		doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1");