]> 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 fcfd222c165e7d6472fa4fdf4b6ea1c34d02ae68..2835d05e872936d0cc069fa806812ff3ab952661 100644 (file)
@@ -14,6 +14,7 @@ Image::Data::Data():
        fmt(RGB),
        width(0),
        height(0),
+       owned_pixels(0),
        pixels(0)
 { }
 
@@ -22,7 +23,8 @@ Image::Data::Data(const Data &other):
        width(other.width),
        height(other.height),
        stride(other.stride),
-       pixels(other.pixels ? new char[stride*height] : 0)
+       owned_pixels(other.pixels ? new char[stride*height] : 0),
+       pixels(owned_pixels)
 {
        if(pixels)
                copy(other.pixels, other.pixels+stride*height, pixels);
@@ -30,8 +32,8 @@ Image::Data::Data(const Data &other):
 
 Image::Data &Image::Data::operator=(const Data &other)
 {
-       delete[] pixels;
-       pixels = 0;
+       delete[] owned_pixels;
+       pixels = owned_pixels = 0;
 
        fmt = other.fmt;
        width = other.width;
@@ -40,7 +42,7 @@ Image::Data &Image::Data::operator=(const Data &other)
 
        if(other.pixels)
        {
-               pixels = new char[stride*height];
+               pixels = owned_pixels = new char[stride*height];
                copy(other.pixels, other.pixels+stride*height, pixels);
        }
 
@@ -49,7 +51,7 @@ Image::Data &Image::Data::operator=(const Data &other)
 
 Image::Data::~Data()
 {
-       delete[] pixels;
+       delete[] owned_pixels;
 }
 
 
@@ -72,6 +74,12 @@ void Image::load(ImageLoader &loader)
        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)