4 #include <msp/io/file.h>
5 #include <msp/io/memory.h>
8 #include "image_private.h"
15 Image::Private::Private()
32 void ensure_devil_image(unsigned &id)
34 static bool init_done = false;
39 ilEnable(IL_ORIGIN_SET);
40 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
55 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
56 throw runtime_error("no image support");
64 ilDeleteImages(1, &priv->id);
72 void Image::load_file(const string &fn)
75 if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
77 IO::BufferedFile file(fn);
78 load_png(file, *priv);
84 ensure_devil_image(priv->id);
85 ilBindImage(priv->id);
86 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
87 throw bad_image_data("IL error");
89 throw unsupported_image_format("DevIL needed for non-PNG images");
95 void Image::load_memory(const void *data, unsigned size)
98 if(size>=8 && is_png(data, 8))
100 IO::Memory mem(reinterpret_cast<const char *>(data), size);
101 load_png(mem, *priv);
107 ensure_devil_image(priv->id);
108 ilBindImage(priv->id);
109 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
110 throw bad_image_data("IL error");
112 throw unsupported_image_format("DevIL needed for non-PNG images");
119 PixelFormat Image::get_format() const
128 ilBindImage(priv->id);
129 switch(ilGetInteger(IL_IMAGE_FORMAT))
131 case IL_COLOR_INDEX: return COLOR_INDEX;
132 case IL_LUMINANCE: return LUMINANCE;
133 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
134 case IL_RGB: return RGB;
135 case IL_RGBA: return RGBA;
136 case IL_BGR: return BGR;
137 case IL_BGRA: return BGRA;
138 // XXX bad, should throw when loading
139 default: throw invalid_argument("unknown pixel format in image");
146 unsigned Image::get_width() const
155 ilBindImage(priv->id);
156 return ilGetInteger(IL_IMAGE_WIDTH);
162 unsigned Image::get_height() const
171 ilBindImage(priv->id);
172 return ilGetInteger(IL_IMAGE_HEIGHT);
178 const void *Image::get_data() const
187 ilBindImage(priv->id);
194 } // namespace Graphics