PfmParser.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Raster/PfmParser.H"
00039
00040 #include "Util/Assert.H"
00041 #include "Image/Image.H"
00042 #include "Image/Pixels.H"
00043 #include "Raster/GenericFrame.H"
00044 #include "Util/FileUtil.H"
00045 #include "Util/log.H"
00046 #include "rutz/shared_ptr.h"
00047
00048 #include <istream>
00049 #include <limits>
00050 #include <string>
00051 #include <deque>
00052
00053 using std::deque;
00054 using std::string;
00055
00056
00057 struct PfmParser::Rep
00058 {
00059 Rep(const std::string& fname) :
00060 strm(openMaybeCompressedFile(fname)),
00061 mode('\0'), w(-1), h(-1), maxGrey(1), comments("")
00062 {
00063 ASSERT(strm.get() != 0);
00064 }
00065
00066 rutz::shared_ptr<std::istream> strm;
00067 char mode;
00068 int w, h;
00069 float maxGrey;
00070 string comments;
00071 deque<string> tagName;
00072 deque<string> tagValue;
00073 };
00074
00075
00076 PfmParser::PfmParser(const std::string& fname) :
00077 rep(new Rep(fname))
00078 {
00079 const int c = rep->strm->get();
00080 if (c != 'P')
00081 LFATAL("Missing magic number in pbm file '%s'.", fname.c_str());
00082
00083 (*rep->strm) >> rep->mode >> std::ws;
00084
00085 string temp;
00086
00087
00088
00089 while (rep->strm->peek() == '#')
00090 {
00091
00092 std::getline((*rep->strm), temp, '\n');
00093 rep->comments += temp;
00094 }
00095
00096 (*rep->strm) >> rep->w >> rep->h >> std::ws;
00097
00098
00099 while (rep->strm->peek() == '!')
00100 {
00101
00102 std::getline((*rep->strm), temp, '\n');
00103 temp = temp.substr(1);
00104 rep->tagName.push_back(temp);
00105
00106
00107 std::getline((*rep->strm), temp, '\n');
00108 rep->tagValue.push_back(temp);
00109 }
00110
00111 (*rep->strm) >> rep->maxGrey;
00112
00113
00114 const int spc = rep->strm->get();
00115 if (!isspace(spc))
00116 LFATAL("Missing whitespace after maxGrey in pfm file '%s'.", fname.c_str());
00117 LDEBUG("PFM Reading Image: %s", fname.c_str());
00118 }
00119
00120
00121 PfmParser::~PfmParser()
00122 {
00123 delete rep;
00124 }
00125
00126
00127 GenericFrameSpec PfmParser::getFrameSpec() const
00128 {
00129 GenericFrameSpec result;
00130
00131 result.nativeType = GenericFrame::GRAY_F32;
00132 result.videoFormat = VIDFMT_AUTO;
00133 result.videoByteSwap = false;
00134 result.dims = Dims(rep->w, rep->h);
00135 result.floatFlags = 0;
00136
00137 return result;
00138 }
00139
00140
00141 string PfmParser::getComments() const
00142 { return rep->comments; }
00143
00144
00145 uint PfmParser::getTagCount() const
00146 { return rep->tagName.size(); }
00147
00148
00149 bool PfmParser::getTag(uint tag, string& name, string& value) const
00150 {
00151 if (tag < rep->tagName.size())
00152 {
00153 name = rep->tagName[tag];
00154 value = rep->tagValue[tag];
00155 return true;
00156 }
00157 else
00158 return false;
00159 }
00160
00161
00162 GenericFrame PfmParser::getFrame()
00163 {
00164 ASSERT(rep->mode == 'F');
00165
00166 Image<float> img(rep->w, rep->h, NO_INIT);
00167 rep->strm->read(reinterpret_cast<char*>(img.getArrayPtr()),
00168 img.getSize() * sizeof(float));
00169 return GenericFrame(img, 0);
00170 }
00171
00172
00173
00174
00175
00176