Added utility functions
This commit is contained in:
parent
14294fcebf
commit
e0dddb42d0
57
Src/BMP.cpp
57
Src/BMP.cpp
@ -13,25 +13,11 @@ namespace Leonetienne::BmpPP {
|
|||||||
{
|
{
|
||||||
|
|
||||||
pixelBuffer.clear();
|
pixelBuffer.clear();
|
||||||
pixelBuffer.resize(size.x * size.y * ColormodeToPixelSize(colormode));
|
pixelBuffer.resize(size.x * size.y * GetNumColorChannels());
|
||||||
|
|
||||||
return;
|
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 {
|
bool BMP::Write(const std::string &filename) const {
|
||||||
|
|
||||||
// Create the bmp header
|
// Create the bmp header
|
||||||
@ -49,7 +35,7 @@ namespace Leonetienne::BmpPP {
|
|||||||
// Populate dib header
|
// Populate dib header
|
||||||
bmpHeader.dibHeader.imageWidth = size.x;
|
bmpHeader.dibHeader.imageWidth = size.x;
|
||||||
bmpHeader.dibHeader.imageHeight = size.y;
|
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)
|
// 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());
|
packedPixels.reserve(pixelBuffer.size());
|
||||||
|
|
||||||
// How many channels do we have?
|
// 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
|
// Calculate how many padding bytes to add per row
|
||||||
std::size_t paddingBytesPerRow = (4 - ((size.x * numChannels) % 4)) % 4;
|
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) {
|
std::uint8_t *BMP::GetPixel(const Eule::Vector2i &position) {
|
||||||
const std::size_t pixelIndex =
|
const std::size_t pixelIndex =
|
||||||
(position.y * size.x + position.x) * ColormodeToPixelSize(colormode);
|
(position.y * size.x + position.x) * GetNumColorChannels();
|
||||||
|
|
||||||
if (pixelIndex >= pixelBuffer.size())
|
if (pixelIndex >= pixelBuffer.size())
|
||||||
throw std::runtime_error("Pixel index out of range!");
|
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::uint8_t *BMP::GetPixel(const Eule::Vector2i &position) const {
|
||||||
const std::size_t pixelIndex =
|
const std::size_t pixelIndex =
|
||||||
(position.y * size.x + position.x) * ColormodeToPixelSize(colormode);
|
(position.y * size.x + position.x) * GetNumColorChannels();
|
||||||
|
|
||||||
if (pixelIndex >= pixelBuffer.size())
|
if (pixelIndex >= pixelBuffer.size())
|
||||||
throw std::runtime_error("Pixel index out of range!");
|
throw std::runtime_error("Pixel index out of range!");
|
||||||
@ -196,4 +182,37 @@ namespace Leonetienne::BmpPP {
|
|||||||
return;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
21
Src/BMP.h
21
Src/BMP.h
@ -24,10 +24,25 @@ namespace Leonetienne::BmpPP {
|
|||||||
//! Will set the color of a pixel at a given position
|
//! 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);
|
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 a pointer to the raw pixel data
|
||||||
//! Will return the corresponding pixel size (in bytes) of a colormode. Like, 3 for RGB and 4 for RGBA.
|
std::uint8_t* data();
|
||||||
static int ColormodeToPixelSize(const Colormode& colormode);
|
|
||||||
|
|
||||||
|
//! 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;
|
Eule::Vector2i size;
|
||||||
Colormode colormode;
|
Colormode colormode;
|
||||||
std::vector<std::uint8_t> pixelBuffer;
|
std::vector<std::uint8_t> pixelBuffer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user