cvs: gd /playground/gdbmp gd_bmp.c

[email protected] ("Scott MacVicar") Tue, 11 Mar 2008 19:49:27 -0000
Newsgroups php.gd.cvs
Message-ID <cvsscottmac1205264967@cvsserver>
scottmac		Tue Mar 11 19:49:27 2008 UTC

  Modified files:              
    /gd/playground/gdbmp	gd_bmp.c 
  Log:
  Fix compiler warnings, error with top row being lost, add padding for image rows that aren't divisable by 4 bytes and add defines for gd 2.0.x support for now.
  
http://cvs.php.net/viewvc.cgi/gd/playground/gdbmp/gd_bmp.c?r1=1.7&r2=1.8&diff_format=u
Index: gd/playground/gdbmp/gd_bmp.c
diff -u gd/playground/gdbmp/gd_bmp.c:1.7 gd/playground/gdbmp/gd_bmp.c:1.8
--- gd/playground/gdbmp/gd_bmp.c:1.7	Fri Feb 29 21:46:32 2008
+++ gd/playground/gdbmp/gd_bmp.c	Tue Mar 11 19:49:27 2008
@@ -12,7 +12,7 @@
 
 	----------------------------------------------------------------------------
  */
-/* $Id: gd_bmp.c,v 1.7 2008/02/29 21:46:32 pajoye Exp $ */
+/* $Id: gd_bmp.c,v 1.8 2008/03/11 19:49:27 scottmac Exp $ */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -37,9 +37,11 @@
 static int bmp_read_8bit(gdImagePtr im, gdIOCtxPtr infile, bmp_info_t *info, bmp_hdr_t *header);
 static int bmp_read_rle(gdImagePtr im, gdIOCtxPtr infile, bmp_info_t *info);
 
+#if GD_MAJOR_VERSION == 2 && GD_MINOR_VERSION < 1
 /* Byte helper functions, since added to GD 2.1 */
-static int gdBMPGetInt(signed int *result, gdIOCtx * ctx);
-static int gdBMPGetWord(signed short int *result, gdIOCtx * ctx);
+static int gdGetIntLSB(signed int *result, gdIOCtx * ctx);
+static int gdGetWordLSB(signed short int *result, gdIOCtx * ctx);
+#endif
 
 #define BMP_DEBUG(s)
 
@@ -80,7 +82,7 @@
 
 BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out)
 {
-	int bitmap_size, info_size, total_size;
+	int bitmap_size, info_size, total_size, padding;
 	int i, row, xpos, pixel;
 
 	bitmap_size = ((im->sx * (im->trueColor ? 24 : 8)) / 8) * im->sy;
@@ -116,7 +118,13 @@
 	gdBMPPutInt(out, im->colorsTotal); /* colours used */
 	gdBMPPutInt(out, 0); /* important colours */
 
-	/* 16-bit colours */
+	/* The line must be divisible by 4, else its padded with NULLs */
+	padding = ((int)(im->trueColor ? 3 : 1) * im->sx) % 4;
+	if (padding) {
+		padding = 4 - padding;
+	}
+
+	/* 8-bit colours */
 	if (!im->trueColor) {
 		for(i = 0; i< im->colorsTotal; ++i) {			
 			Putchar(gdImageBlue(im, i), out);
@@ -124,20 +132,29 @@
 			Putchar(gdImageRed(im, i), out);
 			Putchar(0, out);
         }
-        for (row = im->sy; row > 0; row--) {
+        for (row = (im->sy - 1); row >= 0; row--) {
 			for (xpos = 0; xpos < im->sx; xpos++) {
 				Putchar(gdImageGetPixel(im, xpos, row), out);
-			}	
+			}
+			/* Add padding to make sure we have n mod 4 == 0 bytes per row */
+			for (xpos = padding; xpos > 0; --xpos) {
+				Putchar('\0', out);
+			}
 		}
 	} else {
-        for (row = im->sy; row > 0; row--) {
+        for (row = (im->sy - 1); row >= 0; row--) {
 			for (xpos = 0; xpos < im->sx; xpos++) {
 				pixel = gdImageGetPixel(im, xpos, row);
 				
 				Putchar(gdTrueColorGetBlue(pixel), out);
 				Putchar(gdTrueColorGetGreen(pixel), out);
 				Putchar(gdTrueColorGetRed(pixel), out);
-			}	
+			}
+
+			/* Add padding to make sure we have n mod 4 == 0 bytes per row */
+			for (xpos = padding; xpos > 0; --xpos) {
+				Putchar('\0', out);
+			}
 		}
 	}
 }
@@ -250,11 +267,11 @@
 static int bmp_read_header(gdIOCtx *infile, bmp_hdr_t *hdr)
 {
 	if(
-	!gdBMPGetWord(&hdr->magic, infile) ||
-	!gdBMPGetInt(&hdr->size, infile) ||
-	!gdBMPGetWord(&hdr->reserved1, infile) ||
-	!gdBMPGetWord(&hdr->reserved2 , infile) ||
-	!gdBMPGetInt(&hdr->off , infile)
+	!gdGetWordLSB(&hdr->magic, infile) ||
+	!gdGetIntLSB(&hdr->size, infile) ||
+	!gdGetWordLSB(&hdr->reserved1, infile) ||
+	!gdGetWordLSB(&hdr->reserved2 , infile) ||
+	!gdGetIntLSB(&hdr->off , infile)
 	) {
 		return 1;
 	}
@@ -264,7 +281,7 @@
 static int bmp_read_info(gdIOCtx *infile, bmp_info_t *info)
 {
 	/* read BMP length so we can work out the version */
-	if (!gdBMPGetInt(&info->len, infile)) {
+	if (!gdGetIntLSB(&info->len, infile)) {
 		return 1;
 	}
 
@@ -298,16 +315,16 @@
 static int bmp_read_windows_v3_info(gdIOCtxPtr infile, bmp_info_t *info)
 {
 	if (
-		!gdBMPGetInt(&info->width, infile) ||
-		!gdBMPGetInt(&info->height, infile) ||
-		!gdBMPGetWord(&info->numplanes, infile) ||
-		!gdBMPGetWord(&info->depth, infile) ||
-		!gdBMPGetInt(&info->enctype, infile) ||
-		!gdBMPGetInt(&info->size, infile) ||
-		!gdBMPGetInt(&info->hres, infile) ||
-		!gdBMPGetInt(&info->vres, infile) ||
-		!gdBMPGetInt(&info->numcolors, infile) ||
-		!gdBMPGetInt(&info->mincolors, infile)
+		!gdGetIntLSB(&info->width, infile) ||
+		!gdGetIntLSB(&info->height, infile) ||
+		!gdGetWordLSB(&info->numplanes, infile) ||
+		!gdGetWordLSB(&info->depth, infile) ||
+		!gdGetIntLSB(&info->enctype, infile) ||
+		!gdGetIntLSB(&info->size, infile) ||
+		!gdGetIntLSB(&info->hres, infile) ||
+		!gdGetIntLSB(&info->vres, infile) ||
+		!gdGetIntLSB(&info->numcolors, infile) ||
+		!gdGetIntLSB(&info->mincolors, infile)
 	) {
 		return 1;
 	}
@@ -332,10 +349,10 @@
 static int bmp_read_os2_v1_info(gdIOCtxPtr infile, bmp_info_t *info)
 {
 	if (
-		!gdBMPGetWord(&info->width, infile) ||
-		!gdBMPGetWord(&info->height, infile) ||
-		!gdBMPGetWord(&info->numplanes, infile) ||
-		!gdBMPGetWord(&info->depth, infile)
+		!gdGetWordLSB((signed short int *)&info->width, infile) ||
+		!gdGetWordLSB((signed short int *)&info->height, infile) ||
+		!gdGetWordLSB(&info->numplanes, infile) ||
+		!gdGetWordLSB(&info->depth, infile)
 	) {
 		return 1;
 	}
@@ -358,16 +375,16 @@
 {
 	char useless_bytes[24];
 	if (
-		!gdBMPGetInt(&info->width, infile) ||
-		!gdBMPGetInt(&info->height, infile) ||
-		!gdBMPGetWord(&info->numplanes, infile) ||
-		!gdBMPGetWord(&info->depth, infile) ||
-		!gdBMPGetInt(&info->enctype, infile) ||
-		!gdBMPGetInt(&info->size, infile) ||
-		!gdBMPGetInt(&info->hres, infile) ||
-		!gdBMPGetInt(&info->vres, infile) ||
-		!gdBMPGetInt(&info->numcolors, infile) ||
-		!gdBMPGetInt(&info->mincolors, infile)
+		!gdGetIntLSB(&info->width, infile) ||
+		!gdGetIntLSB(&info->height, infile) ||
+		!gdGetWordLSB(&info->numplanes, infile) ||
+		!gdGetWordLSB(&info->depth, infile) ||
+		!gdGetIntLSB(&info->enctype, infile) ||
+		!gdGetIntLSB(&info->size, infile) ||
+		!gdGetIntLSB(&info->hres, infile) ||
+		!gdGetIntLSB(&info->vres, infile) ||
+		!gdGetIntLSB(&info->numcolors, infile) ||
+		!gdGetIntLSB(&info->mincolors, infile)
 	) {
 		return 1;
 	}
@@ -397,8 +414,9 @@
 
 static int bmp_read_direct(gdImagePtr im, gdIOCtxPtr infile, bmp_info_t *info, bmp_hdr_t *header)
 {
-	int ypos = 0, xpos = 0, row = 0, data = 0;
+	int ypos = 0, xpos = 0, row = 0;
 	int padding = 0, alpha = 0, red = 0, green = 0, blue = 0;
+	signed short int data = 0;
 
 	switch(info->enctype) {
 		case BMP_BI_RGB:
@@ -453,7 +471,7 @@
 
 		for (xpos = 0; xpos < info->width; xpos++) {
 			if (info->depth == 16) {
-				if (!gdBMPGetWord(&data, infile)) {
+				if (!gdGetWordLSB(&data, infile)) {
 					return 1;
 				}
 				BMP_DEBUG(printf("Data: %X\n", data));
@@ -806,9 +824,10 @@
 	return 0;
 }
 
-static int gdBMPGetWord(signed short int *result, gdIOCtx * ctx)
+#if GD_MAJOR_VERSION == 2 && GD_MINOR_VERSION < 1
+static int gdGetWordLSB(signed short int *result, gdIOCtx * ctx)
 {
-	int high = 0, low = 0;
+	unsigned int high = 0, low = 0;
 	low = (ctx->getC) (ctx);
 	if (low == EOF) {
  		return 0;
@@ -826,9 +845,11 @@
 	return 1;
 }
 
-static int gdBMPGetInt(signed int *result, gdIOCtx * ctx)
+static int gdGetIntLSB(signed int *result, gdIOCtx * ctx)
 {
-	int c = 0, r = 0;
+	int c = 0;
+	unsigned int r = 0;
+
 	c = (ctx->getC) (ctx);
 	if (c == EOF) {
  		return 0;
@@ -857,8 +878,9 @@
 	r |= (c << 24);
 
 	if (result) {
-		*result = r;
+		*result = (signed int)r;
 	}
 
 	return 1;
 }
+#endif