Browse Source

Add image::get_size()

master
C. J. Howard 4 years ago
parent
commit
693bbceb55
2 changed files with 25 additions and 9 deletions
  1. +14
    -8
      src/resources/image.cpp
  2. +11
    -1
      src/resources/image.hpp

+ 14
- 8
src/resources/image.cpp View File

@ -24,7 +24,8 @@ image::image():
width(0), width(0),
height(0), height(0),
channels(4), channels(4),
pixels(nullptr)
pixels(nullptr),
size(0)
{} {}
image::~image() image::~image()
@ -60,13 +61,18 @@ void image::resize(unsigned int width, unsigned int height)
void image::allocate_pixels() 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; pixels = nullptr;
size = 0;
} }
} }

+ 11
- 1
src/resources/image.hpp View File

@ -20,6 +20,8 @@
#ifndef ANTKEEPER_IMAGE_HPP #ifndef ANTKEEPER_IMAGE_HPP
#define ANTKEEPER_IMAGE_HPP #define ANTKEEPER_IMAGE_HPP
#include <cstdlib>
/** /**
* Stores basic image data. * Stores basic image data.
*/ */
@ -65,6 +67,9 @@ public:
/// @copydoc image::get_pixels() const /// @copydoc image::get_pixels() const
void* get_pixels(); void* get_pixels();
/// Returns the size of the image, in bytes.
std::size_t get_size() const;
private: private:
void allocate_pixels(); void allocate_pixels();
@ -75,6 +80,7 @@ private:
unsigned int height; unsigned int height;
unsigned int channels; unsigned int channels;
void* pixels; void* pixels;
std::size_t size;
}; };
inline bool image::is_hdr() const inline bool image::is_hdr() const
@ -107,5 +113,9 @@ inline void* image::get_pixels()
return pixels; return pixels;
} }
#endif // ANTKEEPER_IMAGE_HPP
inline std::size_t image::get_size() const
{
return size;
}
#endif // ANTKEEPER_IMAGE_HPP

Loading…
Cancel
Save