]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/image.cpp
Make it possible to load an image into an externally allocated buffer
[libs/gui.git] / source / graphics / image.cpp
index b22be1969d057e161cd60af0be3d9785c320b438..2835d05e872936d0cc069fa806812ff3ab952661 100644 (file)
@@ -14,12 +14,44 @@ Image::Data::Data():
        fmt(RGB),
        width(0),
        height(0),
-       data(0)
+       owned_pixels(0),
+       pixels(0)
 { }
 
+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] : 0),
+       pixels(owned_pixels)
+{
+       if(pixels)
+               copy(other.pixels, other.pixels+stride*height, pixels);
+}
+
+Image::Data &Image::Data::operator=(const Data &other)
+{
+       delete[] owned_pixels;
+       pixels = owned_pixels = 0;
+
+       fmt = other.fmt;
+       width = other.width;
+       height = other.height;
+       stride = other.stride;
+
+       if(other.pixels)
+       {
+               pixels = owned_pixels = new char[stride*height];
+               copy(other.pixels, other.pixels+stride*height, pixels);
+       }
+
+       return *this;
+}
+
 Image::Data::~Data()
 {
-       delete[] data;
+       delete[] owned_pixels;
 }
 
 
@@ -37,8 +69,23 @@ void Image::load_io(IO::Seekable &io)
 
 void Image::load(ImageLoader &loader)
 {
+       if(loader.get_state()==ImageLoader::INITIAL)
+               data = Data();
        loader.load(data);
 }
 
+void Image::load_into(ImageLoader &loader, void *buffer)
+{
+       data.pixels = reinterpret_cast<char *>(buffer);
+       load(loader);
+}
+
+void Image::load_headers(ImageLoader &loader)
+{
+       if(loader.get_state()==ImageLoader::INITIAL)
+               data = Data();
+       loader.load_headers(data);
+}
+
 } // namespace Graphics
 } // namespace Msp