1 #define PNG_SKIP_SETJMP_CHECK
4 #include <lib/gdi/epng.h>
11 int loadPNG(ePtr<gPixmap> &result, const char *filename)
14 FILE *fp=fopen(filename, "rb");
18 // eDebug("couldn't open %s", filename );
21 if (!fread(header, 8, 1, fp))
23 eDebug("couldn't read");
27 if (png_sig_cmp(header, 0, 8))
32 png_structp png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
39 png_infop info_ptr=png_create_info_struct(png_ptr);
42 eDebug("no info ptr");
43 png_destroy_read_struct(&png_ptr, (png_infopp)0, (png_infopp)0);
47 png_infop end_info = png_create_info_struct(png_ptr);
51 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
55 if (setjmp(png_ptr->jmpbuf))
57 eDebug("das war wohl nix");
58 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
63 png_init_io(png_ptr, fp);
64 png_set_sig_bytes(png_ptr, 8);
65 png_set_invert_alpha(png_ptr);
66 png_read_info(png_ptr, info_ptr);
68 png_uint_32 width, height;
72 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
74 if (color_type == PNG_COLOR_TYPE_GRAY || color_type & PNG_COLOR_MASK_PALETTE)
76 result=new gPixmap(eSize(width, height), bit_depth);
77 gSurface *surface = result->surface;
79 png_bytep *rowptr=new png_bytep[height];
81 for (unsigned int i=0; i<height; i++)
82 rowptr[i]=((png_byte*)(surface->data))+i*surface->stride;
83 png_read_rows(png_ptr, rowptr, 0, height);
87 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE))
91 png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
93 surface->clut.data=new gRGB[num_palette];
96 surface->clut.colors=num_palette;
98 for (int i=0; i<num_palette; i++)
100 surface->clut.data[i].a=0;
101 surface->clut.data[i].r=palette[i].red;
102 surface->clut.data[i].g=palette[i].green;
103 surface->clut.data[i].b=palette[i].blue;
105 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
108 png_get_tRNS(png_ptr, info_ptr, &trans, &num_palette, 0);
109 for (int i=0; i<num_palette; i++)
110 surface->clut.data[i].a=255-trans[i];
114 surface->clut.data=0;
115 surface->clut.colors=0;
117 surface->clut.start=0;
118 png_read_end(png_ptr, end_info);
121 eDebug("%s: %dx%dx%d png, %d", filename, (int)width, (int)height, (int)bit_depth, color_type);
124 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
129 struct my_error_mgr {
130 struct jpeg_error_mgr pub;
131 jmp_buf setjmp_buffer;
134 typedef struct my_error_mgr * my_error_ptr;
137 my_error_exit (j_common_ptr cinfo)
139 my_error_ptr myerr = (my_error_ptr) cinfo->err;
140 (*cinfo->err->output_message) (cinfo);
141 longjmp(myerr->setjmp_buffer, 1);
144 int loadJPG(ePtr<gPixmap> &result, const char *filename, ePtr<gPixmap> alpha)
146 struct jpeg_decompress_struct cinfo;
147 struct my_error_mgr jerr;
151 infile = fopen(filename, "rb");
156 if (alpha->surface->bpp != 8)
158 eWarning("alpha channel for jpg must be 8bit");
165 cinfo.err = jpeg_std_error(&jerr.pub);
166 jerr.pub.error_exit = my_error_exit;
167 if (setjmp(jerr.setjmp_buffer)) {
169 jpeg_destroy_decompress(&cinfo);
173 jpeg_create_decompress(&cinfo);
174 jpeg_stdio_src(&cinfo, infile);
175 (void) jpeg_read_header(&cinfo, TRUE);
176 cinfo.out_color_space = JCS_RGB;
177 cinfo.scale_denom = 1;
179 (void) jpeg_start_decompress(&cinfo);
181 int grayscale = cinfo.output_components == 1;
185 if (((int)cinfo.output_width != alpha->surface->x) || ((int)cinfo.output_height != alpha->surface->y))
187 eWarning("alpha channel size (%dx%d) must match jpeg size (%dx%d)", alpha->surface->x, alpha->surface->y, cinfo.output_width, cinfo.output_height);
192 eWarning("we don't support grayscale + alpha at the moment");
197 result = new gPixmap(eSize(cinfo.output_width, cinfo.output_height), grayscale ? 8 : 32);
199 row_stride = cinfo.output_width * cinfo.output_components;
200 buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
201 while (cinfo.output_scanline < cinfo.output_height) {
202 int y = cinfo.output_scanline;
203 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
204 unsigned char *dst = ((unsigned char*)result->surface->data) + result->surface->stride * y;
205 unsigned char *src = (unsigned char*)buffer[0];
206 unsigned char *palpha = alpha ? ((unsigned char*)alpha->surface->data + alpha->surface->stride * y) : 0;
208 memcpy(dst, src, cinfo.output_width);
212 for (x = 0; x < (int)cinfo.output_width; ++x)
225 (void) jpeg_finish_decompress(&cinfo);
226 jpeg_destroy_decompress(&cinfo);
231 int savePNG(const char *filename, gPixmap *pixmap)
234 eDebug("\33[33m %s \33[0m",filename);
235 FILE *fp=fopen(filename, "wb");
239 gSurface *surface = pixmap->surface;
243 png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
246 eDebug("write png, couldnt allocate write struct");
251 png_infop info_ptr=png_create_info_struct(png_ptr);
255 png_destroy_write_struct(&png_ptr, 0);
261 png_set_IHDR(png_ptr, info_ptr, surface->x, surface->y, surface->bpp/surface->bypp,
262 PNG_COLOR_TYPE_RGB_ALPHA,
263 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
265 if (setjmp(png_ptr->jmpbuf))
268 png_destroy_write_struct(&png_ptr, &info_ptr);
273 png_init_io(png_ptr, fp);
274 png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
275 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
277 png_write_info(png_ptr, info_ptr);
278 png_set_packing(png_ptr);
280 png_byte *row_pointer;
281 png_byte *cr = new png_byte[surface->y * surface->stride];
284 printf("Error: malloc\n");
287 for (int i=0; i<surface->y; ++i)
289 row_pointer=((png_byte*)surface->data)+i*surface->stride;
290 if (surface->bypp == 4)
292 memcpy(cr, row_pointer, surface->stride);
293 for (int j=0; j<surface->stride; j+=4)
295 unsigned char tmp = cr[j];
299 png_write_row(png_ptr, cr);
302 png_write_row(png_ptr, row_pointer);
306 png_write_end(png_ptr, info_ptr);
307 png_destroy_write_struct(&png_ptr, &info_ptr);
309 eDebug("wrote png ! fine !");