]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image_devil.cpp
Implement graphical reporting for uncaught exceptions
[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_io(IO::Base &io, Image::Private &priv, const char *sig_buf, unsigned sig_len)
65 {
66         vector<char> data(sig_buf, sig_buf+sig_len);
67         while(1)
68         {
69                 unsigned pos = data.size();
70                 data.resize(pos+16384);
71                 unsigned len = io.read(&data[pos], 16384);
72                 data.resize(pos+len);
73                 if(len==0)
74                         break;
75         }
76
77         init_load(priv);
78         if(!ilLoadL(IL_TYPE_UNKNOWN, &data.front(), data.size()))
79                 throw bad_image_data("IL error");
80         finish_load(priv);
81 }
82
83 } // namespace Graphics
84 } // namespace Msp
85
86 #endif