static int x11_get_image_size(w3mimg_op * self, W3MImage * img, char *fname, int *w, int *h) { struct x11_info *xi; #if defined(USE_IMLIB) ImlibImage *im; #elif defined(USE_IMLIB2) Imlib_Image im; #elif defined(USE_GDKPIXBUF) GdkPixbufAnimation *animation; #endif if (self == NULL) return 0; xi = (struct x11_info *)self->priv; if (xi == NULL) return 0; #if defined(USE_IMLIB) im = Imlib_load_image(xi->id, fname); if (!im) return 0; *w = im->rgb_width; *h = im->rgb_height; Imlib_kill_image(xi->id, im); #elif defined(USE_IMLIB2) im = imlib_load_image(fname); if (im == NULL) return 0; imlib_context_set_image(im); *w = imlib_image_get_width(); *h = imlib_image_get_height(); imlib_free_image(); #elif defined(USE_GDKPIXBUF) animation = gdk_pixbuf_animation_new_from_file(fname); if (!animation) return 0; get_animation_size(animation, w, h, NULL); gdk_pixbuf_animation_unref(animation); #endif return 1; }
int main (int argc, char **argv) { volatile int opt, i,j; char *filename = NULL; char *outname = NULL; char option_chars [BUFSIZ]; int qscale = 1; int type = 2; int width = -1; int height = -1; int limit = 65536; #if ENABLE_NLS #if HAVE_SETLOCALE setlocale (LC_ALL, ""); #endif bindtextdomain (PACKAGE, LOCALEDIR); bindtextdomain (program_invocation_short_name, LOCALEDIR); textdomain (PACKAGE); #endif /* ** build the option-char string from the option struct. */ for (i=j=0; long_options[i].name; i++) { switch (long_options[i].has_arg) { case no_argument: option_chars[j++] = (char) long_options[i].val; break; case required_argument: option_chars[j++] = (char) long_options[i].val; option_chars[j++] = ':'; break; } } option_chars[j] = 0; while ((opt = getopt_long(argc,argv,option_chars,long_options,NULL)) != EOF) { switch (opt) { case OPTION_HELP: printf1 (copyright_text,program_invocation_short_name); printf1 (_("Usage: %s [options] <filename>\n"),program_invocation_short_name); for (i=0; long_options[i].name; i++) { switch (long_options[i].has_arg) { case no_argument: printf1 ("--%s,-%c \t%s\n",long_options[i].name,long_options[i].val,_(option_help[i])); break; case required_argument: printf1 ("--%s=x,-%c\t%s\n",long_options[i].name,long_options[i].val,_(option_help[i])); break; } } exit (EXIT_SUCCESS); case OPTION_VERSION: printf1 (copyright_text,program_invocation_short_name); printf1 ("%s version %s\n",program_invocation_short_name,version_text); exit (EXIT_SUCCESS); case OPTION_QUIET: verbose_level = 0; break; case OPTION_VERBOSE: verbose_level = 2; break; case OPTION_OUTPUT: outname = optarg; break; case OPTION_QSCALE: qscale = strtoul(optarg,0,0); break; case OPTION_TYPE: type = strtoul(optarg,0,0); break; case OPTION_WIDTH: width = strtoul(optarg,0,0); break; case OPTION_HEIGHT: height = strtoul(optarg,0,0); break; case OPTION_LIMIT: limit = strtoul(optarg,0,0); break; default: exit(1); } } if (optind < argc) filename = argv[optind]; if (getenv("PSXDEV_QUIET") && (verbose_level==1)) verbose_level = 0; bs_init(); if (filename) { FILE *fp = stdout; int size = 99999999; unsigned short buf[0x80000]; while (limit < size) { ImlibData id; ImlibImage *im, *im2; bs_input_image_t img; // this is kind of a hack, // I do not initialize imlib! // so this utilitiy may not always work // correctly. im = Imlib_load_image (&id,filename); if (im) { printf2 (_("%s: input dimension: %dx%d\n"),program_invocation_short_name,im->rgb_width,im->rgb_height); if (width!=-1 || height!=-1) { im2 = Imlib_clone_scaled_image(&id,im, (width==-1) ? im->rgb_width : width, (height==-1) ? im->rgb_height : height); Imlib_kill_image (&id,im); im = im2; printf2 (_("%s: scaled to: %dx%d\n"),program_invocation_short_name,im->rgb_width,im->rgb_height); } img.width = im->rgb_width; img.height = im->rgb_height; img.lpbits = im->rgb_data; img.top = img.lpbits; img.nextline = img.width*3; img.bit = 24; // depth size = bs_encode ((bs_header_t*)buf,&img,type,qscale,0); printf2 (_("%s: output size: %d bytes\n"),program_invocation_short_name,size); printf2 (_("%s: Q scale factor: %d\n"),program_invocation_short_name,qscale); qscale++; if (qscale>255) { errorf("can't compress!\n"); } Imlib_kill_image (&id,im); } } if (outname) freopen(outname,"wb",fp); fwrite(buf,1,size,fp); fflush (fp); if (outname) fclose (fp); } return EXIT_SUCCESS; }
static int __doSaveImage(Image *image, char *filename, CSOUND *csound) { #ifdef USE_LIBPNG png_structp png_ptr; png_infop info_ptr; png_bytepp row_pointers; unsigned rowbytes; int i; FILE *fp; void *fd; fd = csound->FileOpen2(csound, &fp, CSFILE_STD, filename, "wb", "", CSFTYPE_IMAGE_PNG, 0); if (UNLIKELY(fd == NULL)) { return csound->InitError(csound, Str("imageload: cannot open image %s for writing.\n"), filename); } png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (UNLIKELY(!png_ptr)){ csound->FileClose(csound, fd); return csound->InitError(csound, Str("imageload: out of memory.\n")); } info_ptr = png_create_info_struct(png_ptr); if (UNLIKELY(!info_ptr)) { png_destroy_write_struct(&png_ptr, (png_infopp)NULL); csound->FileClose(csound, fd); return csound->InitError(csound, Str("imageload: out of memory.\n")); } png_init_io(png_ptr, fp); png_set_IHDR(png_ptr, info_ptr, image->w, image->h, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png_ptr, info_ptr); row_pointers = (png_bytepp)malloc(image->h*sizeof(png_bytep)); if (UNLIKELY(row_pointers == NULL)) { png_destroy_write_struct(&png_ptr, &info_ptr); return csound->InitError(csound, Str("imageload: out of memory.\n")); } rowbytes = png_get_rowbytes(png_ptr, info_ptr); for (i = 0; i < image->h; i++) row_pointers[i] = image->imageData + i*rowbytes; png_write_image(png_ptr, row_pointers); png_write_end(png_ptr, info_ptr); free(row_pointers); png_destroy_write_struct(&png_ptr, &info_ptr); csound->FileClose(csound, fd); return OK; #else Display *disp; ImlibData *id; ImlibImage *im; disp=XOpenDisplay(NULL); id=Imlib_init(disp); im = Imlib_create_image_from_data(id, image->imageData, NULL, image->w, image->h); Imlib_save_image(id, im, filename, NULL); Imlib_kill_image(id, im); return OK; #endif }
static int x11_load_image(w3mimg_op * self, W3MImage * img, char *fname, int w, int h) { struct x11_info *xi; #if defined(USE_IMLIB) ImlibImage *im; #elif defined(USE_IMLIB2) Imlib_Image im; #elif defined(USE_GDKPIXBUF) GdkPixbufAnimation *animation; GList *frames; int i, j, iw, ih, n, frame_num, delay, max_anim; double ratio_w, ratio_h; struct x11_image *ximg; Pixmap tmp_pixmap; #endif if (self == NULL) return 0; xi = (struct x11_info *)self->priv; if (xi == NULL) return 0; #if defined(USE_IMLIB) im = Imlib_load_image(xi->id, fname); if (!im) return 0; if (w <= 0) w = im->rgb_width; if (h <= 0) h = im->rgb_height; img->pixmap = (void *)XCreatePixmap(xi->display, xi->parent, w, h, DefaultDepth(xi->display, 0)); if (!img->pixmap) return 0; XSetForeground(xi->display, xi->imageGC, xi->background_pixel); XFillRectangle(xi->display, (Pixmap) img->pixmap, xi->imageGC, 0, 0, w, h); Imlib_paste_image(xi->id, im, (Pixmap) img->pixmap, 0, 0, w, h); Imlib_kill_image(xi->id, im); #elif defined(USE_IMLIB2) im = imlib_load_image(fname); if (!im) return 0; imlib_context_set_image(im); if (w <= 0) w = imlib_image_get_width(); if (h <= 0) h = imlib_image_get_height(); img->pixmap = (void *)XCreatePixmap(xi->display, xi->parent, w, h, DefaultDepth(xi->display, 0)); if (!img->pixmap) return 0; XSetForeground(xi->display, xi->imageGC, xi->background_pixel); XFillRectangle(xi->display, (Pixmap) img->pixmap, xi->imageGC, 0, 0, w, h); imlib_context_set_display(xi->display); imlib_context_set_visual(DefaultVisual(xi->display, 0)); imlib_context_set_colormap(DefaultColormap(xi->display, 0)); imlib_context_set_drawable((Drawable) img->pixmap); imlib_render_image_on_drawable_at_size(0, 0, w, h); imlib_free_image(); #elif defined(USE_GDKPIXBUF) max_anim = self->max_anim; animation = gdk_pixbuf_animation_new_from_file(fname); if (!animation) return 0; frames = gdk_pixbuf_animation_get_frames(animation); frame_num = n = gdk_pixbuf_animation_get_num_frames(animation); get_animation_size(animation, &iw, &ih, &delay); if (delay <= 0) max_anim = -1; if (max_anim < 0) { frame_num = (-max_anim > n) ? n : -max_anim; } else if (max_anim > 0) { frame_num = n = (max_anim > n) ? n : max_anim; } if (w < 1 || h < 1) { w = iw; h = ih; ratio_w = ratio_h = 1; } else { ratio_w = 1.0 * w / iw; ratio_h = 1.0 * h / ih; } tmp_pixmap = XCreatePixmap(xi->display, xi->parent, w, h, DefaultDepth(xi->display, 0)); XFillRectangle(xi->display, (Pixmap) tmp_pixmap, xi->imageGC, 0, 0, w, h); if (!tmp_pixmap) { gdk_pixbuf_animation_unref(animation); return 0; } ximg = x11_img_new(xi, w, h, frame_num); if (!ximg) { XFreePixmap(xi->display, tmp_pixmap); gdk_pixbuf_animation_unref(animation); return 0; } for (j = 0; j < n; j++) { GdkPixbufFrame *frame; GdkPixbuf *org_pixbuf, *pixbuf; int width, height, ofstx, ofsty; if (max_anim < 0) { i = (j - n + frame_num > 0) ? (j - n + frame_num) : 0; } else { i = j; } frame = (GdkPixbufFrame *) g_list_nth_data(frames, j); org_pixbuf = gdk_pixbuf_frame_get_pixbuf(frame); ofstx = gdk_pixbuf_frame_get_x_offset(frame); ofsty = gdk_pixbuf_frame_get_y_offset(frame); delay = gdk_pixbuf_frame_get_delay_time(frame); width = gdk_pixbuf_get_width(org_pixbuf); height = gdk_pixbuf_get_height(org_pixbuf); if (ofstx == 0 && ofsty == 0 && width == w && height == h) { pixbuf = resize_image(org_pixbuf, w, h); } else { pixbuf = resize_image(org_pixbuf, width * ratio_w, height * ratio_h); ofstx *= ratio_w; ofsty *= ratio_h; } width = gdk_pixbuf_get_width(pixbuf); height = gdk_pixbuf_get_height(pixbuf); if (delay > ximg->delay) ximg->delay = delay; XCopyArea(xi->display, tmp_pixmap, ximg->pixmap[i], xi->imageGC, 0, 0, w, h, 0, 0); gdk_pixbuf_xlib_render_to_drawable_alpha(pixbuf, (Drawable) ximg->pixmap[i], 0, 0, ofstx, ofsty, width, height, GDK_PIXBUF_ALPHA_BILEVEL, 1, XLIB_RGB_DITHER_NORMAL, 0, 0); switch (gdk_pixbuf_frame_get_action(frame)) { case GDK_PIXBUF_FRAME_RETAIN: XCopyArea(xi->display, ximg->pixmap[i], tmp_pixmap, xi->imageGC, 0, 0, w, h, 0, 0); break; case GDK_PIXBUF_FRAME_DISPOSE: break; case GDK_PIXBUF_FRAME_REVERT: XCopyArea(xi->display, ximg->pixmap[0], tmp_pixmap, xi->imageGC, 0, 0, w, h, 0, 0); break; default: XCopyArea(xi->display, ximg->pixmap[0], tmp_pixmap, xi->imageGC, 0, 0, w, h, 0, 0); break; } if (org_pixbuf != pixbuf) gdk_pixbuf_finalize(pixbuf); } XFreePixmap(xi->display, tmp_pixmap); gdk_pixbuf_animation_unref(animation); img->pixmap = ximg; #endif img->width = w; img->height = h; return 1; }