int Pygame_SDL2_SaveJPEG(SDL_Surface *surface, const char *file) { SDL_Surface *rgb_surf; JSAMPROW *samples; int i, rv; rgb_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGB24, 0); if (! rgb_surf) { return -1; } samples = (JSAMPROW *) malloc (sizeof(JSAMPROW) * rgb_surf->h); if (!samples) { SDL_FreeSurface(rgb_surf); return -1; } /* copy pointers to the scanlines... since they might not be packed. */ for (i = 0; i < rgb_surf->h; i++) { samples[i] = ((unsigned char*)rgb_surf->pixels) + i * rgb_surf->pitch; } rv = write_jpeg (file, samples, surface->w, surface->h, 90); free(samples); SDL_FreeSurface(rgb_surf); return rv; }
static int save_stdjpeg(const char *name, struct ImBuf *ibuf) { FILE * outfile; struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo; struct my_error_mgr jerr; if ((outfile = fopen(name, "wb")) == NULL) return 0; jpeg_default_quality = 75; cinfo->err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = jpeg_error; /* Establish the setjmp return context for jpeg_error to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ jpeg_destroy_compress(cinfo); fclose(outfile); remove(name); return 0; } init_jpeg(outfile, cinfo, ibuf); write_jpeg(cinfo, ibuf); fclose(outfile); jpeg_destroy_compress(cinfo); return 1; }
int main (int argc, char *argv[]) { unsigned char *rawpic = NULL; unsigned char *warpedpic = NULL; size_t width, height; int num_components, color_space; /* Get raw data */ rawpic = read_jpeg ("shrek_cat_eyes.jpg", &width, &height, &num_components, &color_space); if (!rawpic) goto out; warpedpic = calloc(1, width*height*num_components); if (!warpedpic) goto out; /* do some processing here */ warp(rawpic, warpedpic, width, height, num_components); //thresholding(rawpic, warpedpic, width, height, num_components); //shift(rawpic, warpedpic, width, height, num_components); /* write it back as jpeg */ write_jpeg ("out.jpg", warpedpic, width, height, num_components, color_space); printf("Done :)\n"); out: if (rawpic) free(rawpic); if (warpedpic) free(warpedpic); return 0; }
//将采集好的数据放到文件中 int process_image(void *addr,int length) { printf("process_image\n"); FILE *fp; static int num = 0; char picture_name[20]; sprintf(picture_name,"picture%d.jpg",num ++); if (-1 == write_jpeg(picture_name, addr, 75, 640, 420, 0)) { perror("write_jpeg error!\n"); } usleep(500); #if 0 if((fp = fopen(picture_name,"w")) == NULL) { perror("Fail to fopen"); exit(EXIT_FAILURE); } fwrite(addr,length,1,fp); usleep(500); fclose(fp); #endif return 0; }
void* thread_save_image(void*) { timespec time_save0, time_save1, t_sleep, t_rem; t_sleep.tv_sec = 0; t_sleep.tv_nsec = 10; for (;;) { // Wait for image capture if (save_frame_buffer.size() > 0) { clock_gettime( CLOCK_REALTIME, &time_save0); write_jpeg(save_frame_buffer.front()); // write_png(save_frame_buffer.front()); if (save_frame_buffer.size() > 4) { int bufsize = save_frame_buffer.front().width * save_frame_buffer.front().height * save_frame_buffer.size() / 1024.0; std::cout << "\rBuffer queue: " << bufsize << " kB " << std::flush; } std::cout << "d" << std::flush; pthread_mutex_lock( &save_buffer_mutex ); save_frame_buffer.pop(); pthread_mutex_unlock( &save_buffer_mutex ); clock_gettime(CLOCK_REALTIME, &time_save1); double twrite = tdiff(time_save1, time_save0); } else { nanosleep(&t_sleep, &t_rem); } } }
void write_image(char *file_name, int render_fmt, VInfo *ji, uint8_t *buf) { FILE *x; if ((x = open_outfile(file_name))) { switch (render_fmt) { case FMT_JPG: if (write_jpeg(ji, buf, JPEG_QUALITY, x)) dlog(LOG_ERR, "IMF: Could not write jpeg: %s\n", file_name); break; case FMT_PNG: if (write_png(ji, buf, x)) dlog(LOG_ERR, "IMF: Could not write png: %s\n", file_name); break; case FMT_PPM: if (write_ppm(ji, buf, x)) dlog(LOG_ERR, "IMF: Could not write ppm: %s\n", file_name); break; default: dlog(LOG_ERR, "IMF: Unknown outformat %d\n", render_fmt); break; } if (strcmp(file_name, "-")) fclose(x); dlog(LOG_INFO, "IMF: Outputfile %s closed\n", file_name); } else dlog(LOG_ERR, "IMF: Could not open outfile: %s\n", file_name); return; }
void writebyte(const unsigned char b) { jpgbuff[jpgn++] = b; if (jpgn == sizeof(jpgbuff)) { jpgn = 0; write_jpeg(jpgbuff, sizeof(jpgbuff)); } }
int main (int argc, char **argv) { int arg; for (arg=1; arg < argc; arg++) { if (!(ifp = fopen (argv[arg], "rb"))) { perror (argv[arg]); continue; } find_jpeg(); write_jpeg (argv[arg], 2); find_jpeg(); write_jpeg (argv[arg], 1); if (ftell(ifp) & 1) fgetc(ifp); write_jpeg (argv[arg], 3); fclose (ifp); } return 0; }
void image::write(std::string filename) { // switch on extension // that's the only way to find the format if (filename.find(".ppm") != std::string::npos) throw image_write_exception("image::write() Cannot Write .ppm files"); else if (filename.find(".jpg") != std::string::npos || filename.find(".jpeg") != std::string::npos) write_jpeg(filename); else if (filename.find(".png") != std::string::npos) write_png(filename); else throw image_write_exception("image::write() Unrecognized Extention"); }
bool write_image(char *file_name, int width, int height, int nbytes, UCHAR *image, double *geotiffInfo, DWORD jpegQuality, DWORD tiffCompression) { const char *p = strrchr(file_name, '.'); if (!p) return false; if (!_stricmp(p, ".jpg")) return write_jpeg(file_name, width, height, nbytes, image, jpegQuality); if (!_stricmp(p, ".tif")) return write_tiff(file_name, width, height, nbytes, image, geotiffInfo, jpegQuality, tiffCompression); if (!_stricmp(p, ".png")) return write_png(file_name, width, height, nbytes, image); return false; }
/** * Save image * Parameters: * path: output path * mem: bitmap in memory * Return: * 0 OK * -1 ERROR */ int saveImage (const char *path, const Bitmap_t *mem) { VALIDATE_NOT_NULL2 (path, mem); const char const *postfix = getFilePostfix (path); if (NULL == postfix) { LogE ("Failed getFilePostfix in saveImage\n"); return -1; } if (strcasecmp (postfix, "jpg") == 0) { return write_jpeg (path, mem); } else if (strcasecmp (postfix, "png") == 0) { return write_png (path, mem); } else { LogE ("Invalid postfix name (%s) in saveImage\n", postfix); return -1; } }
/****************************************************************************** ** huffman_stop ** -------------------------------------------------------------------------- ** Finalize Huffman encoding by flushing bit-buffer, writing End of Image (EOI) ** into output buffer and flusing this buffer. ** ** ARGUMENTS: - ** ** RETURN: - ******************************************************************************/ void huffman_stop(void) { close_PDU(&bitbuf); write_jpeg(jpgbuff, jpgn); jpgn = 0; }
int decode(char *input, char *output, struct lego lego) { int rc = 0, x = 0, y = 0, i = 0, l = 0; int width = lego.width, height = lego.height; int brick_width = 1, brick_height = 1; char *ext = NULL; unsigned char *data = NULL; struct coords coords; struct BRKS BRKS = lego.BRKS; struct brick brick; struct brick_def brick_def; struct color_def color_def; struct image image; if (!input) { printf("No filename provided\n"); rc = 1; goto cleanup; } if (!BRKS.count) { printf("No bricks to decode\n"); rc = 1; goto cleanup; } for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { canvas[x][y] = -1; } } for (i = 0, l = BRKS.count; i < l; i++) { brick = BRKS.bricks[i]; brick_def = get_brick_by_id(brick.id); brick_width = brick_def.width; brick_height = brick_def.height; if (brick.rotation == ROTATION_90 || brick.rotation == ROTATION_270) { brick_width = brick_def.height; brick_height = brick_def.width; } coords = find_origin(width, height); for (y = coords.y; y < coords.y + brick_height; y++) { for (x = coords.x; x < coords.x + brick_width; x++) { canvas[x][y] = brick.color; } } } data = malloc(sizeof(char) * (width * height * 3) + 1); for (y = 0, i = 0; y < height; y++) { for (x = 0; x < width; x++) { color_def = get_color_by_id(canvas[x][y]); data[i++] = color_def.red; data[i++] = color_def.green; data[i++] = color_def.blue; } } image.data = data; image.width = width; image.height = height; if (!output) { output = malloc(sizeof(char) * strlen(input) + 1); strcpy(output, input); ext = strrchr(output, '.'); output[strlen(output) - strlen(ext)] = '\0'; strcat(output, ".png"); rc = write_png(image, output); free(output); goto cleanup; } if (strstr(output, ".jpg") || strstr(output, ".jpeg")) { rc = write_jpeg(image, output); } else if (strstr(output, ".png")) { rc = write_png(image, output); } else { printf("Unsupported file format %s\n", output); rc = 1; goto cleanup; } cleanup: if (data) { free(data); } return rc; }
size_t format_image(uint8_t **out, int render_fmt, int misc_int, VInfo *ji, uint8_t *buf) { #ifdef HAVE_WINDOWS char tfn[64] = ""; #endif #ifdef __USE_XOPEN2K8 size_t rs = 0; FILE *x = open_memstream((char**) out, &rs); #else FILE *x = tmpfile(); #endif #ifdef HAVE_WINDOWS if (!x) { // wine and ancient version of windows don't support tmpfile() // srand is per thread :( struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_sec * tv.tv_usec * 100000); snprintf(tfn, sizeof(tfn), "harvid.tmp.%d", rand()); x = fopen(tfn, "w+b"); // we should really use open(tfn, O_RDWR | O_CREAT | O_EXCL, 0600); and fdopen } #endif if (!x) { dlog(LOG_ERR, "IMF: tmpfile() creation failed.\n"); return(0); } switch (render_fmt) { case 1: { if (misc_int < 5 || misc_int > 100) misc_int = JPEG_QUALITY; if (write_jpeg(ji, buf, misc_int, x)) dlog(LOG_ERR, "IMF: Could not write jpeg\n"); break; } case 2: if (write_png(ji, buf, x)) dlog(LOG_ERR, "IMF: Could not write png\n"); break; case 3: if (write_ppm(ji, buf, x)) dlog(LOG_ERR, "IMF: Could not write ppm\n"); break; default: dlog(LOG_ERR, "IMF: Unknown outformat %d\n", render_fmt); fclose(x); return 0; break; } #ifdef __USE_XOPEN2K8 fclose(x); return rs; #elif defined HAVE_WINDOWS if (strlen(tfn) > 0) { fclose(x); x = fopen(tfn, "rb"); } else { fflush(x); } #else fflush(x); #endif /* re-read image from tmp-file */ fseek (x , 0 , SEEK_END); long int rsize = ftell (x); rewind(x); if (fseek(x, 0L, SEEK_SET) < 0) { dlog(LOG_WARNING, "IMF: fseek failed\n"); } fflush(x); *out = (uint8_t*) malloc(rsize*sizeof(uint8_t)); if (fread(*out, sizeof(char), rsize, x) != rsize) { dlog(LOG_WARNING, "IMF: short read. - possibly incomplete image\n"); } fclose(x); #ifdef HAVE_WINDOWS if (strlen(tfn) > 0) { unlink(tfn); } #endif return (rsize); }
int main(int argc, char **argv) { char *ai, *fname; char flamename[256]; char *prefix = args("prefix", ""); int first_frame = argi("begin", 0); int last_frame = argi("end", 0); int frame_time = argi("time", 0); int dtime = argi("dtime", 1); int do_fields = argi("fields", 0); int write_genome = argi("write_genome",0); double qs = argf("qs", 1.0); double ss = argf("ss", 1.0); char *format = getenv("format"); int verbose = argi("verbose", 1); int transparency = argi("transparency", 0); int bits = argi("bits", 33); int sub_batch_size = argi("sub_batch_size", 10000); int bpc = argi("bpc",8); int earlyclip = argi("earlyclip",0); int ftime, channels; unsigned char *image; flam3_genome *cps,center_cp; int i, ncps = 0; char *inf = getenv("in"); double pixel_aspect = argf("pixel_aspect", 1.0); int num_threads = argi("nthreads",0); FILE *in,*fp,*genfp; flam3_frame f; flam3_img_comments fpc; stat_struct stats,stats2; char badval_string[64]; char numiter_string[64]; char rtime_string[64]; #ifdef WIN32 char *slashloc; char exepath[256]; char palpath[256]; memset(exepath,0,256); memset(palpath,0,256); slashloc = strrchr(argv[0],'\\'); if (NULL==slashloc) { sprintf(palpath,"flam3_palettes=flam3-palettes.xml"); } else { strncpy(exepath,argv[0],slashloc-argv[0]+1); sprintf(palpath,"flam3_palettes=%sflam3-palettes.xml",exepath); } putenv(palpath); #endif memset(¢er_cp,0, sizeof(flam3_genome)); if (1 != argc) { docstring(); exit(0); } /* Init random number generators */ flam3_init_frame(&f); flam3_srandom(); /* Set the number of threads */ if (num_threads==0) { num_threads = flam3_count_nthreads(); if (verbose > 1) fprintf(stderr,"Automatically detected %d core(s)...\n",num_threads); } else{ if (verbose) fprintf(stderr,"Manually specified %d thread(s)...\n",num_threads); } if (getenv("frame")) { if (getenv("time")) { fprintf(stderr, "cannot specify both time and frame.\n"); exit(1); } if (getenv("begin") || getenv("end")) { fprintf(stderr, "cannot specify both frame and begin or end.\n"); exit(1); } first_frame = last_frame = atoi(getenv("frame")); } if (getenv("time")) { if (getenv("begin") || getenv("end")) { fprintf(stderr, "cannot specify both time and begin or end.\n"); exit(1); } first_frame = last_frame = frame_time; } if (NULL == format) format = "png"; if (strcmp(format, "jpg") && strcmp(format, "ppm") && strcmp(format, "png")) { fprintf(stderr, "format must be either jpg, ppm, or png, not %s.\n", format); exit(1); } if (pixel_aspect <= 0.0) { fprintf(stderr, "pixel aspect ratio must be positive, not %g.\n", pixel_aspect); exit(1); } if (inf) in = fopen(inf, "rb"); else in = stdin; if (NULL == in) { perror(inf); exit(1); } cps = flam3_parse_from_file(in, inf, flam3_defaults_on, &ncps); if (inf) fclose(in); if (NULL == cps) { fprintf(stderr," error reading genomes.\n"); exit(1); } if (0 == ncps) { fprintf(stderr, "error: no genomes.\n"); exit(1); } for (i = 0; i < ncps; i++) { cps[i].sample_density *= qs; cps[i].height = (int)(cps[i].height * ss); cps[i].width = (int)(cps[i].width * ss); cps[i].pixels_per_unit *= ss; if (cps[i].height<=0 || cps[i].width<=0) { fprintf(stderr,"output image has dimension <=0, aborting.\n"); exit(1); } if (i > 0 && cps[i].time <= cps[i-1].time) { fprintf(stderr, "error: control points must be sorted by time, but %g <= %g, index %d.\n", cps[i].time, cps[i-1].time, i); exit(1); } if ((cps[i].width != cps[0].width) || (cps[i].height != cps[0].height)) { fprintf(stderr, "warning: flame %d at time %g size mismatch. " "(%d,%d) should be (%d,%d).\n", i, cps[i].time, cps[i].width, cps[i].height, cps[0].width, cps[0].height); cps[i].width = cps[0].width; cps[i].height = cps[0].height; } } if (!getenv("time") && !getenv("frame")) { if (!getenv("begin")) { first_frame = (int) cps[0].time; } if (!getenv("end")) { last_frame = (int) cps[ncps-1].time - 1; if (last_frame < first_frame) last_frame = first_frame; } } channels = strcmp(format, "png") ? 3 : 4; /* Check for 16-bit-per-channel processing */ if ( (16 == bpc) && (strcmp(format,"png") != 0)) { fprintf(stderr,"Support for 16 bpc images is only present for the png format.\n"); exit(1); } else if (bpc != 8 && bpc != 16) { fprintf(stderr,"Unexpected bpc specified (%d)\n",bpc); exit(1); } // f.temporal_filter_radius = argf("blur", 0.5); f.pixel_aspect_ratio = pixel_aspect; f.genomes = cps; f.ngenomes = ncps; f.verbose = verbose; f.bits = bits; f.progress = 0; f.earlyclip = earlyclip; f.nthreads = num_threads; f.sub_batch_size = sub_batch_size; if (16==bpc) f.bytes_per_channel = 2; else f.bytes_per_channel = 1; image = (void *) calloc((size_t)channels * (size_t)cps[0].width * (size_t)cps[0].height * f.bytes_per_channel, sizeof(char)); if (dtime < 1) { fprintf(stderr, "dtime must be positive, not %d.\n", dtime); exit(1); } for (ftime = first_frame; ftime <= last_frame; ftime += dtime) { f.time = (double) ftime; if (verbose && ((last_frame-first_frame)/dtime) >= 1) { fprintf(stderr, "time = %d/%d/%d\n", ftime, last_frame, dtime); } if (do_fields) { if (flam3_render(&f, image, flam3_field_even, channels, transparency,&stats)) { fprintf(stderr,"error rendering image: aborting.\n"); exit(1); } f.time += 0.5; if (flam3_render(&f, image, flam3_field_odd, channels, transparency,&stats2)) { fprintf(stderr,"error rendering image: aborting.\n"); exit(1); } stats.badvals+=stats2.badvals; stats.render_seconds+=stats2.render_seconds; stats.num_iters+=stats2.num_iters; } else { if (flam3_render(&f, image, flam3_field_both, channels, transparency,&stats)) { fprintf(stderr,"error rendering image: aborting.\n"); exit(1); } } if (getenv("out")) fname = getenv("out"); else { fname = malloc(strlen(prefix) + 20); sprintf(fname, "%s%05d.%s", prefix, ftime, format); } if (verbose) fprintf(stderr, "writing %s...", fname); if (write_genome) { sprintf(flamename,"%s.flam3",fname); /* get center genome */ flam3_interpolate(f.genomes, f.ngenomes, f.time, 0, ¢er_cp); /* write it out */ genfp = fopen(flamename,"w"); if (NULL == genfp) { perror(flamename); exit(1); } flam3_print(genfp, ¢er_cp, NULL, flam3_print_edits); fclose(genfp); } fp = fopen(fname, "wb"); if (NULL == fp) { perror(fname); exit(1); } /* Get center cp for embedding in png file */ flam3_interpolate(f.genomes, f.ngenomes, f.time, 0, ¢er_cp); /* Convert to string */ fpc.genome = flam3_print_to_string(¢er_cp); sprintf(badval_string, "%g",stats.badvals/(double)stats.num_iters); fpc.badvals = badval_string; sprintf(numiter_string,"%g",(double)stats.num_iters); fpc.numiters = numiter_string; sprintf(rtime_string,"%d",stats.render_seconds); fpc.rtime = rtime_string; if (!strcmp(format, "png")) { write_png(fp, image, cps[0].width, cps[0].height, &fpc, f.bytes_per_channel); } else if (!strcmp(format, "jpg")) { write_jpeg(fp, image, cps[0].width, cps[0].height, &fpc); } else { int size = 3 * cps[0].width * cps[0].height; fprintf(fp, "P6\n"); fprintf(fp, "%d %d\n255\n", cps[0].width, cps[0].height); if (size != fwrite(image, 1, size, fp)) { perror(fname); } } /* Free string */ free(fpc.genome); clear_cp(¢er_cp,0); fclose(fp); if (!getenv("out")) free(fname); } for (i = 0; i < ncps; i++) { xmlFreeDoc(cps[i].edits); clear_cp(&cps[i],0); } free(cps); free(image); if (verbose) fprintf(stderr, "done.\n"); fflush(stderr); return 0; }
int write_image(const char *filename, int width, int height, unsigned char *rgb) { FILE *outfile; char *extension = strrchr(filename, '.'); char *lowercase; char *ptr; int success = 0; lowercase = malloc(strlen(extension) + 1); strcpy(lowercase, extension); ptr = lowercase; while (*ptr != '\0') *ptr++ = tolower(*extension++); outfile = fopen(filename, "wb"); if (outfile == NULL) return(0); if (strcmp(lowercase, ".bmp" ) == 0) { success = write_bmp(filename, width, height, rgb); } else if (strcmp(lowercase, ".gif" ) == 0) { #ifdef HAVE_LIBGIF success = write_gif(filename, width, height, rgb); #else fprintf(stderr, "Sorry, this program was not compiled with GIF support\n"); success = 0; #endif /* HAVE_LIBPNG */ } else if (( strcmp(lowercase, ".jpg" ) == 0) || (strcmp(lowercase, ".jpeg") == 0)) { #ifdef HAVE_LIBJPEG success = write_jpeg(outfile, width, height, rgb, Q); #else fprintf(stderr, "Sorry, this program was not compiled with JPEG support\n"); success = 0; #endif /* HAVE_LIBJPEG */ } else if (strcmp(lowercase, ".png" ) == 0) { #ifdef HAVE_LIBPNG success = write_png(outfile, width, height, rgb, alpha); #else fprintf(stderr, "Sorry, this program was not compiled with PNG support\n"); success = 0; #endif /* HAVE_LIBPNG */ } else if (( strcmp(lowercase, ".pbm") == 0) || (strcmp(lowercase, ".pgm") == 0) || (strcmp(lowercase, ".ppm") == 0)) { #ifdef HAVE_LIBPNM if (strcmp(lowercase, ".pbm") == 0) success = write_pnm(outfile, width, height, rgb, 1, PBM_TYPE, 0); else if (strcmp(lowercase, ".pgm") == 0) success = write_pnm(outfile, width, height, rgb, 255, PGM_TYPE, 0); else if (strcmp(lowercase, ".ppm") == 0) success = write_pnm(outfile, width, height, rgb, 255, PPM_TYPE, 0); #else fprintf(stderr, "Sorry, this program was not compiled with PNM support\n"); success = 0; #endif /* HAVE_LIBPNM */ } else if ((strcmp(lowercase, ".tif" ) == 0) || (strcmp(lowercase, ".tiff" ) == 0)) { #ifdef HAVE_LIBTIFF success = write_tiff(filename, width, height, rgb); #else fprintf(stderr, "Sorry, this program was not compiled with TIFF support\n"); success = 0; #endif /* HAVE_LIBTIFF */ } else { fprintf(stderr, "Unknown image format\n"); success = 0; } free(lowercase); fclose(outfile); return(success); }
void frame_t::write (int sid, const char *filename) { write_jpeg( img[sid], filename, get_width(sid), get_height(sid), 3); }