]> git.tdb.fi Git - libs/gui.git/commitdiff
Make it possible to load an image into an externally allocated buffer
authorMikko Rasa <tdb@tdb.fi>
Sun, 7 Feb 2021 16:15:18 +0000 (18:15 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 7 Feb 2021 16:15:18 +0000 (18:15 +0200)
source/graphics/bmploader.cpp
source/graphics/devil/devilloader.cpp
source/graphics/image.cpp
source/graphics/image.h
source/graphics/imageloader.cpp
source/graphics/jpeg/jpegloader.cpp
source/graphics/png/pngloader.cpp
source/graphics/quartz/quartzloader.cpp

index d88e2aa6435f36f3c908bf3c274a9799a5b2339e..a3394635611ad93254727fbcae46b508ca54322e 100644 (file)
@@ -98,7 +98,6 @@ void BmpLoader::load_headers_(Image::Data &data)
 
 void BmpLoader::load_pixels_(Image::Data &data)
 {
-       data.pixels = new char[data.stride*data.height];
        if(invert_row_order)
        {
                for(unsigned y=0; y<data.height; ++y)
index a6693080748a001b87612615920e477e642b1018..d4c7bfffbcc3d11f21e60709c02e00065c442628 100644 (file)
@@ -135,7 +135,6 @@ void DevilLoader::load_pixels_(Image::Data &data)
        ilBindImage(id);
 
        unsigned data_size = data.stride*data.height;
-       data.pixels = new char[data_size];
        ILubyte *il_data = ilGetData();
        copy(il_data, il_data+data_size, data.pixels);
 
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)
index 7b5236ecd4f30c151c9c42148e955086c73cc17b..51147cefdcf341a882a426933efe52576721d155 100644 (file)
@@ -20,6 +20,7 @@ public:
                unsigned width;
                unsigned height;
                unsigned stride;
+               char *owned_pixels;
                char *pixels;
 
                Data();
@@ -35,6 +36,7 @@ public:
        void load_file(const std::string &);
        void load_io(IO::Seekable &);
        void load(ImageLoader &);
+       void load_into(ImageLoader &, void *);
        void load_headers(ImageLoader &);
 
        PixelFormat get_format() const { return data.fmt; }
index fa6fcf802b2a57eb6d852e34c6363d375e521ba4..52b087490d21b58612219952ec89844ad06c0e2a 100644 (file)
@@ -91,6 +91,8 @@ void ImageLoader::load(Image::Data &data)
 
        if(state<HEADERS_LOADED)
                load_headers_(data);
+       if(!data.pixels)
+               data.pixels = data.owned_pixels = new char[data.stride*data.height];
        load_pixels_(data);
        state = FINISHED;
 }
index 4dd9440f7856d2731585acc5db4aef8325bb83ac..45689cf65725a032b3020ed62d546be46db4b345 100644 (file)
@@ -136,7 +136,6 @@ void JpegLoader::load_headers_(Image::Data &data)
 
 void JpegLoader::load_pixels_(Image::Data &data)
 {
-       data.pixels = new char[data.stride*data.height];
        JSAMPROW rows[8];
        while(priv->jpeg.output_scanline<data.height)
        {
index 190d3cf297488e10977884080c5ad847b8645099..1dfa77362d2111d88153003f30155a8ff56edd59 100644 (file)
@@ -98,8 +98,6 @@ void PngLoader::load_pixels_(Image::Data &data)
                throw bad_image_data(priv->message);
        }
 
-       data.pixels = new char[data.stride*data.height];
-
        if(priv->interlace==PNG_INTERLACE_ADAM7)
        {
                // ADAM7 requires all rows to be loaded at once
index e025b66cf4786b852ed2a176ed0ee49b7e7d977c..3799bcde6681fe7ccac941f43bc179ceb70392b2 100644 (file)
@@ -121,7 +121,6 @@ void QuartzLoader::load_(Image::Data &data)
 {
        CGDataProviderRef dp = CGImageGetDataProvider(image);
        CFDataRef image_data = CGDataProviderCopyData(dp);
-       data.pixels = new char[data.height*data.stride];
        unsigned offset = (alpha==kCGImageAlphaNoneSkipFirst);
        CFRange range = CFRangeMake(offset, CFDataGetLength(image_data)-offset);
        CFDataGetBytes(image_data, range, reinterpret_cast<UInt8 *>(data.pixels));