3 #include <lib/gdi/epng.h>
10 int loadPNG(ePtr<gPixmap> &result, const char *filename)
13 FILE *fp=fopen(filename, "rb");
17 // eDebug("couldn't open %s", filename );
20 if (!fread(header, 8, 1, fp))
22 eDebug("couldn't read");
26 if (png_sig_cmp(header, 0, 8))
31 png_structp png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
38 png_infop info_ptr=png_create_info_struct(png_ptr);
41 eDebug("no info ptr");
42 png_destroy_read_struct(&png_ptr, (png_infopp)0, (png_infopp)0);
46 png_infop end_info = png_create_info_struct(png_ptr);
50 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
54 if (setjmp(png_ptr->jmpbuf))
56 eDebug("das war wohl nix");
57 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
62 png_init_io(png_ptr, fp);
63 png_set_sig_bytes(png_ptr, 8);
64 png_set_invert_alpha(png_ptr);
65 png_read_info(png_ptr, info_ptr);
67 png_uint_32 width, height;
71 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
73 if (color_type == PNG_COLOR_TYPE_GRAY || color_type & PNG_COLOR_MASK_PALETTE)
75 result=new gPixmap(eSize(width, height), bit_depth);
76 gSurface *surface = result->surface;
78 png_bytep *rowptr=new png_bytep[height];
80 for (unsigned int i=0; i<height; i++)
81 rowptr[i]=((png_byte*)(surface->data))+i*surface->stride;
82 png_read_rows(png_ptr, rowptr, 0, height);
86 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE))
90 png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
92 surface->clut.data=new gRGB[num_palette];
95 surface->clut.colors=num_palette;
97 for (int i=0; i<num_palette; i++)
99 surface->clut.data[i].a=0;
100 surface->clut.data[i].r=palette[i].red;
101 surface->clut.data[i].g=palette[i].green;
102 surface->clut.data[i].b=palette[i].blue;
104 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
107 png_get_tRNS(png_ptr, info_ptr, &trans, &num_palette, 0);
108 for (int i=0; i<num_palette; i++)
109 surface->clut.data[i].a=255-trans[i];
113 surface->clut.data=0;
114 surface->clut.colors=0;
116 surface->clut.start=0;
117 png_read_end(png_ptr, end_info);
120 eDebug("%s: %dx%dx%d png, %d", filename, (int)width, (int)height, (int)bit_depth, color_type);
123 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
128 struct my_error_mgr {
129 struct jpeg_error_mgr pub;
130 jmp_buf setjmp_buffer;
133 typedef struct my_error_mgr * my_error_ptr;
136 my_error_exit (j_common_ptr cinfo)
138 my_error_ptr myerr = (my_error_ptr) cinfo->err;
139 (*cinfo->err->output_message) (cinfo);
140 longjmp(myerr->setjmp_buffer, 1);
143 int loadJPG(ePtr<gPixmap> &result, const char *filename, ePtr<gPixmap> alpha)
145 struct jpeg_decompress_struct cinfo;
146 struct my_error_mgr jerr;
150 infile = fopen(filename, "rb");
155 if (alpha->surface->bpp != 8)
157 eWarning("alpha channel for jpg must be 8bit");
164 cinfo.err = jpeg_std_error(&jerr.pub);
165 jerr.pub.error_exit = my_error_exit;
166 if (setjmp(jerr.setjmp_buffer)) {
168 jpeg_destroy_decompress(&cinfo);
172 jpeg_create_decompress(&cinfo);
173 jpeg_stdio_src(&cinfo, infile);
174 (void) jpeg_read_header(&cinfo, TRUE);
175 cinfo.out_color_space = JCS_RGB;
176 cinfo.scale_denom = 1;
178 (void) jpeg_start_decompress(&cinfo);
180 int grayscale = cinfo.output_components == 1;
184 if (((int)cinfo.output_width != alpha->surface->x) || ((int)cinfo.output_height != alpha->surface->y))
186 eWarning("alpha channel size (%dx%d) must match jpeg size (%dx%d)", alpha->surface->x, alpha->surface->y, cinfo.output_width, cinfo.output_height);
191 eWarning("we don't support grayscale + alpha at the moment");
196 result = new gPixmap(eSize(cinfo.output_width, cinfo.output_height), grayscale ? 8 : 32);
198 row_stride = cinfo.output_width * cinfo.output_components;
199 buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
200 while (cinfo.output_scanline < cinfo.output_height) {
201 int y = cinfo.output_scanline;
202 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
203 unsigned char *dst = ((unsigned char*)result->surface->data) + result->surface->stride * y;
204 unsigned char *src = (unsigned char*)buffer[0];
205 unsigned char *palpha = alpha ? ((unsigned char*)alpha->surface->data + alpha->surface->stride * y) : 0;
207 memcpy(dst, src, cinfo.output_width);
211 for (x = 0; x < (int)cinfo.output_width; ++x)
224 (void) jpeg_finish_decompress(&cinfo);
225 jpeg_destroy_decompress(&cinfo);
230 int savePNG(const char *filename, gPixmap *pixmap)
233 eDebug("\33[33m %s \33[0m",filename);
234 FILE *fp=fopen(filename, "wb");
238 gSurface *surface = pixmap->surface;
242 png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
245 eDebug("write png, couldnt allocate write struct");
250 png_infop info_ptr=png_create_info_struct(png_ptr);
254 png_destroy_write_struct(&png_ptr, 0);
260 png_set_IHDR(png_ptr, info_ptr, surface->x, surface->y, surface->bpp/surface->bypp,
261 PNG_COLOR_TYPE_RGB_ALPHA,
262 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
264 if (setjmp(png_ptr->jmpbuf))
267 png_destroy_write_struct(&png_ptr, &info_ptr);
272 png_init_io(png_ptr, fp);
273 png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
274 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
276 png_write_info(png_ptr, info_ptr);
277 png_set_packing(png_ptr);
279 png_byte *row_pointer;
280 png_byte *cr = new png_byte[surface->y * surface->stride];
283 printf("Error: malloc\n");
286 for (int i=0; i<surface->y; ++i)
288 row_pointer=((png_byte*)surface->data)+i*surface->stride;
289 if (surface->bypp == 4)
291 memcpy(cr, row_pointer, surface->stride);
292 for (int j=0; j<surface->stride; j+=4)
294 unsigned char tmp = cr[j];
298 png_write_row(png_ptr, cr);
301 png_write_row(png_ptr, row_pointer);
305 png_write_end(png_ptr, info_ptr);
306 png_destroy_write_struct(&png_ptr, &info_ptr);
308 eDebug("wrote png ! fine !");