RawWriter.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 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
00134
00135
00136
00137
00138
00139 #endif // RASTER_RAWWRITER_C_DEFINED