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 ImageLoader::Register<DevilLoader> DevilLoader::reg;
64 DevilLoader::DevilLoader(IO::Seekable &i):
67 static bool il_init_done = false;
72 ilEnable(IL_ORIGIN_SET);
73 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
80 DevilLoader::~DevilLoader()
82 ilDeleteImages(1, &id);
85 bool DevilLoader::detect(const string &sig)
87 unsigned type = ilDetermineTypeL(sig.data(), sig.size());
88 return type!=IL_TYPE_UNKNOWN;
91 void DevilLoader::load(Image::Data &data)
93 ilSetRead(0, 0, eof, get, read, seek, tell);
95 ilLoadF(IL_TYPE_UNKNOWN, &io);
97 switch(ilGetInteger(IL_IMAGE_FORMAT))
99 case IL_COLOR_INDEX: data.fmt = COLOR_INDEX; break;
100 case IL_LUMINANCE: data.fmt = LUMINANCE; break;
101 case IL_LUMINANCE_ALPHA: data.fmt = LUMINANCE_ALPHA; break;
102 case IL_RGB: data.fmt = RGB; break;
103 case IL_RGBA: data.fmt = RGBA; break;
104 case IL_BGR: data.fmt = BGR; break;
105 case IL_BGRA: data.fmt = BGRA; break;
106 default: throw unsupported_image_format("unknown pixel format");
109 data.width = ilGetInteger(IL_IMAGE_WIDTH);
110 data.height = ilGetInteger(IL_IMAGE_HEIGHT);
111 unsigned data_size = data.width*data.height*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
112 data.data = new char[data_size];
113 ILubyte *il_data = ilGetData();
114 copy(il_data, il_data+data_size, data.data);
120 } // namespace Graphics