Skip to content

Commit

Permalink
PLYReader: thread safe colors
Browse files Browse the repository at this point in the history
Bug fix: PLYReader would give corrupted colors if used in a parallel
code.
  • Loading branch information
mikhail-matrosov committed Apr 15, 2016
1 parent 9260fa2 commit 4c57676
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 10 additions & 0 deletions io/include/pcl/io/ply_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ namespace pcl
, rgb_offset_before_ (0)
, do_resize_ (false)
, polygons_ (0)
, r_(0), g_(0), b_(0)
, a_(0), rgba_(0)
{}

PLYReader (const PLYReader &p)
Expand All @@ -109,6 +111,8 @@ namespace pcl
, rgb_offset_before_ (0)
, do_resize_ (false)
, polygons_ (0)
, r_(0), g_(0), b_(0)
, a_(0), rgba_(0)
{
*this = p;
}
Expand Down Expand Up @@ -529,6 +533,12 @@ namespace pcl
std::vector<pcl::Vertices> *polygons_;
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW

private:
// RGB values stored by vertexColorCallback()
int32_t r_, g_, b_;
// Color values stored by vertexAlphaCallback()
uint32_t a_, rgba_;
};

/** \brief Point Cloud Data (PLY) file format writer.
Expand Down
18 changes: 8 additions & 10 deletions io/src/ply_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,19 @@ namespace pcl
void
pcl::PLYReader::vertexColorCallback (const std::string& color_name, pcl::io::ply::uint8 color)
{
static int32_t r, g, b;
if ((color_name == "red") || (color_name == "diffuse_red"))
{
r = int32_t (color);
r_ = int32_t (color);
rgb_offset_before_ = vertex_offset_before_;
}
if ((color_name == "green") || (color_name == "diffuse_green"))
{
g = int32_t (color);
g_ = int32_t (color);
}
if ((color_name == "blue") || (color_name == "diffuse_blue"))
{
b = int32_t (color);
int32_t rgb = r << 16 | g << 8 | b;
b_ = int32_t (color);
int32_t rgb = r_ << 16 | g_ << 8 | b_;
memcpy (&cloud_->data[vertex_count_ * cloud_->point_step + rgb_offset_before_],
&rgb,
sizeof (pcl::io::ply::float32));
Expand All @@ -399,17 +398,16 @@ pcl::PLYReader::vertexColorCallback (const std::string& color_name, pcl::io::ply
void
pcl::PLYReader::vertexAlphaCallback (pcl::io::ply::uint8 alpha)
{
static uint32_t a, rgba;
a = uint32_t (alpha);
a_ = uint32_t (alpha);
// get anscient rgb value and store it in rgba
memcpy (&rgba,
memcpy (&rgba_,
&cloud_->data[vertex_count_ * cloud_->point_step + rgb_offset_before_],
sizeof (pcl::io::ply::float32));
// append alpha
rgba = rgba | a << 24;
rgba_ = rgba_ | a_ << 24;
// put rgba back
memcpy (&cloud_->data[vertex_count_ * cloud_->point_step + rgb_offset_before_],
&rgba,
&rgba_,
sizeof (uint32_t));
}

Expand Down

0 comments on commit 4c57676

Please sign in to comment.