6 #include <msp/io/file.h>
7 #include <msp/io/memory.h>
9 #include <msp/core/except.h>
32 Image::Private::Private()
49 void read(png_struct *png, png_byte *data, png_size_t size)
51 IO::Base *in = reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
52 in->read(reinterpret_cast<char *>(data), size);
55 void load_png(IO::Base &in, Image::Private &priv)
63 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
64 info = png_create_info_struct(png);
66 if(setjmp(png_jmpbuf(png)))
67 throw Exception("Error loading PNG image");
69 png_set_read_fn(png, &in, read);
70 png_read_info(png, info);
75 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
79 throw Exception("Only 8-bit PNG images are supported");
82 case PNG_COLOR_TYPE_PALETTE: priv.fmt = COLOR_INDEX; break;
83 case PNG_COLOR_TYPE_GRAY: priv.fmt = LUMINANCE; break;
84 case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
85 case PNG_COLOR_TYPE_RGB: priv.fmt = RGB; break;
86 case PNG_COLOR_TYPE_RGB_ALPHA: priv.fmt = RGBA; break;
87 default: throw Exception("Unknown color type");
90 unsigned nchans = png_get_channels(png, info);
91 if(nchans==4 && priv.fmt==RGB)
92 png_set_strip_alpha(png);
94 unsigned rowstride = priv.width*nchans;
95 priv.data = new char[rowstride*priv.height];
96 for(unsigned y=0; y<priv.height; ++y)
97 png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
100 png_destroy_read_struct(&png, &info, 0);
104 png_destroy_read_struct(&png, &info, 0);
112 void ensure_devil_image(unsigned &id)
114 static bool init_done = false;
119 ilEnable(IL_ORIGIN_SET);
120 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
135 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
136 throw Exception("Image needs either DevIL or libpng support");
144 ilDeleteImages(1, &priv->id);
152 void Image::load_file(const string &fn)
155 if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
157 IO::BufferedFile file(fn);
158 load_png(file, *priv);
164 ensure_devil_image(priv->id);
165 ilBindImage(priv->id);
166 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
167 throw Exception("Error loading image "+fn);
169 throw Exception("Not a PNG image and DevIL support not compiled in");
175 void Image::load_memory(const void *data, unsigned size)
178 if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
180 IO::Memory mem(reinterpret_cast<const char *>(data), size);
181 load_png(mem, *priv);
187 ensure_devil_image(priv->id);
188 ilBindImage(priv->id);
189 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
190 throw Exception("Error loading image from memory");
192 throw Exception("Not a PNG image and DevIL support not compiled in");
199 PixelFormat Image::get_format() const
208 ilBindImage(priv->id);
209 switch(ilGetInteger(IL_IMAGE_FORMAT))
211 case IL_COLOR_INDEX: return COLOR_INDEX;
212 case IL_LUMINANCE: return LUMINANCE;
213 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
214 case IL_RGB: return RGB;
215 case IL_RGBA: return RGBA;
216 case IL_BGR: return BGR;
217 case IL_BGRA: return BGRA;
218 default: throw InvalidParameterValue("Unknown pixel format in image");
225 unsigned Image::get_width() const
234 ilBindImage(priv->id);
235 return ilGetInteger(IL_IMAGE_WIDTH);
241 unsigned Image::get_height() const
250 ilBindImage(priv->id);
251 return ilGetInteger(IL_IMAGE_HEIGHT);
257 const void *Image::get_data() const
266 ilBindImage(priv->id);
273 } // namespace Graphics