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; }
static gdImagePtr puzzle_create_gdimage_from_file(const char * const file) { gdImagePtr gdimage = NULL; FILE *fp; PuzzleImageTypeCode image_type_code; if ((fp = fopen(file, "rb")) == NULL) { return NULL; } image_type_code = puzzle_get_image_type_from_fp(fp); switch (image_type_code) { case PUZZLE_IMAGE_TYPE_JPEG: gdimage = gdImageCreateFromJpeg(fp); break; case PUZZLE_IMAGE_TYPE_PNG: gdimage = gdImageCreateFromPng(fp); break; case PUZZLE_IMAGE_TYPE_GIF: gdimage = gdImageCreateFromGif(fp); break; default: gdimage = NULL; } (void) fclose(fp); return gdimage; }
image_t * bp_image_new (int type, char *filename) { FILE *file = bp_file_open (curr_scene, filename, "rb"); if (file) { image_t *image = NEW_1 (image_t, curr_scene->material_pool); switch (type) { case bp_PNG: image->im = gdImageCreateFromPng (file); break; /* case BMP:*/ /* image->im = gdImageCreateFromBmp (file); */ /* break; */ case bp_JPEG: image->im = gdImageCreateFromJpeg (file); break; case bp_GIF: image->im = gdImageCreateFromGif (file); break; } if (image->im == NULL) { bp_report (BP_LOG_ERROR, "%s declared with wrong type\n", filename); return NULL; } return image; } return NULL; }
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; }
int main() { gdImagePtr im; FILE *fp; char path[1024]; int error = 0; sprintf(path, "%s/gif/bug00066.gif", GDTEST_TOP_DIR); fp = fopen(path, "rb"); if (!fp) { printf("cannot open <%s>\n", path); return -1; } im = gdImageCreateFromGif(fp); fclose(fp); sprintf(path, "%s/gif/bug00066_exp.png", GDTEST_TOP_DIR); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; } gdImageDestroy(im); return error; }
int main(int argc, char **argv) { #ifdef WITH_GIF gdImagePtr im; FILE *in, *out; if (argc != 3) { fprintf(stderr, "Usage: giftogd filename.gif filename.gd\n"); exit(1); } in = fopen(argv[1], "rb"); if (!in) { fprintf(stderr, "Input file does not exist!\n"); exit(1); } im = gdImageCreateFromGif(in); fclose(in); if (!im) { fprintf(stderr, "Input is not in GIF format!\n"); exit(1); } out = fopen(argv[2], "wb"); if (!out) { fprintf(stderr, "Output file cannot be written to!\n"); gdImageDestroy(im); exit(1); } gdImageGd(im, out); fclose(out); gdImageDestroy(im); #else fprintf (stderr, "GIF support is not available.\n"); #endif }
value ImageCreateFromGif(value filename) { FILE *_file = openFileRead(filename); gdImagePtr img = gdImageCreateFromGif(_file); fclose(_file); if (img == NULL) failure("image file could not be loaded: probably wrong format"); return alloc_gc_image(img); }
int main(int argc, char *argv[]) { char red[256]; char green[256]; char blue[256]; chdir(argv[3]); // Directory where user-controlled "image" file lives. static char command[256] = "/usr/bin/file "; strcat(command, argv[1]); static FILE *stream = popen(command, "r"); if (!stream) exit(-1); fgets(haystack, 1034, stream); if (!strstr(haystack, "GIF image data")) { puts("Only GIF File Support"); exit(-1); } pclose(stream); // Image is read as a gif, png, jpeg, or bmp; whatever works. int t = time(NULL); int i = 0; while (1) { stream = fopen(argv[1], "rb"); switch (i) { case 1: image = gdImageCreateFromGif(stream, "rb"); break; case 2: image = gdImageCreateFromPng(stream, "rb"); break; case 3: image = gdImageCreateFromJpeg(stream, "rb"); break; case 4: image = gdImageCreateFromBmp(stream, "rb"); break; } if (image) break; int i = (i + 1) % 5; fclose(stream); if (t < time(NULL) - 2) exit(-1); } // Some initial bytes from the first row of the image are read. // image->sx is the image width, so the image must be at least // `256 * 257` pixels wide to trigger the stack overflow. for (static int i = 0; i < image->sx / 256 - 1; i++) { int x = gdImageGetTrueColorPixel(image, i, 0); red[i] = x; green[i] = x >> 8; blue[i] = x >> 16; } switch (atoi(argv[2])) { case 1: puts("Red"); puts(red); break; case 2: puts("Green"); puts(green); break; case 3: puts("Blue"); puts(blue); break; } return 0; }
int main() { gdImagePtr im; FILE *fp = gdTestFileOpen("gif/bug00060.gif"); im = gdImageCreateFromGif(fp); fclose(fp); gdImageDestroy(im); return 0; }
LISP lgdImageCreateFromGif(LISP f) { LISP result; long iflag; result = cons(NIL,NIL); result->type = tc_gdimage; iflag = no_interrupt(1); result->storage_as.string.data = (char *) gdImageCreateFromGif(get_c_file(f,NULL)); no_interrupt(iflag); return(result); }
gdImagePtr fromGif(char *fname) { FILE *fp; gdImagePtr im; fp = fopen(fname, "rb"); if(!fp){ fprintf(stderr, "Can't open [%s]\n", fname); return NULL; } im = gdImageCreateFromGif(fp); fclose(fp); return im; }
gdImagePtr ReadGIF ( const JCharacter* fileName ) { FILE* input = fopen(fileName, "rb"); assert( input != NULL ); gdImagePtr image = gdImageCreateFromGif(input); if (image == NULL) { cerr << fileName << " is not a GIF" << endl; exit(1); } fclose(input); return image; }
EF_Error ef_video_load_texture_file(utf8 *filename, GLuint id, int build_mipmaps) { FILE *file = fopen(filename, "rb"); if(!file) { return EF_ERROR_FILE; } gdImagePtr image = NULL; uint8_t magic_buffer[4]; if(1 != fread(magic_buffer, sizeof(magic_buffer), 1, file)) { fclose(file); return EF_ERROR_IMAGE_DATA; } fseek(file, 0, SEEK_SET); if((magic_buffer[0] == 0x89) && (magic_buffer[1] == 'P') && (magic_buffer[2] == 'N') && (magic_buffer[3] == 'G')) { image = gdImageCreateFromPng(file); } else if((magic_buffer[0] == 'G') && (magic_buffer[1] == 'I') && (magic_buffer[2] == 'F') && (magic_buffer[3] == '8')) { image = gdImageCreateFromGif(file); } else if((magic_buffer[0] == 0xFF) && (magic_buffer[1] == 0xD8)) { image = gdImageCreateFromJpeg(file); } fclose(file); if(!image) { return EF_ERROR_IMAGE_DATA; } EF_Error result = ef_internal_video_load_texture_gd_image(image, id, build_mipmaps); gdImageDestroy(image); return result; }
/* 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; }
int main() { gdImagePtr im; char * giffiles[4] = {"bug00005_0.gif", "bug00005_1.gif", "bug00005_2.gif", "bug00005_3.gif"}; int valid[4] = {0, 0, 0, 0}; char *exp[4] = {NULL, NULL, "bug00005_2_exp.png", NULL}; const int files_cnt = 4; FILE *fp; int i = 0; int error = 0; char path[1024]; for (i=0; i < files_cnt; i++) { sprintf(path, "%s/gif/%s", GDTEST_TOP_DIR, giffiles[i]); fp = fopen(path, "rb"); if (!fp) { gdTestErrorMsg("<%s> Input file does not exist!\n", path); return 1; } im = gdImageCreateFromGif(fp); fclose(fp); if (valid[i]) { if (!im) { error = 1; } else { sprintf(path, "%s/gif/%s", GDTEST_TOP_DIR, exp[i]); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; } gdImageDestroy(im); } } else { if (!gdTestAssert(im == NULL)) { error = 1; } } } return error; }
/* 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; }
static VALUE fastimage_native_resize( VALUE self, VALUE rb_in, VALUE rb_out, VALUE rb_w, VALUE rb_h, VALUE rb_image_type, VALUE rb_jpeg_quality, VALUE rb_orientation ) { char *filename_in = StringValuePtr(rb_in); char *filename_out = StringValuePtr(rb_out); int w = NUM2INT(rb_w); int h = NUM2INT(rb_h); int image_type = NUM2INT(rb_image_type); int jpeg_quality = NUM2INT(rb_jpeg_quality); int orientation = NUM2INT(rb_orientation); gdImagePtr im_in, im_out; FILE *in, *out; int trans = 0, x = 0, y = 0, f = 0; in = fopen(filename_in, "rb"); if (!in) return Qnil; switch(image_type) { case 0: im_in = gdImageCreateFromJpeg(in); break; case 1: im_in = gdImageCreateFromPng(in); break; case 2: im_in = gdImageCreateFromGif(in); trans = gdImageGetTransparent(im_in); /* find a transparent pixel, then turn off transparency so that it copies correctly */ if (trans >= 0) { for (x=0; x<gdImageSX(im_in); x++) { for (y=0; y<gdImageSY(im_in); y++) { if (gdImageGetPixel(im_in, x, y) == trans) { f = 1; break; } } if (f) break; } gdImageColorTransparent(im_in, -1); if (!f) trans = -1; /* no transparent pixel found */ } break; } if (!im_in) { fclose(in); return Qnil; } /* Handle orientation */ if (orientation == 5 || orientation == 6) { im_in = gdImageRotateInterpolated(im_in, 270.0, 0); } if (orientation == 7 || orientation == 8) { im_in = gdImageRotateInterpolated(im_in, 90.0, 0); } if (!im_in) { fclose(in); return Qnil; } if (orientation == 2 || orientation == 5 || orientation == 7) { gdImageFlipHorizontal(im_in); } if (orientation == 3) { gdImageFlipBoth(im_in); } if (orientation == 4) { gdImageFlipVertical(im_in); } /* Compute target size */ if (w == 0 || h == 0) { int originalWidth = gdImageSX(im_in); int originalHeight = gdImageSY(im_in); if (h != 0) { w = (int)(h * originalWidth / originalHeight); } else if (w != 0) { h = (int)(w * originalHeight / originalWidth); } else { w = originalWidth; h = originalHeight; } } im_out = gdImageCreateTrueColor(w, h); /* must be truecolor */ if (im_out) { if (image_type == 1) { gdImageAlphaBlending(im_out, 0); /* handle transparency correctly */ gdImageSaveAlpha(im_out, 1); } fclose(in); } else { fclose(in); return Qnil; } /* Now copy the original */ gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, gdImageSX(im_out), gdImageSY(im_out), gdImageSX(im_in), gdImageSY(im_in)); out = fopen(filename_out, "wb"); if (out) { switch(image_type) { case 0: gdImageJpeg(im_out, out, jpeg_quality); break; case 1: gdImagePng(im_out, out); break; case 2: gdImageTrueColorToPalette(im_out, 0, 256); if (trans >= 0) { trans = gdImageGetPixel(im_out, x, y); /* get the color index of our transparent pixel */ gdImageColorTransparent(im_out, trans); /* may not always work as hoped */ } gdImageGif(im_out, out); break; } fclose(out); } gdImageDestroy(im_in); gdImageDestroy(im_out); return Qnil; }
main(int argc, char *argv[]){ FILE *fp; gdImagePtr im, im2; int i,x,y,xsize,ysize,c; if(argc<7){ fprintf(stderr, "[%s] compiled [%s/%s]\n", argv[0], __DATE__, __TIME__); fprintf(stderr, "Usage : %s x1 x2 y1 y2 in-gif out-gif [-blue]\n", argv[0]); exit(1); } if(argc>7){ if(strcmp(argv[7], "-blue")==0) blueMode = 1; } x1 = atoi(argv[1]); x2 = atoi(argv[2]); y1 = atoi(argv[3]); y2 = atoi(argv[4]); infile = argv[5]; outfile = argv[6]; fp = fopen(infile, "rb"); if(!fp){ fprintf(stderr, "Can't open [%s]\n", infile); exit(1); } im = gdImageCreateFromGif(fp); fclose(fp); xsize = gdImageSX(im); ysize = gdImageSY(im); im2 = gdImageCreate(xsize, ysize); if(blueMode){ // use bluish gradation for low index value int j; for(i=0; i<256; i++){ j = (i-blueValue)*255/(255-blueValue); //////////////// blueValue --> 0, 255 --> 255 j = (j<0)?0:j; j= (j>255)?255:j; gdImageColorAllocate(im2, j, j, i); } } else { for(i=0; i<256; i++) gdImageColorAllocate(im2, i, i, i); } for(y=0; y<ysize; y++){ for(x=0; x<xsize; x++){ c = gdImageGetPixel(im, x, y); { int r,g,b; // fixed 2001.07.09 r = gdImageRed(im, c); g = gdImageGreen(im, c); b = gdImageBlue(im, c); c = ( g + g + r + b )/4; } indexMap(&c); gdImageSetPixel(im2, x, y, c); } } gdImageDestroy(im); fp = fopen(outfile, "wb"); if(!fp){ fprintf(stderr, "Can't open [%s]\n", outfile); exit(1); } gdImageGif(im2, fp); fclose(fp); }
int main(int argc, char **argv) { FILE *in; FILE *out; char outFn[20]; int useStdinStdout=0; /* Declare our image pointer */ gdImagePtr im = 0; int i; /* We'll clear 'no' once we know the user has made a reasonable request. */ int no = 1; /* We'll set 'write' once we know the user's request requires that the image be written back to disk. */ int write = 0; /* C programs always get at least one argument; we want at least one more (the image), more in practice. */ if (argc < 2 || !strcmp(argv[1], "--help")) { no = 1; goto usage; } /* The last argument should be the image. Open the file. */ if (strcmp("-", argv[argc-1])==0) { /* - is synonymous with STDIN */ useStdinStdout = 1; in = stdin; } else { in = fopen(argv[argc-1], "rb"); } if (!in) { fprintf(stderr, "Error: can't open file %s.\n", argv[argc-1]); exit(1); } /* Now load the image. */ im = gdImageCreateFromGif(in); fclose(in); /* If the load failed, it must not be a GIF file. */ if (!im) { fprintf(stderr, "Error: %s is not a valid gif file.\n", argv[argc-1]); exit(1); } /* Consider each argument in turn. */ for (i=1; (i < (argc-1)); i++) { /* -i turns on and off interlacing. */ if (!strcmp(argv[i], "--help")) { /* Every program should use this for help! :) */ no = 1; goto usage; } else if (!strcmp(argv[i], "-i")) { if (i == (argc-2)) { fprintf(stderr, "Error: -i specified without y or n.\n"); no = 1; goto usage; } if (!strcmp(argv[i+1], "y")) { /* Set interlace. */ gdImageInterlace(im, 1); } else if (!strcmp(argv[i+1], "n")) { /* Clear interlace. */ gdImageInterlace(im, 0); } else { fprintf(stderr, "Error: -i specified without y or n.\n"); no = 1; goto usage; } i++; no = 0; write = 1; } else if (!strcmp(argv[i], "-t")) { /* Set transparent index (or none). */ int index; if (i == (argc-2)) { fprintf(stderr, "Error: -t specified without a color table index.\n"); no = 1; goto usage; } if (!strcmp(argv[i+1], "none")) { /* -1 means not transparent. */ gdImageColorTransparent(im, -1); } else { /* OK, get an integer and set the index. */ index = atoi(argv[i+1]); gdImageColorTransparent(im, index); } i++; write = 1; no = 0; } else if (!strcmp(argv[i], "-l")) { /* List the colors in the color table. */ int j; /* Tabs used below. */ printf("Index Red Green Blue\n"); for (j=0; (j < gdImageColorsTotal(im)); j++) { /* Use access macros to learn colors. */ printf("%d %d %d %d\n", j, gdImageRed(im, j), gdImageGreen(im, j), gdImageBlue(im, j)); } no = 0; } else if (!strcmp(argv[i], "-d")) { /* Output dimensions, etc. */ int t; printf("Width: %d Height: %d Colors: %d\n", gdImageSX(im), gdImageSY(im), gdImageColorsTotal(im)); t = gdImageGetTransparent(im); if (t != (-1)) { printf("Transparent index: %d\n", t); } else { /* -1 means the image is not transparent. */ printf("Transparent index: none\n"); } if (gdImageGetInterlaced(im)) { printf("Interlaced: yes\n"); } else { printf("Interlaced: no\n"); } no = 0; } else { fprintf(stderr, "Unknown argument: %s\n", argv[i]); break; } } usage: if (no) { /* If the command failed, output an explanation. */ fprintf(stderr, "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n" " -i [y|n] Turns on/off interlace\n" " -l Prints the table of color indexes\n" " -t [index] Set the transparent color to the specified index (0-255 or none)\n" " -d Reports the dimensions and other characteristics of the image.\n" "\n" "If you specify '-' as the input file, stdin/stdout will be used input/output.\n" ); } if (write) { if (useStdinStdout) { out = stdout; } else { /* Open a temporary file. */ /* "temp.tmp" is not good temporary filename. */ sprintf(outFn, "webgif.tmp%d", getpid()); out = fopen(outFn, "wb"); if (!out) { fprintf(stderr, "Unable to write to %s -- exiting\n", outFn); exit(1); } } /* Write the new gif. */ gdImageGif(im, out); if (!useStdinStdout) { fclose(out); /* Erase the old gif. */ unlink(argv[argc-1]); /* Rename the new to the old. */ if (rename(outFn, argv[argc-1])!=0) { perror("rename"); exit(1); } } } /* Delete the image from memory. */ if (im) { gdImageDestroy(im); } /* All's well that ends well. */ return 0; }
static VALUE fir_resize_image(VALUE self, VALUE raw_filename_in, VALUE raw_filename_out, VALUE raw_width, VALUE raw_height, VALUE raw_image_type, VALUE raw_jpeg_quality) { gdImagePtr im_in, im_out; FILE *in, *out; char *filename_in; char *filename_out; int w; int h; int image_type; int jpeg_quality; int trans = 0, x = 0, y = 0, f = 0; filename_in = RSTRING_PTR(raw_filename_in); filename_out = RSTRING_PTR(raw_filename_out); w = NUM2INT(raw_width); h = NUM2INT(raw_height); image_type = NUM2INT(raw_image_type); jpeg_quality = NUM2INT(raw_jpeg_quality); in = fopen(filename_in, "rb"); if (!in) return Qnil; switch(image_type) { case 0: im_in = gdImageCreateFromJpeg(in); break; case 1: im_in = gdImageCreateFromPng(in); break; case 2: im_in = gdImageCreateFromGif(in); trans = gdImageGetTransparent(im_in); /* find a transparent pixel, then turn off transparency so that it copies correctly */ if (trans >= 0) { for (x=0; x<gdImageSX(im_in); x++) { for (y=0; y<gdImageSY(im_in); y++) { if (gdImageGetPixel(im_in, x, y) == trans) { f = 1; break; } } if (f) break; } gdImageColorTransparent(im_in, -1); if (!f) trans = -1; /* no transparent pixel found */ } break; default: return Qnil; } if (w == 0 || h == 0) { int originalWidth = gdImageSX(im_in); int originalHeight = gdImageSY(im_in); if (w == 0) { w = (int)(h * originalWidth / originalHeight); } else { h = (int)(w * originalHeight / originalWidth); } } im_out = gdImageCreateTrueColor(w, h); /* must be truecolor */ if (image_type == 1) { gdImageAlphaBlending(im_out, 0); /* handle transparency correctly */ gdImageSaveAlpha(im_out, 1); } fclose(in); /* Now copy the original */ gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, gdImageSX(im_out), gdImageSY(im_out), gdImageSX(im_in), gdImageSY(im_in)); out = fopen(filename_out, "wb"); if (out) { switch(image_type) { case 0: gdImageJpeg(im_out, out, jpeg_quality); break; case 1: gdImagePng(im_out, out); break; case 2: gdImageTrueColorToPalette(im_out, 0, 256); if (trans >= 0) { trans = gdImageGetPixel(im_out, x, y); /* get the color index of our transparent pixel */ gdImageColorTransparent(im_out, trans); /* may not always work as hoped */ } gdImageGif(im_out, out); break; } fclose(out); } gdImageDestroy(im_in); gdImageDestroy(im_out); return Qnil; }
int main(int argc, char **argv) { FILE *in; FILE *out; /* Declare our image pointer */ gdImagePtr im = 0; int i; /* We'll clear 'no' once we know the user has made a reasonable request. */ int no = 1; /* We'll set 'write' once we know the user's request requires that the image be written back to disk. */ int write = 0; /* C programs always get at least one argument; we want at least one more (the image), more in practice. */ if (argc < 2) { no = 1; goto usage; } /* The last argument should be the image. Open the file. */ in = fopen(argv[argc-1], "rb"); if (!in) { fprintf(stderr, "Error: can't open file %s.\n", argv[argc-1]); } /* Now load the image. */ im = gdImageCreateFromGif(in); fclose(in); /* If the load failed, it must not be a GIF file. */ if (!im) { fprintf(stderr, "Error: %s is not a valid gif file.\n", argv[1]); exit(1); } /* Consider each argument in turn. */ for (i=1; (i < (argc-1)); i++) { /* -i turns on and off interlacing. */ if (!strcmp(argv[i], "-i")) { if (i == (argc-2)) { fprintf(stderr, "Error: -i specified without y or n.\n"); no = 1; goto usage; } if (!strcmp(argv[i+1], "y")) { /* Set interlace. */ gdImageInterlace(im, 1); } else if (!strcmp(argv[i+1], "n")) { /* Clear interlace. */ gdImageInterlace(im, 0); } else { fprintf(stderr, "Error: -i specified without y or n.\n"); no = 1; goto usage; } i++; no = 0; write = 1; } else if (!strcmp(argv[i], "-t")) { /* Set transparent index (or none). */ int index; if (i == (argc-2)) { fprintf(stderr, "Error: -t specified without a color table index.\n"); no = 1; goto usage; } if (!strcmp(argv[i+1], "none")) { /* -1 means not transparent. */ gdImageColorTransparent(im, -1); } else { /* OK, get an integer and set the index. */ index = atoi(argv[i+1]); gdImageColorTransparent(im, index); } i++; write = 1; no = 0; } else if (!strcmp(argv[i], "-l")) { /* List the colors in the color table. */ int j; /* Tabs used below. */ printf("Index Red Green Blue\n"); for (j=0; (j < gdImageColorsTotal(im)); j++) { /* Use access macros to learn colors. */ printf("%d %d %d %d\n", j, gdImageRed(im, j), gdImageGreen(im, j), gdImageBlue(im, j)); } no = 0; } else if (!strcmp(argv[i], "-d")) { /* Output dimensions, etc. */ int t; printf("Width: %d Height: %d Colors: %d\n", gdImageSX(im), gdImageSY(im), gdImageColorsTotal(im)); t = gdImageGetTransparent(im); if (t != (-1)) { printf("Transparent index: %d\n", t); } else { /* -1 means the image is not transparent. */ printf("Transparent index: none\n"); } if (gdImageGetInterlaced(im)) { printf("Interlaced: yes\n"); } else { printf("Interlaced: no\n"); } no = 0; } else { fprintf(stderr, "Unknown argument: %s\n", argv[i]); break; } } usage: if (no) { /* If the command failed, output an explanation. */ fprintf(stderr, "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n"); fprintf(stderr, "Where -i controls interlace (specify y or n for yes or no),\n"); fprintf(stderr, "-l outputs a table of color indexes, -t sets the specified\n"); fprintf(stderr, "color index (0-255 or none) to be the transparent color, and\n"); fprintf(stderr, "-d reports the dimensions and other characteristics of the image.\n"); fprintf(stderr, "Note: you may wish to pipe to \"more\" when using the -l option.\n"); } if (write) { /* Open a temporary file. */ out = fopen("temp.tmp", "wb"); if (!out) { fprintf(stderr, "Unable to write to temp.tmp -- exiting\n"); exit(1); } /* Write the new gif. */ gdImageGif(im, out); fclose(out); /* Erase the old gif. */ unlink(argv[argc-1]); /* Rename the new to the old. */ rename("temp.tmp", argv[argc-1]); } /* Delete the image from memory. */ if (im) { gdImageDestroy(im); } /* All's well that ends well. */ return 0; }
static gdImagePtr gd_loadimage(GVJ_t * job, usershape_t *us) { assert(job); assert(us); assert(us->name); if (us->data) { if (us->datafree != gd_freeimage) { us->datafree(us); /* free incompatible cache data */ us->data = NULL; us->datafree = NULL; } } if (!us->data) { /* read file into cache */ if (!gvusershape_file_access(us)) return NULL; switch (us->type) { #if 0 case FT_GD: im = gdImageCreateFromGd(us->f); break; case FT_GD2: im = gdImageCreateFromGd2(us->f); break; #endif #ifdef HAVE_GD_PNG case FT_PNG: us->data = (void*)gdImageCreateFromPng(us->f); break; #endif #ifdef HAVE_GD_GIF case FT_GIF: us->data = (void*)gdImageCreateFromGif(us->f); break; #endif #ifdef HAVE_GD_JPEG case FT_JPEG: us->data = (void*)gdImageCreateFromJpeg(us->f); break; #endif #if 0 #ifdef HAVE_GD_XPM case FT_XPM: us->data = (void*)gdImageCreateFromXpm(us->f); break; #endif #ifdef HAVE_GD_WBMP case FT_WBMP: us->data = (void*)gdImageCreateFromWbmp(us->f); break; #endif #endif default: break; } if (us->data) us->datafree = gd_freeimage; gvusershape_file_release(us); } return (gdImagePtr)(us->data); }
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; }
/* 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 main(int argc, char *argv[]) { if (argc < 3) { fprintf(stderr, "Converts a gif/png/jpg to a c array of a 16-bit bitmap, " "format {x, y, data}\n" "Need var name as first arg, valid image file as second arg\n" "(Compact mode on if third arg is 1)"); return -1; } if (!valid_varname(VARNAME)) { fprintf(stderr, "Invalid C varname %s ", VARNAME); return -2; } FILE *imgdata; gdImagePtr img = NULL; char header[4]; int w, h; int x, y; int i; int type, color, rgb; int compact = atoi(argv[3]); imgdata = fopen(FILENAME, "rb"); if (!imgdata) { fprintf(stderr, "File %s read error: does not exist?\n", FILENAME); return -4; } img = gdImageCreateFromJpeg(imgdata); if (!img) img = gdImageCreateFromGif(imgdata); if (!img) img = gdImageCreateFromPng(imgdata); if (!img) { fprintf(stderr, "File %s is invalid %s\n", FILENAME, type == 0 ? "JPG" : type == 1 ? "GIF" : type == 2 ? "PNG" : "image"); return -5; } fclose(imgdata); w = img->sx; h = img->sy; printf("/* 16-bit GBA bitmap array generated by imgtogba */\n\n"); printf("const unsigned short %s[] = {\n/* xdim ydim, */\n", VARNAME); printf(" %4d, %4d\n", w, h); if (compact == 1) { for (y = i = 0; y < h; ++y) { for (x = 0; x < w; ++x) { color = gdImageGetPixel(img, x, y); rgb = rgb_conv( gdImageRed(img, color), gdImageGreen(img, color), gdImageBlue(img, color) ); printf(",%d", rgb); } } } else { for (y = i = 0; y < h; ++y) { for (x = 0; x < w; ++x) { color = gdImageGetPixel(img, x, y); rgb = rgb_conv( gdImageRed(img, color), gdImageGreen(img, color), gdImageBlue(img, color) ); printf("%s0x%04x", (i++ % 10 ? ", " : ",\n"), rgb); } } } printf("};"); gdImageDestroy(img); 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; }
// Generates the count image from the count string, returns true if all ok bool CreateImage(const char *pcCount, const char *pcFont, int iDigits) { // Set the spare value int iCountLen = strlen(pcCount); if (iDigits < iCountLen) iDigits = iCountLen; int iSpare = 0; if (iDigits > iCountLen) iSpare = iDigits - iCountLen; // Loop vars char pcDigitName[STR_SIZE] = ""; gdImagePtr pImg = NULL; FILE *pPic = NULL; // Load the zero digit image sprintf(pcDigitName, COUNTER_ZERO, pcFont); pPic = fopen(pcDigitName, "rb+"); if (pPic == NULL) return false; pImg = gdImageCreateFromGif(pPic); fclose(pPic); if (pImg == NULL) return false; // Create the output image gdImagePtr pOutImg = NULL; pOutImg = gdImageCreate(iDigits * pImg->sx, pImg->sy); if (pOutImg == NULL) { gdImageDestroy(pImg); return false; } // Copy the palette info from one to the other... int iColTotal = gdImageColorsTotal(pImg); for (int i = 0; i < iColTotal; i++) { gdImageColorAllocate(pOutImg, gdImageRed(pImg, i), gdImageGreen(pImg, i), gdImageBlue(pImg, i)); } int iTransCol = gdImageGetTransparent(pImg); if (iTransCol >= 0) { gdImageColorTransparent(pOutImg, iTransCol); gdImageFill(pOutImg, 0, 0, iTransCol); } // Loop through each leading zero int iPos = 0; while (iSpare-- > 0) { // paste in the image gdImageCopy(pOutImg, pImg, iPos, 0, 0, 0, pImg->sx, pImg->sy); iPos += pImg->sx; } // Delete the zero image gdImageDestroy(pImg); // Loop through each counter character const char *pcPos = pcCount; while (*pcPos != '\0') { sprintf(pcDigitName, COUNTER_PIC, pcFont, *pcPos); // Load the image pPic = fopen(pcDigitName, "rb+"); if (pPic == NULL) { gdImageDestroy(pOutImg); return false; } pImg = gdImageCreateFromGif(pPic); if (pImg == NULL) { gdImageDestroy(pOutImg); return false; } // Paste in the image gdImageCopy(pOutImg, pImg, iPos, 0, 0, 0, pImg->sx, pImg->sy); iPos += pImg->sx; // Delete the image gdImageDestroy(pImg); // Update the position counter pcPos++; } // Write out the output image if (g_oCGI.Debug()) { char pcGIFName[STR_SIZE] = COUNTER_ROOT; strcat(pcGIFName, "out.gif"); FILE *pGIFFile = NULL; pGIFFile = fopen(pcGIFName, "wb"); if (pGIFFile) { gdImageGif(pOutImg, pGIFFile); fclose(pGIFFile); } } else { gdImageInterlace(pOutImg, 1); #ifdef WIN32 _setmode(_fileno(stdout), _O_BINARY); #endif printf("Content-type: image/gif\n\n"); gdImageGif(pOutImg, stdout); } // Destroy the image gdImageDestroy(pOutImg); return true; } // CreateImage
static gdImagePtr imageLoad (char *filename) { FILE *f; char *ext, *cmd, *tmp; gdImagePtr im; int rc; struct stat statbuf; ext = strrchr(filename, '.'); if (!ext) { fprintf(stderr, "Filename \"%s\" has no file extension.\n", filename); exit(EX_USAGE); } rc = stat(filename, &statbuf); if (rc) { fprintf(stderr, "Failed to stat \"%s\"\n", filename); exit(EX_NOINPUT); } if (strcasecmp(ext, ".ps") == 0) { ext = ".png"; tmp = malloc(strlen(filename) + strlen(ext) + 1); strcpy(tmp,filename); strcat(tmp,ext); cmd = malloc(strlen(pstopng) + 2 + strlen(filename) + 2 + strlen(tmp) + 1); strcpy(cmd,pstopng); strcat(cmd," <"); strcat(cmd,filename); strcat(cmd," >"); strcat(cmd,tmp); rc = system(cmd); free(cmd); f = fopen(tmp, "rb"); free(tmp); if (!f) { fprintf(stderr, "Failed to open converted \"%s%s\"\n", filename, ext); exit(EX_NOINPUT); } } else { f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Failed to open \"%s\"\n", filename); exit(EX_NOINPUT); } } im = 0; if (strcasecmp(ext, ".png") == 0) { #ifdef HAVE_GD_PNG im = gdImageCreateFromPng(f); #else fprintf(stderr, "PNG support is not available\n"); exit(EX_UNAVAILABLE); #endif } else if (strcasecmp(ext, ".gif") == 0) { #ifdef HAVE_GD_GIF im = gdImageCreateFromGif(f); #else fprintf(stderr, "GIF support is not available\n"); exit(EX_UNAVAILABLE); #endif } else if (strcasecmp(ext, ".jpg") == 0) { #ifndef HAVE_HAVE_LIBJPEG #undef HAVE_GD_JPEG #endif #ifdef HAVE_GD_JPEG im = gdImageCreateFromJpeg(f); #else fprintf(stderr, "JPEG support is not available\n"); exit(EX_UNAVAILABLE); #endif } fclose(f); if (!im) { fprintf(stderr, "Loading image from file \"%s\" failed!\n", filename); exit(EX_DATAERR); } return im; }
gdImagePtr loadImage(char* fileName, char* fileType) { // Read in image FILE *file = NULL; file = fopen(fileName, "r"); // Check for error if (file == NULL) { printf("\n"); perror("fopen"); return NULL; } gdImagePtr image = NULL; // Process appropriate image if (strcmp(fileType, ".gif") == 0) { image = gdImageCreateFromGif(file); } else if (0 == strcmp(fileType, ".png")) { image = gdImageCreateFromPng(file); } else if ((strcmp(fileType, ".jpg") == 0) || (strcmp(fileType, ".jpeg") == 0)) { image = gdImageCreateFromJpeg(file); } else { printf("\nCannot handle image type %s\n", fileType); } fclose(file); return image; }