int main(void) { /* 2.0.22: can't depend on PNG either */ #ifndef HAVE_LIBPNG fprintf(stderr, "Requires PNG support, gd was compiled without it\n"); return 0; #else char *error; FILE *in = 0; FILE *out; gdImagePtr im; int radius; /* Create an image of text on a circle, with an * alpha channel so that we can copy it onto a * background * TBB: 2.0.18: shouldn't depend on JPEG */ #ifdef HAVE_LIBJPEG in = fopen("eleanor.jpg", "rb"); if(!in) { im = gdImageCreateTrueColor(300, 300); } else { im = gdImageCreateFromJpeg(in); fclose(in); } #else im = gdImageCreateTrueColor(300, 300); #endif /* HAVE_LIBJPEG */ if(!im) { fprintf(stderr, "gdImageCreateTrueColor failed \n"); return 1; } if(gdImageSX(im) < gdImageSY(im)) { radius = gdImageSX(im) / 2; } else { radius = gdImageSY(im) / 2; } error = gdImageStringFTCircle(im, gdImageSX(im) / 2, gdImageSY(im) / 2, radius, radius / 2, 0.8, "arial", 24, "top text", "bottom text", gdTrueColorAlpha(192, 100, 255, 32) ); if(error) { fprintf(stderr, "gdImageStringFTEx error: %s\n", error); } out = fopen("gdfx.png", "wb"); if(!out) { fprintf(stderr, "Can't create gdfx.png\n"); return 1; } gdImagePng(im, out); fclose(out); gdImageDestroy(im); #endif /* HAVE_LIBPNG */ return 0; }
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; }
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; }
void makeMeme(char *input, char *output, char *text) { FILE *in, *out; gdImagePtr im; int black; int white; in = fopen(input, "r"); // if (in == NULL) return 1; //im = gdImageCreateFromPng(in); im = gdImageCreateFromJpeg(in); fclose(in); black = gdImageColorAllocate(im, 0, 0, 0); white = gdImageColorAllocate(im, 255, 255, 255); gdImageString(im, gdFontGetLarge(), im->sx / 2 - (strlen(text) * gdFontGetLarge()->w / 2), im->sy - im->sy / 10, text, black); //gdImagePng(im, out); out = fopen(output, "w"); // printf("Meme created!\n"); gdImageJpeg(im, out, 95); gdImageDestroy(im); //if (out == NULL) return 1; fclose(out); //return 0; }
int main() { int error; gdImagePtr im; FILE *fp; char path[1024]; sprintf(path, "%s/jpeg/conv_test.jpeg", GDTEST_TOP_DIR); fp = fopen(path, "rb"); if (!fp) { printf("failed, cannot open file\n"); return 1; } im = gdImageCreateFromJpeg(fp); fclose(fp); sprintf(path, "%s/jpeg/conv_test_exp.png", GDTEST_TOP_DIR); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; } else { if (im) { gdImageDestroy(im); error = 0; } else { error = 1; } } return error; }
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; }
int main() { #if defined(JPEG_LIB_VERSION_MAJOR) && JPEG_LIB_VERSION_MAJOR >= 8 printf("skip, JPEG Major version too high (%i)\n", JPEG_LIB_VERSION_MAJOR); return 0; #else gdImagePtr im; FILE *fp; char path[1024]; snprintf(path, sizeof(path)-1, "%s/jpeg/conv_test.jpeg", GDTEST_TOP_DIR); fp = fopen(path, "rb"); if (!fp) { gdTestErrorMsg("failed, cannot open file: %s\n", path); return 1; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (im == NULL) { gdTestErrorMsg("gdImageCreateFromJpeg failed.\n"); return 1; } snprintf(path, sizeof(path), "%s/jpeg/conv_test_exp.png", GDTEST_TOP_DIR); if (!gdAssertImageEqualsToFile(path, im)) { gdTestErrorMsg("gdAssertImageEqualsToFile failed: <%s>.\n", path); gdImageDestroy(im); return 1; } return 0; #endif }
int main() { gdImagePtr im; FILE *fp; char path[1024]; gdSetErrorMethod(gdSilence); sprintf(path, "%s/jpeg/empty.jpeg", GDTEST_TOP_DIR); fp = fopen(path, "rb"); if (!fp) { printf("failed, cannot open file\n"); return 1; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (!im) { return 0; } else { gdImageDestroy(im); return 1; } }
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 ) { FILE *fp; gdImagePtr original, cropped,resized; if( argc != 3 ) { printf( "usage: %s in-jpeg out-jpeg\n", argv[0] ); exit( 1 ); } if( !(fp = fopen( argv[1], "r" )) ) { printf( "unable to open \"%s\"\n", argv[1] ); exit( 1 ); } if( !(original = gdImageCreateFromJpeg( fp )) ) { printf( "unable to load \"%s\"\n", argv[1] ); exit( 1 ); } fclose( fp ); gdRect crop; crop.x = 100; crop.y = 100; crop.width = original->sx - 200; crop.height = original->sy - 200; cropped = gdImageCrop(original, &crop); gdImageDestroy( original ); original = 0; if( !(cropped) ) { printf( "unable to crop image\n" ); exit( 1 ); } resized = gdImageScale(cropped, crop.width * 0.9, crop.height * 0.9); gdImageDestroy( cropped ); cropped = 0; if( !(resized) ) { printf( "unable to resize image\n" ); exit( 1 ); } //gdImageSharpen is extremely slow //gdImageSharpen( resized, 75 ); if( !(fp = fopen( argv[2], "w" )) ) { printf( "unable to open \"%s\"\n", argv[2] ); exit( 1 ); } gdImageJpeg( resized, fp, -1 ); fclose( fp ); gdImageDestroy( resized ); return( 0 ); }
value ImageCreateFromJpeg(value filename) { FILE *_file = openFileRead(filename); gdImagePtr img = gdImageCreateFromJpeg(_file); fclose(_file); if (img == NULL) failure("image file could not be loaded: probably wrong format"); value ret = alloc_gc_image(img); return ret; }
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; }
//for libgd2 < 2.0.32 gdImagePtr gdImageCreateFromJpegPtr(int size, void *data) { FILE *fp; gdImagePtr ret; fp = fmemopen(data, size, "r"); ret = gdImageCreateFromJpeg(fp); fclose(fp); return ret; }
int main (int argc, char *argv[]) { FILE *fp; gdImagePtr in, out; int w, h; /* Help */ if (argc<=4) { printf("%s input.jpg output.jpg width height\n", argv[0]); return 1; } /* Size */ w = atoi(argv[3]); h = atoi(argv[4]); if (w<=0 || h<=0) { fprintf(stderr, "Bad size %dx%d\n", h, w); return 2; } /* Input */ fp = fopen(argv[1], "rb"); if (!fp) { fprintf(stderr, "Can't read image %s\n", argv[1]); return 3; } in = gdImageCreateFromJpeg(fp); fclose(fp); if (!in) { fprintf(stderr, "Can't create image from %s\n", argv[1]); return 4; } /* Resize */ gdImageSetInterpolationMethod(in, GD_BILINEAR_FIXED); out = gdImageScale(in, w, h); if (!out) { fprintf(stderr, "gdImageScale fails\n"); return 5; } /* Output */ fp = fopen(argv[2], "wb"); if (!fp) { fprintf(stderr, "Can't save image %s\n", argv[2]); return 6; } gdImageJpeg(out, fp, 90); fclose(fp); /* Cleanups */ gdImageDestroy(in); gdImageDestroy(out); return 0; }
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); }
int main(int argc, char *argv[]) { // Output directory mkdir("results", S_IRWXU | S_IRWXG | S_IRWXO); // Directory for reading and file entry DIR *directory; struct dirent *entry; // Main loop for(directory = opendir("."); (entry = readdir(directory)) != 0;) { if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; if(strstr(entry->d_name, ".jpg") != entry->d_name + strlen(entry->d_name) - 4) continue; if(strstr(entry->d_name, ".resized.jpg") == entry->d_name + strlen(entry->d_name) - 12) continue; printf("Processing <%s>\n", entry->d_name); FILE *file = fopen(entry->d_name, "r"); gdImagePtr source = gdImageCreateFromJpeg(file); int source_width = gdImageSX(source); int source_height = gdImageSY(source); int destination_width = 1024; int destination_height = 768; gdImagePtr destination = gdImageCreateTrueColor(destination_width, destination_height); gdImageCopyResampled(destination, source, 0, 0, 0, 0, destination_width, destination_height, source_width, source_height); sprintf(destination_filename, "results/%s.resized.jpg", entry->d_name); FILE *destination_file = fopen(destination_filename, "w"); fseek(destination_file, 0, SEEK_SET); gdImageJpeg(destination, destination_file, 90); fclose(destination_file); gdImageDestroy(destination); gdImageDestroy(source); fclose(file); } closedir(directory); printf("Job done!\n"); 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; }
unsigned char *readjpeg(const char *filename,int *width, int *height, int skip_local){ FILE *file; gdImagePtr image; unsigned char *dataptr,*dptr; int i,j; unsigned int intrgb; int WIDTH, HEIGHT; int NEWWIDTH, NEWHEIGHT; int jump; file = fopen(filename, "rb"); if(file == NULL)return NULL; image = gdImageCreateFromJpeg(file); fclose(file); if(image==NULL)return NULL; WIDTH=gdImageSX(image); HEIGHT=gdImageSY(image); if(skip_local<0)skip_local=0; jump = skip_local + 1; NEWWIDTH=WIDTH/jump; if(WIDTH%jump!=0)NEWWIDTH++; NEWHEIGHT=HEIGHT/jump; if(HEIGHT%jump!=0)NEWHEIGHT++; *width=NEWWIDTH; *height=NEWHEIGHT; if( NewMemory((void **)&dataptr,(unsigned int)(4*NEWWIDTH*NEWHEIGHT) )==0){ gdImageDestroy(image); return NULL; } dptr=dataptr; for (i = 0; i<HEIGHT; i+=jump){ for(j=0;j<WIDTH;j+=jump){ 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; }
CarGPS::CarGPS(CarApp *main, Logger *log):CarModule(main, log) { logger->log(DEBUG, "CarGPS::CarGPS", "start"); FILE *fp; fp = fopen("gps.jpg", "rb"); if (fp != NULL) { graphic = gdImageCreateFromJpeg(fp); fclose(fp); gdImageCopy(img, graphic, 0, img->sy - graphic->sy, 0, 0, graphic->sx, graphic->sy); } sensor = new GPSSensor(logger); display_type = SENSOR_READOUT; logger->log(DEBUG, "CarGPS::CarGPS", "end"); }
int main() { #ifdef HAVE_JPEG gdImagePtr im, im2; FILE *fp; char path[2048]; fp=fopen("resampledbug.jpeg", "rb"); if (!fp) { fprintf(stderr, "Can't load /home/pierre/IM3801.jpg\n"); return 1; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (!im) { fprintf(stderr, "Can't load TIFF image %s\n", path); return 1; } im2 = gdImageNeuQuant(im, 256, 3); if (im2) { gdImageSaveAlpha(im2, 1); save_png(im2, "a_nnquant.png"); gdImageDestroy(im2); } else { printf("neu quant failed.\n"); } gdImageTrueColorToPalette(im, 1, 256); gdImageSaveAlpha(im, 1); save_png(im, "a_jquant_dither.png"); gdImageDestroy(im); #else printf("JPEG support is required for this example. Please recompile GD with JPEG or change this example to use another format as input."); return 1; #endif return 0; }
int main() { gdImagePtr im; FILE *fp; fp = fopen("empty.jpeg", "rb"); if (!fp) { printf("failed, cannot open file\n"); return 1; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (!im) { return 0; } else { gdImageDestroy(im); return 1; } }
static bool picNote(const char *path, char *text) { FILE *fp; int c; int x, y, w, h; int rect[8] = { 0 }; gdImagePtr im; if ((fp = fopen(path, "rb")) == NULL) { return false; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (im == NULL) { return false; } c = gdImageColorAllocate(im, 255, 255, 255); w = gdImageSX(im); h = gdImageSY(im); memset(rect, 0x00, sizeof(rect)); gdImageStringFT(NULL, rect, c, "SimHei", 13, 0.0, 0, 0, text); x = w - rect[4] - 5; y = h - 10; gdImageStringFT(im, NULL, c, "SimHei", 13, 0.0, x, y, text); if ((fp = fopen(path, "wb")) == NULL) { gdImageDestroy(im); return false; } gdImageJpeg(im, fp, -1); fclose(fp); gdImageDestroy(im); return true; }
int main() { #if defined(JPEG_LIB_VERSION_MAJOR) && JPEG_LIB_VERSION_MAJOR >= 8 printf("skip, JPEG Major version too high (%i)\n", JPEG_LIB_VERSION_MAJOR); return 0; #else gdImagePtr im; int error = 0; FILE *fp = gdTestFileOpen("jpeg/conv_test.jpeg"); im = gdImageCreateFromJpeg(fp); fclose(fp); if (im == NULL) { gdTestErrorMsg("gdImageCreateFromJpeg failed.\n"); return 1; } if (!gdAssertImageEqualsToFile("jpeg/conv_test_exp.png", im)) error = 1; gdImageDestroy(im); return error; #endif }
int main() { gdImagePtr im, im2; FILE *fp; char path[2048]; fp=fopen("resampledbug.jpeg", "rb"); if (!fp) { fprintf(stderr, "Can't load /home/pierre/IM3801.jpg\n"); return 1; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (!im) { fprintf(stderr, "Can't load TIFF image %s\n", path); return 1; } im2 = gdImageNeuQuant(im, 256, 3); if (im2) { gdImageSaveAlpha(im2, 1); save_png(im2, "a_nnquant.png"); gdImageDestroy(im2); } else { printf("neu quant failed.\n"); } gdImageTrueColorToPalette(im, 1, 256); gdImageSaveAlpha(im, 1); save_png(im, "a_jquant_dither.png"); gdImageDestroy(im); return 0; }
int main() { gdImagePtr im; FILE *fp; char path[1024]; sprintf(path, "%s/gd2/empty.gd2", GDTEST_TOP_DIR); fp = fopen(path, "rb"); if (!fp) { printf("failed, cannot open file (%s)\n", path); return 1; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (!im) { return 0; } else { gdImageDestroy(im); return 1; } }
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; }
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; }
int main(int argc, char *argv[]) { #ifndef HAVE_LIBFREETYPE (void)argc; (void)argv; /* 2.0.12 */ fprintf(stderr, "annotate is not useful without freetype.\n" "Install freetype, then './configure; make clean; make install'\n" "the gd library again.\n" ); return 1; #else gdImagePtr im; char *iin, *iout; FILE *in, *out; char s[1024]; int bounds[8]; int lines = 1; int color = gdTrueColor(0, 0, 0); char font[1024]; int size = 12; int align = left; int x = 0, y = 0; char *fontError; strcpy(font, "times"); if(argc != 3) { fprintf(stderr, "Usage: annotate imagein.jpg imageout.jpg\n\n"); fprintf(stderr, "Standard input should consist of\n"); fprintf(stderr, "lines in the following formats:\n"); fprintf(stderr, "color r g b (0-255 each) [a (0-127, 0 is opaque)]\n"); fprintf(stderr, "font fontname (max name length 1024)\n"); fprintf(stderr, "size pointsize\n"); fprintf(stderr, "align (left|right|center)\n"); fprintf(stderr, "move x y\n"); fprintf(stderr, "text actual-output-text\n\n"); fprintf(stderr, "If the file 'paris.ttf' exists in /usr/share/fonts/truetype or in a\n"); fprintf(stderr, "location specified in the GDFONTPATH environment variable, 'font paris' is\n"); fprintf(stderr, "sufficient. You may also specify the full, rooted path of a font file.\n"); exit(1); } iin = argv[1]; iout = argv[2]; in = fopen(iin, "rb"); if(!in) { fprintf(stderr, "Couldn't open %s\n", iin); exit(2); } #ifdef HAVE_LIBJPEG im = gdImageCreateFromJpeg(in); #else fprintf(stderr, "No JPEG library support available.\n"); exit(1); #endif fclose(in); if(!im) { fprintf(stderr, "%s did not load properly\n", iin); exit(3); } while(fgets(s, sizeof(s), stdin)) { char *st; char *text; st = strtok(s, " \t\r\n"); if(!st) { /* Be nice about blank lines */ continue; } if(!strcmp(st, "font")) { char *st = strtok(0, " \t\r\n"); if(!st) { goto badLine; } else { const unsigned int font_len = strlen(st); if (font_len >= 1024) { fprintf(stderr, "Font maximum length is 1024, %d given\n", font_len); goto badLine; } strncpy(font, st, font_len); } } else if(!strcmp(st, "align")) { char *st = strtok(0, " \t\r\n"); if(!st) { goto badLine; } if(!strcmp(st, "left")) { align = 0; } else if(!strcmp(st, "center")) { align = 1; } else if(!strcmp(st, "right")) { align = 2; } } else if(!strcmp(st, "size")) { char *st = strtok(0, " \t\r\n"); if(!st) { goto badLine; } size = atoi(st); } else if(!strcmp(st, "color")) { char *st = strtok(0, "\r\n"); int r, g, b, a = 0; if(!st) { goto badLine; } if(sscanf(st, "%d %d %d %d", &r, &g, &b, &a) < 3) { fprintf(stderr, "Bad color at line %d\n", lines); exit(2); } color = gdTrueColorAlpha(r, g, b, a); } else if(!strcmp(st, "move")) { char *st = strtok(0, "\r\n"); if(!st) { goto badLine; } if(sscanf(st, "%d %d", &x, &y) != 2) { fprintf(stderr, "Missing coordinates at line %d\n", lines); exit(3); } } else if(!strcmp(st, "text")) { int rx = x; text = strtok(0, "\r\n"); if(!text) { text = ""; } gdImageStringFT(0, bounds, color, font, size, 0, x, y, text); switch(align) { case left: break; case center: rx -= (bounds[2] - bounds[0]) / 2; break; case right: rx -= (bounds[2] - bounds[0]); break; } fontError = gdImageStringFT(im, 0, color, font, size, 0, rx, y, text); if(fontError) { fprintf(stderr, "font error at line %d: %s\n", lines, fontError); exit(7); } y -= (bounds[7] - bounds[1]); } else { goto badLine; } lines++; continue; badLine: fprintf(stderr, "Bad syntax, line %d\n", lines); exit(4); } out = fopen(iout, "wb"); if(!out) { fprintf(stderr, "Cannot create %s\n", iout); exit(5); } #ifdef HAVE_LIBJPEG gdImageJpeg(im, out, 95); #else fprintf(stderr, "No JPEG library support available.\n"); #endif gdImageDestroy(im); fclose(out); return 0; #endif /* HAVE_LIBFREETYPE */ }
int main(int argc, char *argv[]) { int errorexit = 0; if (argc>1) { g_rate = (argc>2?atoi(argv[2]):RATE); } else { errorexit = 1; } if (g_rate > MAXRATE) { errorexit=1; } if (errorexit) { fprintf(stderr, "Usage: %s wavfile.wav [sample rate]\n",argv[0]); fprintf(stderr, " default sample rate = %d\n",RATE); fprintf(stderr, " maximum samplerate = %d\n",MAXRATE); return 1; } // locals uint32_t starttime = time(NULL) ; uint8_t ft ; char inputfile[255], outputfile[255] ; // string hygeine memset( inputfile , 0 , 255 ) ; memset( outputfile , 0 , 255 ) ; // assign values to globals double temp1, temp2, temp3 ; temp1 = (double)( 1 << (BITS - 1) ) ; temp2 = VOLPCT / 100.0 ; temp3 = temp1 * temp2 ; g_scale = (uint32_t)temp3 ; g_twopioverrate = 2.0 * M_PI / g_rate ; g_uspersample = 1000000.0 / (double)g_rate ; g_theta = 0.0 ; g_samples = 0.0 ; g_fudge = 0.0 ; printf( "Constants check:\n" ) ; printf( " rate = %d\n" , g_rate ) ; printf( " BITS = %d\n" , BITS ) ; printf( " VOLPCT = %d\n" , VOLPCT ) ; printf( " scale = %d\n" , g_scale ) ; printf( " us/samp = %f\n" , g_uspersample ) ; printf( " 2p/rate = %f\n\n" , g_twopioverrate ) ; // set filenames strncpy( inputfile , argv[1] , strlen( argv[1] ) ) ; ft = filetype( inputfile ) ; if ( ft == FILETYPE_ERR ) { printf( "Exiting.\n" ) ; return 2 ; } strncpy( outputfile, inputfile , strlen( inputfile ) ) ; #ifdef AUDIO_AIFF strcat( outputfile , ".aiff" ) ; #endif #ifdef AUDIO_WAV strcat( outputfile , ".wav" ) ; #endif printf( "Input file is [%s].\n" , inputfile ) ; printf( "Output file is [%s].\n" , outputfile ) ; // prep g_imgfp = fopen( inputfile , "r" ) ; g_outfp = fopen( outputfile , "w" ) ; printf( "FILE ptrs opened.\n" ) ; if ( ft == FILETYPE_JPG ) { g_imgp = gdImageCreateFromJpeg( g_imgfp ) ; } else if ( ft == FILETYPE_PNG ) { g_imgp = gdImageCreateFromPng( g_imgfp ) ; } else { printf( "Some weird error!\n" ) ; return 3 ; } printf( "Image ptr opened.\n" ) ; // go! addvisheader() ; buildaudio() ; addvistrailer() ; #ifdef AUDIO_AIFF writefile_aiff() ; #endif #ifdef AUDIO_WAV writefile_wav(); #endif // cleanup fclose( g_imgfp ) ; fclose( g_outfp ) ; // brag uint32_t endtime = time(NULL) ; printf( "Created soundfile in %d seconds.\n" , ( endtime - starttime ) ) ; 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; }