]> git.tdb.fi Git - libs/gui.git/blob - source/gbase/image.cpp
4fc74221057d16ba62af5e945d93e5ca9526667d
[libs/gui.git] / source / gbase / 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 <msp/core/except.h>
10 #include "image.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace Graphics {
16
17 struct Image::Private
18 {
19 #ifdef WITH_DEVIL
20         unsigned id;
21 #endif
22 #ifdef WITH_LIBPNG
23         PixelFormat fmt;
24         unsigned width;
25         unsigned height;
26         char *data;
27 #endif
28
29         Private();
30 };
31
32 Image::Private::Private()
33 {
34 #ifdef WITH_DEVIL
35         id=0;
36 #endif
37 #ifdef WITH_LIBPNG
38         fmt=RGB;
39         width=0;
40         height=0;
41         data=0;
42 #endif
43 }
44
45
46 namespace {
47
48 #ifdef WITH_LIBPNG
49 void read(png_struct *png, png_byte *data, png_size_t size)
50 {
51         IO::Base *in=reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
52         in->read(reinterpret_cast<char *>(data), size);
53 }
54
55 void load_png(IO::Base &in, Image::Private &priv)
56 {
57         png_struct *png=0;
58         png_info *info=0;
59         priv.data=0;
60
61         try
62         {
63                 png=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
64                 info=png_create_info_struct(png);
65
66                 if(setjmp(png_jmpbuf(png)))
67                         throw Exception("Error loading PNG image");
68
69                 png_set_read_fn(png, &in, read);
70                 png_read_info(png, info);
71                 png_uint_32 width;
72                 png_uint_32 height;
73                 int depth;
74                 int color;
75                 png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
76                 priv.width=width;
77                 priv.height=height;
78                 if(depth!=8)
79                         throw Exception("Only 8-bit PNG images are supported");
80                 switch(color)
81                 {
82                 case PNG_COLOR_TYPE_PALETTE:    priv.fmt=COLOR_INDEX; break;
83                 case PNG_COLOR_TYPE_GRAY:       priv.fmt=LUMINANCE; break;
84                 case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt=LUMINANCE_ALPHA; break;
85                 case PNG_COLOR_TYPE_RGB:        priv.fmt=RGB; break;
86                 case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt=RGBA; break;
87                 default: throw Exception("Unknown color type");
88                 }
89
90                 unsigned nchans=png_get_channels(png, info);
91                 if(nchans==4 && priv.fmt==RGB)
92                         png_set_strip_alpha(png);
93
94                 unsigned rowstride=priv.width*nchans;
95                 priv.data=new char[rowstride*priv.height];
96                 for(unsigned y=0; y<priv.height; ++y)
97                         png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
98
99                 png_read_end(png, 0);
100                 png_destroy_read_struct(&png, &info, 0);
101         }
102         catch(...)
103         {
104                 png_destroy_read_struct(&png, &info, 0);
105                 delete[] priv.data;
106                 throw;
107         }
108 }
109 #endif
110
111 #ifdef WITH_DEVIL
112 void ensure_devil_image(unsigned &id)
113 {
114         static bool init_done=false;
115
116         if(!init_done)
117         {
118                 ilInit();
119                 ilEnable(IL_ORIGIN_SET);
120                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
121                 init_done=true;
122         }
123
124         if(!id)
125                 ilGenImages(1, &id);
126 }
127 #endif
128
129 }
130
131
132 Image::Image():
133         priv(new Private)
134 {
135 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
136         throw Exception("Image needs either DevIL or libpng support");
137 #endif
138 }
139
140 Image::~Image()
141 {
142 #ifdef WITH_DEVIL
143         if(priv->id)
144                 ilDeleteImages(1, &priv->id);
145 #endif
146 #ifdef WITH_LIBPNG
147         delete[] priv->data;
148 #endif
149         delete priv;
150 }
151
152 void Image::load_file(const string &fn)
153 {
154 #ifdef WITH_LIBPNG
155         if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
156         {
157                 IO::BufferedFile file(fn);
158                 load_png(file, *priv);
159         }
160         else
161 #endif
162         {
163 #ifdef WITH_DEVIL
164                 ensure_devil_image(priv->id);
165                 ilBindImage(priv->id);
166                 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
167                         throw Exception("Error loading image "+fn);
168 #else
169                 throw Exception("Not a PNG image and DevIL support not compiled in");
170 #endif
171         }
172         (void)fn;
173 }
174
175 void Image::load_memory(const void *data, unsigned size)
176 {
177 #ifdef WITH_LIBPNG
178         if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
179         {
180                 IO::Memory mem(reinterpret_cast<const char *>(data), size);
181                 load_png(mem, *priv);
182         }
183         else
184 #endif
185         {
186 #ifdef WITH_DEVIL
187                 ensure_devil_image(priv->id);
188                 ilBindImage(priv->id);
189                 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
190                         throw Exception("Error loading image from memory");
191 #else
192                 throw Exception("Not a PNG image and DevIL support not compiled in");
193 #endif
194         }
195         (void)data;
196         (void)size;
197 }
198
199 PixelFormat Image::get_format() const
200 {
201 #ifdef WITH_LIBPNG
202         if(priv->data)
203                 return priv->fmt;
204 #endif
205 #ifdef WITH_DEVIL
206         if(priv->id)
207         {
208                 ilBindImage(priv->id);
209                 switch(ilGetInteger(IL_IMAGE_FORMAT))
210                 {
211                 case IL_COLOR_INDEX: return COLOR_INDEX;
212                 case IL_LUMINANCE: return LUMINANCE;
213                 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
214                 case IL_RGB: return RGB;
215                 case IL_RGBA: return RGBA;
216                 case IL_BGR: return BGR;
217                 case IL_BGRA: return BGRA;
218                 default: throw InvalidParameterValue("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