From a1573af40519d9908d6bc0faf4446fd8e387da7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Hern=C3=A1ndez=20Cordero?= Date: Mon, 13 Sep 2021 13:26:04 +0200 Subject: [PATCH] Efficiently handle 3-bytes pixel formats (#743) (#750) * Efficiently handle 3-bytes pixel formats Signed-off-by: ahcorde * Added feedback Signed-off-by: ahcorde * Fixed windows warnings Signed-off-by: ahcorde --- .../interaction/selection_manager.cpp | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/rviz_common/src/rviz_common/interaction/selection_manager.cpp b/rviz_common/src/rviz_common/interaction/selection_manager.cpp index 3bdef205e..73e589462 100644 --- a/rviz_common/src/rviz_common/interaction/selection_manager.cpp +++ b/rviz_common/src/rviz_common/interaction/selection_manager.cpp @@ -261,20 +261,23 @@ void SelectionManager::setHighlightRect(Ogre::Viewport * viewport, int x1, int y void SelectionManager::unpackColors(const Ogre::PixelBox & box) { - auto w = box.getWidth(); - auto h = box.getHeight(); + uint32_t w = box.getWidth(); + uint32_t h = box.getHeight(); pixel_buffer_.clear(); pixel_buffer_.reserve(w * h); - for (uint32_t y = 0; y < h; ++y) { - for (uint32_t x = 0; x < w; ++x) { - uint32_t pos = (x + y * w) * 4; - - uint32_t pix_val = *reinterpret_cast(static_cast(box.data) + pos); - uint32_t handle = colorToHandle(box.format, pix_val); - - pixel_buffer_.push_back(handle); + size_t size = Ogre::PixelUtil::getMemorySize(1, 1, 1, box.format); + + for (uint32_t y = 0; y < h; y++) { + for (uint32_t x = 0; x < w; x++) { + uint32_t pos = static_cast((x + y * w) * size); + uint32_t pix_val = 0; + memcpy( + reinterpret_cast(&pix_val), + reinterpret_cast(box.data + pos), + size); + pixel_buffer_.push_back(colorToHandle(box.format, pix_val)); } } }