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 pngtofile(FILE *fin, FILE *fout) { gdImagePtr im = gdImageCreateFromPng(fin); int c = gdImageGetPixel(im, gdImageSX(im) - 1, gdImageSY(im) - 1); int data_size = (gdImageRed(im, c) << 8*2) + (gdImageGreen(im, c) << 8*1) + (gdImageBlue(im, c)); unsigned char buf[3]; long written_bytes = 0; int x, y; int nb = 0; for(y = 0; y < gdImageSY(im); y++) { for(x = 0; x < gdImageSX(im); x++) { c = gdImageGetPixel(im, x, y); buf[0] = gdImageRed(im, c); buf[1] = gdImageGreen(im, c); buf[2] = gdImageBlue(im, c); if(written_bytes >= data_size) { break; /* FIXME */ } else { nb = written_bytes + 3 > data_size ? data_size - written_bytes : 3; written_bytes += fwrite(buf, 1, nb, fout); } } } gdImageDestroy(im); return 1; }
unsigned char *readpng(const char *filename,int *width, int *height){ FILE *file; gdImagePtr image; unsigned char *dataptr,*dptr; int i,j; unsigned int intrgb; file = fopen(filename, "rb"); if(file == NULL)return NULL; image = gdImageCreateFromPng(file); fclose(file); *width=gdImageSX(image); *height=gdImageSY(image); if( NewMemory((void **)&dataptr,(unsigned int)(4*(*width)*(*height)) )==0){ gdImageDestroy(image); return NULL; } dptr=dataptr; for (i = 0; i<*height; i++){ for(j=0;j<*width;j++){ intrgb=(unsigned int)gdImageGetPixel(image,j,(unsigned int)(*height-(1+i))); *dptr++ = (intrgb>>16)&255; *dptr++ = (intrgb>>8)&255; *dptr++ = intrgb&255; *dptr++ = 0xff; } } gdImageDestroy(image); return dataptr; }
value ImageCreateFromPng(value filename) { FILE *_file = openFileRead(filename); gdImagePtr img = gdImageCreateFromPng(_file); fclose(_file); if (img == NULL) failure("image file could not be loaded: probably wrong format"); return alloc_gc_image(img); }
int fswc_draw_overlay(fswebcam_config_t *config, char *filename, gdImage *image){ FILE *f; gdImage *overlay; if(!filename) return(-1); f = fopen(filename, "rb"); if(!f) { ERROR("Unable to open '%s'", filename); ERROR("fopen: %s", strerror(errno)); return(-1); } overlay = gdImageCreateFromPng(f); fclose(f); if(!overlay) { ERROR("Unable to read '%s'. Not a PNG image?", filename); return(-1); } gdImageCopy(image, overlay, 0, 0, 0, 0, overlay->sx, overlay->sy); gdImageDestroy(overlay); return(0); }
/* This routine reads a png-file from disk and puts it into buff in a format the LCD understands. (basically 16-bit rgb) */ int readpic(char* filename, char* buff) { FILE *in; gdImagePtr im; int x,y; int c,r,g,b; in=fopen(filename,"rb"); if (in==NULL) return 0; //Should try other formats too im=gdImageCreateFromPng(in); fclose(in); if (im==NULL) return 0; for (y=0; y<128; y++) { for (x=0; x<128; x++) { c = gdImageGetPixel(im, x, y); if (gdImageTrueColor(im) ) { r=gdTrueColorGetRed(c); g=gdTrueColorGetGreen(c); b=gdTrueColorGetBlue(c); } else { r=gdImageRed(im,c); g=gdImageGreen(im,c); b=gdImageBlue(im,c); } r>>=3; g>>=2; b>>=3; c=(r<<11)+(g<<5)+b; buff[x*2+y*256]=(c>>8); buff[x*2+y*256+1]=(c&255); } } gdImageDestroy(im); return 1; }
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; }
int main(int argc, char **argv) { gdImagePtr im; FILE *in, *out; if (argc != 3) { fprintf(stderr, "Usage: pngtogd filename.png filename.gd\n"); exit(1); } in = fopen(argv[1], "rb"); if (!in) { fprintf(stderr, "Input file does not exist!\n"); exit(1); } im = gdImageCreateFromPng(in); fclose(in); if (!im) { fprintf(stderr, "Input is not in PNG 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); return 0; }
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; }
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(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, ex; FILE *fp; gdScatter s; int colors[] = {0xFF0000, 0x00FF00}; CuTestImageResult r; fp = gdTestFileOpen("gdimagescatterex/bug00208.png"); im = gdImageCreateFromPng(fp); fclose(fp); if (!im) { fprintf(stderr, "could not create image\n"); return 1; } s.sub = 1; s.plus = 3; s.seed = 0; s.num_colors = 2; s.colors = colors; if (!gdImageScatterEx(im, &s)) { gdImageDestroy(im); fprintf(stderr, "could not scatter\n"); return 1; } fp = gdTestFileOpen("gdimagescatterex/bug00208_2.png"); ex = gdImageCreateFromPng(fp); fclose(fp); if (!ex) { fprintf(stderr, "could not create image\n"); gdImageDestroy(im); return 1; } r.pixels_changed = 0; gdTestImageDiff(im, ex, NULL, &r); gdImageDestroy(ex); gdImageDestroy(im); if (r.pixels_changed > 10000) { fprintf(stderr, "too much diff: %d\n", r.pixels_changed); return 1; } return 0; }
uint8_t LcdWriteImagePng(LcdSpi* lcd, const char *filename) { uint16_t x = 100; uint16_t y = 100; uint16_t xm = 0; uint16_t ym = 0; uint16_t i = 0; int c; gdImagePtr img; FILE *filePtr; uint8_t b; if (!lcd) { return 1; } if (!filename) { return 2; } filePtr = fopen(filename, "rb"); if (filePtr == NULL) { return 3; } img = gdImageCreateFromPng(filePtr); if (img == NULL) { return 4; } xm = gdImageSX(img); ym = gdImageSY(img); printf("%i x %i\n", xm, ym); if (xm != 132 || ym != 32) { return 5; } for (y = 0; y < ym; y += 8) { LcdSetPos(lcd, y / 8, 0); GpioSetValue(PIN_LCD_A0, 1); for (x = 0; x < xm; x++) { b = 0; for (i = 0; i < 8; i++) { c = gdImageGetPixel(img, x, y + i); if (img->red[c] < 0x7f || img->green[c] < 0x7f || img->blue[c] < 0x7f) { //if (c) { b |= (1 << i); } } LcdWriteByte(lcd->mS0, b); } } fclose(filePtr); gdImageDestroy(img); return 0; }
gdImagePtr gdImageCreateFromPngPtr(int size, void *data) { FILE *fp; gdImagePtr ret; fp = fmemopen(data, size, "r"); ret = gdImageCreateFromPng(fp); fclose(fp); return ret; }
void MessageEncryption::encrypt(std::string imgpath,std::string outpath){ std::default_random_engine rand (this->key); int currand; int i, j; int width, height; FILE* in, *out; gdImagePtr img; printf("o\n"); int ext = getexten(imgpath); printf("a\n"); in = fopen(imgpath.c_str(),"rb"); if(ext==0){ img=gdImageCreateFromJpeg(in); } else if(ext==1){ img=gdImageCreateFromPng(in); } else{ exit(1); } fclose(in); printf("b\n"); width = gdImageSX(img); height = gdImageSY(img); for(i=0;i<ITER_COUNT;i++){ currand = rand()%FLIP_CHANCE; if(currand==0){ // flip horizontally fliphoriz(img); } else if(currand==1){ // flip vertically flipvert(img); } else if(currand<=1+FLIP_CHANCE/2){ // shift some rows for(j=0;j<=(currand%(width/3));j++){ shiftcol(img,(3*i+j)%width,(7*currand+4*i)%height); } } else{ // shift some columns for(j=0;j<=(currand%(height/3));j++){ shiftrow(img,(3*i+j)%height,(7*currand+4*i)%width); } } } printf("c\n"); ext = getexten(outpath); out = fopen(outpath.c_str(),"wb"); if(ext==0){ gdImageJpeg(img,out,-1); } else if(ext==1){ gdImagePng(img,out); } else{ exit(1); } fclose(out); gdImageDestroy(img); }
gdImagePtr gdTestImageFromPng(const char *filename) { gdImagePtr image; FILE *fp; fp = fopen(filename, "rb"); if (!fp) { return NULL; } image = gdImageCreateFromPng(fp); fclose(fp); return image; }
void readBack(){ gdImagePtr im; FILE *in; int result; int i, j, x, y, maxX, maxY, c; in = fopen("back55.png", "rb"); im = gdImageCreateFromPng(in); if (!im) { printf("Cannot open file\n"); exit(1);} maxX = gdImageSX(im); maxY = gdImageSY(im); for (y=0; y< maxY; y++){ for (x=0; x < maxX; x++){ c = gdImageGetPixel(im, x, y); backSkyBox[maxY-y-1][x][0] = gdImageRed(im, c); backSkyBox[maxY-y-1][x][1] = gdImageGreen(im, c); backSkyBox[maxY-y-1][x][2] = gdImageBlue(im, c); backSkyBox[maxY-y-1][x][3] = 255; } } }
int main (int argc, char **argv) { gdImagePtr im; FILE *in, *out; int cs, fmt; if (argc != 5) { fprintf (stderr, "Usage: pngtogd2 filename.png filename.gd2 cs fmt\n"); fprintf (stderr, " where cs is the chunk size\n"); fprintf (stderr, " fmt is 1 for raw, 2 for compressed\n"); exit (1); } in = fopen (argv[1], "rb"); if (!in) { fprintf (stderr, "Input file does not exist!\n"); exit (1); } #ifdef HAVE_LIBPNG im = gdImageCreateFromPng (in); #else fprintf (stderr, "No PNG library support available.\n"); #endif fclose (in); if (!im) { fprintf (stderr, "Input is not in PNG format!\n"); exit (1); } out = fopen (argv[2], "wb"); if (!out) { fprintf (stderr, "Output file cannot be written to!\n"); gdImageDestroy (im); exit (1); } cs = atoi (argv[3]); fmt = atoi (argv[4]); gdImageGd2 (im, out, cs, fmt); fclose (out); gdImageDestroy (im); return 0; }
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; }
/* This routine reads a png-file from disk and puts it into buff in a format lib understands. (basically 24-bit rgb) */ int sendpic(st2205_handle *h, char* filename) { FILE *in; gdImagePtr im; unsigned char* pixels; int x,y; int p=0; unsigned int c,r,g,b; pixels=malloc(h->width*h->height*3); in=fopen(filename,"rb"); if (in==NULL) return 0; //Should try other formats too im=gdImageCreateFromPng(in); fclose(in); if (im==NULL) { printf("%s: not a png-file.\n",filename); return 0; } for (y=0; y<h->width; y++) { for (x=0; x<h->height; x++) { c = gdImageGetPixel(im, x, y); if (gdImageTrueColor(im) ) { r=gdTrueColorGetRed(c); g=gdTrueColorGetGreen(c); b=gdTrueColorGetBlue(c); } else { r=gdImageRed(im,c); g=gdImageGreen(im,c); b=gdImageBlue(im,c); } // pixels[p++]=0xff; // pixels[p++]=0; // pixels[p++]=0; pixels[p++]=r; pixels[p++]=g; pixels[p++]=b; } } st2205_send_data(h,pixels); gdImageDestroy(im); return 1; }
int main (int argc, char *argv[]) { /* Input and output files */ FILE *in; /* Input image */ gdImagePtr im_in = 0; if (argc != 2) { fprintf (stderr, "Usage: testac filename.png\n"); exit (1); } /* Load original PNG, which should contain alpha channel information. We will use it in two ways: preserving it literally, for use with compatible browsers, and compositing it ourselves against a background of our choosing (alpha blending). We'll change its size and try creating palette versions of it. */ in = fopen (argv[1], "rb"); if (!in) { fprintf (stderr, "Can't load %s.\n", argv[1]); exit (1); } else { im_in = gdImageCreateFromPng (in); fclose (in); } testDrawing (im_in, 1.0, 0, 0, "noblending-fullsize-truecolor.png"); testDrawing (im_in, 1.0, 1, 0, "blending-fullsize-truecolor.png"); testDrawing (im_in, 0.5, 0, 0, "noblending-halfsize-truecolor.png"); testDrawing (im_in, 0.5, 1, 0, "blending-halfsize-truecolor.png"); testDrawing (im_in, 2.0, 0, 0, "noblending-doublesize-truecolor.png"); testDrawing (im_in, 2.0, 1, 0, "blending-doublesize-truecolor.png"); testDrawing (im_in, 1.0, 0, 1, "noblending-fullsize-palette.png"); testDrawing (im_in, 1.0, 1, 1, "blending-fullsize-palette.png"); testDrawing (im_in, 0.5, 0, 1, "noblending-halfsize-palette.png"); testDrawing (im_in, 0.5, 1, 1, "blending-halfsize-palette.png"); testDrawing (im_in, 2.0, 0, 1, "noblending-doublesize-palette.png"); testDrawing (im_in, 2.0, 1, 1, "blending-doublesize-palette.png"); gdImageDestroy (im_in); return 0; }
gdImagePtr read_png(const char *filename) { FILE * fp; gdImagePtr im; fp = fopen(filename, "rb"); if (!fp) { fprintf(stderr, "Can't read png image %s\n", filename); return NULL; } #ifdef HAVE_LIBPNG im = gdImageCreateFromPng(fp); #else im = NULL; printf("No PNG support. Cannot read image.\n"); #endif fclose(fp); return im; }
int main() { gdImagePtr im; FILE *fp; gdSetErrorMethod(gdSilence); fp = gdTestFileOpen("png/bug00033.png"); im = gdImageCreateFromPng(fp); fclose(fp); if (im) { gdImageDestroy(im); return 1; } else { return 0; } }
void generate(std::string fname, int sizex, int sizey, double minx, double miny, double maxx, double maxy, int iterations, bool truecolor=false) { gdImagePtr img; int *colors = NULL; if (truecolor) { img = gdImageCreateTrueColor(sizex, sizey); } else { img = gdImageCreate(sizex, sizey); std::cout << "Load palette: " << std::flush; FILE *in; if ((in = fopen(PALETTEFILE, "r")) != false) { colors = new int[iterations+3]; gdImagePtr palette = gdImageCreateFromPng(in); for (int i = 0; i < gdImageSX(palette) && i < iterations; i++) { int rgb = gdImageGetPixel(palette, i, 0); colors[i] = gdImageColorAllocate(img, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF); } colors[iterations+1] = gdImageColorAllocate(img, 0, 0, 0); colors[iterations+2] = gdImageColorAllocate(img, 255, 255, 255); fclose(in); std::cout << "done" << std::endl; } else {
int main() { int error; gdImagePtr im; FILE *fp; char path[1024]; const char * files[2] = {"bug00088_1.png", "bug00088_2.png"}; const char * files_exp[2] = {"bug00088_1_exp.png", "bug00088_2_exp.png"}; int i, cnt = 2; error = 0; for (i = 0; i < cnt; i++) { sprintf(path, "%s/png/%s", GDTEST_TOP_DIR, files[i]); fp = fopen(path, "rb"); if (!fp) { printf("failed, cannot open file <%s>\n", path); return 1; } im = gdImageCreateFromPng(fp); fclose(fp); if (!im) { error |= 1; continue; } sprintf(path, "%s/png/%s", GDTEST_TOP_DIR, files_exp[i]); if (!gdAssertImageEqualsToFile(path, im)) { error |= 1; } gdImageDestroy(im); } return error; }
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 main (void) { #ifdef HAVE_LIBPNG /* Input and output files */ FILE *in; FILE *out; /* Input and output images */ gdImagePtr im_in = 0, im_out = 0; /* Brush image */ gdImagePtr brush; /* Color indexes */ int white; int blue; int red; int green; /* Points for polygon */ gdPoint points[3]; int i; /* Create output image, in true color. */ im_out = gdImageCreateTrueColor (256 + 384, 384); /* 2.0.2: first color allocated would automatically be background in a palette based image. Since this is a truecolor image, with an automatic background of black, we must fill it explicitly. */ white = gdImageColorAllocate (im_out, 255, 255, 255); gdImageFilledRectangle (im_out, 0, 0, gdImageSX (im_out), gdImageSY (im_out), white); /* Set transparent color. */ gdImageColorTransparent (im_out, white); /* Try to load demoin.png and paste part of it into the output image. */ in = fopen ("demoin.png", "rb"); if (!in) { fprintf (stderr, "Can't load source image; this demo\n"); fprintf (stderr, "is much more impressive if demoin.png\n"); fprintf (stderr, "is available.\n"); im_in = 0; } else { int a; im_in = gdImageCreateFromPng (in); fclose (in); /* Now copy, and magnify as we do so */ gdImageCopyResampled (im_out, im_in, 32, 32, 0, 0, 192, 192, 255, 255); /* Now display variously rotated space shuttles in a circle of our own */ for (a = 0; (a < 360); a += 45) { int cx = cos (a * .0174532925) * 128; int cy = -sin (a * .0174532925) * 128; gdImageCopyRotated (im_out, im_in, 256 + 192 + cx, 192 + cy, 0, 0, gdImageSX (im_in), gdImageSY (im_in), a); } } red = gdImageColorAllocate (im_out, 255, 0, 0); green = gdImageColorAllocate (im_out, 0, 255, 0); blue = gdImageColorAllocate (im_out, 0, 0, 255); /* Fat Rectangle */ gdImageSetThickness (im_out, 4); gdImageLine (im_out, 16, 16, 240, 16, green); gdImageLine (im_out, 240, 16, 240, 240, green); gdImageLine (im_out, 240, 240, 16, 240, green); gdImageLine (im_out, 16, 240, 16, 16, green); gdImageSetThickness (im_out, 1); /* Circle */ gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue); /* Arc */ gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue); /* Flood fill: doesn't do much on a continuously variable tone jpeg original. */ gdImageFill (im_out, 8, 8, blue); /* Polygon */ points[0].x = 64; points[0].y = 0; points[1].x = 0; points[1].y = 128; points[2].x = 128; points[2].y = 128; gdImageFilledPolygon (im_out, points, 3, green); /* 2.0.12: Antialiased Polygon */ gdImageSetAntiAliased (im_out, green); for (i = 0; (i < 3); i++) { points[i].x += 128; } gdImageFilledPolygon (im_out, points, 3, gdAntiAliased); /* Brush. A fairly wild example also involving a line style! */ if (im_in) { int style[8]; brush = gdImageCreateTrueColor (16, 16); gdImageCopyResized (brush, im_in, 0, 0, 0, 0, gdImageSX (brush), gdImageSY (brush), gdImageSX (im_in), gdImageSY (im_in)); gdImageSetBrush (im_out, brush); /* With a style, so they won't overprint each other. Normally, they would, yielding a fat-brush effect. */ style[0] = 0; style[1] = 0; style[2] = 0; style[3] = 0; style[4] = 0; style[5] = 0; style[6] = 0; style[7] = 1; gdImageSetStyle (im_out, style, 8); /* Draw the styled, brushed line */ gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed); } /* Text (non-truetype; see gdtestft for a freetype demo) */ gdImageString (im_out, gdFontGiant, 32, 32, (unsigned char *) "hi", red); gdImageStringUp (im_out, gdFontSmall, 64, 64, (unsigned char *) "hi", red); /* Random antialiased lines; coordinates all over the image, but the output will respect a small clipping rectangle */ gdImageSetClip(im_out, 0, gdImageSY(im_out) - 100, 100, gdImageSY(im_out)); /* Fixed seed for reproducibility of results */ srand(100); for (i = 0; (i < 100); i++) { int x1 = rand() % gdImageSX(im_out); int y1 = rand() % gdImageSY(im_out); int x2 = rand() % gdImageSX(im_out); int y2 = rand() % gdImageSY(im_out); gdImageSetAntiAliased(im_out, white); gdImageLine (im_out, x1, y1, x2, y2, gdAntiAliased); } /* Make output image interlaced (progressive, in the case of JPEG) */ gdImageInterlace (im_out, 1); out = fopen ("demoout.png", "wb"); /* Write PNG */ gdImagePng (im_out, out); fclose (out); /* 2.0.12: also write a paletteized version */ out = fopen ("demooutp.png", "wb"); gdImageTrueColorToPalette (im_out, 0, 256); gdImagePng (im_out, out); fclose (out); gdImageDestroy (im_out); if (im_in) { gdImageDestroy (im_in); } #else fprintf (stderr, "No PNG library support.\n"); #endif /* HAVE_LIBPNG */ return 0; }
int main(void) { /* Input and output files */ FILE *in; FILE *out; /* Input and output images */ gdImagePtr im_in, im_out; /* Brush image */ gdImagePtr brush; /* Color indexes */ int white; int blue; int red; int green; /* Points for polygon */ gdPoint points[3]; /* Create output image, 128 by 128 pixels. */ im_out = gdImageCreate(128, 128); /* First color allocated is background. */ white = gdImageColorAllocate(im_out, 255, 255, 255); /* Set transparent color. */ gdImageColorTransparent(im_out, white); /* Try to load demoin.png and paste part of it into the output image. */ in = fopen("demoin.png", "rb"); if (!in) { fprintf(stderr, "Can't load source image; this demo\n"); fprintf(stderr, "is much more impressive if demoin.png\n"); fprintf(stderr, "is available.\n"); im_in = 0; } else { im_in = gdImageCreateFromPng(in); fclose(in); /* Now copy, and magnify as we do so */ gdImageCopyResized(im_out, im_in, 16, 16, 0, 0, 96, 96, 127, 127); } red = gdImageColorAllocate(im_out, 255, 0, 0); green = gdImageColorAllocate(im_out, 0, 255, 0); blue = gdImageColorAllocate(im_out, 0, 0, 255); /* Rectangle */ gdImageLine(im_out, 8, 8, 120, 8, green); gdImageLine(im_out, 120, 8, 120, 120, green); gdImageLine(im_out, 120, 120, 8, 120, green); gdImageLine(im_out, 8, 120, 8, 8, green); /* Circle */ gdImageArc(im_out, 64, 64, 30, 10, 0, 360, blue); /* Arc */ gdImageArc(im_out, 64, 64, 20, 20, 45, 135, blue); /* Flood fill */ gdImageFill(im_out, 4, 4, blue); /* Polygon */ points[0].x = 32; points[0].y = 0; points[1].x = 0; points[1].y = 64; points[2].x = 64; points[2].y = 64; gdImageFilledPolygon(im_out, points, 3, green); /* Brush. A fairly wild example also involving a line style! */ if (im_in) { int style[8]; brush = gdImageCreate(8, 8); gdImageCopyResized(brush, im_in, 0, 0, 0, 0, gdImageSX(brush), gdImageSY(brush), gdImageSX(im_in), gdImageSY(im_in)); gdImageSetBrush(im_out, brush); /* With a style, so they won't overprint each other. Normally, they would, yielding a fat-brush effect. */ style[0] = 0; style[1] = 0; style[2] = 0; style[3] = 0; style[4] = 0; style[5] = 0; style[6] = 0; style[7] = 1; gdImageSetStyle(im_out, style, 8); /* Draw the styled, brushed line */ gdImageLine(im_out, 0, 127, 127, 0, gdStyledBrushed); } /* Text */ gdImageString(im_out, gdFontGiant, 16, 16, (unsigned char *) "hi", red); gdImageStringUp(im_out, gdFontSmall, 32, 32, (unsigned char *) "hi", red); /* Make output image interlaced (allows "fade in" in some viewers, and in the latest web browsers) */ gdImageInterlace(im_out, 1); out = fopen("demoout.png", "wb"); /* Write PNG */ gdImagePng(im_out, out); fclose(out); gdImageDestroy(im_out); if (im_in) { gdImageDestroy(im_in); } return 0; }
int main(int argc, char *argv[]) { ProgramArgs args = {300, 1, 10000, 0}; int nextRenderedImage = 0; checkArgs(argc, argv, &args); std::cout << "Settings:" << std::endl << " rendering image every ~" << args.renderImageEvery << std::endl << " children/generation: " << args.numberOfChildren << std::endl << " number of generations: " << args.generationLimit << std::endl << " environment image: " << args.environmentFilename << std::endl; ei::Settings settings; settings.activate(); // Mutation algorithm // Load environment image and perform sanity checks FILE *environmentFile = fopen(args.environmentFilename, "rb"); if (!environmentFile) { std::cout << "Could not open " << args.environmentFilename << std::endl; return 1; } gdImagePtr environment = gdImageCreateFromPng(environmentFile); fclose(environmentFile); if (!environment) { std::cout << "Could not create image from " << args.environmentFilename << std::endl; return 1; } if (gdImageSX(environment) != ei::Tools::maxWidth || gdImageSY(environment) != ei::Tools::maxHeight) { gdImageDestroy(environment); std::cout << "environment.png is incorrect size (expected " << ei::Tools::maxWidth << "x" << ei::Tools::maxHeight << ")" << std::endl; return 1; } // Generate 1st Drawing. Calc difference. Save image&diff as "last". ei::DnaDrawing *lastDrwg = new ei::DnaDrawing(); lastDrwg->init(); gdImagePtr tempImage = renderDrawing(lastDrwg); double lastDifference = diffImages(environment, tempImage); renderImageFile(environment, 0); // save environment as 0 renderImageFile(tempImage, 1); // always save off first specimen as 1 std::cout << "Initial difference = " << lastDifference << std::endl; gdImageDestroy(tempImage); // Iterate the following: for (int generationCount=2; generationCount <= args.generationLimit; generationCount++) { // Periodically report current convergence if (0 == generationCount % 5000) { std::cout << "Current difference is " << lastDifference << " at mutation " << generationCount << std::endl; } // 1. Clone last drawing and mutate. Save as "newDrwg" typedef struct { ei::DnaDrawing *drawing; gdImagePtr image; } DrawingInfo; DrawingInfo children[args.numberOfChildren]; int child; // looping index int minChild; // child with minimal difference double newDifference; for (child=0; child < args.numberOfChildren; child++) { children[child].drawing = lastDrwg->clone(); children[child].drawing->mutate(); // 2. Calc difference between child and environment. children[child].image = renderDrawing(children[child].drawing); double difference = diffImages(environment, children[child].image); // Locate child with the best fit to environment (smallest difference) if (child == 0) { newDifference = difference; minChild = 0; } else { if (difference < newDifference) // found new min { minChild = child; newDifference = difference; } } } // 3. If a child's difference is less than last difference, then save it if (newDifference < lastDifference) { // 3.1 free last image delete lastDrwg; // 3.2 save newDrwg&diff as "last" lastDrwg = children[minChild].drawing; lastDifference = newDifference; children[minChild].drawing = 0; // 3.3 render image to file named by iteration // but limit it to sparse changes. if (generationCount > nextRenderedImage) { renderImageFile(children[minChild].image, generationCount); // if every = 100, then next after 171 is (171/100 + 1)*100 = 200 nextRenderedImage = ( (generationCount / args.renderImageEvery + 1) * args.renderImageEvery); #if 0 if (newDifference < 2.86891e7) { std::cout << "Difference of " << newDifference << " reached at " << generationCount << " generations" << std::endl << "for a total of " << generationCount * args.numberOfChildren << " mutations" << std::endl; generationCount = args.generationLimit + 1; // terminate main loop } #endif } // time to render an image } // new difference is lower // 4 clean up this iteration. If a child improved the // drawing, it's pointer will be 0 already. Delete all // DnaDrawings and gdImagePtrs. for (child=0; child < args.numberOfChildren; child++) { delete children[child].drawing; gdImageDestroy(children[child].image); } } // for each generation... std::cout << "Cleaning up" << std::endl; delete lastDrwg; gdImageDestroy(environment); return 0; }
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; }