From e0dddb42d0513fc53b52ea7914771119b859df3c Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sat, 5 Mar 2022 19:37:02 +0100 Subject: [PATCH] Added utility functions --- Src/BMP.cpp | 57 +++++++++++++++++++++++++++++++++++------------------ Src/BMP.h | 21 +++++++++++++++++--- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/Src/BMP.cpp b/Src/BMP.cpp index 39682d5..340b9f3 100644 --- a/Src/BMP.cpp +++ b/Src/BMP.cpp @@ -13,25 +13,11 @@ namespace Leonetienne::BmpPP { { pixelBuffer.clear(); - pixelBuffer.resize(size.x * size.y * ColormodeToPixelSize(colormode)); + pixelBuffer.resize(size.x * size.y * GetNumColorChannels()); return; } - int BMP::ColormodeToPixelSize(const Colormode &colormode) { - - switch (colormode) { - case Colormode::RGB: - return 3; - - case Colormode::RGBA: - return 4; - } - - // Unreachable - return -1; - } - bool BMP::Write(const std::string &filename) const { // Create the bmp header @@ -49,7 +35,7 @@ namespace Leonetienne::BmpPP { // Populate dib header bmpHeader.dibHeader.imageWidth = size.x; bmpHeader.dibHeader.imageHeight = size.y; - bmpHeader.dibHeader.numBitsPerPixel = ColormodeToPixelSize(colormode) * 8; + bmpHeader.dibHeader.numBitsPerPixel = GetNumColorChannels() * 8; // The size of the pixel array is not known yet (because rows require to be padded) @@ -58,7 +44,7 @@ namespace Leonetienne::BmpPP { packedPixels.reserve(pixelBuffer.size()); // How many channels do we have? - const std::size_t numChannels = ColormodeToPixelSize(colormode); + const std::size_t numChannels = GetNumColorChannels(); // Calculate how many padding bytes to add per row std::size_t paddingBytesPerRow = (4 - ((size.x * numChannels) % 4)) % 4; @@ -152,7 +138,7 @@ namespace Leonetienne::BmpPP { std::uint8_t *BMP::GetPixel(const Eule::Vector2i &position) { const std::size_t pixelIndex = - (position.y * size.x + position.x) * ColormodeToPixelSize(colormode); + (position.y * size.x + position.x) * GetNumColorChannels(); if (pixelIndex >= pixelBuffer.size()) throw std::runtime_error("Pixel index out of range!"); @@ -162,7 +148,7 @@ namespace Leonetienne::BmpPP { const std::uint8_t *BMP::GetPixel(const Eule::Vector2i &position) const { const std::size_t pixelIndex = - (position.y * size.x + position.x) * ColormodeToPixelSize(colormode); + (position.y * size.x + position.x) * GetNumColorChannels(); if (pixelIndex >= pixelBuffer.size()) throw std::runtime_error("Pixel index out of range!"); @@ -196,4 +182,37 @@ namespace Leonetienne::BmpPP { return; } + std::uint8_t *BMP::data() { + return pixelBuffer.data(); + } + + const std::uint8_t *BMP::data() const { + return pixelBuffer.data(); + } + + const Eule::Vector2i &BMP::GetDimensions() const { + return size; + } + + const Colormode &BMP::GetColormode() const { + return colormode; + } + + std::size_t BMP::GetNumColorChannels() const { + switch (colormode) { + case Colormode::RGB: + return 3; + + case Colormode::RGBA: + return 4; + } + + // Unreachable + return -1; + } + + std::size_t BMP::GetPixelbufferSize() const { + return pixelBuffer.size(); + } + } diff --git a/Src/BMP.h b/Src/BMP.h index 8fc1b3a..e9f47ab 100644 --- a/Src/BMP.h +++ b/Src/BMP.h @@ -24,10 +24,25 @@ namespace Leonetienne::BmpPP { //! Will set the color of a pixel at a given position void SetPixel(const Eule::Vector2i& position, const std::uint8_t r, const std::uint8_t g, const std::uint8_t b, const std::uint8_t a = 0xFF); - private: - //! Will return the corresponding pixel size (in bytes) of a colormode. Like, 3 for RGB and 4 for RGBA. - static int ColormodeToPixelSize(const Colormode& colormode); + //! Will return a pointer to the raw pixel data + std::uint8_t* data(); + //! Will return a pointer to the raw pixel data + const std::uint8_t* data() const; + + //! Will return the dimensions of the image + const Eule::Vector2i& GetDimensions() const; + + //! Will return the color mode of the image + const Colormode& GetColormode() const; + + //! Will return the amount of color channels used + std::size_t GetNumColorChannels() const; + + //! Will return the size of the raw pixel buffer, in bytes + std::size_t GetPixelbufferSize() const; + + private: Eule::Vector2i size; Colormode colormode; std::vector pixelBuffer;