diff --git a/examples/segmentation/example_supervoxels.cpp b/examples/segmentation/example_supervoxels.cpp index a6c43111be0..bcfb5cf539a 100644 --- a/examples/segmentation/example_supervoxels.cpp +++ b/examples/segmentation/example_supervoxels.cpp @@ -230,8 +230,9 @@ main (int argc, char ** argv) new_point.z = depth; } - uint32_t rgb = static_cast(color_pixel[0]) << 16 | static_cast(color_pixel[1]) << 8 | static_cast(color_pixel[2]); - new_point.rgb = *reinterpret_cast (&rgb); + new_point.r = color_pixel[0]; + new_point.g = color_pixel[1]; + new_point.b = color_pixel[2]; cloud->points.push_back (new_point); } diff --git a/io/include/pcl/compression/organized_pointcloud_conversion.h b/io/include/pcl/compression/organized_pointcloud_conversion.h index d6469274927..5e737ea304d 100644 --- a/io/include/pcl/compression/organized_pointcloud_conversion.h +++ b/io/include/pcl/compression/organized_pointcloud_conversion.h @@ -324,20 +324,17 @@ namespace pcl if (convertToMono) { // Encode point color - const uint32_t rgb = *reinterpret_cast (&point.rgb); - uint8_t grayvalue = static_cast(0.2989 * static_cast((rgb >> 16) & 0x0000ff) + - 0.5870 * static_cast((rgb >> 8) & 0x0000ff) + - 0.1140 * static_cast((rgb >> 0) & 0x0000ff)); + uint8_t grayvalue = static_cast(0.2989 * point.r + + 0.5870 * point.g + + 0.1140 * point.b); rgbData_arg.push_back (grayvalue); } else { // Encode point color - const uint32_t rgb = *reinterpret_cast (&point.rgb); - - rgbData_arg.push_back ( (rgb >> 16) & 0x0000ff); - rgbData_arg.push_back ( (rgb >> 8) & 0x0000ff); - rgbData_arg.push_back ( (rgb >> 0) & 0x0000ff); + rgbData_arg.push_back (point.r); + rgbData_arg.push_back (point.g); + rgbData_arg.push_back (point.b); } @@ -446,35 +443,22 @@ namespace pcl { if (monoImage_arg) { - const uint8_t& pixel_r = rgbData_arg[i]; - const uint8_t& pixel_g = rgbData_arg[i]; - const uint8_t& pixel_b = rgbData_arg[i]; - // Define point color - uint32_t rgb = (static_cast(pixel_r) << 16 - | static_cast(pixel_g) << 8 - | static_cast(pixel_b)); - newPoint.rgb = *reinterpret_cast(&rgb); + newPoint.r = rgbData_arg[i]; + newPoint.g = rgbData_arg[i]; + newPoint.b = rgbData_arg[i]; } else { - const uint8_t& pixel_r = rgbData_arg[i*3+0]; - const uint8_t& pixel_g = rgbData_arg[i*3+1]; - const uint8_t& pixel_b = rgbData_arg[i*3+2]; - // Define point color - uint32_t rgb = (static_cast(pixel_r) << 16 - | static_cast(pixel_g) << 8 - | static_cast(pixel_b)); - newPoint.rgb = *reinterpret_cast(&rgb); + newPoint.r = rgbData_arg[i*3+0]; + newPoint.g = rgbData_arg[i*3+1]; + newPoint.b = rgbData_arg[i*3+2]; } } else { // Set white point color - uint32_t rgb = (static_cast(255) << 16 - | static_cast(255) << 8 - | static_cast(255)); - newPoint.rgb = *reinterpret_cast(&rgb); + newPoint.rgba = 0xffffffffu; } } else { @@ -564,35 +548,22 @@ namespace pcl { if (monoImage_arg) { - const uint8_t& pixel_r = rgbData_arg[i]; - const uint8_t& pixel_g = rgbData_arg[i]; - const uint8_t& pixel_b = rgbData_arg[i]; - // Define point color - uint32_t rgb = (static_cast(pixel_r) << 16 - | static_cast(pixel_g) << 8 - | static_cast(pixel_b)); - newPoint.rgb = *reinterpret_cast(&rgb); + newPoint.r = rgbData_arg[i]; + newPoint.g = rgbData_arg[i]; + newPoint.b = rgbData_arg[i]; } else { - const uint8_t& pixel_r = rgbData_arg[i*3+0]; - const uint8_t& pixel_g = rgbData_arg[i*3+1]; - const uint8_t& pixel_b = rgbData_arg[i*3+2]; - // Define point color - uint32_t rgb = (static_cast(pixel_r) << 16 - | static_cast(pixel_g) << 8 - | static_cast(pixel_b)); - newPoint.rgb = *reinterpret_cast(&rgb); + newPoint.r = rgbData_arg[i*3+0]; + newPoint.g = rgbData_arg[i*3+1]; + newPoint.b = rgbData_arg[i*3+2]; } } else { // Set white point color - uint32_t rgb = (static_cast(255) << 16 - | static_cast(255) << 8 - | static_cast(255)); - newPoint.rgb = *reinterpret_cast(&rgb); + newPoint.rgba = 0xffffffffu; } } else { diff --git a/test/common/test_common.cpp b/test/common/test_common.cpp index 5976ac80cdb..179b7b66e3c 100644 --- a/test/common/test_common.cpp +++ b/test/common/test_common.cpp @@ -57,15 +57,13 @@ TEST (PCL, PointXYZRGB) PointXYZRGB p; uint8_t r = 127, g = 64, b = 254; - uint32_t rgb = (static_cast (r) << 16 | - static_cast (g) << 8 | - static_cast (b)); - p.rgb = *reinterpret_cast(&rgb); + p.r = r; + p.g = g; + p.b = b; - rgb = *reinterpret_cast(&p.rgb); - uint8_t rr = (rgb >> 16) & 0x0000ff; - uint8_t gg = (rgb >> 8) & 0x0000ff; - uint8_t bb = (rgb) & 0x0000ff; + uint8_t rr = (p.rgba >> 16) & 0x0000ff; + uint8_t gg = (p.rgba >> 8) & 0x0000ff; + uint8_t bb = (p.rgba) & 0x0000ff; EXPECT_EQ (r, rr); EXPECT_EQ (g, gg); @@ -75,7 +73,7 @@ TEST (PCL, PointXYZRGB) EXPECT_EQ (bb, 254); p.r = 0; p.g = 127; p.b = 0; - rgb = *reinterpret_cast(&p.rgb); + uint32_t rgb = p.rgba; rr = (rgb >> 16) & 0x0000ff; gg = (rgb >> 8) & 0x0000ff; bb = (rgb) & 0x0000ff; @@ -94,12 +92,11 @@ TEST (PCL, PointXYZRGBNormal) uint32_t rgb = (static_cast (r) << 16 | static_cast (g) << 8 | static_cast (b)); - p.rgb = *reinterpret_cast(&rgb); + p.rgba = rgb; - rgb = *reinterpret_cast(&p.rgb); - uint8_t rr = (rgb >> 16) & 0x0000ff; - uint8_t gg = (rgb >> 8) & 0x0000ff; - uint8_t bb = (rgb) & 0x0000ff; + uint8_t rr = (p.rgba >> 16) & 0x0000ff; + uint8_t gg = (p.rgba >> 8) & 0x0000ff; + uint8_t bb = (p.rgba) & 0x0000ff; EXPECT_EQ (r, rr); EXPECT_EQ (g, gg); @@ -109,7 +106,7 @@ TEST (PCL, PointXYZRGBNormal) EXPECT_EQ (bb, 254); p.r = 0; p.g = 127; p.b = 0; - rgb = *reinterpret_cast(&p.rgb); + rgb = p.rgba; rr = (rgb >> 16) & 0x0000ff; gg = (rgb >> 8) & 0x0000ff; bb = (rgb) & 0x0000ff; @@ -413,7 +410,8 @@ TEST (PCL, CopyIfFieldExists) EXPECT_EQ (z_val, 3.0); pcl::for_each_type (CopyIfFieldExists (p, "rgb", is_rgb, rgb_val)); EXPECT_EQ (is_rgb, true); - int rgb = *reinterpret_cast(&rgb_val); + uint32_t rgb; + std::memcpy (&rgb, &rgb_val, sizeof(rgb_val)); EXPECT_EQ (rgb, 0xff7f40fe); // alpha is 255 pcl::for_each_type (CopyIfFieldExists (p, "normal_x", is_normal_x, normal_x_val)); EXPECT_EQ (is_normal_x, true); diff --git a/test/filters/test_filters.cpp b/test/filters/test_filters.cpp index c7a6b48e0f2..352cb99fe2e 100644 --- a/test/filters/test_filters.cpp +++ b/test/filters/test_filters.cpp @@ -895,11 +895,9 @@ TEST (VoxelGrid_RGB, Filters) for (int i = 0; i < 10; ++i) { PointXYZRGB pt; - int rgb = (col_r[i] << 16) | (col_g[i] << 8) | col_b[i]; - pt.x = 0.0f; - pt.y = 0.0f; - pt.z = 0.0f; - pt.rgb = *reinterpret_cast (&rgb); + pt.r = col_r[i]; + pt.g = col_g[i]; + pt.b = col_b[i]; cloud_rgb_.points.push_back (pt); }