Fix crashes and bogus output in the CMYK and decode-to-YUV features that occurred if JCS_EXTENSIONS wasn't defined.


git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@1260 632fc199-4ca6-4c93-a231-07263d6284db
diff --git a/turbojpeg.c b/turbojpeg.c
index c824132..5ce1594 100644
--- a/turbojpeg.c
+++ b/turbojpeg.c
@@ -634,7 +634,7 @@
 	if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
 
 	#ifndef JCS_EXTENSIONS
-	if(pixelFormat!=TJPF_GRAY)
+	if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK)
 	{
 		rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
 		if(!rgbBuf) _throw("tjCompress2(): Memory allocation failure");
@@ -749,7 +749,7 @@
 	if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
 
 	#ifndef JCS_EXTENSIONS
-	if(pixelFormat!=TJPF_GRAY)
+	if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK)
 	{
 		rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
 		if(!rgbBuf) _throw("tjEncodeYUV3(): Memory allocation failure");
@@ -1189,7 +1189,7 @@
 	if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];
 
 	#ifndef JCS_EXTENSIONS
-	if(pixelFormat!=TJPF_GRAY &&
+	if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK &&
 		(RGB_RED!=tjRedOffset[pixelFormat] ||
 			RGB_GREEN!=tjGreenOffset[pixelFormat] ||
 			RGB_BLUE!=tjBlueOffset[pixelFormat] ||
@@ -1309,6 +1309,7 @@
 	jpeg_component_info *compptr;
 	#ifndef JCS_EXTENSIONS
 	unsigned char *rgbBuf=NULL;
+	unsigned char *_dstBuf=NULL;  int _pitch=0;
 	#endif
 	JMETHOD(int, old_read_markers, (j_decompress_ptr));
 	JMETHOD(void, old_reset_marker_reader, (j_decompress_ptr));
@@ -1339,17 +1340,6 @@
 		_throw("tjDecodeYUV(): Cannot decode YUV images into CMYK pixels.");
 
 	if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
-
-	#ifndef JCS_EXTENSIONS
-	if(pixelFormat!=TJPF_GRAY)
-	{
-		rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
-		if(!rgbBuf) _throw("tjDecodeYUV(): Memory allocation failure");
-		srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf);
-		pitch=width*RGB_PIXELSIZE;
-	}
-	#endif
-
 	dinfo->image_width=width;
 	dinfo->image_height=height;
 
@@ -1383,6 +1373,20 @@
 
 	if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];
 
+	#ifndef JCS_EXTENSIONS
+	if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK &&
+		(RGB_RED!=tjRedOffset[pixelFormat] ||
+			RGB_GREEN!=tjGreenOffset[pixelFormat] ||
+			RGB_BLUE!=tjBlueOffset[pixelFormat] ||
+			RGB_PIXELSIZE!=tjPixelSize[pixelFormat]))
+	{
+		rgbBuf=(unsigned char *)malloc(width*height*3);
+		if(!rgbBuf) _throw("tjDecodeYUV(): Memory allocation failure");
+		_pitch=pitch;  pitch=width*3;
+		_dstBuf=dstBuf;  dstBuf=rgbBuf;
+	}
+	#endif
+
 	if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL)
 		_throw("tjDecodeYUV(): Memory allocation failure");
 	for(i=0; i<height; i++)
@@ -1435,6 +1439,10 @@
 	}
 	jpeg_abort_decompress(dinfo);
 
+	#ifndef JCS_EXTENSIONS
+	fromRGB(rgbBuf, _dstBuf, width, _pitch, height, pixelFormat);
+	#endif
+
 	bailout:
 	if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
 	#ifndef JCS_EXTENSIONS