static int x11_init(w3mimg_op * self) { struct x11_info *xi; if (self == NULL) return 0; xi = (struct x11_info *)self->priv; if (xi == NULL) return 0; #if defined(USE_IMLIB) if (!xi->id) { xi->id = Imlib_init(xi->display); if (!xi->id) return 0; } #elif defined(USE_GDKPIXBUF) if (!xi->init_flag) { gdk_pixbuf_xlib_init(xi->display, 0); xi->init_flag = TRUE; } #endif if (!xi->imageGC) { xi->imageGC = XCreateGC(xi->display, xi->parent, 0, NULL); if (!xi->imageGC) return 0; } return 1; }
/* * Notify callback function for `button1'. */ void go_func(Panel_item item, Event *event) { int i; int ht,wd; time_t now = time(0) - (time(0) % 86400); static ImlibData *imdata = NULL; ImlibImage *image; FILE *outfile = NULL; char pathname[1024]; cclock_window1_objects *ip = (cclock_window1_objects *) xv_get(item, XV_KEY_DATA, INSTANCE); ht = xv_get(Cclock_window1->canvas1,XV_HEIGHT); wd = xv_get(Cclock_window1->canvas1,XV_WIDTH); if( imdata == NULL) imdata = Imlib_init(dpy); for(i=0; i < 720; i++) { XFillRectangle(dpy,can_xid,b_gc,0,0,wd,ht); XUDRdraw_clock(dpy,can_xid,w_gc,(wd/2),(ht/2),(ht/2)-2,now + (i * 60),0); XFlush(dpy); sprintf(pathname,"CLK_%03d.png",i); image = Imlib_create_image_from_drawable (imdata, can_xid, can_xid, 0, 0, wd, ht); Imlib_save_image (imdata, image, pathname, NULL); Imlib_destroy_image (imdata, image); /* usleep(100000); */ } }
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 __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 }