00001 /*!@file Media/MrawvEncoder.C Low-level encoder for multi-frame raw video formats */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Media/MrawvEncoder.C $ 00035 // $Id: MrawvEncoder.C 14376 2011-01-11 02:44:34Z pez $ 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> // for open() 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 // in order to handle large files (>2GiB) on 32-bit linux systems, 00097 // we need to use the os-level open() call rather than fopen(), so 00098 // that we can pass in the O_LARGEFILE flag; the resulting file 00099 // descriptor is passed to fdopen() to leave us with a normal FILE* 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 /* So things look consistent in everyone's emacs... */ 00165 /* Local Variables: */ 00166 /* mode: c++ */ 00167 /* indent-tabs-mode: nil */ 00168 /* End: */ 00169 00170 #endif // MEDIA_MRAWVENCODER_C_DEFINED