HashOutputSeries.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 #ifndef TRANSPORT_HASHOUTPUTSERIES_C_DEFINED
00039 #define TRANSPORT_HASHOUTPUTSERIES_C_DEFINED
00040
00041 #include "Transport/HashOutputSeries.H"
00042
00043 #include "Image/Image.H"
00044 #include "Image/Pixels.H"
00045 #include "Raster/GenericFrame.H"
00046 #include "Util/FileUtil.H"
00047 #include "Util/jenkinshash.H"
00048
00049 #include <cstdio>
00050 #include <set>
00051
00052 struct HashOutputSeries::Impl
00053 {
00054 Impl()
00055 :
00056 fileName(""),
00057 fileOwned(true),
00058 frameNumber(-1),
00059 file(0)
00060 {}
00061
00062 ~Impl()
00063 {
00064 if (this->file != 0)
00065 {
00066 fflush(this->file);
00067
00068
00069
00070
00071 if (this->fileOwned)
00072 fclose(this->file);
00073 }
00074 }
00075
00076 void openFile()
00077 {
00078 if (this->file == 0)
00079 {
00080 this->file = stdOutputFileOpen(this->fileName,
00081 &this->fileOwned);
00082
00083
00084 ASSERT(this->file != 0);
00085 }
00086 }
00087
00088 void doPrintHash(const uint32 h,
00089 const std::string& imgname, const std::string& imgtype)
00090 {
00091 this->openFile();
00092
00093 const bool repeat = (this->hashes.find(h) != this->hashes.end());
00094 if (!repeat)
00095 this->hashes.insert(h);
00096
00097 fprintf(this->file, "%06d %08x %d %% %s (%s)\n",
00098 this->frameNumber, h, int(repeat),
00099 imgname.c_str(), imgtype.c_str());
00100 fflush(this->file);
00101 }
00102
00103 template <class T>
00104 void printHash(const Image<T>& ima,
00105 const std::string& imgname, const std::string& imgtype)
00106 {
00107 const uint32 h =
00108 jenkinshash(reinterpret_cast<const byte*>(ima.getArrayPtr()),
00109 ima.getSize() * sizeof(T),
00110 0);
00111
00112 this->doPrintHash(h, imgname, imgtype);
00113 }
00114
00115 void printHash(const VideoFrame& frame,
00116 const std::string& imgname, const std::string& imgtype)
00117 {
00118 const uint32 h =
00119 jenkinshash(reinterpret_cast<const byte*>(frame.getBuffer()),
00120 frame.getBufSize(),
00121 0);
00122
00123 this->doPrintHash(h, imgname, imgtype);
00124 }
00125
00126 std::string fileName;
00127 bool fileOwned;
00128 int frameNumber;
00129 FILE* file;
00130 std::set<uint32> hashes;
00131 };
00132
00133
00134 HashOutputSeries::HashOutputSeries(OptionManager& mgr)
00135 :
00136 FrameOstream(mgr, "Hash Output Series", "HashOutputSeries"),
00137 rep(new Impl)
00138 {}
00139
00140
00141 HashOutputSeries::~HashOutputSeries()
00142 {
00143 delete rep;
00144 }
00145
00146
00147 void HashOutputSeries::setConfigInfo(const std::string& filename)
00148 {
00149
00150
00151
00152
00153 this->setFileName(filename);
00154 }
00155
00156
00157 bool HashOutputSeries::setFrameNumber(int n)
00158 {
00159 rep->frameNumber = n;
00160
00161 return true;
00162 }
00163
00164
00165 void HashOutputSeries::writeFrame(const GenericFrame& f,
00166 const std::string& shortname,
00167 const FrameInfo& auxinfo)
00168 {
00169 const std::string nm = shortname;
00170 const std::string tp = f.nativeTypeName();
00171
00172 switch (f.nativeType())
00173 {
00174 case GenericFrame::NONE: break;
00175 case GenericFrame::RGB_U8: rep->printHash(f.asRgbU8(),nm,tp); break;
00176 case GenericFrame::RGBD: rep->printHash(f.asRgbU8(),nm,tp); break;
00177 case GenericFrame::RGB_F32: rep->printHash(f.asRgbF32(),nm,tp); break;
00178 case GenericFrame::GRAY_U8: rep->printHash(f.asGrayU8(),nm,tp); break;
00179 case GenericFrame::GRAY_F32: rep->printHash(f.asGrayF32(),nm,tp);break;
00180 case GenericFrame::VIDEO: rep->printHash(f.asVideo(),nm,tp); break;
00181 case GenericFrame::RGB_U16: break;
00182 case GenericFrame::GRAY_U16: break;
00183 }
00184 }
00185
00186
00187 void HashOutputSeries::closeStream(const std::string& shortname)
00188 {
00189
00190 }
00191
00192
00193 void HashOutputSeries::setFileName(const std::string& s)
00194 {
00195 if (rep->file != 0)
00196 LFATAL("can't change filename while output file is already open");
00197
00198 rep->fileName = s;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207 #endif // TRANSPORT_HASHOUTPUTSERIES_C_DEFINED