Skip to content

Commit

Permalink
Fix memory access in case of 3-byte pixel formats (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaschke authored Jun 23, 2020
1 parent 171eb3c commit e5ef587
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/rviz/selection/selection_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,24 +575,27 @@ void SelectionManager::setHighlightRect(Ogre::Viewport* viewport, int x1, int y1
highlight_rectangle_->setCorners(nx1, ny1, nx2, ny2);
}

void SelectionManager::unpackColors(const Ogre::PixelBox& box, V_CollObject& pixels)
void SelectionManager::unpackColors(Ogre::PixelBox& box, V_CollObject& pixels)
{
int w = box.getWidth();
int h = box.getHeight();

pixels.clear();
pixels.reserve(w * h);
size_t size = Ogre::PixelUtil::getMemorySize(1, 1, 1, box.format);

for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
uint32_t pos = (x + y * w) * 4;

uint32_t pix_val = *(uint32_t*)((uint8_t*)box.data + pos);
uint32_t handle = colorToHandle(box.format, pix_val);

pixels.push_back(handle);
if (size == 4) // In case of a 4-byte color format, we can directly process the 32-bit values
{
uint32_t pos = (x + y * w) * 4;
uint32_t pix_val = *(uint32_t*)((uint8_t*)box.data + pos);
pixels.push_back(colorToHandle(box.format, pix_val));
}
else // otherwise perform "official" transformation into float-based Ogre::ColourValue and back
pixels.push_back(colorToHandle(box.getColourAt(x, y, 1)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rviz/selection/selection_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private Q_SLOTS:
unsigned texture_width,
unsigned textured_height);

void unpackColors(const Ogre::PixelBox& box, V_CollObject& pixels);
void unpackColors(Ogre::PixelBox& box, V_CollObject& pixels);

void setDepthTextureSize(unsigned width, unsigned height);

Expand Down

0 comments on commit e5ef587

Please sign in to comment.