(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
authorpeter1138
Tue, 13 Jun 2006 17:13:46 +0000
changeset 4019 05b2ea531da0
parent 4018 12f167e98f69
child 4020 83d7762d77af
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
screenshot.c
--- a/screenshot.c	Tue Jun 13 14:04:12 2006 +0000
+++ b/screenshot.c	Tue Jun 13 17:13:46 2006 +0000
@@ -285,6 +285,7 @@
 	uint maxlines;
 	uint y;
 	PcxHeader pcx;
+	bool success;
 
 	if (pixelformat != 8 || w == 0)
 		return false;
@@ -388,14 +389,24 @@
 		return false;
 	}
 
-	{assert_compile(sizeof(*palette) == 3);}
-	if (fwrite(palette, 256 * sizeof(*palette), 1, f) != 1) {
-		fclose(f);
-		return false;
+	if (sizeof(*palette) == 3) {
+		success = fwrite(palette, 256 * sizeof(*palette), 1, f) == 1;
+	} else {
+		/* If the palette is word-aligned, copy it to a temporary byte array */
+		byte *tmp = malloc(256 * 3);
+		uint i;
+		for (i = 0; i < 256; i++) {
+			tmp[i * 3 + 0] = palette[i].r;
+			tmp[i * 3 + 1] = palette[i].g;
+			tmp[i * 3 + 2] = palette[i].b;
+		}
+		success = fwrite(tmp, 256 * 3, 1, f) == 1;
+		free(tmp);
 	}
+
 	fclose(f);
 
-	return true;
+	return success;
 }
 
 //************************************************