]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/imageloader.cpp
Use the append function to build hex dump
[libs/gui.git] / source / graphics / imageloader.cpp
index da897984449993b56e3163b5ffd8830da06e22a9..e224af63cd161495a7e13a7ee795c92273864678 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/core/refptr.h>
 #include <msp/io/file.h>
 #include <msp/strings/format.h>
+#include <msp/strings/utils.h>
 #include "bmploader.h"
 #include "imageloader.h"
 #ifdef WITH_LIBPNG
@@ -22,7 +23,8 @@ namespace Msp {
 namespace Graphics {
 
 ImageLoader::ImageLoader():
-       source(0)
+       source(0),
+       state(INITIAL)
 { }
 
 ImageLoader::~ImageLoader()
@@ -72,11 +74,7 @@ ImageLoader *ImageLoader::open_io(IO::Seekable &io)
        {
                string sig_hex;
                for(unsigned i=0; i<sig_len; ++i)
-               {
-                       if(i)
-                               sig_hex += ' ';
-                       sig_hex += format("%02X", static_cast<unsigned char>(sig_buf[i]));
-               }
+                       append(sig_hex, " ", format("%02X", static_cast<unsigned char>(sig_buf[i])));
                throw unsupported_image_format(sig_hex);
        }
 
@@ -85,8 +83,24 @@ ImageLoader *ImageLoader::open_io(IO::Seekable &io)
 
 void ImageLoader::load(Image::Data &data)
 {
-       load_headers(data);
-       load_data(data);
+       if(state>=FINISHED)
+               throw logic_error("already loaded");
+
+       if(state<HEADERS_LOADED)
+               load_headers_(data);
+       if(!data.pixels)
+               data.pixels = data.owned_pixels = new char[data.stride*data.height];
+       load_pixels_(data);
+       state = FINISHED;
+}
+
+void ImageLoader::load_headers(Image::Data &data)
+{
+       if(state>=HEADERS_LOADED)
+               throw logic_error("headers already loaded");
+
+       load_headers_(data);
+       state = HEADERS_LOADED;
 }
 
 ImageLoader::Registry &ImageLoader::get_registry()