]> git.tdb.fi Git - libs/gui.git/commitdiff
Replace Image::load_memory with load_io
authorMikko Rasa <tdb@tdb.fi>
Wed, 16 Jan 2013 16:37:11 +0000 (18:37 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 16 Jan 2013 16:37:11 +0000 (18:37 +0200)
source/graphics/image.cpp
source/graphics/image.h
source/graphics/image_devil.cpp
source/graphics/image_devil.h
source/graphics/image_png.cpp
source/graphics/image_png.h

index 5d2a21bb66e969d85da76f34811d4ff5e1f9836b..46c2b3fab3c29f0ce952133b1d55966531252e94 100644 (file)
@@ -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<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
index bc7151b1250ba1dee336439350589027b6540363..137f856f21f5736d40dbecae6275a26919c42536 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <stdexcept>
 #include <string>
+#include <msp/io/base.h>
 #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;
index d355e55c539e54ffd1af51901aff1d0bea41c749..b09ae3dd9078703fa020a2dc52d64fa6e43521a5 100644 (file)
@@ -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<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);
 }
index 4e669e777b028ed5a15d1555ecc8dc86461c622b..790f387da3fe3b788ba081831a3b1f7b8494afe8 100644 (file)
@@ -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
index f2247863dac57d8f2636af3eaadeb5dc4a8f6edd..28384480f488a6cb9534f598fbccb59f8f75c37b 100644 (file)
@@ -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<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;
@@ -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;
index e07536ab32e05bf1035039778b0ef0dfb83b00af..8291a2ff55e46614a9af66bc3139269b661feec7 100644 (file)
@@ -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