void image_drawme(t_image *x, t_glist *glist, int firsttime) { if (firsttime) { if (x->x_image == &s_) // if we have a blank image name, use the included filler { x->x_image = gensym("::moonlib::image::noimage"); x->x_type = 1; pd_error(x, "[image]: no image found"); } if(x->x_type) { sys_vgui(".x%lx.c create image %d %d -tags %xS\n", glist_getcanvas(glist), text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x); sys_vgui(".x%lx.c itemconfigure %xS -image %s\n", glist_getcanvas(glist),x,x->x_image->s_name); } else { const char *fname=image_get_filename(x,x->x_image->s_name); if(!x->x_localimage) { sys_vgui("image create photo img%x\n",x); x->x_localimage=1; } if(fname) sys_vgui("::moonlib::image::configure .x%lx img%x {%s}\n",x,x,fname); sys_vgui(".x%lx.c create image %d %d -image img%x -tags %xS\n", glist_getcanvas(glist), text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x,x); } /* TODO callback from gui sys_vgui("image_size logo"); */ } else { sys_vgui(".x%lx.c coords %xS \ %d %d\n", glist_getcanvas(glist), x, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)); } }
t_image *img_read_png(const char *filename) { FILE *file ; png_byte magic[8]; file = fopen(filename,"rb"); if (!file) { printf("couldn't read %s\n",filename); return NULL; } size_t r = fread(magic,1,sizeof(magic),file); if(r != sizeof(magic)) printf("read error\n"); if (!png_check_sig(magic,sizeof(magic))) { printf("no magic\n"); return NULL; } png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); if (!png_ptr) { fclose(file); return NULL; } png_infop info_ptr = png_create_info_struct(png_ptr); if(!info_ptr) { fclose(file); png_destroy_read_struct(&png_ptr,NULL,NULL); return NULL; } png_init_io(png_ptr,file); png_set_sig_bytes(png_ptr,sizeof(magic)); png_read_info(png_ptr,info_ptr); int bit_depth,color_type; bit_depth = png_get_bit_depth(png_ptr,info_ptr); color_type = png_get_color_type(png_ptr,info_ptr); if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_gray_1_2_4_to_8 (png_ptr); if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha (png_ptr); if (bit_depth == 16) png_set_strip_16 (png_ptr); else if (bit_depth < 8) png_set_packing (png_ptr); png_read_update_info(png_ptr,info_ptr); png_uint_32 w, h; png_get_IHDR (png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, NULL, NULL, NULL); char name_raw[_NAME_]; image_get_filename( filename, name_raw); t_image *image = image_new( name_raw); image->width=w; image->height=h; image->file_type = IMG_PNG; image->data_type = IMG_BYTE; switch (color_type) { case PNG_COLOR_TYPE_GRAY: image->color_type = IMG_GRAYSCALE; image->alpha = 0; image->bpp = 1; break; case PNG_COLOR_TYPE_GRAY_ALPHA: image->color_type = IMG_GRAYSCALE; image->alpha = 1; image->bpp = 2; break; case PNG_COLOR_TYPE_RGB: image->color_type = IMG_RGB; image->alpha = 0; image->bpp = 3; break; case PNG_COLOR_TYPE_RGB_ALPHA: image->color_type = IMG_RGB; image->alpha = 1; image->bpp = 4; break; default: break; } image->vlst = vlst_make( "vlst", dt_uchar, image->width * image->height, image->bpp, NULL); png_bytep *row_pointers; row_pointers = (png_bytep *)mem_malloc (sizeof (png_bytep) * image->height); int i; for (i = 0; i < image->height; ++i) { row_pointers[i] = (png_bytep)(image->vlst->data + ((image->height - (i + 1)) * image->width * image->bpp)); } png_read_image (png_ptr, row_pointers); free (row_pointers); png_read_end (png_ptr, NULL); png_destroy_read_struct (&png_ptr, &info_ptr, NULL); fclose (file); return image; }