]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image_png.cpp
Separate PNG loading to a separate file
[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 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 }
15
16
17 namespace Msp {
18 namespace Graphics {
19
20 bool is_png(const void *buf, unsigned len)
21 {
22         return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(buf)), 0, len);
23 }
24
25 void load_png(IO::Base &in, Image::Private &priv)
26 {
27         png_struct *png = 0;
28         png_info *info = 0;
29         priv.data = 0;
30
31         try
32         {
33                 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
34                 info = png_create_info_struct(png);
35
36                 if(setjmp(png_jmpbuf(png)))
37                         throw bad_image_data("PNG error");
38
39                 png_set_read_fn(png, &in, read);
40                 png_read_info(png, info);
41                 png_uint_32 width;
42                 png_uint_32 height;
43                 int depth;
44                 int color;
45                 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
46                 priv.width = width;
47                 priv.height = height;
48                 if(depth!=8)
49                         throw unsupported_image_format("depth!=8");
50                 switch(color)
51                 {
52                 case PNG_COLOR_TYPE_PALETTE:    priv.fmt = COLOR_INDEX; break;
53                 case PNG_COLOR_TYPE_GRAY:       priv.fmt = LUMINANCE; break;
54                 case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
55                 case PNG_COLOR_TYPE_RGB:        priv.fmt = RGB; break;
56                 case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt = RGBA; break;
57                 default: throw unsupported_image_format("unknown color type");
58                 }
59
60                 unsigned nchans = png_get_channels(png, info);
61                 if(nchans==4 && priv.fmt==RGB)
62                         png_set_strip_alpha(png);
63
64                 unsigned rowstride = priv.width*nchans;
65                 priv.data = new char[rowstride*priv.height];
66                 for(unsigned y=0; y<priv.height; ++y)
67                         png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
68
69                 png_read_end(png, 0);
70                 png_destroy_read_struct(&png, &info, 0);
71         }
72         catch(...)
73         {
74                 png_destroy_read_struct(&png, &info, 0);
75                 delete[] priv.data;
76                 throw;
77         }
78 }
79
80 } // namespace Graphics
81 } // namespace Msp
82 #endif