remove old comment, fix returncodes on signal
[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                 surface->clut.start=0;
115                 png_read_end(png_ptr, end_info);
116         } else
117                 result=0;
118
119         png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
120         fclose(fp);
121         return 0;
122 }
123
124 int savePNG(const char *filename, gPixmap *pixmap)
125 {
126         FILE *fp=fopen(filename, "wb");
127         if (!fp)
128                 return -1;
129         
130         gSurface *surface = pixmap->surface;
131         if (!surface)
132                 return -2;
133         
134         png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
135         if (!png_ptr)
136         {
137                 eDebug("write png, couldnt allocate write struct");
138                 fclose(fp);
139                 unlink(filename);
140                 return -2;
141         }
142         png_infop info_ptr=png_create_info_struct(png_ptr);
143         if (!info_ptr)
144         {
145                 eDebug("info");
146                 png_destroy_write_struct(&png_ptr, 0);
147                 fclose(fp);
148                 unlink(filename);
149                 return -3;
150         }
151         if (setjmp(png_ptr->jmpbuf))
152         {
153                 eDebug("error :/");
154                 png_destroy_write_struct(&png_ptr, &info_ptr);
155                 fclose(fp);
156                 unlink(filename);
157                 return -4;
158         }
159         png_init_io(png_ptr, fp);
160         png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
161         png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
162
163         png_set_IHDR(png_ptr, info_ptr, surface->x, surface->y, surface->bpp, 
164                 surface->clut.data ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY, 
165                 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
166         if (surface->clut.data)
167         {
168                 png_color palette[surface->clut.colors];
169                 png_byte trans[surface->clut.colors];
170                 for (int i=0; i<surface->clut.colors; ++i)
171                 {
172                         palette[i].red=surface->clut.data[i].r;
173                         palette[i].green=surface->clut.data[i].g;
174                         palette[i].blue=surface->clut.data[i].b;
175                         trans[i]=255-surface->clut.data[i].a;
176                 }
177                 png_set_PLTE(png_ptr, info_ptr, palette, surface->clut.colors);
178                 png_set_tRNS(png_ptr, info_ptr, trans, surface->clut.colors, 0);
179         }
180         png_write_info(png_ptr, info_ptr);
181         png_set_packing(png_ptr);
182         png_byte *row_pointers[surface->y];
183         for (int i=0; i<surface->y; ++i)
184                 row_pointers[i]=((png_byte*)surface->data)+i*surface->stride;
185         png_write_image(png_ptr, row_pointers);
186         png_write_end(png_ptr, info_ptr);
187         png_destroy_write_struct(&png_ptr, &info_ptr);
188         fclose(fp);
189         eDebug("wrote png ! fine !");
190         return 0;
191 }