]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image_devil.cpp
Put (most of) DevIL code in its own file
[libs/gui.git] / source / graphics / image_devil.cpp
1 #ifdef WITH_DEVIL
2 #include <IL/il.h>
3 #include "image_devil.h"
4 #include "image_private.h"
5
6 using namespace std;
7
8 namespace {
9
10 using namespace Msp::Graphics;
11
12 void init_load(Image::Private &priv)
13 {
14         static bool il_init_done = false;
15
16         if(!il_init_done)
17         {
18                 ilInit();
19                 ilEnable(IL_ORIGIN_SET);
20                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
21                 il_init_done = true;
22         }
23
24         if(!priv.id)
25                 ilGenImages(1, &priv.id);
26         ilBindImage(priv.id);
27 }
28
29 void finish_load(Image::Private &priv)
30 {
31         switch(ilGetInteger(IL_IMAGE_FORMAT))
32         {
33         case IL_COLOR_INDEX:     priv.fmt = COLOR_INDEX; break;
34         case IL_LUMINANCE:       priv.fmt = LUMINANCE; break;
35         case IL_LUMINANCE_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
36         case IL_RGB:             priv.fmt = RGB; break;
37         case IL_RGBA:            priv.fmt = RGBA; break;
38         case IL_BGR:             priv.fmt = BGR; break;
39         case IL_BGRA:            priv.fmt = BGRA; break;
40         default: throw unsupported_image_format("unknown pixel format");
41         }
42
43         priv.width = ilGetInteger(IL_IMAGE_WIDTH);
44         priv.height = ilGetInteger(IL_IMAGE_HEIGHT);
45         priv.data = reinterpret_cast<char *>(ilGetData());
46
47         ilBindImage(0);
48 }
49
50 }
51
52
53 namespace Msp {
54 namespace Graphics {
55
56 void load_devil_file(const string &fn, Image::Private &priv)
57 {
58         init_load(priv);
59         if(!ilLoadImage(const_cast<char *>(fn.c_str())))
60                 throw bad_image_data("IL error");
61         finish_load(priv);
62 }
63
64 void load_devil_mem(const void *data, unsigned size, Image::Private &priv)
65 {
66         init_load(priv);
67         if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
68                 throw bad_image_data("IL error");
69         finish_load(priv);
70 }
71
72 } // namespace Graphics
73 } // namespace Msp
74
75 #endif