]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image_png.cpp
Implement graphical reporting for uncaught exceptions
[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 char *buf, unsigned len)
35 {
36         return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<char*>(buf)), 0, len);
37 }
38
39 void load_png(IO::Base &in, Image::Private &priv, const char *, unsigned sig_len)
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_set_sig_bytes(png, sig_len);
56                 png_read_info(png, info);
57                 png_uint_32 width;
58                 png_uint_32 height;
59                 int depth;
60                 int color;
61                 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
62                 priv.width = width;
63                 priv.height = height;
64                 if(depth!=8)
65                         throw unsupported_image_format("depth!=8");
66                 switch(color)
67                 {
68                 case PNG_COLOR_TYPE_PALETTE:    priv.fmt = COLOR_INDEX; break;
69                 case PNG_COLOR_TYPE_GRAY:       priv.fmt = LUMINANCE; break;
70                 case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
71                 case PNG_COLOR_TYPE_RGB:        priv.fmt = RGB; break;
72                 case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt = RGBA; break;
73                 default: throw unsupported_image_format("unknown color type");
74                 }
75
76                 unsigned nchans = png_get_channels(png, info);
77                 if(nchans==4 && priv.fmt==RGB)
78                         png_set_strip_alpha(png);
79
80                 unsigned rowstride = priv.width*nchans;
81                 priv.data = new char[rowstride*priv.height];
82                 for(unsigned y=0; y<priv.height; ++y)
83                         png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
84
85                 png_read_end(png, 0);
86                 png_destroy_read_struct(&png, &info, 0);
87         }
88         catch(...)
89         {
90                 png_destroy_read_struct(&png, &info, 0);
91                 delete[] priv.data;
92                 throw;
93         }
94 }
95
96 } // namespace Graphics
97 } // namespace Msp
98 #endif