]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/devil/devilloader.cpp
Add some state checking to ImageLoader
[libs/gui.git] / source / graphics / devil / devilloader.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include <IL/il.h>
4 #include "devilloader.h"
5
6 using namespace std;
7
8 namespace {
9
10 unsigned char eof(void *handle)
11 {
12         return reinterpret_cast<Msp::IO::Seekable *>(handle)->eof();
13 }
14
15 int get(void *handle)
16 {
17         return reinterpret_cast<Msp::IO::Seekable *>(handle)->get();
18 }
19
20 int read(void *buf, unsigned size, unsigned count, void *handle)
21 {
22         Msp::IO::Seekable *io = reinterpret_cast<Msp::IO::Seekable *>(handle);
23         char *cbuf = reinterpret_cast<char *>(buf);
24         unsigned i = 0;
25         for(; i<count; ++i)
26         {
27                 unsigned len = io->read(cbuf, size);
28                 cbuf += size;
29                 if(len<size)
30                         break;
31         }
32         return i;
33 }
34
35 int seek(void *handle, int offset, int il_type)
36 {
37         Msp::IO::SeekType type;
38         if(il_type==IL_SEEK_SET)
39                 type = Msp::IO::S_BEG;
40         else if(il_type==IL_SEEK_CUR)
41                 type = Msp::IO::S_CUR;
42         else if(il_type==IL_SEEK_END)
43                 type = Msp::IO::S_END;
44         else
45                 return -1;
46
47         reinterpret_cast<Msp::IO::Seekable *>(handle)->seek(offset, type);
48
49         return 0;
50 }
51
52 int tell(void *handle)
53 {
54         return reinterpret_cast<Msp::IO::Seekable *>(handle)->tell();
55 }
56
57 std::string error_string(ILenum err)
58 {
59         switch(err)
60         {
61         case IL_FORMAT_NOT_SUPPORTED: return "Format not supported";
62         case IL_INTERNAL_ERROR: return "DevIL internal error";
63         case IL_INVALID_FILE_HEADER: return "Invalid file header";
64         case IL_FILE_READ_ERROR: return "File read error";
65         case IL_LIB_PNG_ERROR: return "LibPNG error";
66         case IL_LIB_JPEG_ERROR: return "LibJPEG error";
67         default: return Msp::format("Unknown error (%04X)", err);
68         }
69 }
70
71 } // namespace
72
73
74 namespace Msp {
75 namespace Graphics {
76
77 DevilLoader::DevilLoader(IO::Seekable &i):
78         io(i)
79 {
80         static bool il_init_done = false;
81
82         if(!il_init_done)
83         {
84                 ilInit();
85                 ilEnable(IL_ORIGIN_SET);
86                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
87                 il_init_done = true;
88         }
89
90         ilGenImages(1, &id);
91 }
92
93 DevilLoader::~DevilLoader()
94 {
95         ilDeleteImages(1, &id);
96 }
97
98 bool DevilLoader::detect(const string &sig)
99 {
100         unsigned type = ilDetermineTypeL(sig.data(), sig.size());
101         return type!=IL_TYPE_UNKNOWN;
102 }
103
104 void DevilLoader::load_(Image::Data &data)
105 {
106         ilSetRead(0, 0, eof, get, read, seek, tell);
107         ilBindImage(id);
108
109         ilGetError();
110         if(!ilLoadF(IL_TYPE_UNKNOWN, &io))
111                 throw bad_image_data(error_string(ilGetError()));
112
113         switch(ilGetInteger(IL_IMAGE_FORMAT))
114         {
115         case IL_COLOR_INDEX:     data.fmt = COLOR_INDEX; break;
116         case IL_LUMINANCE:       data.fmt = LUMINANCE; break;
117         case IL_LUMINANCE_ALPHA: data.fmt = LUMINANCE_ALPHA; break;
118         case IL_RGB:             data.fmt = RGB; break;
119         case IL_RGBA:            data.fmt = RGBA; break;
120         case IL_BGR:             data.fmt = BGR; break;
121         case IL_BGRA:            data.fmt = BGRA; break;
122         default: throw unsupported_image_format("unknown pixel format");
123         }
124
125         data.width = ilGetInteger(IL_IMAGE_WIDTH);
126         data.height = ilGetInteger(IL_IMAGE_HEIGHT);
127         data.stride = data.width*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
128         unsigned data_size = data.stride*data.height;
129         data.pixels = new char[data_size];
130         ILubyte *il_data = ilGetData();
131         copy(il_data, il_data+data_size, data.pixels);
132
133         ilBindImage(0);
134         ilResetRead();
135 }
136
137 } // namespace Graphics
138 } // namespace Msp