3 #include "devilloader.h"
18 unsigned char eof(void *handle)
20 return reinterpret_cast<IOContext *>(handle)->io->eof();
25 IOContext *ctx = reinterpret_cast<IOContext *>(handle);
26 if(ctx->position<ctx->buf_fill)
27 return ctx->buffer[ctx->position++];
30 char c = ctx->io->get();
31 if(ctx->position<ctx->buf_max)
32 ctx->buffer[ctx->position] = c;
38 int read(void *buf, unsigned size, unsigned count, void *handle)
40 IOContext *ctx = reinterpret_cast<IOContext *>(handle);
41 char *cbuf = reinterpret_cast<char *>(buf);
42 unsigned len = size*count;
45 if(ctx->position<ctx->buf_fill)
47 unsigned copy_len = min(ctx->buf_fill-ctx->position, len);
48 copy(ctx->buffer+ctx->position, ctx->buffer+ctx->position+copy_len, cbuf);
49 ctx->position += copy_len;
57 unsigned read_len = ctx->io->read(cbuf, len);
58 if(ctx->position<ctx->buf_max)
60 unsigned copy_len = min(ctx->buf_max-ctx->position, read_len);
61 copy(cbuf, cbuf+copy_len, ctx->buffer+ctx->position);
62 ctx->buf_fill += copy_len;
65 ctx->position += read_len;
71 int seek(void *handle, int offset, int type)
73 IOContext *ctx = reinterpret_cast<IOContext *>(handle);
75 unsigned new_pos = ctx->position;
78 else if(type==IL_SEEK_CUR)
83 if(new_pos>ctx->buf_fill)
84 new_pos = ctx->buf_fill;
85 ctx->position = new_pos;
90 int tell(void *handle)
92 return reinterpret_cast<IOContext *>(handle)->position;
101 DevilLoader::DevilLoader(IO::Base &i, const string &s):
105 static bool il_init_done = false;
110 ilEnable(IL_ORIGIN_SET);
111 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
118 DevilLoader::~DevilLoader()
120 ilDeleteImages(1, &id);
123 bool DevilLoader::detect(const string &sig)
125 unsigned type = ilDetermineTypeL(sig.data(), sig.size());
126 return type!=IL_TYPE_UNKNOWN;
129 void DevilLoader::load(Image::Data &data)
135 ctx.buf_max = sizeof(buffer);
136 copy(signature.begin(), signature.end(), buffer);
137 ctx.buf_fill = signature.size();
140 ilSetRead(0, 0, eof, get, read, seek, tell);
142 ilLoadF(IL_TYPE_UNKNOWN, &ctx);
144 switch(ilGetInteger(IL_IMAGE_FORMAT))
146 case IL_COLOR_INDEX: data.fmt = COLOR_INDEX; break;
147 case IL_LUMINANCE: data.fmt = LUMINANCE; break;
148 case IL_LUMINANCE_ALPHA: data.fmt = LUMINANCE_ALPHA; break;
149 case IL_RGB: data.fmt = RGB; break;
150 case IL_RGBA: data.fmt = RGBA; break;
151 case IL_BGR: data.fmt = BGR; break;
152 case IL_BGRA: data.fmt = BGRA; break;
153 default: throw unsupported_image_format("unknown pixel format");
156 data.width = ilGetInteger(IL_IMAGE_WIDTH);
157 data.height = ilGetInteger(IL_IMAGE_HEIGHT);
158 unsigned data_size = data.width*data.height*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
159 data.data = new char[data_size];
160 ILubyte *il_data = ilGetData();
161 copy(il_data, il_data+data_size, data.data);
167 } // namespace Graphics