Added implementations for MirrorVertically and MirrorHorizontally

This commit is contained in:
Leonetienne 2022-03-06 13:32:01 +01:00
parent ae92a1cb40
commit fdc5d6f23f
3 changed files with 64 additions and 3 deletions

View File

@ -6,7 +6,7 @@ using namespace Leonetienne::BmpPP;
int main() {
/*
BMP bmp({800, 600}, Colormode::RGB);
for (int x = 0; x < 800; x++)
@ -21,6 +21,15 @@ int main() {
}
bmp.Write("write.bmp");
*/
BMP bmp("test.bmp");
//BMP newBmp = bmp.MirrorHorizontally();
BMP newBmp = bmp.MirrorVertically();
if(!newBmp.Write("testwrite.bmp"))
std::cerr << "What the hell" << std::endl;
/*

View File

@ -211,6 +211,58 @@ namespace Leonetienne::BmpPP {
return pixelBuffer;
}
BMP BMP::MirrorHorizontally() const {
// Create a new image matching this's metadata
BMP bmp(size, colormode);
// Now copy over the pixels, mirroring it horizontally
const std::size_t numChannels = GetNumChannels();
for (std::size_t y = 0; y < size.y; y++) {
const std::size_t rowIndex = y * size.x * numChannels;
for (std::size_t x = 0; x < size.x; x++) {
const std::size_t pixelIndex = rowIndex + x * numChannels;
const std::size_t flippedPixelIndex = rowIndex + (size.x - 1 - x) * numChannels;
// Copy over the whole pixel
std::copy(
pixelBuffer.cbegin() + flippedPixelIndex,
pixelBuffer.cbegin() + flippedPixelIndex + numChannels,
bmp.pixelBuffer.begin() + pixelIndex
);
}
}
// return it
return bmp;
}
BMP BMP::MirrorVertically() const {
// Create a new image matching this's metadata
BMP bmp(size, colormode);
const std::size_t numChannels = GetNumChannels();
const std::size_t rowLength = size.x * numChannels;
// Now iterate over all rows, and copy them over, mirroring it vertically
for (std::size_t y = 0; y < size.y; y++) {
const std::size_t rowIndex = y * rowLength;
const std::size_t flippedRowIndex = (size.y - 1 - y) * rowLength;
// Copy over the whole row
std::copy(
pixelBuffer.cbegin() + flippedRowIndex,
pixelBuffer.cbegin() + flippedRowIndex + rowLength,
bmp.pixelBuffer.begin() + rowIndex
);
}
// return it
return bmp;
}
}
#undef CHECK_IF_INITIALIZED

View File

@ -79,10 +79,10 @@ namespace Leonetienne::BmpPP {
//! Will compare two images for not being exactly identical regarding resolution, bit depth, and pixel values.
bool operator!=(const BMP& other) const;
//! Will mirror the image horizontally, and return it as a new image
//! Will mirror the image horizontally (flips x axis), and return it as a new image
BMP MirrorHorizontally() const;
//! Will mirror the image vertically, and return it as a new image
//! Will mirror the image vertically (flips y axis), and return it as a new image
BMP MirrorVertically() const;
//! Will rotate the image by 90deg, clockwise, and return it as a new image