4 #include "image_private.h"
8 void read(png_struct *png, png_byte *data, png_size_t size)
10 Msp::IO::Base *in = reinterpret_cast<Msp::IO::Base *>(png_get_io_ptr(png));
11 in->read(reinterpret_cast<char *>(data), size);
20 bool is_png(const void *buf, unsigned len)
22 return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(buf)), 0, len);
25 void load_png(IO::Base &in, Image::Private &priv)
33 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
34 info = png_create_info_struct(png);
36 if(setjmp(png_jmpbuf(png)))
37 throw bad_image_data("PNG error");
39 png_set_read_fn(png, &in, read);
40 png_read_info(png, info);
45 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
49 throw unsupported_image_format("depth!=8");
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");
60 unsigned nchans = png_get_channels(png, info);
61 if(nchans==4 && priv.fmt==RGB)
62 png_set_strip_alpha(png);
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);
70 png_destroy_read_struct(&png, &info, 0);
74 png_destroy_read_struct(&png, &info, 0);
80 } // namespace Graphics