-#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_png.h"
-#include "image_private.h"
+#include "imageloader.h"
using namespace std;
namespace Msp {
namespace Graphics {
-Image::Private::Private()
+Image::Data::Data():
+ fmt(RGB),
+ width(0),
+ height(0),
+ data(0)
+{ }
+
+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)
{
-#ifdef WITH_DEVIL
- id = 0;
-#endif
-#ifdef WITH_LIBPNG
- fmt = RGB;
- width = 0;
- height = 0;
- data = 0;
-#endif
+ if(data)
+ copy(other.data, other.data+stride*height, data);
}
-
-namespace {
-
-#ifdef WITH_DEVIL
-void ensure_devil_image(unsigned &id)
+Image::Data &Image::Data::operator=(const Data &other)
{
- static bool init_done = false;
+ delete[] data;
+ data = 0;
+
+ fmt = other.fmt;
+ width = other.width;
+ height = other.height;
+ stride = other.stride;
- if(!init_done)
+ if(other.data)
{
- ilInit();
- ilEnable(IL_ORIGIN_SET);
- ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
- init_done = true;
+ data = new char[stride*height];
+ copy(other.data, other.data+stride*height, data);
}
- if(!id)
- ilGenImages(1, &id);
+ return *this;
}
-#endif
-}
-
-
-Image::Image():
- priv(new Private)
+Image::Data::~Data()
{
-#if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
- throw runtime_error("no image support");
-#endif
+ delete[] data;
}
-Image::~Image()
-{
-#ifdef WITH_DEVIL
- if(priv->id)
- ilDeleteImages(1, &priv->id);
-#endif
-#ifdef WITH_LIBPNG
- delete[] priv->data;
-#endif
- delete priv;
-}
void Image::load_file(const string &fn)
{
-#ifdef WITH_LIBPNG
- if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
- {
- IO::BufferedFile file(fn);
- load_png(file, *priv);
- }
- else
-#endif
- {
-#ifdef WITH_DEVIL
- ensure_devil_image(priv->id);
- ilBindImage(priv->id);
- if(!ilLoadImage(const_cast<char *>(fn.c_str())))
- throw bad_image_data("IL error");
-#else
- throw unsupported_image_format("DevIL needed for non-PNG images");
-#endif
- }
- (void)fn;
+ RefPtr<ImageLoader> loader = ImageLoader::open_file(fn);
+ load(*loader);
}
-void Image::load_memory(const void *data, unsigned size)
+void Image::load_io(IO::Seekable &io)
{
-#ifdef WITH_LIBPNG
- if(size>=8 && is_png(data, 8))
- {
- IO::Memory mem(reinterpret_cast<const char *>(data), size);
- load_png(mem, *priv);
- }
- else
-#endif
- {
-#ifdef WITH_DEVIL
- ensure_devil_image(priv->id);
- ilBindImage(priv->id);
- if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
- throw bad_image_data("IL error");
-#else
- throw unsupported_image_format("DevIL needed for non-PNG images");
-#endif
- }
- (void)data;
- (void)size;
+ RefPtr<ImageLoader> loader = ImageLoader::open_io(io);
+ load(*loader);
}
-PixelFormat Image::get_format() const
+void Image::load(ImageLoader &loader)
{
-#ifdef WITH_LIBPNG
- if(priv->data)
- return priv->fmt;
-#endif
-#ifdef WITH_DEVIL
- if(priv->id)
- {
- ilBindImage(priv->id);
- switch(ilGetInteger(IL_IMAGE_FORMAT))
- {
- case IL_COLOR_INDEX: return COLOR_INDEX;
- case IL_LUMINANCE: return LUMINANCE;
- case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
- case IL_RGB: return RGB;
- case IL_RGBA: return RGBA;
- case IL_BGR: return BGR;
- case IL_BGRA: return BGRA;
- // XXX bad, should throw when loading
- default: throw invalid_argument("unknown pixel format in image");
- }
- }
-#endif
- return RGB;
-}
-
-unsigned Image::get_width() const
-{
-#ifdef WITH_LIBPNG
- if(priv->data)
- return priv->width;
-#endif
-#ifdef WITH_DEVIL
- if(priv->id)
- {
- ilBindImage(priv->id);
- return ilGetInteger(IL_IMAGE_WIDTH);
- }
-#endif
- return 0;
-}
-
-unsigned Image::get_height() const
-{
-#ifdef WITH_LIBPNG
- if(priv->data)
- return priv->height;
-#endif
-#ifdef WITH_DEVIL
- if(priv->id)
- {
- ilBindImage(priv->id);
- return ilGetInteger(IL_IMAGE_HEIGHT);
- }
-#endif
- return 0;
-}
-
-const void *Image::get_data() const
-{
-#ifdef WITH_LIBPNG
- if(priv->data)
- return priv->data;
-#endif
-#ifdef WITH_DEVIL
- if(priv->id)
- {
- ilBindImage(priv->id);
- return ilGetData();
- }
-#endif
- return 0;
+ loader.load(data);
}
} // namespace Graphics