]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/image.cpp
Proper support for copying images
[libs/gui.git] / source / graphics / image.cpp
index 5d2a21bb66e969d85da76f34811d4ff5e1f9836b..a0e6e380ad24d54b20c5144c6001208147f9dbc5 100644 (file)
-#ifdef WITH_DEVIL
-#include <IL/il.h>
-#endif
+#include <msp/core/refptr.h>
 #include <msp/fs/utils.h>
 #include <msp/io/file.h>
 #include <msp/io/memory.h>
 #include "image.h"
-#include "image_devil.h"
-#include "image_png.h"
-#include "image_private.h"
+#include "imageloader.h"
 
 using namespace std;
 
 namespace Msp {
 namespace Graphics {
 
-Image::Private::Private()
-{
-#ifdef WITH_DEVIL
-       id = 0;
-#endif
-       fmt = RGB;
-       width = 0;
-       height = 0;
-       data = 0;
-}
+Image::Data::Data():
+       fmt(RGB),
+       width(0),
+       height(0),
+       data(0)
+{ }
 
-
-Image::Image():
-       priv(new Private)
+Image::Data::Data(const Data &other):
+       fmt(other.fmt),
+       width(other.width),
+       height(other.height),
+       stride(other.stride),
+       data(other.data ? new char[stride*height] : 0)
 {
-#if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
-       throw runtime_error("no image support");
-#endif
+       if(data)
+               copy(other.data, other.data+stride*height, data);
 }
 
-Image::~Image()
+Image::Data &Image::Data::operator=(const Data &other)
 {
-#ifdef WITH_DEVIL
-       if(priv->id)
-               ilDeleteImages(1, &priv->id);
-       else
-#endif
-       delete[] priv->data;
-       delete priv;
-}
+       delete[] data;
+       data = 0;
 
-void Image::load_file(const string &fn)
-{
-       string ext = FS::extpart(fn);
-#ifdef WITH_LIBPNG
-       if(ext==".png")
-       {
-               IO::BufferedFile file(fn);
-               load_png(file, *priv);
-       }
-       else
-#endif
-       {
-#ifdef WITH_DEVIL
-               load_devil_file(fn, *priv);
-#else
-               throw unsupported_image_format("DevIL needed for non-PNG images");
-#endif
-       }
-       (void)fn;
-}
+       fmt = other.fmt;
+       width = other.width;
+       height = other.height;
+       stride = other.stride;
 
-void Image::load_memory(const void *data, unsigned size)
-{
-#ifdef WITH_LIBPNG
-       if(size>=8 && is_png(data, 8))
+       if(other.data)
        {
-               IO::Memory mem(reinterpret_cast<const char *>(data), size);
-               load_png(mem, *priv);
+               data = new char[stride*height];
+               copy(other.data, other.data+stride*height, data);
        }
-       else
-#endif
-       {
-#ifdef WITH_DEVIL
-               load_devil_mem(data, size, *priv);
-#else
-               throw unsupported_image_format("DevIL needed for non-PNG images");
-#endif
-       }
-       (void)data;
-       (void)size;
+
+       return *this;
 }
 
-PixelFormat Image::get_format() const
+Image::Data::~Data()
 {
-       return priv->fmt;
+       delete[] data;
 }
 
-unsigned Image::get_width() const
+
+void Image::load_file(const string &fn)
 {
-       return priv->width;
+       RefPtr<ImageLoader> loader = ImageLoader::open_file(fn);
+       load(*loader);
 }
 
-unsigned Image::get_height() const
+void Image::load_io(IO::Seekable &io)
 {
-       return priv->height;
+       RefPtr<ImageLoader> loader = ImageLoader::open_io(io);
+       load(*loader);
 }
 
-const void *Image::get_data() const
+void Image::load(ImageLoader &loader)
 {
-       return priv->data;
+       loader.load(data);
 }
 
 } // namespace Graphics