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