Point Cloud Library (PCL)  1.7.1
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 <string>
47 #include <vector>
48 
49 namespace pcl
50 {
51  namespace io
52  {
53  /** \brief Saves 8-bit encoded image to PNG file.
54  * \param[in] file_name the name of the file to write to disk
55  * \param[in] mono_image image grayscale data
56  * \param[in] width image width
57  * \param[in] height image height
58  * \param[in] channels number of channels
59  * \ingroup io
60  */
61  PCL_EXPORTS void
62  saveCharPNGFile (const std::string& file_name, const unsigned char *mono_image, int width, int height, int channels);
63 
64  /** \brief Saves 16-bit encoded image to PNG file.
65  * \param[in] file_name the name of the file to write to disk
66  * \param[in] short_image image short data
67  * \param[in] width image width
68  * \param[in] height image height
69  * \param[in] channels number of channels
70  * \ingroup io
71  */
72  PCL_EXPORTS void
73  saveShortPNGFile (const std::string& file_name, const unsigned short *short_image, int width, int height, int channels);
74 
75  /** \brief Saves 8-bit encoded RGB image to PNG file.
76  * \param[in] file_name the name of the file to write to disk
77  * \param[in] rgb_image image rgb data
78  * \param[in] width image width
79  * \param[in] height image height
80  * \ingroup io
81  */
82  PCL_EXPORTS void
83  saveRgbPNGFile (const std::string& file_name, const unsigned char *rgb_image, int width, int height)
84  {
85  saveCharPNGFile(file_name, rgb_image, width, height, 3);
86  }
87 
88  /** \brief Saves 8-bit grayscale cloud as image to PNG file.
89  * \param[in] file_name the name of the file to write to disk
90  * \param[in] cloud point cloud to save
91  * \ingroup io
92  */
93  void
94  savePNGFile (const std::string& file_name, const pcl::PointCloud<unsigned char>& cloud)
95  {
96  saveCharPNGFile(file_name, &cloud.points[0], cloud.width, cloud.height, 1);
97  }
98 
99  /** \brief Saves 16-bit grayscale cloud as image to PNG file.
100  * \param[in] file_name the name of the file to write to disk
101  * \param[in] cloud point cloud to save
102  * \ingroup io
103  */
104 
105  void
106  savePNGFile (const std::string& file_name, const pcl::PointCloud<unsigned short>& cloud)
107  {
108  saveShortPNGFile(file_name, &cloud.points[0], cloud.width, cloud.height, 1);
109  }
110 
111  /** \brief Saves RGB fields of cloud as image to PNG file.
112  * \param[in] file_name the name of the file to write to disk
113  * \param[in] cloud point cloud to save
114  * \ingroup io
115  */
116  template <typename T> void
117  savePNGFile (const std::string& file_name, const pcl::PointCloud<T>& cloud)
118  {
119  std::vector<unsigned char> data(cloud.width * cloud.height * 3);
120 
121  for (size_t i = 0; i < cloud.points.size (); ++i)
122  {
123  data[i*3 + 0] = cloud.points[i].r;
124  data[i*3 + 1] = cloud.points[i].g;
125  data[i*3 + 2] = cloud.points[i].b;
126  }
127  saveRgbPNGFile(file_name, &data[0], cloud.width, cloud.height);
128  }
129 
130  /** \brief Saves Labeled Point cloud as image to PNG file.
131  * \param[in] file_name the name of the file to write to disk
132  * \param[in] cloud point cloud to save
133  * \ingroup io
134  * Warning: Converts to 16 bit (for png), labels using more than 16 bits will cause problems
135  */
136  void
137  savePNGFile (const std::string& file_name, const pcl::PointCloud<pcl::PointXYZL>& cloud)
138  {
139  std::vector<unsigned short> data(cloud.width * cloud.height);
140 
141  for (size_t i = 0; i < cloud.points.size (); ++i)
142  {
143  data[i] = static_cast<unsigned short> (cloud.points[i].label);
144  }
145  saveShortPNGFile(file_name, &data[0], cloud.width, cloud.height,1);
146  }
147  }
148 }
149 
150 #endif //#ifndef PCL_IO_PNG_IO_H_