From 527433c2659b176ae2ba4ae2fdc6c5898a5e6233 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@debian.org>
Date: Fri, 11 Mar 2011 21:46:02 +0000
Subject: [PATCH 1/2] Revert increased RGB_PIXELSIZE in libjpeg

Instead, stretch RGB to RGBA in tr_image_jpg, like we do for 8-bit
greyscale.

See https://bugzilla.icculus.org/show_bug.cgi?id=4564 for discussion
---
 code/jpeg-6b/ioq3-changes.diff |    9 ---------
 code/jpeg-6b/jmorecfg.h        |    2 +-
 code/renderer/tr_image_jpg.c   |   32 ++++++++++++++++++++++++++------
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/code/jpeg-6b/ioq3-changes.diff b/code/jpeg-6b/ioq3-changes.diff
index 1ce424b..9831582 100644
--- a/code/jpeg-6b/ioq3-changes.diff
+++ b/code/jpeg-6b/ioq3-changes.diff
@@ -861,15 +861,6 @@ diff -u -w /home/tma/sources/jpeg-6b/jmorecfg.h ./jmorecfg.h
  
  /* more capability options later, no doubt */
  
-@@ -314,7 +315,7 @@
- #define RGB_RED		0	/* Offset of Red in an RGB scanline element */
- #define RGB_GREEN	1	/* Offset of Green */
- #define RGB_BLUE	2	/* Offset of Blue */
--#define RGB_PIXELSIZE	3	/* JSAMPLEs per RGB scanline element */
-+#define RGB_PIXELSIZE	4	/* JSAMPLEs per RGB scanline element */
- 
- 
- /* Definitions for speed-related optimizations. */
 diff -u -w /home/tma/sources/jpeg-6b/jpeglib.h ./jpeglib.h
 --- /home/tma/sources/jpeg-6b/jpeglib.h	1998-02-21 19:48:14.000000000 +0000
 +++ ./jpeglib.h	2008-08-22 00:01:58.000000000 +0100
diff --git a/code/jpeg-6b/jmorecfg.h b/code/jpeg-6b/jmorecfg.h
index d677127..fb48dd7 100644
--- a/code/jpeg-6b/jmorecfg.h
+++ b/code/jpeg-6b/jmorecfg.h
@@ -317,7 +317,7 @@ typedef unsigned char boolean;
 #define RGB_RED		0	/* Offset of Red in an RGB scanline element */
 #define RGB_GREEN	1	/* Offset of Green */
 #define RGB_BLUE	2	/* Offset of Blue */
-#define RGB_PIXELSIZE	4	/* JSAMPLEs per RGB scanline element */
+#define RGB_PIXELSIZE	3	/* JSAMPLEs per RGB scanline element */
 
 
 /* Definitions for speed-related optimizations. */
diff --git a/code/renderer/tr_image_jpg.c b/code/renderer/tr_image_jpg.c
index 2e1a52a..0a57276 100644
--- a/code/renderer/tr_image_jpg.c
+++ b/code/renderer/tr_image_jpg.c
@@ -176,15 +176,35 @@ void R_LoadJPG( const char *filename, unsigned char **pic, int *width, int *heig
 		buf[--dindex] = greyshade;
 	} while(sindex);
   }
-  else
+  else if(cinfo.output_components == 3)
   {
-	// clear all the alphas to 255
-	int	i;
+	// Hopefully this is 24-bit JPEG (RGB); expand to RGBA, and clear
+	// all the alphas to 255
+	int sindex = pixelcount * 3, dindex = memcount;
+	unsigned char red, green, blue;
 
-	for ( i = 3 ; i < memcount ; i+=4 )
+	// Only pixelcount number of bytes have been written.
+	// Expand the color values over the rest of the buffer, starting
+	// from the end.
+	do
 	{
-		buf[i] = 255;
-	}
+		blue = buf[--sindex];
+		green = buf[--sindex];
+		red = buf[--sindex];
+
+		buf[--dindex] = 255;
+		buf[--dindex] = blue;
+		buf[--dindex] = green;
+		buf[--dindex] = red;
+	} while(sindex);
+  }
+  else
+  {
+		(void) jpeg_finish_decompress(&cinfo);
+		jpeg_destroy_decompress(&cinfo);
+		ri.FS_FreeFile (fbuffer.v);
+		ri.Error (ERR_DROP, "LoadJPG: %s has unexpected component count %d\n",
+				filename, cinfo.output_components);
   }
 
   *pic = out;
-- 
1.7.4.1