From 1ca709deba08e55d95066be564a8fd43a321af19 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 16 Jan 2013 18:37:11 +0200 Subject: [PATCH] Replace Image::load_memory with load_io --- source/graphics/image.cpp | 17 +++++++---------- source/graphics/image.h | 3 ++- source/graphics/image_devil.cpp | 15 +++++++++++++-- source/graphics/image_devil.h | 2 +- source/graphics/image_png.cpp | 7 ++++--- source/graphics/image_png.h | 4 ++-- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/source/graphics/image.cpp b/source/graphics/image.cpp index 5d2a21b..46c2b3f 100644 --- a/source/graphics/image.cpp +++ b/source/graphics/image.cpp @@ -52,7 +52,7 @@ void Image::load_file(const string &fn) if(ext==".png") { IO::BufferedFile file(fn); - load_png(file, *priv); + load_png(file, *priv, 0, 0); } else #endif @@ -66,25 +66,22 @@ void Image::load_file(const string &fn) (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(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 diff --git a/source/graphics/image.h b/source/graphics/image.h index bc7151b..137f856 100644 --- a/source/graphics/image.h +++ b/source/graphics/image.h @@ -3,6 +3,7 @@ #include #include +#include #include "pixelformat.h" namespace Msp { @@ -36,7 +37,7 @@ public: ~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; diff --git a/source/graphics/image_devil.cpp b/source/graphics/image_devil.cpp index d355e55..b09ae3d 100644 --- a/source/graphics/image_devil.cpp +++ b/source/graphics/image_devil.cpp @@ -61,10 +61,21 @@ void load_devil_file(const string &fn, Image::Private &priv) 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 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(data), size)) + if(!ilLoadL(IL_TYPE_UNKNOWN, &data.front(), data.size())) throw bad_image_data("IL error"); finish_load(priv); } diff --git a/source/graphics/image_devil.h b/source/graphics/image_devil.h index 4e669e7..790f387 100644 --- a/source/graphics/image_devil.h +++ b/source/graphics/image_devil.h @@ -8,7 +8,7 @@ namespace Graphics { #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 diff --git a/source/graphics/image_png.cpp b/source/graphics/image_png.cpp index f224786..2838448 100644 --- a/source/graphics/image_png.cpp +++ b/source/graphics/image_png.cpp @@ -31,12 +31,12 @@ void error(png_struct *png, const char *msg) 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(const_cast(buf)), 0, len); + return !png_sig_cmp(reinterpret_cast(const_cast(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; @@ -52,6 +52,7 @@ void load_png(IO::Base &in, Image::Private &priv) 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; diff --git a/source/graphics/image_png.h b/source/graphics/image_png.h index e07536a..8291a2f 100644 --- a/source/graphics/image_png.h +++ b/source/graphics/image_png.h @@ -8,8 +8,8 @@ namespace Msp { 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 -- 2.45.2