src/lib/gifwrite.c 1.8
1.8 2009/08/19 22:28:36 angie
Added option to mgSaveToGif and its call stack, to use GIF's Graphic Control Extension to make memgfx's background color (0) transparent. Also corrected terminology for PNG in .h files: useAlpha -> useTransparency.
Index: src/lib/gifwrite.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/gifwrite.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -b -B -U 4 -r1.7 -r1.8
--- src/lib/gifwrite.c 22 Apr 2009 01:57:00 -0000 1.7
+++ src/lib/gifwrite.c 19 Aug 2009 22:28:36 -0000 1.8
@@ -7,11 +7,27 @@
static char const rcsid[] = "$Id$";
static char gifsig[] = "GIF87a";
+// GIF Graphic Control Extension, for making the background color transparent:
+static struct gif_gce
+ {
+ unsigned char extensionIntroducer, graphicControlLabel, blockSize;
+ unsigned char flags;
+ unsigned char delayTimeLo,delayTimeHi;
+ unsigned char transparentColorIndex;
+ unsigned char blockTerminator;
+ } gce = {0x21, 0xF9, 0x04, // fixed bytes from spec
+ 0x01, // transparency on (no user input or disposal options)
+ 0x00, 0x00, // no animation delay time
+ 0x00, // color index 0 (white) is transparent
+ 0x00};
-boolean mgSaveToGif(FILE *gif_file, struct memGfx *screen)
-/* Save GIF to an already open file. */
+boolean mgSaveToGif(FILE *gif_file, struct memGfx *screen, boolean useTransparency)
+/* Save GIF to an already open file.
+ * If useTransparency, then the first color in memgfx's colormap/palette is
+ * assumed to be the image background color, and pixels of that color
+ * are made transparent. */
{
int i;
struct gif_header gif;
struct gif_image gim;
@@ -30,8 +46,13 @@
goto TRUNCOUT;
/* write global color map */
if (fwrite(screen->colorMap, 3, 256, gif_file) < 256)
goto TRUNCOUT;
+
+if (useTransparency)
+ if (fwrite(&gce, sizeof(gce), 1, gif_file ) < 1)
+ goto TRUNCOUT;
+
if (fputc(',', gif_file) < 0) /* comma to start image */
goto TRUNCOUT;
if (fwrite(&gim, sizeof(gim), 1, gif_file) < 1)
goto TRUNCOUT;
@@ -58,12 +79,16 @@
BADOUT:
return(FALSE);
}
-void mgSaveGif(struct memGfx *screen, char *name)
+void mgSaveGif(struct memGfx *screen, char *name, boolean useTransparency)
+/* Save memory bitmap as a gif.
+ * If useTransparency, then the first color in memgfx's colormap/palette is
+ * assumed to be the image background color, and pixels of that color
+ * are made transparent. */
{
FILE *gifFile = mustOpen(name, "wb");
-if (!mgSaveToGif(gifFile, screen))
+if (!mgSaveToGif(gifFile, screen, useTransparency))
{
remove(name);
errAbort("Couldn't save %s", name);
}