]> git.tdb.fi Git - libs/gui.git/blob - source/gbase/image.cpp
Make xf86vidmode support optional
[libs/gui.git] / source / gbase / image.cpp
1 /* $Id$
2
3 This file is part of libmspgbase
4 Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifdef WITH_DEVIL
9 #include <IL/il.h>
10 #endif
11 #ifdef WITH_LIBPNG
12 #include <png.h>
13 #include <msp/io/file.h>
14 #include <msp/io/memory.h>
15 #endif
16 #include <msp/core/except.h>
17 #include "image.h"
18
19 using namespace std;
20
21 namespace Msp {
22 namespace Graphics {
23
24 struct Image::Private
25 {
26 #ifdef WITH_DEVIL
27         unsigned id;
28 #endif
29 #ifdef WITH_LIBPNG
30         PixelFormat fmt;
31         unsigned width;
32         unsigned height;
33         char *data;
34 #endif
35
36         Private();
37 };
38
39 Image::Private::Private()
40 {
41 #ifdef WITH_DEVIL
42         id=0;
43 #endif
44 #ifdef WITH_LIBPNG
45         fmt=RGB;
46         width=0;
47         height=0;
48         data=0;
49 #endif
50 }
51
52
53 namespace {
54
55 #ifdef WITH_LIBPNG
56 void read(png_struct *png, png_byte *data, png_size_t size)
57 {
58         IO::Base *in=reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
59         in->read(reinterpret_cast<char *>(data), size);
60 }
61
62 void load_png(IO::Base &in, Image::Private &priv)
63 {
64         png_struct *png=0;
65         png_info *info=0;
66         priv.data=0;
67
68         try
69         {
70                 png=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
71                 info=png_create_info_struct(png);
72
73                 if(setjmp(png_jmpbuf(png)))
74                         throw Exception("Error loading PNG image");
75
76                 png_set_read_fn(png, &in, read);
77                 png_read_info(png, info);
78                 png_uint_32 width;
79                 png_uint_32 height;
80                 int depth;
81                 int color;
82                 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
83                 priv.width=width;
84                 priv.height=height;
85                 if(depth!=8)
86                         throw Exception("Only 8-bit PNG images are supported");
87                 switch(color)
88                 {
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");
95                 }
96
97                 unsigned nchans=png_get_channels(png, info);
98                 if(nchans==4 && priv.fmt==RGB)
99                         png_set_strip_alpha(png);
100
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);
105
106                 png_read_end(png, 0);
107                 png_destroy_read_struct(&png, &info, 0);
108         }
109         catch(...)
110         {
111                 png_destroy_read_struct(&png, &info, 0);
112                 delete[] priv.data;
113                 throw;
114         }
115 }
116 #endif
117
118 #ifdef WITH_DEVIL
119 void ensure_devil_image(unsigned &id)
120 {
121         static bool init_done=false;
122
123         if(!init_done)
124         {
125                 ilInit();
126                 ilEnable(IL_ORIGIN_SET);
127                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
128                 init_done=true;
129         }
130
131         if(!id)
132                 ilGenImages(1, &id);
133 }
134 #endif
135
136 }
137
138
139 Image::Image():
140         priv(new Private)
141 {
142 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
143         throw Exception("Image needs either DevIL or libpng support");
144 #endif
145 }
146
147 Image::~Image()
148 {
149 #ifdef WITH_DEVIL
150         if(priv->id)
151                 ilDeleteImages(1, &priv->id);
152 #endif
153 #ifdef WITH_LIBPNG
154         delete[] priv->data;
155 #endif
156 }
157
158 void Image::load_file(const string &fn)
159 {
160 #ifdef WITH_LIBPNG
161         if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
162         {
163                 IO::BufferedFile file(fn);
164                 load_png(file, *priv);
165         }
166         else
167 #endif
168         {
169 #ifdef WITH_DEVIL
170                 ensure_devil_image(priv->id);
171                 ilBindImage(priv->id);
172                 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
173                         throw Exception("Error loading image "+fn);
174 #else
175                 throw Exception("Not a PNG image and DevIL support not compiled in");
176 #endif
177         }
178         (void)fn;
179 }
180
181 void Image::load_memory(const void *data, unsigned size)
182 {
183 #ifdef WITH_LIBPNG
184         if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
185         {
186                 IO::Memory mem(reinterpret_cast<const char *>(data), size);
187                 load_png(mem, *priv);
188         }
189         else
190 #endif
191         {
192 #ifdef WITH_DEVIL
193                 ensure_devil_image(priv->id);
194                 ilBindImage(priv->id);
195                 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
196                         throw Exception("Error loading image from memory");
197 #else
198                 throw Exception("Not a PNG image and DevIL support not compiled in");
199 #endif
200         }
201         (void)data;
202         (void)size;
203 }
204
205 PixelFormat Image::get_format() const
206 {
207 #ifdef WITH_LIBPNG
208         if(priv->data)
209                 return priv->fmt;
210 #endif
211 #ifdef WITH_DEVIL
212         if(priv->id)
213         {
214                 ilBindImage(priv->id);
215                 switch(ilGetInteger(IL_IMAGE_FORMAT))
216                 {
217                 case IL_COLOR_INDEX: return COLOR_INDEX;
218                 case IL_LUMINANCE: return LUMINANCE;
219                 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
220                 case IL_RGB: return RGB;
221                 case IL_RGBA: return RGBA;
222                 case IL_BGR: return BGR;
223                 case IL_BGRA: return BGRA;
224                 default: throw InvalidParameterValue("Unknown pixel format in image");
225                 }
226         }
227 #endif
228         return RGB;
229 }
230
231 unsigned Image::get_width() const
232 {
233 #ifdef WITH_LIBPNG
234         if(priv->data)
235                 return priv->width;
236 #endif
237 #ifdef WITH_DEVIL
238         if(priv->id)
239         {
240                 ilBindImage(priv->id);
241                 return ilGetInteger(IL_IMAGE_WIDTH);
242         }
243 #endif
244         return 0;
245 }
246
247 unsigned Image::get_height() const
248 {
249 #ifdef WITH_LIBPNG
250         if(priv->data)
251                 return priv->height;
252 #endif
253 #ifdef WITH_DEVIL
254         if(priv->id)
255         {
256                 ilBindImage(priv->id);
257                 return ilGetInteger(IL_IMAGE_HEIGHT);
258         }
259 #endif
260         return 0;
261 }
262
263 const void *Image::get_data() const
264 {
265 #ifdef WITH_LIBPNG
266         if(priv->data)
267                 return priv->data;
268 #endif
269 #ifdef WITH_DEVIL
270         if(priv->id)
271         {
272                 ilBindImage(priv->id);
273                 return ilGetData();
274         }
275 #endif
276         return 0;
277 }
278
279 } // namespace Graphics
280 } // namespace Msp