gd-libgd / src / gdtest.c

Diff from to

src/gdtest.c

 #endif
 #include "gd.h"
 
-/* A short program which converts a .png file into a .gd file, for
-	your convenience in creating images on the fly from a
-	basis image that must be loaded quickly. The .gd format
-	is not intended to be a general-purpose format. */
-
 void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2);
 
 static int freadWrapper(void *context, char *buf, int len);
 	int		colRed, colBlu;
 	gdSource	imgsrc;
 	gdSink		imgsnk;
-
+	int		foreground;
+	int		i;
 	if (argc != 2) {
 		fprintf(stderr, "Usage: gdtest filename.png\n");
 		exit(1);
 
         in = fopen("test/gdtest_merge.png", "rb");
         if (!in) {
-                fprintf(stderr, "gdtest.gd2 does not exist!\n");
+                fprintf(stderr, "gdtest_merge.png does not exist!\n");
                 exit(1);
         }
         im3 = gdImageCreateFromPng(in);
 		"to the input image manually. Some difference must be\n"
 		"expected as JPEG is a lossy file format.\n");
 #endif /* HAVE_JPEG */
+	/* Assume the color closest to black is the foreground
+		color for the B&W wbmp image. */	
+	fprintf(stderr, "NOTE: the WBMP output image will NOT match the original unless the original\n"
+	"is also black and white. This is OK!\n");
+	foreground = gdImageColorClosest(im, 0, 0, 0);
+	fprintf(stderr, "Foreground index is %d\n", foreground);
+	if (foreground == -1) {
+		fprintf(stderr, "Source image has no colors, skipping wbmp test.\n");
+	} else {
+		out = fopen("test/gdtest.wbmp", "wb");
+		if (!out) {
+			fprintf(stderr, "Can't create file test/gdtest.wbmp.\n");
+			exit(1);
+		}
+		gdImageWBMP(im, foreground, out);	
+		fclose(out);
+		in = fopen("test/gdtest.wbmp", "rb");
+		if (!in) {
+			fprintf(stderr, "Can't open file test/gdtest.wbmp.\n");
+			exit(1);
+		}
+		im2 = gdImageCreateFromWBMP(in);
+		fprintf(stderr, "WBMP has %d colors\n", gdImageColorsTotal(im2));
+		fprintf(stderr, "WBMP colors are:\n");
+		for (i = 0; (i < gdImageColorsTotal(im2)); i++) {
+			fprintf(stderr, "%02X%02X%02X\n",
+				gdImageRed(im2, i), 
+				gdImageGreen(im2, i),
+				gdImageBlue(im2, i)); 
+		}
+		fclose(in);
+		if (!im2) {
+			fprintf(stderr, "gdImageCreateFromWBMP failed.\n");
+			exit(1);
+		}
+		CompareImages("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
+		out = fopen("test/gdtest_wbmp_to_png.png", "wb");
+		if (!out) {
+			fprintf(stderr, "Can't create file test/gdtest_wbmp_to_png.png.\n");
+			exit(1);
+		}
+		gdImagePng(im2, out);	
+		fclose(out);
+		gdImageDestroy(im2);	
+	}
 	gdImageDestroy(im);
 	gdImageDestroy(ref);
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.