00001 /*!@file Raster/RawWriter.C Write RAW image files */ 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/Raster/RawWriter.C $ 00035 // $Id: RawWriter.C 14290 2010-12-01 21:44:03Z itti $ 00036 // 00037 00038 #ifndef RASTER_RAWWRITER_C_DEFINED 00039 #define RASTER_RAWWRITER_C_DEFINED 00040 00041 #include "Raster/RawWriter.H" 00042 00043 #include "Image/Image.H" 00044 #include "Image/Pixels.H" 00045 #include "Raster/GenericFrame.H" 00046 00047 #include <fstream> 00048 00049 RawWriter::RawWriter() {} 00050 00051 RawWriter::~RawWriter() {} 00052 00053 std::string RawWriter::writeFrame(const GenericFrame& image, 00054 const std::string& fname) 00055 { 00056 switch (image.nativeType()) 00057 { 00058 case GenericFrame::NONE: 00059 LFATAL("cannot write an empty image to '%s'", fname.c_str()); 00060 break; 00061 00062 case GenericFrame::RGB_U8: 00063 case GenericFrame::RGBD: 00064 RawWriter::writeRGB(image.asRgbU8(), fname); 00065 break; 00066 00067 case GenericFrame::RGB_U16: 00068 RawWriter::writeRGB(image.asRgbU16(), fname); 00069 break; 00070 00071 case GenericFrame::RGB_F32: 00072 RawWriter::writeRGB(image.asRgbU8(), fname); 00073 break; 00074 00075 case GenericFrame::GRAY_U8: 00076 RawWriter::writeGray(image.asGrayU8(), fname); 00077 break; 00078 00079 case GenericFrame::GRAY_U16: 00080 RawWriter::writeGray(image.asGrayU16(), fname); 00081 break; 00082 00083 case GenericFrame::GRAY_F32: 00084 RawWriter::writeFloat(image.asGrayF32(), fname); 00085 break; 00086 00087 case GenericFrame::VIDEO: 00088 LFATAL("cannot write a video frame '%s' in RAW format", fname.c_str()); 00089 break; 00090 } 00091 00092 return fname; 00093 } 00094 00095 void RawWriter::writeRGB(const Image<PixRGB<byte> >& image, 00096 const std::string& fname) 00097 { 00098 std::ofstream ofs(fname.c_str(), std::ios::binary); 00099 if (!ofs.is_open()) 00100 LFATAL("Couldn't open RAW file '%s' for writing.", fname.c_str()); 00101 ofs.write(reinterpret_cast<const char*>(image.getArrayPtr()), 00102 image.getSize() * sizeof(PixRGB<byte>)); 00103 ofs.close(); 00104 if (ofs.fail()) 00105 LFATAL("Output stream failure while writing '%s'.", fname.c_str()); 00106 } 00107 00108 void RawWriter::writeGray(const Image<byte>& image, 00109 const std::string& fname) 00110 { 00111 std::ofstream ofs(fname.c_str(), std::ios::binary); 00112 if (!ofs.is_open()) 00113 LFATAL("Couldn't open RAW file '%s' for writing.", fname.c_str()); 00114 ofs.write(reinterpret_cast<const char*>(image.getArrayPtr()), 00115 image.getSize()); 00116 if (ofs.fail()) 00117 LFATAL("Output stream failure while writing '%s'.", fname.c_str()); 00118 } 00119 00120 void RawWriter::writeFloat(const Image<float>& image, 00121 const std::string& fname) 00122 { 00123 std::ofstream ofs(fname.c_str(), std::ios::binary); 00124 if (!ofs.is_open()) 00125 LFATAL("Couldn't open RAW file '%s' for writing.", fname.c_str()); 00126 ofs.write(reinterpret_cast<const char*>(image.getArrayPtr()), 00127 image.getSize() * sizeof(float)); 00128 if (ofs.fail()) 00129 LFATAL("Output stream failure while writing '%s'.", fname.c_str()); 00130 } 00131 00132 // ###################################################################### 00133 /* So things look consistent in everyone's emacs... */ 00134 /* Local Variables: */ 00135 /* mode: c++ */ 00136 /* indent-tabs-mode: nil */ 00137 /* End: */ 00138 00139 #endif // RASTER_RAWWRITER_C_DEFINED