]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/image.cpp
Use nullptr in place of 0 or NULL
[libs/gui.git] / source / graphics / image.cpp
index e22d8eaef79958eb7ff83d389de018d0365c53c5..813672bca52d8fdec846629e53845af9c3a6dd17 100644 (file)
@@ -1,8 +1,8 @@
+#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 "imageloader.h"
 
 using namespace std;
@@ -10,19 +10,13 @@ using namespace std;
 namespace Msp {
 namespace Graphics {
 
-Image::Data::Data():
-       fmt(RGB),
-       width(0),
-       height(0),
-       pixels(0)
-{ }
-
 Image::Data::Data(const Data &other):
        fmt(other.fmt),
        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] : nullptr),
+       pixels(owned_pixels)
 {
        if(pixels)
                copy(other.pixels, other.pixels+stride*height, pixels);
@@ -30,8 +24,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 = nullptr;
 
        fmt = other.fmt;
        width = other.width;
@@ -40,7 +34,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 +43,7 @@ Image::Data &Image::Data::operator=(const Data &other)
 
 Image::Data::~Data()
 {
-       delete[] pixels;
+       delete[] owned_pixels;
 }
 
 
@@ -67,8 +61,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