00001
00002
00003 #include "common.h"
00004 #include "memgfx.h"
00005 #include "gifcodes.h"
00006
00007 static char const rcsid[] = "$Id: gifwrite.c,v 1.6 2003/05/21 21:02:22 kent Exp $";
00008
00009 static char gifsig[] = "GIF87a";
00010
00011
00012 boolean mgSaveToGif(FILE *gif_file, struct memGfx *screen)
00013
00014 {
00015 int i;
00016 struct gif_header gif;
00017 struct gif_image gim;
00018 long gif_wcount;
00019
00020 gif_wcount = (long)screen->width * screen->height;
00021 zeroBytes(&gif, sizeof(gif));
00022 strcpy(gif.giftype, gifsig);
00023 gif.wlo = gim.wlo = ((screen->width)&0xff);
00024 gif.whi = gim.whi = ((screen->width>>8)&0xff);
00025 gif.hlo = gim.hlo = ((screen->height)&0xff);
00026 gif.hhi = gim.hhi = ((screen->height>>8)&0xff);
00027 gim.xlo = gim.xhi = gim.ylo = gim.yhi = gim.flags = 0;
00028 gif.colpix = COLPIXVGA13;
00029 if (fwrite(&gif, sizeof(gif), 1, gif_file ) < 1)
00030 goto TRUNCOUT;
00031
00032 if (fwrite(screen->colorMap, 3, 256, gif_file) < 256)
00033 goto TRUNCOUT;
00034 if (fputc(',', gif_file) < 0)
00035 goto TRUNCOUT;
00036 if (fwrite(&gim, sizeof(gim), 1, gif_file) < 1)
00037 goto TRUNCOUT;
00038 fputc(8,gif_file);
00039 fflush(gif_file);
00040 i = gif_compress_data(8, screen->pixels, gif_wcount, gif_file);
00041 switch (i)
00042 {
00043 case 0:
00044 break;
00045 case -2:
00046 warn("Out of memory writing GIF");
00047 goto BADOUT;
00048 case -3:
00049 goto TRUNCOUT;
00050 default:
00051 warn("Error code %d writing gif", i);
00052 goto BADOUT;
00053 }
00054 fputc(';', gif_file);
00055 return(TRUE);
00056 TRUNCOUT:
00057 warn("Disk full writing GIF");
00058 BADOUT:
00059 return(FALSE);
00060 }
00061
00062 void mgSaveGif(struct memGfx *screen, char *name)
00063 {
00064 FILE *gifFile = mustOpen(name, "wb");
00065 if (!mgSaveToGif(gifFile, screen))
00066 {
00067 remove(name);
00068 errAbort("Couldn't save %s", name);
00069 }
00070 if (fclose(gifFile) != 0)
00071 errnoAbort("fclose failed");
00072 }