static gdImagePtr _gdCreateFromFile (gdIOCtx * in, int *sx, int *sy) { gdImagePtr im; int gd2xFlag = 0; int trueColorFlag = 0; if (!gdGetWord (sx, in)) { goto fail1; } if ((*sx == 65535) || (*sx == 65534)) { /* This is a gd 2.0 .gd file */ gd2xFlag = 1; /* 2.0.12: 65534 signals a truecolor .gd file. There is a slight redundancy here but we can live with it. */ if (*sx == 65534) { trueColorFlag = 1; } if (!gdGetWord (sx, in)) { goto fail1; } } if (!gdGetWord (sy, in)) { goto fail1; } GD2_DBG (printf ("Image is %dx%d\n", *sx, *sy)); if (trueColorFlag) { im = gdImageCreateTrueColor (*sx, *sy); } else { im = gdImageCreate (*sx, *sy); } if (!im) { goto fail1; } if (!_gdGetColors (in, im, gd2xFlag)) { goto fail2; } return im; fail2: gdImageDestroy (im); fail1: return 0; }
static gdImagePtr _gdCreateFromFile (gdIOCtx * in, int *sx, int *sy) { gdImagePtr im; int gd2xFlag = 0; if (!gdGetWord (sx, in)) { goto fail1; } if (*sx == 65535) { /* This is a gd 2.0 .gd file */ gd2xFlag = 1; if (!gdGetWord (sx, in)) { goto fail1; } } if (!gdGetWord (sy, in)) { goto fail1; } GD2_DBG (printf ("Image is %dx%d\n", *sx, *sy)); im = gdImageCreate (*sx, *sy); if (!_gdGetColors (in, im, gd2xFlag)) { goto fail2; } return im; fail2: gdImageDestroy (im); fail1: return 0; }
static int _gd2GetHeader (gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, int *fmt, int *ncx, int *ncy, t_chunk_info ** chunkIdx) { int i; int ch; char id[5]; t_chunk_info *cidx; int sidx; int nc; GD2_DBG (printf ("Reading gd2 header info\n")); for (i = 0; i < 4; i++) { ch = gdGetC (in); if (ch == EOF) { goto fail1; }; id[i] = ch; }; id[4] = 0; GD2_DBG (printf ("Got file code: %s\n", id)); /* Equiv. of 'magick'. */ if (strcmp (id, GD2_ID) != 0) { GD2_DBG (printf ("Not a valid gd2 file\n")); goto fail1; }; /* Version */ if (gdGetWord (vers, in) != 1) { goto fail1; }; GD2_DBG (printf ("Version: %d\n", *vers)); if ((*vers != 1) && (*vers != 2)) { GD2_DBG (printf ("Bad version: %d\n", *vers)); goto fail1; }; /* Image Size */ if (!gdGetWord (sx, in)) { GD2_DBG (printf ("Could not get x-size\n")); goto fail1; } if (!gdGetWord (sy, in)) { GD2_DBG (printf ("Could not get y-size\n")); goto fail1; } GD2_DBG (printf ("Image is %dx%d\n", *sx, *sy)); /* Chunk Size (pixels, not bytes!) */ if (gdGetWord (cs, in) != 1) { goto fail1; }; GD2_DBG (printf ("ChunkSize: %d\n", *cs)); if ((*cs < GD2_CHUNKSIZE_MIN) || (*cs > GD2_CHUNKSIZE_MAX)) { GD2_DBG (printf ("Bad chunk size: %d\n", *cs)); goto fail1; }; /* Data Format */ if (gdGetWord (fmt, in) != 1) { goto fail1; }; GD2_DBG (printf ("Format: %d\n", *fmt)); if ((*fmt != GD2_FMT_RAW) && (*fmt != GD2_FMT_COMPRESSED) && (*fmt != GD2_FMT_TRUECOLOR_RAW) && (*fmt != GD2_FMT_TRUECOLOR_COMPRESSED)) { GD2_DBG (printf ("Bad data format: %d\n", *fmt)); goto fail1; }; /* # of chunks wide */ if (gdGetWord (ncx, in) != 1) { goto fail1; }; GD2_DBG (printf ("%d Chunks Wide\n", *ncx)); /* # of chunks high */ if (gdGetWord (ncy, in) != 1) { goto fail1; }; GD2_DBG (printf ("%d Chunks vertically\n", *ncy)); if (gd2_compressed (*fmt)) { nc = (*ncx) * (*ncy); GD2_DBG (printf ("Reading %d chunk index entries\n", nc)); sidx = sizeof (t_chunk_info) * nc; cidx = gdCalloc (sidx, 1); if (!cidx) { goto fail1; } for (i = 0; i < nc; i++) { if (gdGetInt (&cidx[i].offset, in) != 1) { goto fail1; }; if (gdGetInt (&cidx[i].size, in) != 1) { goto fail1; }; }; *chunkIdx = cidx; }; GD2_DBG (printf ("gd2 header complete\n")); return 1; fail1: return 0; }
int _gdGetColors (gdIOCtx * in, gdImagePtr im, int gd2xFlag) { int i; if (gd2xFlag) { int trueColorFlag; if (!gdGetByte (&trueColorFlag, in)) { goto fail1; } /* 2.0.12: detect bad truecolor .gd files created by pre-2.0.12. Beginning in 2.0.12 truecolor is indicated by the initial 2-byte signature. */ if (trueColorFlag != im->trueColor) { goto fail1; } /* This should have been a word all along */ if (!im->trueColor) { if (!gdGetWord (&im->colorsTotal, in)) { goto fail1; } if (im->colorsTotal > gdMaxColors) { goto fail1; } } /* Int to accommodate truecolor single-color transparency */ if (!gdGetInt (&im->transparent, in)) { goto fail1; } } else { if (!gdGetByte (&im->colorsTotal, in)) { goto fail1; } if (!gdGetWord (&im->transparent, in)) { goto fail1; } if (im->transparent == 257) { im->transparent = (-1); } } GD2_DBG (printf ("Palette had %d colours (T=%d)\n", im->colorsTotal, im->transparent)); if (im->trueColor) { return TRUE; } for (i = 0; (i < gdMaxColors); i++) { if (!gdGetByte (&im->red[i], in)) { goto fail1; } if (!gdGetByte (&im->green[i], in)) { goto fail1; } if (!gdGetByte (&im->blue[i], in)) { goto fail1; } if (gd2xFlag) { if (!gdGetByte (&im->alpha[i], in)) { goto fail1; } } } for (i = 0; (i < im->colorsTotal); i++) { im->open[i] = 0; }; return TRUE; fail1: return FALSE; }
int _gdGetColors (gdIOCtx * in, gdImagePtr im, int gd2xFlag) { int i; if (gd2xFlag) { if (!gdGetByte (&im->trueColor, in)) { goto fail1; } /* This should have been a word all along */ if (!im->trueColor) { if (!gdGetWord (&im->colorsTotal, in)) { goto fail1; } } /* Int to accommodate truecolor single-color transparency */ if (!gdGetInt (&im->transparent, in)) { goto fail1; } } else { if (!gdGetByte (&im->colorsTotal, in)) { goto fail1; } if (!gdGetWord (&im->transparent, in)) { goto fail1; } if (im->transparent == 257) { im->transparent = (-1); } } GD2_DBG (printf ("Pallette had %d colours (T=%d)\n", im->colorsTotal, im->transparent)); for (i = 0; (i < gdMaxColors); i++) { if (!gdGetByte (&im->red[i], in)) { goto fail1; } if (!gdGetByte (&im->green[i], in)) { goto fail1; } if (!gdGetByte (&im->blue[i], in)) { goto fail1; } if (gd2xFlag) { if (!gdGetByte (&im->alpha[i], in)) { goto fail1; } } } for (i = 0; (i < im->colorsTotal); i++) { im->open[i] = 0; }; return TRUE; fail1: return FALSE; }