rangeimageio.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Fraunhofer FKIE
00003  *
00004  * Authors: Bastian Gaspers
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * * Neither the name of the Fraunhofer FKIE nor the names of its
00015  *   contributors may be used to endorse or promote products derived from
00016  *   this software without specific prior written permission.
00017  *
00018  * This file is part of the StructureColoring ROS package.
00019  *
00020  * The StructureColoring ROS package is free software:
00021  * you can redistribute it and/or modify it under the terms of the
00022  * GNU Lesser General Public License as published by the Free
00023  * Software Foundation, either version 3 of the License, or
00024  * (at your option) any later version.
00025  *
00026  * The StructureColoring ROS package is distributed in the hope that it will be useful,
00027  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00028  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029  * GNU Lesser General Public License for more details.
00030  *
00031  * You should have received a copy of the GNU Lesser General Public License
00032  * along with The StructureColoring ROS package.
00033  * If not, see <http://www.gnu.org/licenses/>.
00034  */
00035 
00036 #include "pcl/point_types.h"
00037 #include "pcl/point_cloud.h"
00038 #include "rangetocoords.h"
00039 #include "../structures/PlanePatch.h"
00040 #include <string>
00041 #include <arpa/inet.h>
00042 
00043 #ifndef RANGEIMAGEIO_H_
00044 #define RANGEIMAGEIO_H_
00045 #define RAS_MAGIC 0x59a66a95
00046 
00047 typedef pcl::PointXYZRGB PointT;
00048 typedef pcl::PointCloud<PointT> PointCloud;
00049 
00050 int getInt(FILE* file);
00051 
00052 template<typename DataType>
00053 void readPGM(FILE* fp, DataType* buffer, unsigned int& col, unsigned int& row) {
00054         unsigned int i, j, k = 0;
00055         int z;
00056 
00057         char filetype[3];
00058         int result = fscanf(fp, "%s", filetype);
00059         if ((result == EOF) || (result == 0))
00060         {
00061 //              ROS_ERROR("Could not read first line. File not in PGM format.");
00062                 throw std::runtime_error("File not in PGM format.");
00063         }
00064         if (strcmp(filetype, "P5") && strcmp(filetype, "P2"))
00065         {
00066                 col = 0;
00067                 row = 0;
00068 //              ROS_ERROR("File not in PGM format");
00069                 throw std::runtime_error("File not in PGM format.");
00070         }
00071 
00072         col = getInt(fp);
00073         row = getInt(fp);
00074 
00075         // skip comments in header *
00076         while (255 != (i = getInt(fp)))
00077                 ;
00078 
00079         if (0 == strcmp(filetype, "P5")) {
00080                 // binary
00081 //              for (i = 1; i <= row; i++)
00082 //                      for (j = 1; j <= col; j++)
00083 //                              buffer[k++] = (int) fgetc(fp);
00084                 if (fread(buffer,sizeof(DataType),row * col,fp) != row * col){
00085 //                      ROS_ERROR("rasterfile error, could not read %d rows and %d cols", row, col);
00086                         exit(1);
00087                 }
00088         } else {
00089                 // ascii
00090                 for (i = 1; i <= row; i++)
00091                         for (j = 1; j <= col; j++) {
00092                                 if (fscanf(fp, "%d", &z) != 1)
00093                                 {
00094 //                                      ROS_ERROR("could not read value");
00095                                         throw std::runtime_error("Could not read value. File not in PGM format.");
00096                                 }
00097                                 buffer[k++] = (DataType)z;
00098                         }
00099         }
00100 }
00101 
00102 void writePGM(const std::string& filename, const unsigned int& width, const unsigned int& height, const std::vector<unsigned char>& data);
00103 
00104 //void readRasterfile(FILE* fp, unsigned char* buffer, unsigned int& col, unsigned int& row);
00105 
00106 template<typename DataType>
00107 void readRasterfile(FILE* fp, DataType * buffer, unsigned int& cols, unsigned int& rows){
00108         unsigned int rasterHeader[8];
00109         if (fread(rasterHeader, sizeof(unsigned int), 8, fp) != 8){
00110 //              ROS_ERROR("rasterfile header could not be read!");
00111                 exit(1);
00112         }
00113         if (rasterHeader[0] != RAS_MAGIC) {
00114                 if (ntohl(rasterHeader[0]) != RAS_MAGIC){
00115 //                      ROS_ERROR("rasterfile magic number %u does not match %u", rasterHeader[0], RAS_MAGIC);
00116                         exit(1);
00117                 }
00118                 for (unsigned int i=0; i < 8; ++i){
00119                         rasterHeader[i] = ntohl(rasterHeader[i]);
00120                 }
00121         }
00122         cols = rasterHeader[1];
00123         rows = rasterHeader[2];
00124         if (fread(buffer,sizeof(DataType),rows * cols,fp) != rows * cols){
00125 //              ROS_ERROR("rasterfile error, could not read %d rows and %d cols", rows, cols);
00126                 exit(1);
00127         }
00128 }
00129 
00130 void writeRasterfile(const std::string& filename, const unsigned int& width, const unsigned int& height, const std::vector<unsigned char>& data);
00131 
00132 void writeMSNormals(const std::string& filename, const PlanePatch::PlanePatches& planes);
00133 
00134 void writeTimeToFile(const std::string& filename, const double timeInSec);
00135 
00136 void readPointCloudFromABWFile(PointCloud& pointCloud, const std::string& filename, unsigned int& width, unsigned int& height, std::vector<unsigned int>& undefPoints, bool pgm = false);
00137 void readPointCloudFromPERCEPTRONFile(PointCloud& pointCloud, const std::string& filename, unsigned int& width, unsigned int& height, std::vector<unsigned int>& undefPoints);
00138 
00139 #endif /* RANGEIMAGEIO_H_ */


structure_coloring_fkie
Author(s): Bastian Gaspers
autogenerated on Sun Jan 5 2014 11:38:09