From 83eab47c0574b71cefab6c0203c4a770513dd89e Mon Sep 17 00:00:00 2001 From: varnie Date: Thu, 20 Mar 2014 10:49:01 +0600 Subject: [PATCH] Image::flipVertically(), Image::flipHorizontally() optimizations. --- src/SFML/Graphics/Image.cpp | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index dae88508d..ba50b1cd9 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -271,20 +271,19 @@ void Image::flipHorizontally() { if (!m_pixels.empty()) { - std::vector before = m_pixels; - for (unsigned int y = 0; y < m_size.y; ++y) - { - const Uint8* source = &before[y * m_size.x * 4]; - Uint8* dest = &m_pixels[(y + 1) * m_size.x * 4 - 4]; - for (unsigned int x = 0; x < m_size.x; ++x) - { - dest[0] = source[0]; - dest[1] = source[1]; - dest[2] = source[2]; - dest[3] = source[3]; + std::size_t rowSize = m_size.x * 4; - source += 4; - dest -= 4; + for (std::size_t y = 0; y < m_size.y; ++y) + { + std::vector::iterator left = m_pixels.begin() + y * rowSize; + std::vector::iterator right = m_pixels.begin() + (y + 1) * rowSize - 4; + + for (std::size_t x = 0; x < m_size.x / 2; ++x) + { + std::swap_ranges(left, left + 4, right); + + left += 4; + right -= 4; } } } @@ -296,16 +295,17 @@ void Image::flipVertically() { if (!m_pixels.empty()) { - std::vector before = m_pixels; - const Uint8* source = &before[m_size.x * (m_size.y - 1) * 4]; - Uint8* dest = &m_pixels[0]; std::size_t rowSize = m_size.x * 4; - for (unsigned int y = 0; y < m_size.y; ++y) + std::vector::iterator top = m_pixels.begin(); + std::vector::iterator bottom = m_pixels.end() - rowSize; + + for (std::size_t y = 0; y < m_size.y / 2; ++y) { - std::memcpy(dest, source, rowSize); - source -= rowSize; - dest += rowSize; + std::swap_ranges(top, top + rowSize, bottom); + + top += rowSize; + bottom -= rowSize; } } }