]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/png/pngloader.cpp
Add some state checking to ImageLoader
[libs/gui.git] / source / graphics / png / pngloader.cpp
1 #include <png.h>
2 #include "pngloader.h"
3
4 using namespace std;
5
6 namespace {
7
8 void read(png_struct *png, png_byte *data, png_size_t size)
9 {
10         Msp::IO::Base *in = reinterpret_cast<Msp::IO::Base *>(png_get_io_ptr(png));
11         in->read(reinterpret_cast<char *>(data), size);
12 }
13
14 void error(png_struct *png, const char *msg)
15 {
16         string *message = reinterpret_cast<string *>(png_get_error_ptr(png));
17         *message = msg;
18         longjmp(png_jmpbuf(png), 1);
19 }
20
21 } // namespace
22
23
24 namespace Msp {
25 namespace Graphics {
26
27 struct PngLoader::Private
28 {
29         std::string message;
30         png_struct *png;
31         png_info *info;
32 };
33
34
35 PngLoader::PngLoader(IO::Base &io, unsigned sig_bytes):
36         priv(new Private)
37 {
38         priv->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, &priv->message, error, 0);
39         priv->info = png_create_info_struct(priv->png);
40
41         // These probably won't give any errors
42         png_set_read_fn(priv->png, &io, read);
43         png_set_sig_bytes(priv->png, sig_bytes);
44 }
45
46 PngLoader::~PngLoader()
47 {
48         png_destroy_read_struct(&priv->png, &priv->info, 0);
49         delete priv;
50 }
51
52 bool PngLoader::detect(const std::string &sig)
53 {
54         return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<char*>(sig.data())), 0, sig.size());
55 }
56
57 void PngLoader::load_(Image::Data &data)
58 {
59         png_byte **rows = 0;
60
61         if(setjmp(png_jmpbuf(priv->png)))
62         {
63                 delete[] rows;
64                 throw bad_image_data(priv->message);
65         }
66
67         png_read_info(priv->png, priv->info);
68         png_uint_32 width;
69         png_uint_32 height;
70         int depth;
71         int color;
72         int interlace;
73         png_get_IHDR(priv->png, priv->info, &width, &height, &depth, &color, &interlace, 0, 0);
74         unsigned nchans = png_get_channels(priv->png, priv->info);
75
76         if(depth!=8)
77                 throw unsupported_image_format("depth!=8");
78
79         data.width = width;
80         data.height = height;
81         data.stride = data.width*nchans;
82
83         switch(color)
84         {
85         case PNG_COLOR_TYPE_PALETTE:    data.fmt = COLOR_INDEX; break;
86         case PNG_COLOR_TYPE_GRAY:       data.fmt = LUMINANCE; break;
87         case PNG_COLOR_TYPE_GRAY_ALPHA: data.fmt = LUMINANCE_ALPHA; break;
88         case PNG_COLOR_TYPE_RGB:        data.fmt = (nchans==4 ? RGBX : RGB); break;
89         case PNG_COLOR_TYPE_RGB_ALPHA:  data.fmt = RGBA; break;
90         default: throw unsupported_image_format("unknown color type");
91         }
92
93         data.pixels = new char[data.stride*data.height];
94
95         if(interlace==PNG_INTERLACE_ADAM7)
96         {
97                 // ADAM7 requires all rows to be loaded at once
98                 unsigned n_passes = png_set_interlace_handling(priv->png);
99                 rows = new png_byte *[data.height];
100                 for(unsigned y=0; y<data.height; ++y)
101                         rows[y] = reinterpret_cast<png_byte *>(data.pixels+data.stride*(data.height-1-y));
102
103                 for(unsigned i=0; i<n_passes; ++i)
104                         png_read_rows(priv->png, rows, 0, data.height);
105
106                 delete[] rows;
107                 rows = 0;
108         }
109         else
110         {
111                 for(unsigned y=0; y<data.height; ++y)
112                         png_read_row(priv->png, reinterpret_cast<png_byte *>(data.pixels+data.stride*(data.height-1-y)), 0);
113         }
114
115         png_read_end(priv->png, 0);
116 }
117
118 } // namespace Graphics
119 } // namespace Msp