]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image.cpp
Separate PNG loading to a separate file
[libs/gui.git] / source / graphics / image.cpp
1 #ifdef WITH_DEVIL
2 #include <IL/il.h>
3 #endif
4 #include <msp/io/file.h>
5 #include <msp/io/memory.h>
6 #include "image.h"
7 #include "image_png.h"
8 #include "image_private.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace Graphics {
14
15 Image::Private::Private()
16 {
17 #ifdef WITH_DEVIL
18         id = 0;
19 #endif
20 #ifdef WITH_LIBPNG
21         fmt = RGB;
22         width = 0;
23         height = 0;
24         data = 0;
25 #endif
26 }
27
28
29 namespace {
30
31 #ifdef WITH_DEVIL
32 void ensure_devil_image(unsigned &id)
33 {
34         static bool init_done = false;
35
36         if(!init_done)
37         {
38                 ilInit();
39                 ilEnable(IL_ORIGIN_SET);
40                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
41                 init_done = true;
42         }
43
44         if(!id)
45                 ilGenImages(1, &id);
46 }
47 #endif
48
49 }
50
51
52 Image::Image():
53         priv(new Private)
54 {
55 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
56         throw runtime_error("no image support");
57 #endif
58 }
59
60 Image::~Image()
61 {
62 #ifdef WITH_DEVIL
63         if(priv->id)
64                 ilDeleteImages(1, &priv->id);
65 #endif
66 #ifdef WITH_LIBPNG
67         delete[] priv->data;
68 #endif
69         delete priv;
70 }
71
72 void Image::load_file(const string &fn)
73 {
74 #ifdef WITH_LIBPNG
75         if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
76         {
77                 IO::BufferedFile file(fn);
78                 load_png(file, *priv);
79         }
80         else
81 #endif
82         {
83 #ifdef WITH_DEVIL
84                 ensure_devil_image(priv->id);
85                 ilBindImage(priv->id);
86                 if(!ilLoadImage(const_cast<char *>(fn.c_str())))
87                         throw bad_image_data("IL error");
88 #else
89                 throw unsupported_image_format("DevIL needed for non-PNG images");
90 #endif
91         }
92         (void)fn;
93 }
94
95 void Image::load_memory(const void *data, unsigned size)
96 {
97 #ifdef WITH_LIBPNG
98         if(size>=8 && is_png(data, 8))
99         {
100                 IO::Memory mem(reinterpret_cast<const char *>(data), size);
101                 load_png(mem, *priv);
102         }
103         else
104 #endif
105         {
106 #ifdef WITH_DEVIL
107                 ensure_devil_image(priv->id);
108                 ilBindImage(priv->id);
109                 if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
110                         throw bad_image_data("IL error");
111 #else
112                 throw unsupported_image_format("DevIL needed for non-PNG images");
113 #endif
114         }
115         (void)data;
116         (void)size;
117 }
118
119 PixelFormat Image::get_format() const
120 {
121 #ifdef WITH_LIBPNG
122         if(priv->data)
123                 return priv->fmt;
124 #endif
125 #ifdef WITH_DEVIL
126         if(priv->id)
127         {
128                 ilBindImage(priv->id);
129                 switch(ilGetInteger(IL_IMAGE_FORMAT))
130                 {
131                 case IL_COLOR_INDEX: return COLOR_INDEX;
132                 case IL_LUMINANCE: return LUMINANCE;
133                 case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
134                 case IL_RGB: return RGB;
135                 case IL_RGBA: return RGBA;
136                 case IL_BGR: return BGR;
137                 case IL_BGRA: return BGRA;
138                 // XXX bad, should throw when loading
139                 default: throw invalid_argument("unknown pixel format in image");
140                 }
141         }
142 #endif
143         return RGB;
144 }
145
146 unsigned Image::get_width() const
147 {
148 #ifdef WITH_LIBPNG
149         if(priv->data)
150                 return priv->width;
151 #endif
152 #ifdef WITH_DEVIL
153         if(priv->id)
154         {
155                 ilBindImage(priv->id);
156                 return ilGetInteger(IL_IMAGE_WIDTH);
157         }
158 #endif
159         return 0;
160 }
161
162 unsigned Image::get_height() const
163 {
164 #ifdef WITH_LIBPNG
165         if(priv->data)
166                 return priv->height;
167 #endif
168 #ifdef WITH_DEVIL
169         if(priv->id)
170         {
171                 ilBindImage(priv->id);
172                 return ilGetInteger(IL_IMAGE_HEIGHT);
173         }
174 #endif
175         return 0;
176 }
177
178 const void *Image::get_data() const
179 {
180 #ifdef WITH_LIBPNG
181         if(priv->data)
182                 return priv->data;
183 #endif
184 #ifdef WITH_DEVIL
185         if(priv->id)
186         {
187                 ilBindImage(priv->id);
188                 return ilGetData();
189         }
190 #endif
191         return 0;
192 }
193
194 } // namespace Graphics
195 } // namespace Msp