6 #include <msp/io/file.h>
7 #include <msp/io/memory.h>
31 Image::Private::Private()
48 void read(png_struct *png, png_byte *data, png_size_t size)
50 IO::Base *in = reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
51 in->read(reinterpret_cast<char *>(data), size);
54 void load_png(IO::Base &in, Image::Private &priv)
62 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
63 info = png_create_info_struct(png);
65 if(setjmp(png_jmpbuf(png)))
66 throw bad_image_data("PNG error");
68 png_set_read_fn(png, &in, read);
69 png_read_info(png, info);
74 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
78 throw unsupported_image_format("depth!=8");
81 case PNG_COLOR_TYPE_PALETTE: priv.fmt = COLOR_INDEX; break;
82 case PNG_COLOR_TYPE_GRAY: priv.fmt = LUMINANCE; break;
83 case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
84 case PNG_COLOR_TYPE_RGB: priv.fmt = RGB; break;
85 case PNG_COLOR_TYPE_RGB_ALPHA: priv.fmt = RGBA; break;
86 default: throw unsupported_image_format("unknown color type");
89 unsigned nchans = png_get_channels(png, info);
90 if(nchans==4 && priv.fmt==RGB)
91 png_set_strip_alpha(png);
93 unsigned rowstride = priv.width*nchans;
94 priv.data = new char[rowstride*priv.height];
95 for(unsigned y=0; y<priv.height; ++y)
96 png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
99 png_destroy_read_struct(&png, &info, 0);
103 png_destroy_read_struct(&png, &info, 0);
111 void ensure_devil_image(unsigned &id)
113 static bool init_done = false;
118 ilEnable(IL_ORIGIN_SET);
119 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
134 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
135 throw runtime_error("no image support");
143 ilDeleteImages(1, &priv->id);
151 void Image::load_file(const string &fn)
154 if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
156 IO::BufferedFile file(fn);
157 load_png(file, *priv);
163 ensure_devil_image(priv->id);
164 ilBindImage(priv->id);
165 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
166 throw bad_image_data("IL error");
168 throw unsupported_image_format("DevIL needed for non-PNG images");
174 void Image::load_memory(const void *data, unsigned size)
177 if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
179 IO::Memory mem(reinterpret_cast<const char *>(data), size);
180 load_png(mem, *priv);
186 ensure_devil_image(priv->id);
187 ilBindImage(priv->id);
188 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
189 throw bad_image_data("IL error");
191 throw unsupported_image_format("DevIL needed for non-PNG images");
198 PixelFormat Image::get_format() const
207 ilBindImage(priv->id);
208 switch(ilGetInteger(IL_IMAGE_FORMAT))
210 case IL_COLOR_INDEX: return COLOR_INDEX;
211 case IL_LUMINANCE: return LUMINANCE;
212 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
213 case IL_RGB: return RGB;
214 case IL_RGBA: return RGBA;
215 case IL_BGR: return BGR;
216 case IL_BGRA: return BGRA;
217 // XXX bad, should throw when loading
218 default: throw invalid_argument("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