MrawvEncoder.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 MEDIA_MRAWVENCODER_C_DEFINED
00039 #define MEDIA_MRAWVENCODER_C_DEFINED
00040
00041 #include <string>
00042
00043 #include "Media/MrawvEncoder.H"
00044
00045 #include "Image/Normalize.H"
00046 #include "Raster/GenericFrame.H"
00047 #include "Util/StringUtil.H"
00048 #include "Util/sformat.H"
00049
00050 #include <sys/types.h>
00051 #include <sys/stat.h>
00052 #include <fcntl.h>
00053
00054
00055 namespace
00056 {
00057 std::string extensionFor(const Dims& dims, VideoFormat mode)
00058 {
00059 return sformat(".%dx%d.m%s",
00060 dims.w(), dims.h(),
00061 toLowerCase(convertToString(mode)).c_str());
00062 }
00063
00064 std::string addExtension(const std::string& fstem,
00065 const Dims& dims, VideoFormat mode)
00066 {
00067 const std::string ext = extensionFor(dims, mode);
00068
00069 std::string result(fstem);
00070
00071 if (result.size() < ext.size() ||
00072 result.compare(result.size() - ext.size(), result.npos,
00073 ext) != 0)
00074 result += ext;
00075
00076 ASSERT(result.compare(result.size() - ext.size(), result.npos,
00077 ext) == 0);
00078
00079 return result;
00080 }
00081 }
00082
00083
00084 MrawvEncoder::MrawvEncoder(const GenericFrameSpec& spec,
00085 const std::string& fstem,
00086 const bool scale255)
00087 :
00088 itsFname(),
00089 itsFile(0),
00090 itsScale255(scale255)
00091 {
00092 itsFname =
00093 addExtension(fstem, spec.dims, spec.getActualVideoFormat());
00094
00095 #ifdef HAVE_O_LARGEFILE
00096
00097
00098
00099
00100 const int fd = open(itsFname.c_str(), O_WRONLY | O_CREAT | O_LARGEFILE,
00101 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
00102 | S_IROTH | S_IWOTH));
00103
00104 if (fd == -1)
00105 PLFATAL("Cannot open() %s for writing", itsFname.c_str());
00106
00107 itsFile = fdopen(fd, "w");
00108
00109 if (itsFile == 0)
00110 PLFATAL("Cannot fdopen() %s for writing", itsFname.c_str());
00111
00112 #else // !defined(HAVE_O_LARGEFILE)
00113
00114 itsFile = fopen(itsFname.c_str(), "w");
00115
00116 if (itsFile == 0)
00117 PLFATAL("Cannot fopen() %s for writing", itsFname.c_str());
00118 #endif
00119 }
00120
00121
00122 MrawvEncoder::~MrawvEncoder()
00123 {
00124 this->close();
00125 }
00126
00127
00128 int MrawvEncoder::close()
00129 {
00130 int err = 0;
00131
00132 if (itsFile)
00133 {
00134 err = fclose(itsFile);
00135 if (err)
00136 PLERROR("Error closing file %s", itsFname.c_str());
00137
00138 itsFile = 0;
00139 }
00140
00141 return err;
00142 }
00143
00144
00145 void MrawvEncoder::writeFrame(const GenericFrame& f_)
00146 {
00147 GenericFrame f = f_;
00148 if (itsScale255)
00149 f.setFloatFlags(f.floatFlags() | FLOAT_NORM_0_255);
00150
00151 const VideoFrame vf = f.asVideo();
00152
00153 const size_t siz = vf.getBufSize();
00154
00155 const size_t n = fwrite(vf.getBuffer(), 1, siz, itsFile);
00156
00157 LDEBUG("wrote %"ZU"/%"ZU" bytes to %s", n, siz, itsFname.c_str());
00158
00159 if (n != siz)
00160 PLFATAL("fwrite() failed for %s", itsFname.c_str());
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170 #endif // MEDIA_MRAWVENCODER_C_DEFINED