]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image_png.cpp
f2247863dac57d8f2636af3eaadeb5dc4a8f6edd
[libs/gui.git] / source / graphics / image_png.cpp
1 #ifdef WITH_LIBPNG
2 #include <png.h>
3 #include "image_png.h"
4 #include "image_private.h"
5
6 using namespace std;
7
8 namespace {
9
10 struct ErrorInfo
11 {
12         string msg;
13 };
14
15 void read(png_struct *png, png_byte *data, png_size_t size)
16 {
17         Msp::IO::Base *in = reinterpret_cast<Msp::IO::Base *>(png_get_io_ptr(png));
18         in->read(reinterpret_cast<char *>(data), size);
19 }
20
21 void error(png_struct *png, const char *msg)
22 {
23         ErrorInfo *error_info = reinterpret_cast<ErrorInfo *>(png_get_error_ptr(png));
24         error_info->msg = msg;
25         longjmp(png_jmpbuf(png), 1);
26 }
27
28 }
29
30
31 namespace Msp {
32 namespace Graphics {
33
34 bool is_png(const void *buf, unsigned len)
35 {
36         return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(buf)), 0, len);
37 }
38
39 void load_png(IO::Base &in, Image::Private &priv)
40 {
41         png_struct *png = 0;
42         png_info *info = 0;
43         priv.data = 0;
44
45         try
46         {
47                 ErrorInfo error_info;
48                 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, &error_info, error, 0);
49                 info = png_create_info_struct(png);
50
51                 if(setjmp(png_jmpbuf(png)))
52                         throw bad_image_data(error_info.msg);
53
54                 png_set_read_fn(png, &in, read);
55                 png_read_info(png, info);
56                 png_uint_32 width;
57                 png_uint_32 height;
58                 int depth;
59                 int color;
60                 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
61                 priv.width = width;
62                 priv.height = height;
63                 if(depth!=8)
64                         throw unsupported_image_format("depth!=8");
65                 switch(color)
66                 {
67                 case PNG_COLOR_TYPE_PALETTE:    priv.fmt = COLOR_INDEX; break;
68                 case PNG_COLOR_TYPE_GRAY:       priv.fmt = LUMINANCE; break;
69                 case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
70                 case PNG_COLOR_TYPE_RGB:        priv.fmt = RGB; break;
71                 case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt = RGBA; break;
72                 default: throw unsupported_image_format("unknown color type");
73                 }
74
75                 unsigned nchans = png_get_channels(png, info);
76                 if(nchans==4 && priv.fmt==RGB)
77                         png_set_strip_alpha(png);
78
79                 unsigned rowstride = priv.width*nchans;
80                 priv.data = new char[rowstride*priv.height];
81                 for(unsigned y=0; y<priv.height; ++y)
82                         png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
83
84                 png_read_end(png, 0);
85                 png_destroy_read_struct(&png, &info, 0);
86         }
87         catch(...)
88         {
89                 png_destroy_read_struct(&png, &info, 0);
90                 delete[] priv.data;
91                 throw;
92         }
93 }
94
95 } // namespace Graphics
96 } // namespace Msp
97 #endif