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