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 Image * __doOpenImage(char * filename, CSOUND *csound) { #ifdef USE_LIBPNG #define HS (8) FILE *fp; void *fd; unsigned char header[HS]; png_structp png_ptr; png_infop info_ptr; /* png_infop end_ptr; */ int is_png; png_uint_32 width, height, rowbytes; int bit_depth; int color_type; unsigned char *image_data; png_bytepp row_pointers; unsigned int i; Image *img; fd = csound->FileOpen2(csound, &fp, CSFILE_STD, filename, "rb", "SFDIR;SSDIR", CSFTYPE_IMAGE_PNG, 0); if (UNLIKELY(fd == NULL)) { csound->InitError(csound, Str("imageload: cannot open image %s.\n"), filename); return NULL; } if (UNLIKELY(HS!=fread(header, 1, HS, fp))) csound->InitError(csound, Str("imageload: file %s is not in PNG format.\n"), filename); is_png = !png_sig_cmp(header, 0, HS); if (UNLIKELY(!is_png)) { csound->InitError(csound, Str("imageload: file %s is not in PNG format.\n"), filename); csound->FileClose(csound, fd); return NULL; } png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (UNLIKELY(!png_ptr)) { csound->InitError(csound, Str("imageload: out of memory.\n")); csound->FileClose(csound, fd); return NULL; } info_ptr = png_create_info_struct(png_ptr); if (UNLIKELY(!info_ptr)) { png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); csound->InitError(csound, Str("imageload: out of memory.\n")); csound->FileClose(csound, fd); return NULL; } /* end_ptr = png_create_info_struct(png_ptr); */ /* if (UNLIKELY(!end_ptr)) { */ /* png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); */ /* csound->InitError(csound, Str("imageload: out of memory.\n")); */ /* csound->FileClose(csound, fd); */ /* return NULL; */ /* } */ png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, HS); png_read_info(png_ptr, info_ptr); { png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); } if (color_type & PNG_COLOR_MASK_ALPHA) png_set_strip_alpha(png_ptr); if (bit_depth == 16) png_set_strip_16(png_ptr); if (bit_depth < 8) png_set_packing(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr); png_read_update_info(png_ptr, info_ptr); rowbytes = png_get_rowbytes(png_ptr, info_ptr); if (UNLIKELY((image_data = (unsigned char *) malloc(rowbytes * height))==NULL)) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); csound->InitError(csound, Str("imageload: out of memory.\n")); return NULL; } row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep)); if (UNLIKELY(row_pointers == NULL)) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); free(image_data); image_data = NULL; csound->InitError(csound, Str("imageload: out of memory.\n")); return NULL; } for (i = 0; i < height; i++) row_pointers[i] = image_data + i*rowbytes; png_read_image(png_ptr, row_pointers); free(row_pointers); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); csound->FileClose(csound, fd); img = malloc(sizeof(Image)); if (UNLIKELY(!img)) { free(image_data); csound->InitError(csound, Str("imageload: out of memory.\n")); return NULL; } img->w = width; img->h = height; img->imageData = image_data; return img; #else Display *disp; ImlibData *id; ImlibImage *im; Image *img; size_t datasize; disp=XOpenDisplay(NULL); id=Imlib_init(disp); im=Imlib_load_image(id, filename); img = malloc(sizeof(Image)); img->w = im->rgb_width; img->h = im->rgb_height; datasize = img->w*img->h*3 * sizeof(unsigned char); img->imageData = malloc(datasize); memcpy(img->imageData, im->rgb_data, datasize); return img; #endif /* SDL */ /* Image *img; size_t datasize; SDL_Surface *srfc; int x,y; int bpp; int indcount = 0; Uint32 *pixel; Uint8 r, g, b; srfc = IMG_Load(filename); if (srfc) { SDL_LockSurface(srfc); img = malloc(sizeof(Image)); img->w = srfc->w; img->h = srfc->h; bpp = srfc->format->BitsPerPixel; datasize = img->w*img->h*3 * sizeof(unsigned char); img->imageData = malloc(datasize); for(y = 0; y < img->h; y++) { for(x = 0; x < img->w; x++) { if (bpp<=8) //need to test on other platforms pixel = srfc->pixels + y * srfc->pitch + x * bpp; else pixel = srfc->pixels + y * srfc->pitch + x * bpp / 8; SDL_GetRGB(*pixel,srfc->format, &r, &g, &b); img->imageData[indcount]= r; img->imageData[indcount+1]= g; img->imageData[indcount+2]= b; indcount += 3; } } SDL_UnlockSurface(srfc); SDL_FreeSurface ( srfc ); return img; } return NULL; */ }
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; }