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
00039 #ifndef MBARI_MBARIFRAMESERIES_C_DEFINED
00040 #define MBARI_MBARIFRAMESERIES_C_DEFINED
00041
00042 #include "MBARI/MbariFrameSeries.H"
00043
00044 #include "Component/GlobalOpts.H"
00045 #include "Component/ModelOptionDef.H"
00046 #include "Component/OptionManager.H"
00047 #include "Image/CutPaste.H"
00048 #include "Image/Image.H"
00049 #include "Image/Pixels.H"
00050 #include "Image/ShapeOps.H"
00051 #include "Media/MediaOpts.H"
00052 #include "Raster/Raster.H"
00053 #include "Transport/RasterInputSeries.H"
00054 #include "Transport/TransportOpts.H"
00055 #include "Util/FileUtil.H"
00056 #include "Util/TextLog.H"
00057 #include "Util/log.H"
00058 #include "Util/sformat.H"
00059
00060 #include <cstdio>
00061
00062
00063 #define MAXDISP 20
00064
00065 const ModelOptionCateg MOC_MBARIRV = {
00066 MOC_SORTPRI_4, "MBARI Result Viewer Related Options" };
00067
00068
00069 static const ModelOptionDef OPT_InputMbariPreserveAspect =
00070 { MODOPT_FLAG, "InputMbariPreserveAspect", &MOC_MBARIRV, OPTEXP_MRV,
00071 "Preserve input frame aspect ratio if rescaling to fixed dims",
00072 "mbari-preserve-input-aspect", '\0', "", "false" };
00073
00074
00075 static const ModelOptionDef OPT_OutputMbariShowFrames =
00076 { MODOPT_FLAG, "OutputMbariShowFrames", &MOC_MBARIRV, OPTEXP_MRV,
00077 "Show output frames",
00078 "mbari-display-output-frames", '\0', "", "true" };
00079
00080
00081
00082
00083
00084 InputMbariFrameSeries::InputMbariFrameSeries(OptionManager& mgr,
00085 const std::string& descrName,
00086 const std::string& tag) :
00087 ModelComponent(mgr, "Input "+descrName, "Input"+tag),
00088 itsDims(&OPT_InputFrameDims, this),
00089 itsPreserveAspect(&OPT_InputMbariPreserveAspect, this),
00090 itsRasterFileFormat(&OPT_InputRasterFileFormat, this),
00091 itsStem("")
00092 {}
00093
00094
00095 InputMbariFrameSeries::~InputMbariFrameSeries()
00096 {}
00097
00098
00099 Dims InputMbariFrameSeries::peekDims(const int fnum)
00100 {
00101
00102 if (itsDims.getVal().w() != 0 && itsDims.getVal().h() != 0)
00103 return itsDims.getVal();
00104
00105 return Raster::getImageDims(sformat("%s%06d", itsStem.c_str(), fnum),
00106 itsRasterFileFormat.getVal());
00107 }
00108
00109
00110 Image<PixRGB<byte> > InputMbariFrameSeries::readRGB(const int fnum)
00111 {
00112 const Image< PixRGB<byte> > ima =
00113 Raster::ReadRGB(sformat("%s%06d", itsStem.c_str(), fnum),
00114 itsRasterFileFormat.getVal());
00115
00116 if (itsDims.getVal().isEmpty())
00117 return ima;
00118
00119 if (itsPreserveAspect.getVal())
00120 {
00121 Image<PixRGB<byte> > res(itsDims.getVal(), ZEROS);
00122 PixRGB<byte> bg = PixRGB<byte>(64, 64, 64);
00123 inplaceEmbed(res, ima, res.getBounds(), bg, true);
00124 return res;
00125 }
00126
00127 return rescale(ima, itsDims.getVal());
00128 }
00129
00130
00131 void InputMbariFrameSeries::setFileStem(const std::string& stem)
00132 {
00133 itsStem = stem;
00134 }
00135
00136
00137
00138
00139 OutputMbariFrameSeries::OutputMbariFrameSeries(OptionManager& mgr)
00140 :
00141 ModelComponent(mgr, "Output MBARI Frame Series",
00142 "OutputMbariFrameSeries"),
00143 itsLogFile(&OPT_TextLogFile, this),
00144 itsTestMode(&OPT_TestMode, this),
00145 itsDims(&OPT_OutputFrameDims, this),
00146 itsPreserveAspect(&OPT_OutputPreserveAspect, this),
00147 itsShowFrames(&OPT_OutputMbariShowFrames, this),
00148 itsRasterFileFormat(&OPT_OutputRasterFileFormat, this),
00149 itsStem(""),
00150 itsDidDisplay(0)
00151 { }
00152
00153
00154 OutputMbariFrameSeries::~OutputMbariFrameSeries()
00155 { }
00156
00157
00158 void OutputMbariFrameSeries::setFileStem(const std::string& stem)
00159 {
00160 if (started())
00161 LFATAL("Cannot change file stem while started");
00162 itsStem = stem;
00163 }
00164
00165
00166 std::string OutputMbariFrameSeries::getFileStem() const
00167 { return itsStem; }
00168
00169
00170 void OutputMbariFrameSeries::
00171 writeMbariRGB(const Image< PixRGB<byte> >& image,
00172 const std::string& otherstem,
00173 const int framenum)
00174 {
00175
00176 std::string fname(computeFileName(framenum, otherstem));
00177
00178
00179 const RasterFileFormat ff = itsRasterFileFormat.getVal();
00180
00181
00182 Image< PixRGB<byte> > ima = OutputMbariFrameSeries::doResizeImage(image);
00183
00184
00185 fname = Raster::WriteRGB(ima, fname, ff);
00186 textLog(itsLogFile.getVal(), "WriteRGB", fname);
00187
00188
00189 if (okToDisplay())
00190 Raster::Display(fname.c_str());
00191 }
00192
00193
00194 void OutputMbariFrameSeries::
00195 writeMbariGray(const Image<byte>& image,
00196 const std::string& otherstem,
00197 const int framenum)
00198 {
00199
00200 std::string fname(computeFileName(framenum, otherstem));
00201
00202
00203 const RasterFileFormat ff = itsRasterFileFormat.getVal();
00204
00205
00206 Image<byte> ima = OutputMbariFrameSeries::doResizeImage(image);
00207
00208
00209 fname = Raster::WriteGray(ima, fname, ff);
00210 textLog(itsLogFile.getVal(), "WriteGray", fname);
00211
00212
00213 if (okToDisplay())
00214 Raster::Display(fname.c_str());
00215 }
00216
00217
00218 void OutputMbariFrameSeries::
00219 writeMbariFloat(const Image<float>& image,
00220 const std::string& otherstem,
00221 int flags,
00222 const int framenum)
00223 {
00224
00225 std::string fname(computeFileName(framenum, otherstem));
00226
00227
00228 RasterFileFormat ff = itsRasterFileFormat.getVal();
00229
00230
00231 Image<float> ima = OutputMbariFrameSeries::doResizeImage(image);
00232
00233
00234
00235
00236 if (flags & FLOAT_NORM_PRESERVE)
00237 {
00238 flags &= ~FLOAT_NORM_0_255;
00239 ff = RASFMT_PFM;
00240 }
00241
00242
00243 fname = Raster::WriteFloat(ima, flags, fname, ff);
00244 textLog(itsLogFile.getVal(), "WriteFloat", fname);
00245
00246
00247 if (okToDisplay())
00248 Raster::Display(fname.c_str());
00249 }
00250
00251
00252 std::string OutputMbariFrameSeries::
00253 computeFileName(const int framenum, const std::string& otherstem) const
00254 {
00255
00256 std::string path, file;
00257 splitPath(itsStem, path, file);
00258
00259
00260 std::string otherpath, otherfile;
00261 splitPath(otherstem, otherpath, otherfile);
00262
00263 return sformat("%s%s%06d", path.c_str(), otherfile.c_str(), framenum);
00264 }
00265
00266
00267 template <class T>
00268 Image<T> OutputMbariFrameSeries::doResizeImage(const Image<T>& input) const
00269 {
00270 if (itsDims.getVal().isEmpty()) return input;
00271 if (itsPreserveAspect.getVal())
00272 {
00273 Image<T> res(itsDims.getVal(), ZEROS);
00274 T bg = T(); bg += 64;
00275 inplaceEmbed(res, input, res.getBounds(), bg, true);
00276 return res;
00277 }
00278 return rescale(input, itsDims.getVal());
00279 }
00280
00281
00282 bool OutputMbariFrameSeries::okToDisplay()
00283 {
00284 if (itsTestMode.getVal() == false && itsShowFrames.getVal())
00285 {
00286 ++ itsDidDisplay;
00287 if (itsDidDisplay > MAXDISP)
00288 LERROR("**** TOO MANY WINDOWS! NOT DISPLAYING IMAGE...");
00289 else
00290 return true;
00291 }
00292 return false;
00293
00294 }
00295
00296
00297
00298
00299
00300
00301
00302 #endif // MBARI_MBARIFRAMESERIES_C_DEFINED