]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image.cpp
Consistently label the graphics part as graphics
[libs/gui.git] / source / graphics / image.cpp
1 #ifdef WITH_DEVIL
2 #include <IL/il.h>
3 #endif
4 #ifdef WITH_LIBPNG
5 #include <png.h>
6 #include <msp/io/file.h>
7 #include <msp/io/memory.h>
8 #endif
9 #include "image.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace Graphics {
15
16 struct Image::Private
17 {
18 #ifdef WITH_DEVIL
19         unsigned id;
20 #endif
21 #ifdef WITH_LIBPNG
22         PixelFormat fmt;
23         unsigned width;
24         unsigned height;
25         char *data;
26 #endif
27
28         Private();
29 };
30
31 Image::Private::Private()
32 {
33 #ifdef WITH_DEVIL
34         id = 0;
35 #endif
36 #ifdef WITH_LIBPNG
37         fmt = RGB;
38         width = 0;
39         height = 0;
40         data = 0;
41 #endif
42 }
43
44
45 namespace {
46
47 #ifdef WITH_LIBPNG
48 void read(png_struct *png, png_byte *data, png_size_t size)
49 {
50         IO::Base *in = reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
51         in->read(reinterpret_cast<char *>(data), size);
52 }
53
54 void load_png(IO::Base &in, Image::Private &priv)
55 {
56         png_struct *png = 0;
57         png_info *info = 0;
58         priv.data = 0;
59
60         try
61         {
62                 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
63                 info = png_create_info_struct(png);
64
65                 if(setjmp(png_jmpbuf(png)))
66                         throw bad_image_data("PNG error");
67
68                 png_set_read_fn(png, &in, read);
69                 png_read_info(png, info);
70                 png_uint_32 width;
71                 png_uint_32 height;
72                 int depth;
73                 int color;
74                 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
75                 priv.width = width;
76                 priv.height = height;
77                 if(depth!=8)
78                         throw unsupported_image_format("depth!=8");
79                 switch(color)
80                 {
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");
87                 }
88
89                 unsigned nchans = png_get_channels(png, info);
90                 if(nchans==4 && priv.fmt==RGB)
91                         png_set_strip_alpha(png);
92
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);
97
98                 png_read_end(png, 0);
99                 png_destroy_read_struct(&png, &info, 0);
100         }
101         catch(...)
102         {
103                 png_destroy_read_struct(&png, &info, 0);
104                 delete[] priv.data;
105                 throw;
106         }
107 }
108 #endif
109
110 #ifdef WITH_DEVIL
111 void ensure_devil_image(unsigned &id)
112 {
113         static bool init_done = false;
114
115         if(!init_done)
116         {
117                 ilInit();
118                 ilEnable(IL_ORIGIN_SET);
119                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
120                 init_done = true;
121         }
122
123         if(!id)
124                 ilGenImages(1, &id);
125 }
126 #endif
127
128 }
129
130
131 Image::Image():
132         priv(new Private)
133 {
134 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
135         throw runtime_error("no image support");
136 #endif
137 }
138
139 Image::~Image()
140 {
141 #ifdef WITH_DEVIL
142         if(priv->id)
143                 ilDeleteImages(1, &priv->id);
144 #endif
145 #ifdef WITH_LIBPNG
146         delete[] priv->data;
147 #endif
148         delete priv;
149 }
150
151 void Image::load_file(const string &fn)
152 {
153 #ifdef WITH_LIBPNG
154         if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
155         {
156                 IO::BufferedFile file(fn);
157                 load_png(file, *priv);
158         }
159         else
160 #endif
161         {
162 #ifdef WITH_DEVIL
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");
167 #else
168                 throw unsupported_image_format("DevIL needed for non-PNG images");
169 #endif
170         }
171         (void)fn;
172 }
173
174 void Image::load_memory(const void *data, unsigned size)
175 {
176 #ifdef WITH_LIBPNG
177         if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
178         {
179                 IO::Memory mem(reinterpret_cast<const char *>(data), size);
180                 load_png(mem, *priv);
181         }
182         else
183 #endif
184         {
185 #ifdef WITH_DEVIL
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");
190 #else
191                 throw unsupported_image_format("DevIL needed for non-PNG images");
192 #endif
193         }
194         (void)data;
195         (void)size;
196 }
197
198 PixelFormat Image::get_format() const
199 {
200 #ifdef WITH_LIBPNG
201         if(priv->data)
202                 return priv->fmt;
203 #endif
204 #ifdef WITH_DEVIL
205         if(priv->id)
206         {
207                 ilBindImage(priv->id);
208                 switch(ilGetInteger(IL_IMAGE_FORMAT))
209                 {
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");
219                 }
220         }
221 #endif
222         return RGB;
223 }
224
225 unsigned Image::get_width() const
226 {
227 #ifdef WITH_LIBPNG
228         if(priv->data)
229                 return priv->width;
230 #endif
231 #ifdef WITH_DEVIL
232         if(priv->id)
233         {
234                 ilBindImage(priv->id);
235                 return ilGetInteger(IL_IMAGE_WIDTH);
236         }
237 #endif
238         return 0;
239 }
240
241 unsigned Image::get_height() const
242 {
243 #ifdef WITH_LIBPNG
244         if(priv->data)
245                 return priv->height;
246 #endif
247 #ifdef WITH_DEVIL
248         if(priv->id)
249         {
250                 ilBindImage(priv->id);
251                 return ilGetInteger(IL_IMAGE_HEIGHT);
252         }
253 #endif
254         return 0;
255 }
256
257 const void *Image::get_data() const
258 {
259 #ifdef WITH_LIBPNG
260         if(priv->data)
261                 return priv->data;
262 #endif
263 #ifdef WITH_DEVIL
264         if(priv->id)
265         {
266                 ilBindImage(priv->id);
267                 return ilGetData();
268         }
269 #endif
270         return 0;
271 }
272
273 } // namespace Graphics
274 } // namespace Msp