if(ext==".png")
{
IO::BufferedFile file(fn);
- load_png(file, *priv);
+ load_png(file, *priv, 0, 0);
}
else
#endif
(void)fn;
}
-void Image::load_memory(const void *data, unsigned size)
+void Image::load_io(IO::Base &io)
{
+ char sig_buf[8];
+ unsigned sig_len = io.read(sig_buf, sizeof(sig_buf));
#ifdef WITH_LIBPNG
- if(size>=8 && is_png(data, 8))
- {
- IO::Memory mem(reinterpret_cast<const char *>(data), size);
- load_png(mem, *priv);
- }
+ if(sig_len==sizeof(sig_buf) && is_png(sig_buf, sig_len))
+ load_png(io, *priv, sig_buf, sig_len);
else
#endif
{
#ifdef WITH_DEVIL
- load_devil_mem(data, size, *priv);
+ load_devil_io(io, *priv, sig_buf, sig_len);
#else
throw unsupported_image_format("DevIL needed for non-PNG images");
#endif
}
- (void)data;
- (void)size;
}
PixelFormat Image::get_format() const
#include <stdexcept>
#include <string>
+#include <msp/io/base.h>
#include "pixelformat.h"
namespace Msp {
~Image();
void load_file(const std::string &);
- void load_memory(const void *, unsigned);
+ void load_io(IO::Base &);
PixelFormat get_format() const;
unsigned get_width() const;
unsigned get_height() const;
finish_load(priv);
}
-void load_devil_mem(const void *data, unsigned size, Image::Private &priv)
+void load_devil_io(IO::Base &io, Image::Private &priv, const char *sig_buf, unsigned sig_len)
{
+ vector<char> data(sig_buf, sig_buf+sig_len);
+ while(1)
+ {
+ unsigned pos = data.size();
+ data.resize(pos+16384);
+ unsigned len = io.read(&data[pos], 16384);
+ data.resize(pos+len);
+ if(len==0)
+ break;
+ }
+
init_load(priv);
- if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
+ if(!ilLoadL(IL_TYPE_UNKNOWN, &data.front(), data.size()))
throw bad_image_data("IL error");
finish_load(priv);
}
#ifdef WITH_DEVIL
void load_devil_file(const std::string &, Image::Private &);
-void load_devil_mem(const void *, unsigned, Image::Private &);
+void load_devil_io(IO::Base &, Image::Private &, const char *, unsigned);
#endif
} // namespace Graphics
namespace Msp {
namespace Graphics {
-bool is_png(const void *buf, unsigned len)
+bool is_png(const char *buf, unsigned len)
{
- return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(buf)), 0, len);
+ return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<char*>(buf)), 0, len);
}
-void load_png(IO::Base &in, Image::Private &priv)
+void load_png(IO::Base &in, Image::Private &priv, const char *, unsigned sig_len)
{
png_struct *png = 0;
png_info *info = 0;
throw bad_image_data(error_info.msg);
png_set_read_fn(png, &in, read);
+ png_set_sig_bytes(png, sig_len);
png_read_info(png, info);
png_uint_32 width;
png_uint_32 height;
namespace Graphics {
#ifdef WITH_LIBPNG
-bool is_png(const void *, unsigned);
-void load_png(IO::Base &, Image::Private &);
+bool is_png(const char *, unsigned);
+void load_png(IO::Base &, Image::Private &, const char *, unsigned);
#endif
} // namespace Graphics