- sdl is now default output
[enigma2.git] / lib / gdi / epng.cpp
1 #include <png.h>
2 #include <stdio.h>
3 #include <lib/gdi/epng.h>
4 #include <unistd.h>
5
6 int loadPNG(ePtr<gPixmap> &result, const char *filename)
7 {
8         __u8 header[8];
9         FILE *fp=fopen(filename, "rb");
10         
11         if (!fp)
12         {
13 //              eDebug("couldn't open %s", filename );
14                 return 0;
15         }
16         if (!fread(header, 8, 1, fp))
17         {
18                 eDebug("couldn't read");
19                 fclose(fp);
20                 return 0;
21         }
22         if (png_sig_cmp(header, 0, 8))
23         {
24                 fclose(fp);
25                 return 0;
26         }
27         png_structp png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
28         if (!png_ptr)
29         {
30                 eDebug("no pngptr");
31                 fclose(fp);
32                 return 0;
33         }
34         png_infop info_ptr=png_create_info_struct(png_ptr);
35         if (!info_ptr)
36         {
37                 eDebug("no info ptr");
38                 png_destroy_read_struct(&png_ptr, (png_infopp)0, (png_infopp)0);
39                 fclose(fp);
40                 return 0;
41         }
42         png_infop end_info = png_create_info_struct(png_ptr);
43         if (!end_info)
44         {
45                 eDebug("no end");
46                 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
47                 fclose(fp);
48                 return 0;
49          }
50         if (setjmp(png_ptr->jmpbuf))
51         {
52                 eDebug("das war wohl nix");
53                 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
54                 fclose(fp);
55                 result = 0;
56                 return 0;
57         }
58         png_init_io(png_ptr, fp);
59         png_set_sig_bytes(png_ptr, 8);
60         png_set_invert_alpha(png_ptr);
61         png_read_info(png_ptr, info_ptr);
62         
63         png_uint_32 width, height;
64         int bit_depth;
65         int color_type;
66         
67         png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
68         
69         eDebug("%s: %dx%dx%d png, %d", filename, (int)width, (int)height, (int)bit_depth, color_type);
70         
71         if (color_type != 6)
72         {
73                 result=new gPixmap(eSize(width, height), bit_depth);
74                 gSurface *surface = result->surface;
75         
76                 png_bytep *rowptr=new png_bytep[height];
77         
78                 for (unsigned int i=0; i<height; i++)
79                         rowptr[i]=((png_byte*)(surface->data))+i*surface->stride;
80                 png_read_rows(png_ptr, rowptr, 0, height);
81         
82                 delete [] rowptr;
83         
84                 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE))
85                 {
86                         png_color *palette;
87                         int num_palette;
88                         png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
89                         if (num_palette)
90                                 surface->clut.data=new gRGB[num_palette];
91                         else
92                                 surface->clut.data=0;
93                         surface->clut.colors=num_palette;
94                         
95                         for (int i=0; i<num_palette; i++)
96                         {
97                                 surface->clut.data[i].a=0;
98                                 surface->clut.data[i].r=palette[i].red;
99                                 surface->clut.data[i].g=palette[i].green;
100                                 surface->clut.data[i].b=palette[i].blue;
101                         }
102                         if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
103                         {
104                                 png_byte *trans;
105                                 png_get_tRNS(png_ptr, info_ptr, &trans, &num_palette, 0);
106                                 for (int i=0; i<num_palette; i++)
107                                         surface->clut.data[i].a=255-trans[i];
108                         }
109                 } else
110                 {
111                         surface->clut.data=0;
112                         surface->clut.colors=0;
113                 }
114                 png_read_end(png_ptr, end_info);
115         } else
116                 result=0;
117
118         png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
119         fclose(fp);
120         return 0;
121 }
122
123 int savePNG(const char *filename, gPixmap *pixmap)
124 {
125         FILE *fp=fopen(filename, "wb");
126         if (!fp)
127                 return -1;
128         
129         gSurface *surface = pixmap->surface;
130         if (!surface)
131                 return -2;
132         
133         png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
134         if (!png_ptr)
135         {
136                 eDebug("write png, couldnt allocate write struct");
137                 fclose(fp);
138                 unlink(filename);
139                 return -2;
140         }
141         png_infop info_ptr=png_create_info_struct(png_ptr);
142         if (!info_ptr)
143         {
144                 eDebug("info");
145                 png_destroy_write_struct(&png_ptr, 0);
146                 fclose(fp);
147                 unlink(filename);
148                 return -3;
149         }
150         if (setjmp(png_ptr->jmpbuf))
151         {
152                 eDebug("error :/");
153                 png_destroy_write_struct(&png_ptr, &info_ptr);
154                 fclose(fp);
155                 unlink(filename);
156                 return -4;
157         }
158         png_init_io(png_ptr, fp);
159         png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
160         png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
161
162         png_set_IHDR(png_ptr, info_ptr, surface->x, surface->y, surface->bpp, 
163                 surface->clut.data ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY, 
164                 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
165         if (surface->clut.data)
166         {
167                 png_color palette[surface->clut.colors];
168                 png_byte trans[surface->clut.colors];
169                 for (int i=0; i<surface->clut.colors; ++i)
170                 {
171                         palette[i].red=surface->clut.data[i].r;
172                         palette[i].green=surface->clut.data[i].g;
173                         palette[i].blue=surface->clut.data[i].b;
174                         trans[i]=255-surface->clut.data[i].a;
175                 }
176                 png_set_PLTE(png_ptr, info_ptr, palette, surface->clut.colors);
177                 png_set_tRNS(png_ptr, info_ptr, trans, surface->clut.colors, 0);
178         }
179         png_write_info(png_ptr, info_ptr);
180         png_set_packing(png_ptr);
181         png_byte *row_pointers[surface->y];
182         for (int i=0; i<surface->y; ++i)
183                 row_pointers[i]=((png_byte*)surface->data)+i*surface->stride;
184         png_write_image(png_ptr, row_pointers);
185         png_write_end(png_ptr, info_ptr);
186         png_destroy_write_struct(&png_ptr, &info_ptr);
187         fclose(fp);
188         eDebug("wrote png ! fine !");
189         return 0;
190 }