-#ifdef WITH_DEVIL
-#include <IL/il.h>
-#endif
+#include "image.h"
+#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()
+Image::Data::Data(const Data &other):
+ fmt(other.fmt),
+ width(other.width),
+ height(other.height),
+ stride(other.stride),
+ owned_pixels(other.pixels ? new char[stride*height] : nullptr),
+ pixels(owned_pixels)
{
-#ifdef WITH_DEVIL
- id = 0;
-#endif
- fmt = RGB;
- width = 0;
- height = 0;
- data = 0;
+ if(pixels)
+ copy(other.pixels, other.pixels+stride*height, pixels);
}
-
-Image::Image():
- priv(new Private)
+Image::Data &Image::Data::operator=(const Data &other)
{
-#if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
- throw runtime_error("no image support");
-#endif
-}
+ delete[] owned_pixels;
+ pixels = owned_pixels = nullptr;
-Image::~Image()
-{
-#ifdef WITH_DEVIL
- if(priv->id)
- ilDeleteImages(1, &priv->id);
- else
-#endif
- delete[] priv->data;
- delete priv;
-}
+ fmt = other.fmt;
+ width = other.width;
+ height = other.height;
+ stride = other.stride;
-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, 0, 0);
- }
- else
-#endif
+ if(other.pixels)
{
-#ifdef WITH_DEVIL
- load_devil_file(fn, *priv);
-#else
- throw unsupported_image_format("DevIL needed for non-PNG images");
-#endif
+ pixels = owned_pixels = new char[stride*height];
+ copy(other.pixels, other.pixels+stride*height, pixels);
}
- (void)fn;
+
+ return *this;
}
-void Image::load_io(IO::Base &io)
+Image::Data::~Data()
{
- char sig_buf[8];
- unsigned sig_len = io.read(sig_buf, sizeof(sig_buf));
-#ifdef WITH_LIBPNG
- 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_io(io, *priv, sig_buf, sig_len);
-#else
- throw unsupported_image_format("DevIL needed for non-PNG images");
-#endif
- }
+ delete[] owned_pixels;
+}
+
+
+void Image::load_file(const string &fn)
+{
+ RefPtr<ImageLoader> loader = ImageLoader::open_file(fn);
+ load(*loader);
}
-PixelFormat Image::get_format() const
+void Image::load_io(IO::Seekable &io)
{
- return priv->fmt;
+ RefPtr<ImageLoader> loader = ImageLoader::open_io(io);
+ load(*loader);
}
-unsigned Image::get_width() const
+void Image::load(ImageLoader &loader)
{
- return priv->width;
+ if(loader.get_state()==ImageLoader::INITIAL)
+ data = Data();
+ loader.load(data);
}
-unsigned Image::get_height() const
+void Image::load_into(ImageLoader &loader, void *buffer)
{
- return priv->height;
+ data.pixels = reinterpret_cast<char *>(buffer);
+ load(loader);
}
-const void *Image::get_data() const
+void Image::load_headers(ImageLoader &loader)
{
- return priv->data;
+ if(loader.get_state()==ImageLoader::INITIAL)
+ data = Data();
+ loader.load_headers(data);
}
} // namespace Graphics