/* int GIFFlusht0(Metafile *mf, char *filename) transparent, color 0 */ int GIFFlusht0(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; int tcolor; int i; FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp);/*save the image as gif */ fclose(fp); /* open the gif, change transparency and then save */ FILE *inGif; FILE *ouGif; gdImagePtr gifImg; inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); ouGif = fopen(filename,"w"); //(0,0,0) is for white background tcolor = gdImageColorClosest(gifImg, 255, 255, 255); for (i=0; i < 256; i+=1) { tcolor = gdImageColorClosest(gifImg, i, i, i); } gdImageColorTransparent(gifImg, tcolor); gdImageGif(gifImg, ouGif); fclose(ouGif); gdImageDestroy(gifImg); } return status; }
/* build a color or find the nearest color in the color table */ int find_color ( gdImagePtr graph, int color ) { int i_col; if ( (i_col = gdImageColorExact(graph,r(color), g(color), b(color))) == -1 ) { if ( (i_col = gdImageColorAllocate(graph,r(color), g(color), b(color))) == -1 ) { i_col = gdImageColorClosest(graph,r(color), g(color), b(color)); } } return (i_col); }
int allocOrExact(gdImagePtr im, int r, int g, int b) { int c; c = gdImageColorExact(im, r,g,b); if(c >= 0) return c; c = gdImageColorAllocate(im, r, g, b); if(c >= 0) return c; return gdImageColorClosest(im, r, g, b); }
result_t Image::colorClosest(int32_t red, int32_t green, int32_t blue, int32_t &retVal) { if (!m_image) return CHECK_ERROR(CALL_E_INVALID_CALL); if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255) return CHECK_ERROR(CALL_E_INVALIDARG); retVal = gdImageColorClosest(m_image, red, green, blue); return 0; }
void Blend ( gdImagePtr image, const JFloat alpha, const JRGB& alphaColor, const JCharacter* outputFileName ) { const JSize w = gdImageSX(image); const JSize h = gdImageSY(image); gdImagePtr i2 = gdImageCreate(w,h); for (JIndex x=0; x<w; x++) { for (JIndex y=0; y<h; y++) { const int c = gdImageGetPixel(image, x,y); const int r = BlendComponent(gdImageRed(image, c), alphaColor.red, alpha); const int g = BlendComponent(gdImageGreen(image, c), alphaColor.green, alpha); const int b = BlendComponent(gdImageBlue(image, c), alphaColor.blue, alpha); int c2 = gdImageColorExact(i2, r,g,b); if (c2 == -1) { c2 = gdImageColorAllocate(i2, r,g,b); if (c2 == -1) { c2 = gdImageColorClosest(i2, r,g,b); } } gdImageSetPixel(i2, x,y, c2); } } WriteGIF(i2, outputFileName); gdImageDestroy(i2); }
void gdImagePaletteCopy(gdImagePtr to, gdImagePtr from) { int i; int x, y, p; int xlate[256]; for (i=0; i < 256 ; i++) { xlate[i] = -1; }; for (x=0 ; x < (to->sx) ; x++) { for (y=0 ; y < (to->sy) ; y++) { p = gdImageGetPixel(to, x, y); if (xlate[p] == -1) { xlate[p] = gdImageColorClosest(from, to->red[p], to->green[p], to->blue[p]); /*printf("Mapping %d (%d, %d, %d) to %d (%d, %d, %d)\n", */ /* p, to->red[p], to->green[p], to->blue[p], */ /* xlate[p], from->red[xlate[p]], from->green[xlate[p]], from->blue[xlate[p]]); */ }; gdImageSetPixel(to, x, y, xlate[p]); }; }; for (i=0; (i < (from->colorsTotal) ) ; i++) { /*printf("Copying color %d (%d, %d, %d)\n", i, from->red[i], from->blue[i], from->green[i]); */ to->red[i] = from->red[i]; to->blue[i] = from->blue[i]; to->green[i] = from->green[i]; to->open[i] = 0; }; for (i=from->colorsTotal ; (i < to->colorsTotal) ; i++) { to->open[i] = 1; }; to->colorsTotal = from->colorsTotal; }
int main (int argc, char **argv) { #ifdef HAVE_LIBPNG gdImagePtr im, ref, im2, im3; FILE *in, *out; void *iptr; int sz; char of[256]; 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 (argv[1], "rb"); if (!in) { fprintf (stderr, "Input file does not exist!\n"); exit (1); } im = gdImageCreateFromPng (in); rewind (in); ref = gdImageCreateFromPng (in); fclose (in); printf ("Reference File has %d Palette entries\n", ref->colorsTotal); CompareImages ("Initial Versions", ref, im); /* */ /* Send to PNG File then Ptr */ /* */ #ifdef VMS sprintf (of, "%s-png", argv[1]); #else sprintf (of, "%s.png", argv[1]); #endif out = fopen (of, "wb"); gdImagePng (im, out); fclose (out); in = fopen (of, "rb"); if (!in) { fprintf (stderr, "PNG Output file does not exist!\n"); exit (1); } im2 = gdImageCreateFromPng (in); fclose (in); CompareImages ("GD->PNG File->GD", ref, im2); unlink (of); gdImageDestroy (im2); /* 2.0.21: use the new From*Ptr functions */ iptr = gdImagePngPtr (im, &sz); im2 = gdImageCreateFromPngPtr (sz, iptr); gdFree (iptr); CompareImages ("GD->PNG ptr->GD", ref, im2); gdImageDestroy (im2); /* */ /* Send to GD2 File then Ptr */ /* */ #ifdef VMS sprintf (of, "%s-gd2", argv[1]); #else sprintf (of, "%s.gd2", argv[1]); #endif out = fopen (of, "wb"); gdImageGd2 (im, out, 128, 2); fclose (out); in = fopen (of, "rb"); if (!in) { fprintf (stderr, "GD2 Output file does not exist!\n"); exit (1); } im2 = gdImageCreateFromGd2 (in); fclose (in); CompareImages ("GD->GD2 File->GD", ref, im2); unlink (of); gdImageDestroy (im2); iptr = gdImageGd2Ptr (im, 128, 2, &sz); /*printf("Got ptr %d (size %d)\n",iptr, sz); */ im2 = gdImageCreateFromGd2Ptr (sz, iptr); gdFree (iptr); /*printf("Got img2 %d\n",im2); */ CompareImages ("GD->GD2 ptr->GD", ref, im2); gdImageDestroy (im2); /* */ /* Send to GD File then Ptr */ /* */ #ifdef VMS sprintf (of, "%s-gd", argv[1]); #else sprintf (of, "%s.gd", argv[1]); #endif out = fopen (of, "wb"); gdImageGd (im, out); fclose (out); in = fopen (of, "rb"); if (!in) { fprintf (stderr, "GD Output file does not exist!\n"); exit (1); } im2 = gdImageCreateFromGd (in); fclose (in); CompareImages ("GD->GD File->GD", ref, im2); unlink (of); gdImageDestroy (im2); iptr = gdImageGdPtr (im, &sz); /*printf("Got ptr %d (size %d)\n",iptr, sz); */ im2 = gdImageCreateFromGdPtr (sz, iptr); gdFree (iptr); /*printf("Got img2 %d\n",im2); */ CompareImages ("GD->GD ptr->GD", ref, im2); gdImageDestroy (im2); /* * Test gdImageCreateFromPngSource' */ in = fopen (argv[1], "rb"); imgsrc.source = freadWrapper; imgsrc.context = in; im2 = gdImageCreateFromPngSource (&imgsrc); fclose (in); if (im2 == NULL) { printf ("GD Source: ERROR Null returned by gdImageCreateFromPngSource\n"); } else { CompareImages ("GD Source", ref, im2); gdImageDestroy (im2); }; /* * Test gdImagePngToSink' */ #ifdef VMS sprintf (of, "%s-snk", argv[1]); #else sprintf (of, "%s.snk", argv[1]); #endif out = fopen (of, "wb"); imgsnk.sink = fwriteWrapper; imgsnk.context = out; gdImagePngToSink (im, &imgsnk); fclose (out); in = fopen (of, "rb"); if (!in) { fprintf (stderr, "GD Sink: ERROR - GD Sink Output file does not exist!\n"); } else { im2 = gdImageCreateFromPng (in); fclose (in); CompareImages ("GD Sink", ref, im2); gdImageDestroy (im2); }; unlink (of); /* */ /* Test Extraction */ /* */ in = fopen ("test/gdtest_200_300_150_100.png", "rb"); if (!in) { fprintf (stderr, "gdtest_200_300_150_100.png does not exist!\n"); exit (1); } im2 = gdImageCreateFromPng (in); fclose (in); in = fopen ("test/gdtest.gd2", "rb"); if (!in) { fprintf (stderr, "gdtest.gd2 does not exist!\n"); exit (1); } im3 = gdImageCreateFromGd2Part (in, 200, 300, 150, 100); fclose (in); CompareImages ("GD2Part (gdtest_200_300_150_100.png, gdtest.gd2(part))", im2, im3); gdImageDestroy (im2); gdImageDestroy (im3); /* */ /* Copy Blend */ /* */ in = fopen ("test/gdtest.png", "rb"); if (!in) { fprintf (stderr, "gdtest.png does not exist!\n"); exit (1); } im2 = gdImageCreateFromPng (in); fclose (in); im3 = gdImageCreate (100, 60); colRed = gdImageColorAllocate (im3, 255, 0, 0); colBlu = gdImageColorAllocate (im3, 0, 0, 255); gdImageFilledRectangle (im3, 0, 0, 49, 30, colRed); gdImageFilledRectangle (im3, 50, 30, 99, 59, colBlu); gdImageCopyMerge (im2, im3, 150, 200, 10, 10, 90, 50, 50); gdImageCopyMerge (im2, im3, 180, 70, 10, 10, 90, 50, 50); gdImageCopyMergeGray (im2, im3, 250, 160, 10, 10, 90, 50, 50); gdImageCopyMergeGray (im2, im3, 80, 70, 10, 10, 90, 50, 50); gdImageDestroy (im3); in = fopen ("test/gdtest_merge.png", "rb"); if (!in) { fprintf (stderr, "gdtest_merge.png does not exist!\n"); exit (1); } im3 = gdImageCreateFromPng (in); fclose (in); printf ("[Merged Image has %d colours]\n", im2->colorsTotal); CompareImages ("Merged (gdtest.png, gdtest_merge.png)", im2, im3); gdImageDestroy (im2); gdImageDestroy (im3); #ifdef HAVE_LIBJPEG out = fopen ("test/gdtest.jpg", "wb"); if (!out) { fprintf (stderr, "Can't create file test/gdtest.jpg.\n"); exit (1); } gdImageJpeg (im, out, -1); fclose (out); in = fopen ("test/gdtest.jpg", "rb"); if (!in) { fprintf (stderr, "Can't open file test/gdtest.jpg.\n"); exit (1); } im2 = gdImageCreateFromJpeg (in); fclose (in); if (!im2) { fprintf (stderr, "gdImageCreateFromJpeg failed.\n"); exit (1); } gdImageDestroy (im2); printf ("Created test/gdtest.jpg successfully. Compare this image\n" "to the input image manually. Some difference must be\n" "expected as JPEG is a lossy file format.\n"); #endif /* HAVE_LIBJPEG */ /* 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); #else fprintf (stderr, "No PNG library support.\n"); #endif /* HAVE_LIBPNG */ return 0; }
/* int GIFFlusht1(Metafile *mf, char *filename) transparent, color 1 (black background) */ int GIFFlusht1(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; int tcolor; int white; /* File w/ ".gif" only are not created (Ferret hack) */ if (strcasecmp(filename, ".gif") != 0){ FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp);/*save the image as gif */ fclose(fp); /* open the gif, change transparency and then save */ FILE *inGif; FILE *ouGif; gdImagePtr gifImg; inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); ouGif = fopen(filename,"w"); //(1,1,1) is for black background tcolor = gdImageColorClosest(gifImg, 0, 0, 0);; gdImageColorTransparent(gifImg, tcolor); gdImageGif(gifImg, ouGif); fclose(ouGif); gdImageDestroy(gifImg); /* copy logo image to an output image */ /* Set up an image the size of the Ferret plot image */ inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); /* Open the image to be the background. Copy to the output image */ FILE *inLog; gdImagePtr logImg; /* inLog = fopen("/home/users/ansley/ans_ferret/users/ansley/centered_noaa_logo.gif","rb"); */ inLog = fopen("/home/users/ansley/Wave-3.gif","rb"); logImg = gdImageCreateFromGif(inLog); fclose(inLog); gdImageCopy(gifImg, logImg, 0, 0, 0, 0, logImg->sx, logImg->sy); /* Now copy the transparent image to the output image */ inGif = fopen(filename,"rb"); transImg = gdImageCreateFromGif(inGif); fclose(inGif); gdImageCopy(gifImg, transImg, 0, 0, 0, 0, gifImg->sx, gifImg->sy); ouGif = fopen(filename,"w"); gdImageGif(gifImg, ouGif); fclose(ouGif); } } else { status = OK; } return status; }
/* int GIFFlusht0(Metafile *mf, char *filename) transparent, color 0 (white background) */ int GIFFlusht0(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; int tcolor; int i; /* Load the logo image */ /* from Jing's email: /* /* Through the function gdImageCopy, you can control the position of you logo and which /* part of your logo to copy. /* /* gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h) /* The dstX and dstY arguments specify the point in the destination image to which the region will be copied. /* The srcX and srcY arguments specify the upper left corner of the region in the source image. /* The w and h arguments specify the width and height of the region. /* more notes (ACM) /* the values of the arguments to gdImageCopy are in pixels. /* the upper left corner is the origin for the location on the destination image /* Specifying values smaller than the size of the logo image has the effect of chopping /* the image, not shrinking it. /* To place the image 100 pixels to the right of the orig in X on the destination image, /* and starting at 50 pixels from the top of the source image */ /* gdImageCopy(meta->image, logImg, 100, 0, 0, 50, logImg->sx, logImg->sy); */ /* FILE *inLog; /* gdImagePtr logImg; /* inLog = fopen("/home/users/ansley/ans_ferret/users/ansley/centered_noaa_logo.gif","rb"); /* inLog = fopen("/home/users/ansley/Wave-3.gif","rb");*/ /* logImg = gdImageCreateFromGif(inLog); /* /* Copy the logo to your figure meta->image */ /* /* gdImageCopy(meta->image, logImg, 0, 0, 0, 0, logImg->sx, logImg->sy); /* fclose(inLog); /* File w/ ".gif" only are not created (Ferret hack) */ if (strcasecmp(filename, ".gif") != 0){ FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp);/*save the image as gif */ fclose(fp); /* open the gif, change transparency and then save */ FILE *inGif; FILE *ouGif; gdImagePtr gifImg; gdImagePtr transImg; inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); ouGif = fopen(filename,"w"); //(0,0,0) is for white background tcolor = gdImageColorClosest(gifImg, 255, 255, 255); for (i=0; i < 256; i+=1) { tcolor = gdImageColorClosest(gifImg, i, i, i); } gdImageColorTransparent(gifImg, tcolor); gdImageGif(gifImg, ouGif); fclose(ouGif); gdImageDestroy(gifImg); /* copy logo image to an output image */ /* Set up an image the size of the Ferret plot image */ inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); /* Open the image to be the background. Copy to the output image */ FILE *inLog; gdImagePtr logImg; /* inLog = fopen("/home/users/ansley/ans_ferret/users/ansley/centered_noaa_logo.gif","rb"); */ inLog = fopen("/home/users/ansley/Wave-3.gif","rb"); logImg = gdImageCreateFromGif(inLog); fclose(inLog); gdImageCopy(gifImg, logImg, 0, 0, 0, 0, logImg->sx, logImg->sy); /* Now copy the transparent image to the output image */ inGif = fopen(filename,"rb"); transImg = gdImageCreateFromGif(inGif); fclose(inGif); gdImageCopy(gifImg, transImg, 0, 0, 0, 0, gifImg->sx, gifImg->sy); ouGif = fopen(filename,"w"); gdImageGif(gifImg, ouGif); fclose(ouGif); } } else { status = OK; } return status; }