3 #include "devilloader.h"
9 unsigned char eof(void *handle)
11 return reinterpret_cast<Msp::IO::Seekable *>(handle)->eof();
16 return reinterpret_cast<Msp::IO::Seekable *>(handle)->get();
19 int read(void *buf, unsigned size, unsigned count, void *handle)
21 Msp::IO::Seekable *io = reinterpret_cast<Msp::IO::Seekable *>(handle);
22 char *cbuf = reinterpret_cast<char *>(buf);
26 unsigned len = io->read(cbuf, size);
34 int seek(void *handle, int offset, int il_type)
36 Msp::IO::SeekType type;
37 if(il_type==IL_SEEK_SET)
38 type = Msp::IO::S_BEG;
39 else if(il_type==IL_SEEK_CUR)
40 type = Msp::IO::S_CUR;
41 else if(il_type==IL_SEEK_END)
42 type = Msp::IO::S_END;
46 reinterpret_cast<Msp::IO::Seekable *>(handle)->seek(offset, type);
51 int tell(void *handle)
53 return reinterpret_cast<Msp::IO::Seekable *>(handle)->tell();
62 DevilLoader::DevilLoader(IO::Seekable &i):
65 static bool il_init_done = false;
70 ilEnable(IL_ORIGIN_SET);
71 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
78 DevilLoader::~DevilLoader()
80 ilDeleteImages(1, &id);
83 bool DevilLoader::detect(const string &sig)
85 unsigned type = ilDetermineTypeL(sig.data(), sig.size());
86 return type!=IL_TYPE_UNKNOWN;
89 void DevilLoader::load(Image::Data &data)
91 ilSetRead(0, 0, eof, get, read, seek, tell);
93 ilLoadF(IL_TYPE_UNKNOWN, &io);
95 switch(ilGetInteger(IL_IMAGE_FORMAT))
97 case IL_COLOR_INDEX: data.fmt = COLOR_INDEX; break;
98 case IL_LUMINANCE: data.fmt = LUMINANCE; break;
99 case IL_LUMINANCE_ALPHA: data.fmt = LUMINANCE_ALPHA; break;
100 case IL_RGB: data.fmt = RGB; break;
101 case IL_RGBA: data.fmt = RGBA; break;
102 case IL_BGR: data.fmt = BGR; break;
103 case IL_BGRA: data.fmt = BGRA; break;
104 default: throw unsupported_image_format("unknown pixel format");
107 data.width = ilGetInteger(IL_IMAGE_WIDTH);
108 data.height = ilGetInteger(IL_IMAGE_HEIGHT);
109 unsigned data_size = data.width*data.height*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
110 data.data = new char[data_size];
111 ILubyte *il_data = ilGetData();
112 copy(il_data, il_data+data_size, data.data);
118 } // namespace Graphics