3 This file is part of libmspgbase
4 Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
13 #include <msp/io/file.h>
14 #include <msp/io/memory.h>
16 #include <msp/core/except.h>
39 Image::Private::Private()
56 void read(png_struct *png, png_byte *data, png_size_t size)
58 IO::Base *in=reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
59 in->read(reinterpret_cast<char *>(data), size);
62 void load_png(IO::Base &in, Image::Private &priv)
70 png=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
71 info=png_create_info_struct(png);
73 if(setjmp(png_jmpbuf(png)))
74 throw Exception("Error loading PNG image");
76 png_set_read_fn(png, &in, read);
77 png_read_info(png, info);
82 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
86 throw Exception("Only 8-bit PNG images are supported");
89 case PNG_COLOR_TYPE_PALETTE: priv.fmt=COLOR_INDEX; break;
90 case PNG_COLOR_TYPE_GRAY: priv.fmt=LUMINANCE; break;
91 case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt=LUMINANCE_ALPHA; break;
92 case PNG_COLOR_TYPE_RGB: priv.fmt=RGB; break;
93 case PNG_COLOR_TYPE_RGB_ALPHA: priv.fmt=RGBA; break;
94 default: throw Exception("Unknown color type");
97 unsigned nchans=png_get_channels(png, info);
98 if(nchans==4 && priv.fmt==RGB)
99 png_set_strip_alpha(png);
101 unsigned rowstride=priv.width*nchans;
102 priv.data=new char[rowstride*priv.height];
103 for(unsigned y=0; y<priv.height; ++y)
104 png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
106 png_read_end(png, 0);
107 png_destroy_read_struct(&png, &info, 0);
111 png_destroy_read_struct(&png, &info, 0);
119 void ensure_devil_image(unsigned &id)
121 static bool init_done=false;
126 ilEnable(IL_ORIGIN_SET);
127 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
142 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
143 throw Exception("Image needs either DevIL or libpng support");
151 ilDeleteImages(1, &priv->id);
159 void Image::load_file(const string &fn)
162 if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
164 IO::BufferedFile file(fn);
165 load_png(file, *priv);
171 ensure_devil_image(priv->id);
172 ilBindImage(priv->id);
173 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
174 throw Exception("Error loading image "+fn);
176 throw Exception("Not a PNG image and DevIL support not compiled in");
182 void Image::load_memory(const void *data, unsigned size)
185 if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
187 IO::Memory mem(reinterpret_cast<const char *>(data), size);
188 load_png(mem, *priv);
194 ensure_devil_image(priv->id);
195 ilBindImage(priv->id);
196 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
197 throw Exception("Error loading image from memory");
199 throw Exception("Not a PNG image and DevIL support not compiled in");
206 PixelFormat Image::get_format() const
215 ilBindImage(priv->id);
216 switch(ilGetInteger(IL_IMAGE_FORMAT))
218 case IL_COLOR_INDEX: return COLOR_INDEX;
219 case IL_LUMINANCE: return LUMINANCE;
220 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
221 case IL_RGB: return RGB;
222 case IL_RGBA: return RGBA;
223 case IL_BGR: return BGR;
224 case IL_BGRA: return BGRA;
225 default: throw InvalidParameterValue("Unknown pixel format in image");
232 unsigned Image::get_width() const
241 ilBindImage(priv->id);
242 return ilGetInteger(IL_IMAGE_WIDTH);
248 unsigned Image::get_height() const
257 ilBindImage(priv->id);
258 return ilGetInteger(IL_IMAGE_HEIGHT);
264 const void *Image::get_data() const
273 ilBindImage(priv->id);
280 } // namespace Graphics