/* returns 0 on success, 1 on failure */ static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out) { gdImagePtr pim = 0, tim = im; int interlace, BitsPerPixel; interlace = im->interlace; if (im->trueColor) { /* Expensive, but the only way that produces an acceptable result: mix down to a palette based temporary image. */ pim = gdImageCreatePaletteFromTrueColor(im, 1, 256); if (!pim) { return 1; } tim = pim; } BitsPerPixel = colorstobpp(tim->colorsTotal); /* All set, let's do it. */ GIFEncode( out, tim->sx, tim->sy, interlace, 0, tim->transparent, BitsPerPixel, tim->red, tim->green, tim->blue, tim); if (pim) { /* Destroy palette based temporary image. */ gdImageDestroy( pim); } return 0; }
int main(int argc, char *argv[]) { int bitsperpix; int i; SET_DEFAULT_BINARY(); SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); progname = argv[0]; samplefac = 0; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { case 'g': gamv = atof(argv[++i]); break; case 'b': greyscale = 1; break; case 'd': dither = !dither; break; case 'c': ncolors = atoi(argv[++i]); break; case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; bradj = atoi(argv[++i]); break; case 'n': samplefac = atoi(argv[++i]); break; default: goto userr; } else break; if (i < argc-2) goto userr; if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { fprintf(stderr, "%s: cannot open input \"%s\"\n", progname, argv[i]); exit(1); } if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { fprintf(stderr, "%s: cannot open output \"%s\"\n", progname, argv[i+1]); exit(1); } if (checkheader(stdin, COLRFMT, NULL) < 0 || fgetresolu(&xmax, &ymax, stdin) < 0) { fprintf(stderr, "%s: bad picture format\n", progname); exit(1); } picstart = ftell(stdin); currow = -1; scanln = (COLR *)malloc(xmax*sizeof(COLR)); if (scanln == NULL) { fprintf(stderr, "%s: out of memory\n", progname); exit(1); } /* set up gamma correction */ setcolrgam(gamv); /* figure out the bits per pixel */ if ((ncolors < 2) | (ncolors > MAXCOLORS)) ncolors = MAXCOLORS; for (bitsperpix = 1; ncolors > 1<<bitsperpix; bitsperpix++) ; /* compute color map */ if (greyscale) mkgrymap(ncolors); else mkclrmap(ncolors); /* convert image */ GIFEncode(stdout, xmax, ymax, 0, 0, bitsperpix, rmap, gmap, bmap, getgifpix); exit(0); userr: fprintf(stderr, "Usage: %s [-b][-d][-n samp][-c ncolors][-g gamv][-e +/-stops] input [output]\n", progname); exit(1); }