]> git.tdb.fi Git - libs/gui.git/blob - source/gbase/image.cpp
0cd0aff4c78340e16c64ce38fcb8ece76075e194
[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         delete priv;
157 }
158
159 void Image::load_file(const string &fn)
160 {
161 #ifdef WITH_LIBPNG
162         if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
163         {
164                 IO::BufferedFile file(fn);
165                 load_png(file, *priv);
166         }
167         else
168 #endif
169         {
170 #ifdef WITH_DEVIL
171                 ensure_devil_image(priv->id);
172                 ilBindImage(priv->id);
173                 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
174                         throw Exception("Error loading image "+fn);
175 #else
176                 throw Exception("Not a PNG image and DevIL support not compiled in");
177 #endif
178         }
179         (void)fn;
180 }
181
182 void Image::load_memory(const void *data, unsigned size)
183 {
184 #ifdef WITH_LIBPNG
185         if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
186         {
187                 IO::Memory mem(reinterpret_cast<const char *>(data), size);
188                 load_png(mem, *priv);
189         }
190         else
191 #endif
192         {
193 #ifdef WITH_DEVIL
194                 ensure_devil_image(priv->id);
195                 ilBindImage(priv->id);
196                 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
197                         throw Exception("Error loading image from memory");
198 #else
199                 throw Exception("Not a PNG image and DevIL support not compiled in");
200 #endif
201         }
202         (void)data;
203         (void)size;
204 }
205
206 PixelFormat Image::get_format() const
207 {
208 #ifdef WITH_LIBPNG
209         if(priv->data)
210                 return priv->fmt;
211 #endif
212 #ifdef WITH_DEVIL
213         if(priv->id)
214         {
215                 ilBindImage(priv->id);
216                 switch(ilGetInteger(IL_IMAGE_FORMAT))
217                 {
218                 case IL_COLOR_INDEX: return COLOR_INDEX;
219                 case IL_LUMINANCE: return LUMINANCE;
220                 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
221                 case IL_RGB: return RGB;
222                 case IL_RGBA: return RGBA;
223                 case IL_BGR: return BGR;
224                 case IL_BGRA: return BGRA;
225                 default: throw InvalidParameterValue("Unknown pixel format in image");
226                 }
227         }
228 #endif
229         return RGB;
230 }
231
232 unsigned Image::get_width() const
233 {
234 #ifdef WITH_LIBPNG
235         if(priv->data)
236                 return priv->width;
237 #endif
238 #ifdef WITH_DEVIL
239         if(priv->id)
240         {
241                 ilBindImage(priv->id);
242                 return ilGetInteger(IL_IMAGE_WIDTH);
243         }
244 #endif
245         return 0;
246 }
247
248 unsigned Image::get_height() const
249 {
250 #ifdef WITH_LIBPNG
251         if(priv->data)
252                 return priv->height;
253 #endif
254 #ifdef WITH_DEVIL
255         if(priv->id)
256         {
257                 ilBindImage(priv->id);
258                 return ilGetInteger(IL_IMAGE_HEIGHT);
259         }
260 #endif
261         return 0;
262 }
263
264 const void *Image::get_data() const
265 {
266 #ifdef WITH_LIBPNG
267         if(priv->data)
268                 return priv->data;
269 #endif
270 #ifdef WITH_DEVIL
271         if(priv->id)
272         {
273                 ilBindImage(priv->id);
274                 return ilGetData();
275         }
276 #endif
277         return 0;
278 }
279
280 } // namespace Graphics
281 } // namespace Msp