static gdImagePtr loadshapeimage(char *name) { gdImagePtr rv = 0; char *shapeimagefile,*suffix; FILE *in = NULL; if ((shapeimagefile=safefile(name))) { #ifndef MSWIN32 in = fopen (shapeimagefile, "r"); #else in = fopen (shapeimagefile, "rb"); #endif } if (!in) agerr(AGERR, "couldn't open image file %s\n",shapeimagefile); else { suffix = strrchr(shapeimagefile,'.'); if (!suffix) suffix = shapeimagefile; else suffix++; if (!strcasecmp(suffix,"wbmp")) rv = gdImageCreateFromWBMP(in); #ifdef WITH_GIF else if (!strcasecmp(suffix,"gif")) rv = gdImageCreateFromGif(in); #endif #ifdef HAVE_LIBPNG else if (!strcasecmp(suffix,"png")) rv = gdImageCreateFromPng(in); #endif #ifdef HAVE_LIBJPEG else if (!strcasecmp(suffix,"jpeg")||!strcasecmp(suffix,"jpg")) rv = gdImageCreateFromJpeg(in); #endif else if (!strcasecmp(suffix,"xbm")) rv = gdImageCreateFromXbm(in); else agerr(AGERR, "image file %s suffix not recognized\n",name); fclose(in); if (!rv) agerr(AGERR, "image file %s contents were not recognized\n",name); } return rv; }
gdImagePtr imagefilter_readFile( char *n ) { if(!n) return NULL; FILE *f = fopen(n,"r"); if(!f) return NULL; gdImagePtr img = NULL; if( strendswith(n,".gd")) img = gdImageCreateFromGd(f); if( strendswith(n,".gd2")) img = gdImageCreateFromGd2(f); if( strendswith(n,".gif")) img = gdImageCreateFromGif(f); if( strendswith(n,".jpg")) img = gdImageCreateFromJpeg(f); if( strendswith(n,".png")) img = gdImageCreateFromPng(f); if( strendswith(n,".bmp")) img = gdImageCreateFromWBMP(f); if( strendswith(n,".xbm")) img = gdImageCreateFromXbm(f); fclose(f); return img; }
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; }
NEOERR* mimg_zoomout(int ftype, FILE *dst, FILE*src, int width, int height) { MCS_NOT_NULLB(dst, src); int ow, oh; gdImagePtr im; FILE *gdin, *gdout; if (width <= 0 && height <= 0) return STATUS_OK; gdin = mfile_get_std_from_safe(src); gdout = mfile_get_std_from_safe(dst); fseek(src, 0, SEEK_SET); fseek(dst, 0, SEEK_SET); switch (ftype) { case MIMG_TYPE_JPEG: im = gdImageCreateFromJpeg(gdin); break; case MIMG_TYPE_PNG: im = gdImageCreateFromPng(gdin); break; case MIMG_TYPE_GIF: im = gdImageCreateFromGif(gdin); break; case MIMG_TYPE_BMP: im = gdImageCreateFromWBMP(gdin); break; default: return nerr_raise(NERR_ASSERT, "file type %d not support", ftype); } if (!im) return nerr_raise(NERR_ASSERT, "读取图片出错,文件格式错误?"); ow = gdImageSX(im); oh = gdImageSY(im); if ((width > 0 && ow > width) || (height > 0 && oh > height)) { if (width <= 0) width = (float)height / oh * ow; if (height <= 0) height = (float)width / ow * oh; gdImagePtr dim = gdImageCreateTrueColor(width, height); gdImageCopyResized(dim, im, 0, 0, 0, 0, width, height, ow, oh); if (dim) { switch (ftype) { case MIMG_TYPE_JPEG: gdImageJpeg(dim, gdout, 70); break; case MIMG_TYPE_PNG: gdImagePng(dim, gdout); break; case MIMG_TYPE_GIF: gdImageGif(dim, gdout); break; case MIMG_TYPE_BMP: gdImageWBMP(dim, 0, gdout); break; default: return nerr_raise(NERR_ASSERT, "file type %d not suport", ftype); } } else return nerr_raise(NERR_ASSERT, "resize image error"); } else { mfile_copy(dst, src); } return STATUS_OK; }
value ImageCreateFromWBMP(value filename) { FILE *_file = openFileRead(filename); gdImagePtr img = gdImageCreateFromWBMP(_file); fclose(_file); return alloc_gc_image(img); }
int main(int argc, char *argv[]) { FILE *fin = NULL; FILE *fout = NULL; gdImagePtr im_in = NULL, im = NULL; char *buf = NULL; int bufsize, ret = 0; /* FIXME get these from the cmdline */ int width = 128, height = 128; if (argc != 4) { fprintf (stderr, "Usage: %s <-d|-c> <inputfile> <outputfile>\n", argv[0]); ret = 1; goto exit; } fin = fopen(argv[2], "r"); if (!fin) { fprintf (stderr, "Error opening: %s: %s\n", argv[2], strerror(errno)); ret = 1; goto exit; } fout = fopen(argv[3], "w"); if (!fout) { fprintf (stderr, "Error opening: %s: %s\n", argv[3], strerror(errno)); ret = 1; goto exit; } bufsize = width * height; buf = malloc(bufsize); if (!buf) { fprintf (stderr, "Error allocating memory\n"); ret = 1; goto exit; } im = gdImageCreateTrueColor(width, height); if (!im) { fprintf (stderr, "Error allocating memory\n"); ret = 1; goto exit; } if (!strcmp(argv[1], "-d")) { if (fread(buf, 1, bufsize, fin) != bufsize) { fprintf (stderr, "Error reading: %s: %s\n", argv[2], strerror(errno)); ret = 1; goto exit; } ax203_decode2(buf, im->tpixels, width, height); gdImagePng (im, fout); } else if (!strcmp(argv[1], "-c")) { im_in = gdImageCreateFromPng(fin); if (im_in == NULL) { rewind(fin); im_in = gdImageCreateFromGif(fin); } if (im_in == NULL) { rewind(fin); im_in = gdImageCreateFromWBMP(fin); } /* gdImageCreateFromJpegPtr is chatty on error, so call it last */ if (im_in == NULL) { rewind(fin); im_in = gdImageCreateFromJpeg(fin); } if (im_in == NULL) { fprintf (stderr, "Error unrecognized file format for file: %s\n", argv[2]); ret = 1; goto exit; } gdImageCopyResampled (im, im_in, 0, 0, 0, 0, im->sx, im->sy, im_in->sx, im_in->sy); gdImageSharpen(im, 100); ax203_encode2(im->tpixels, buf, width, height); if (fwrite (buf, 1, bufsize, fout) != bufsize) { fprintf (stderr, "Error writing: %s: %s\n", argv[3], strerror(errno)); ret = 1; goto exit; } } else { fprintf (stderr, "%s: unkown option: %s\n", argv[0], argv[1]); ret = 1; goto exit; } exit: if (fin) fclose (fin); if (fout) fclose (fout); if (buf) free (buf); if (im) gdImageDestroy (im); if (im_in) gdImageDestroy (im_in); return ret; }