Point Cloud Library (PCL)  1.7.2
png_io.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  * Authors: Anatoly Baksheev
38  */
39 
40 #ifndef PCL_IO_PNG_IO_H_
41 #define PCL_IO_PNG_IO_H_
42 
43 #include <pcl/pcl_macros.h>
44 #include <pcl/point_cloud.h>
45 #include <pcl/point_types.h>
46 #include <pcl/console/print.h>
47 #include <string>
48 #include <vector>
49 #include <pcl/io/point_cloud_image_extractors.h>
50 
51 namespace pcl
52 {
53  namespace io
54  {
55  /** \brief Saves 8-bit encoded image to PNG file.
56  * \param[in] file_name the name of the file to write to disk
57  * \param[in] mono_image image grayscale data
58  * \param[in] width image width
59  * \param[in] height image height
60  * \param[in] channels number of channels
61  * \ingroup io
62  */
63  PCL_EXPORTS void
64  saveCharPNGFile (const std::string& file_name, const unsigned char *mono_image, int width, int height, int channels);
65 
66  /** \brief Saves 16-bit encoded image to PNG file.
67  * \param[in] file_name the name of the file to write to disk
68  * \param[in] short_image image short data
69  * \param[in] width image width
70  * \param[in] height image height
71  * \param[in] channels number of channels
72  * \ingroup io
73  */
74  PCL_EXPORTS void
75  saveShortPNGFile (const std::string& file_name, const unsigned short *short_image, int width, int height, int channels);
76 
77  /** \brief Saves 8-bit encoded RGB image to PNG file.
78  * \param[in] file_name the name of the file to write to disk
79  * \param[in] rgb_image image rgb data
80  * \param[in] width image width
81  * \param[in] height image height
82  * \ingroup io
83  */
84  PCL_EXPORTS void
85  saveRgbPNGFile (const std::string& file_name, const unsigned char *rgb_image, int width, int height);
86 
87  /** \brief Saves 8-bit grayscale cloud as image to PNG file.
88  * \param[in] file_name the name of the file to write to disk
89  * \param[in] cloud point cloud to save
90  * \ingroup io
91  */
92  PCL_EXPORTS void
93  savePNGFile (const std::string& file_name, const pcl::PointCloud<unsigned char>& cloud);
94 
95  /** \brief Saves 16-bit grayscale cloud as image to PNG file.
96  * \param[in] file_name the name of the file to write to disk
97  * \param[in] cloud point cloud to save
98  * \ingroup io
99  */
100  PCL_EXPORTS void
101  savePNGFile (const std::string& file_name, const pcl::PointCloud<unsigned short>& cloud);
102 
103  /** \brief Saves a PCLImage (formely ROS sensor_msgs::Image) to PNG file.
104  * \param[in] file_name the name of the file to write to disk
105  * \param[in] image image to save
106  * \ingroup io
107  * \note Currently only "rgb8", "mono8", and "mono16" image encodings are supported.
108  */
109  PCL_EXPORTS void
110  savePNGFile (const std::string& file_name, const pcl::PCLImage& image);
111 
112  /** \brief Saves RGB fields of cloud as image to PNG file.
113  * \param[in] file_name the name of the file to write to disk
114  * \param[in] cloud point cloud to save
115  * \ingroup io
116  */
117  template <typename T>
118  PCL_DEPRECATED (
119  "pcl::io::savePNGFile<typename T> (file_name, cloud) is deprecated, please use a new generic "
120  "function pcl::io::savePNGFile (file_name, cloud, field_name) with \"rgb\" as the field name."
121  )
122  void
123  savePNGFile (const std::string& file_name, const pcl::PointCloud<T>& cloud)
124  {
125  std::vector<unsigned char> data(cloud.width * cloud.height * 3);
126 
127  for (size_t i = 0; i < cloud.points.size (); ++i)
128  {
129  data[i*3 + 0] = cloud.points[i].r;
130  data[i*3 + 1] = cloud.points[i].g;
131  data[i*3 + 2] = cloud.points[i].b;
132  }
133  saveRgbPNGFile(file_name, &data[0], cloud.width, cloud.height);
134  }
135 
136  /** \brief Saves Labeled Point cloud as image to PNG file.
137  * \param[in] file_name the name of the file to write to disk
138  * \param[in] cloud point cloud to save
139  * \ingroup io
140  * Warning: Converts to 16 bit (for png), labels using more than 16 bits will cause problems
141  */
142  PCL_EXPORTS PCL_DEPRECATED (
143  "savePNGFile (file_name, cloud) is deprecated, please use a new generic function "
144  "savePNGFile (file_name, cloud, field_name) with \"label\" as the field name."
145  )
146  void
147  savePNGFile (const std::string& file_name, const pcl::PointCloud<pcl::PointXYZL>& cloud);
148 
149  /** \brief Saves the data from the specified field of the point cloud as image to PNG file.
150  * \param[in] file_name the name of the file to write to disk
151  * \param[in] cloud point cloud to save
152  * \param[in] field_name the name of the field to extract data from
153  * \ingroup io
154  */
155  template <typename PointT> void
156  savePNGFile (const std::string& file_name, const pcl::PointCloud<PointT>& cloud, const std::string& field_name)
157  {
158  typedef typename PointCloudImageExtractor<PointT>::Ptr PointCloudImageExtractorPtr;
159  PointCloudImageExtractorPtr pcie;
160  if (field_name == "normal")
161  {
162  pcie = PointCloudImageExtractorPtr (new PointCloudImageExtractorFromNormalField<PointT>);
163  }
164  else if (field_name == "rgb")
165  {
166  pcie = PointCloudImageExtractorPtr (new PointCloudImageExtractorFromRGBField<PointT>);
167  }
168  else if (field_name == "label")
169  {
170  pcie = PointCloudImageExtractorPtr (new PointCloudImageExtractorFromLabelField<PointT>);
171  }
172  else if (field_name == "z")
173  {
174  pcie = PointCloudImageExtractorPtr (new PointCloudImageExtractorFromZField<PointT>);
175  }
176  else if (field_name == "curvature")
177  {
178  pcie = PointCloudImageExtractorPtr (new PointCloudImageExtractorFromCurvatureField<PointT>);
179  }
180  else if (field_name == "intensity")
181  {
182  pcie = PointCloudImageExtractorPtr (new PointCloudImageExtractorFromIntensityField<PointT>);
183  }
184  else
185  {
186  PCL_ERROR ("[pcl::io::savePNGFile] Unsupported field \"%s\".\n", field_name.c_str ());
187  return;
188  }
189  pcl::PCLImage image;
190  if (pcie->extract (cloud, image))
191  {
192  savePNGFile(file_name, image);
193  }
194  else
195  {
196  PCL_ERROR ("[pcl::io::savePNGFile] Failed to extract an image from \"%s\" field.\n", field_name.c_str());
197  }
198  }
199 
200  }
201 }
202 
203 #endif //#ifndef PCL_IO_PNG_IO_H_