Added tests for write, and added tests/implementation of operator==. Also added implementation of FillChannel()

This commit is contained in:
Leonetienne
2022-03-06 12:45:09 +01:00
parent eb4fc0e964
commit 04a1104065
8 changed files with 223 additions and 12 deletions

View File

@@ -37,7 +37,11 @@ namespace Leonetienne::BmpPP {
// Re-initialize the pixelbuffer
pixelBuffer.clear();
pixelBuffer.resize(size.x * size.y * GetNumColorChannels());
pixelBuffer.resize(size.x * size.y * GetNumChannels());
// If we're initializing with an alpha channel, set it to 255
if (colormode == Colormode::RGBA)
FillChannel(3, 255);
return;
}
@@ -51,7 +55,11 @@ namespace Leonetienne::BmpPP {
// Re-initialize the pixelbuffer
pixelBuffer.clear();
pixelBuffer.resize(size.x * size.y * GetNumColorChannels());
pixelBuffer.resize(size.x * size.y * GetNumChannels());
// If we're initializing with an alpha channel, set it to 255
if (colormode == Colormode::RGBA)
FillChannel(3, 255);
return;
}
@@ -70,7 +78,7 @@ namespace Leonetienne::BmpPP {
CHECK_IF_INITIALIZED
const std::size_t pixelIndex =
(position.y * size.x + position.x) * GetNumColorChannels();
(position.y * size.x + position.x) * GetNumChannels();
if (pixelIndex >= pixelBuffer.size())
throw std::runtime_error("Pixel index out of range!");
@@ -82,7 +90,7 @@ namespace Leonetienne::BmpPP {
CHECK_IF_INITIALIZED
const std::size_t pixelIndex =
(position.y * size.x + position.x) * GetNumColorChannels();
(position.y * size.x + position.x) * GetNumChannels();
if (pixelIndex >= pixelBuffer.size())
throw std::runtime_error("Pixel index out of range!");
@@ -142,7 +150,7 @@ namespace Leonetienne::BmpPP {
return colormode;
}
std::size_t BMP::GetNumColorChannels() const {
std::size_t BMP::GetNumChannels() const {
CHECK_IF_INITIALIZED
switch (colormode) {
@@ -167,6 +175,34 @@ namespace Leonetienne::BmpPP {
return isInitialized;
}
bool BMP::operator==(const BMP &other) const {
// Check metadata
if (colormode != other.colormode)
return false;
if (size != other.size)
return false;
// Check pixel values
if (pixelBuffer != other.pixelBuffer)
return false;
return true;
}
bool BMP::operator!=(const BMP &other) const {
return !operator==(other);
}
void BMP::FillChannel(const size_t &channel, const std::uint8_t value) {
const std::size_t numChannels = GetNumChannels();
for (std::size_t i = 0; i < pixelBuffer.size(); i += numChannels)
pixelBuffer[i + channel] = value;
return;
}
}
#undef CHECK_IF_INITIALIZED