From 693bbceb555dcae26704011ab360d6a2cc75ea12 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Mon, 17 Aug 2020 17:29:53 -0700 Subject: [PATCH] Add image::get_size() --- src/resources/image.cpp | 22 ++++++++++++++-------- src/resources/image.hpp | 12 +++++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 39f3f59..90c4b34 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -24,7 +24,8 @@ image::image(): width(0), height(0), channels(4), - pixels(nullptr) + pixels(nullptr), + size(0) {} image::~image() @@ -60,13 +61,18 @@ void image::resize(unsigned int width, unsigned int height) void image::allocate_pixels() { - if (hdr) + size = width * height * channels; + + if (size != 0) { - pixels = new float[width * height * channels]; - } - else - { - pixels = new unsigned char[width * height * channels]; + if (hdr) + { + pixels = new float[size]; + } + else + { + pixels = new unsigned char[size]; + } } } @@ -84,6 +90,6 @@ void image::free_pixels() } pixels = nullptr; + size = 0; } } - diff --git a/src/resources/image.hpp b/src/resources/image.hpp index bc06d56..6bedb4e 100644 --- a/src/resources/image.hpp +++ b/src/resources/image.hpp @@ -20,6 +20,8 @@ #ifndef ANTKEEPER_IMAGE_HPP #define ANTKEEPER_IMAGE_HPP +#include + /** * Stores basic image data. */ @@ -65,6 +67,9 @@ public: /// @copydoc image::get_pixels() const void* get_pixels(); + + /// Returns the size of the image, in bytes. + std::size_t get_size() const; private: void allocate_pixels(); @@ -75,6 +80,7 @@ private: unsigned int height; unsigned int channels; void* pixels; + std::size_t size; }; inline bool image::is_hdr() const @@ -107,5 +113,9 @@ inline void* image::get_pixels() return pixels; } -#endif // ANTKEEPER_IMAGE_HPP +inline std::size_t image::get_size() const +{ + return size; +} +#endif // ANTKEEPER_IMAGE_HPP