Point Cloud Library (PCL)  1.7.0
/tmp/buildd/pcl-1.7-1.7.0/io/include/pcl/io/png_io.h
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of the copyright holder(s) nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id$ 
00037  * Authors: Anatoly Baksheev
00038  */
00039 
00040 #ifndef PCL_IO_PNG_IO_H_
00041 #define PCL_IO_PNG_IO_H_
00042 
00043 #include <pcl/pcl_macros.h>
00044 #include <pcl/point_cloud.h>
00045 #include <pcl/point_types.h>
00046 #include <string>
00047 #include <vector>
00048 
00049 namespace pcl
00050 {
00051   namespace io
00052   {
00053     /** \brief Saves 8-bit encoded image to PNG file.
00054       * \param[in] file_name the name of the file to write to disk
00055       * \param[in] mono_image image grayscale data
00056       * \param[in] width image width
00057       * \param[in] height image height
00058       * \param[in] channels number of channels
00059       * \ingroup io
00060       */
00061     PCL_EXPORTS void
00062     saveCharPNGFile (const std::string& file_name, const unsigned char *mono_image, int width, int height, int channels);
00063 
00064     /** \brief Saves 16-bit encoded image to PNG file.
00065       * \param[in] file_name the name of the file to write to disk
00066       * \param[in] short_image image short data
00067       * \param[in] width image width
00068       * \param[in] height image height
00069       * \param[in] channels number of channels
00070       * \ingroup io
00071       */
00072     PCL_EXPORTS void
00073     saveShortPNGFile (const std::string& file_name, const unsigned short *short_image, int width, int height, int channels);
00074 
00075     /** \brief Saves 8-bit encoded RGB image to PNG file.
00076       * \param[in] file_name the name of the file to write to disk
00077       * \param[in] rgb_image image rgb data
00078       * \param[in] width image width
00079       * \param[in] height image height
00080       * \ingroup io
00081       */
00082     PCL_EXPORTS void 
00083     saveRgbPNGFile (const std::string& file_name, const unsigned char *rgb_image, int width, int height)
00084     {
00085       saveCharPNGFile(file_name, rgb_image, width, height, 3);
00086     }
00087 
00088     /** \brief Saves 8-bit grayscale cloud as image to PNG file.
00089       * \param[in] file_name the name of the file to write to disk
00090       * \param[in] cloud point cloud to save
00091       * \ingroup io
00092       */
00093     void
00094     savePNGFile (const std::string& file_name, const pcl::PointCloud<unsigned char>& cloud)
00095     {
00096       saveCharPNGFile(file_name, &cloud.points[0], cloud.width, cloud.height, 1);
00097     }
00098 
00099     /** \brief Saves 16-bit grayscale cloud as image to PNG file.
00100       * \param[in] file_name the name of the file to write to disk
00101       * \param[in] cloud point cloud to save
00102       * \ingroup io
00103       */
00104 
00105     void
00106     savePNGFile (const std::string& file_name, const pcl::PointCloud<unsigned short>& cloud)
00107     {
00108       saveShortPNGFile(file_name, &cloud.points[0], cloud.width, cloud.height, 1);
00109     }
00110 
00111     /** \brief Saves RGB fields of cloud as image to PNG file. 
00112       * \param[in] file_name the name of the file to write to disk
00113       * \param[in] cloud point cloud to save
00114       * \ingroup io
00115       */
00116     template <typename T> void
00117     savePNGFile (const std::string& file_name, const pcl::PointCloud<T>& cloud)
00118     {
00119       std::vector<unsigned char> data(cloud.width * cloud.height * 3);
00120 
00121       for (size_t i = 0; i < cloud.points.size (); ++i)
00122       {
00123         data[i*3 + 0] = cloud.points[i].r;
00124         data[i*3 + 1] = cloud.points[i].g;
00125         data[i*3 + 2] = cloud.points[i].b;        
00126       }
00127       saveRgbPNGFile(file_name, &data[0], cloud.width, cloud.height);
00128     } 
00129     
00130     /** \brief Saves Labeled Point cloud as image to PNG file. 
00131      * \param[in] file_name the name of the file to write to disk
00132      * \param[in] cloud point cloud to save
00133      * \ingroup io
00134      * Warning: Converts to 16 bit (for png), labels using more than 16 bits will cause problems
00135      */
00136     void
00137     savePNGFile (const std::string& file_name, const pcl::PointCloud<pcl::PointXYZL>& cloud)
00138     {
00139       std::vector<unsigned short> data(cloud.width * cloud.height);
00140       
00141       for (size_t i = 0; i < cloud.points.size (); ++i)
00142       {
00143         data[i] = static_cast<unsigned short> (cloud.points[i].label);      
00144       }
00145       saveShortPNGFile(file_name, &data[0], cloud.width, cloud.height,1);
00146     }  
00147   }
00148 }
00149 
00150 #endif  //#ifndef PCL_IO_PNG_IO_H_