diff --git a/Exec/main.cpp b/Exec/main.cpp index 38d2fde..85bc1e0 100644 --- a/Exec/main.cpp +++ b/Exec/main.cpp @@ -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; /* diff --git a/Src/BMP.cpp b/Src/BMP.cpp index 9e716db..37817f0 100644 --- a/Src/BMP.cpp +++ b/Src/BMP.cpp @@ -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 diff --git a/Src/BMP.h b/Src/BMP.h index d465f12..34e2d0c 100644 --- a/Src/BMP.h +++ b/Src/BMP.h @@ -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