PlaintextWriter.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_PLAINTEXTWRITER_C_DEFINED
00039 #define RASTER_PLAINTEXTWRITER_C_DEFINED
00040
00041 #include "Raster/PlaintextWriter.H"
00042
00043 #include "Image/Image.H"
00044 #include "Image/Pixels.H"
00045 #include "Raster/GenericFrame.H"
00046 #include "Util/log.H"
00047
00048 #include <cstdio>
00049
00050
00051 PlaintextWriter::PlaintextWriter()
00052 {}
00053
00054
00055 PlaintextWriter::~PlaintextWriter()
00056 {}
00057
00058
00059 std::string PlaintextWriter::writeFrame(const GenericFrame& image,
00060 const std::string& fname)
00061 {
00062 switch (image.nativeType())
00063 {
00064 case GenericFrame::NONE:
00065 case GenericFrame::VIDEO:
00066 case GenericFrame::RGB_U8:
00067 writePlaintextRgbU8(image.asRgbU8(), fname);
00068 break;
00069
00070 case GenericFrame::RGB_F32:
00071 writePlaintextRgbF32(image.asRgbF32(), fname);
00072 break;
00073
00074 case GenericFrame::GRAY_U8:
00075 writePlaintextGrayU8(image.asGrayU8(), fname);
00076 break;
00077
00078 case GenericFrame::GRAY_F32:
00079 writePlaintextGrayF32(image.asGrayF32(), fname);
00080 break;
00081
00082 default:
00083 LFATAL("unknown GenericFrame::NativeType value %d",
00084 int(image.nativeType()));
00085 }
00086
00087 return fname;
00088 }
00089
00090
00091 void PlaintextWriter::writePlaintextRgbU8(const Image<PixRGB<byte> >& img,
00092 const std::string& fname)
00093 {
00094 FILE* f = fopen(fname.c_str(), "w");
00095 if (f == 0)
00096 LFATAL("couldn't open %s for writing", fname.c_str());
00097
00098 const int w = img.getWidth();
00099 const int h = img.getHeight();
00100 Image<PixRGB<byte> >::const_iterator itr = img.begin();
00101 for (int y = 0; y < h; ++y)
00102 {
00103 const char* sep = "";
00104 for (int x = 0; x < w; ++x)
00105 {
00106 fprintf(f, "%s%3d %3d %3d", sep, (*itr).p[0], (*itr).p[1], (*itr).p[2]);
00107 sep = " ";
00108 ++itr;
00109 }
00110 fprintf(f, "\n");
00111 }
00112
00113 fclose(f);
00114 }
00115
00116
00117 void PlaintextWriter::writePlaintextRgbF32(const Image<PixRGB<float> >& img,
00118 const std::string& fname)
00119 {
00120 FILE* f = fopen(fname.c_str(), "w");
00121 if (f == 0)
00122 LFATAL("couldn't open %s for writing", fname.c_str());
00123
00124 const int w = img.getWidth();
00125 const int h = img.getHeight();
00126 Image<PixRGB<float> >::const_iterator itr = img.begin();
00127 for (int y = 0; y < h; ++y)
00128 {
00129 const char* sep = "";
00130 for (int x = 0; x < w; ++x)
00131 {
00132 fprintf(f, "%s%.9e %.9e %.9e", sep, (*itr).p[0], (*itr).p[1], (*itr).p[2]);
00133 sep = " ";
00134 ++itr;
00135 }
00136 fprintf(f, "\n");
00137 }
00138
00139 fclose(f);
00140 }
00141
00142
00143 void PlaintextWriter::writePlaintextGrayU8(const Image<byte>& img,
00144 const std::string& fname)
00145 {
00146 FILE* f = fopen(fname.c_str(), "w");
00147 if (f == 0)
00148 LFATAL("couldn't open %s for writing", fname.c_str());
00149
00150 const int w = img.getWidth();
00151 const int h = img.getHeight();
00152 Image<byte>::const_iterator itr = img.begin();
00153 for (int y = 0; y < h; ++y)
00154 {
00155 const char* sep = "";
00156 for (int x = 0; x < w; ++x)
00157 {
00158 fprintf(f, "%s%3d", sep, *itr);
00159 sep = " ";
00160 ++itr;
00161 }
00162 fprintf(f, "\n");
00163 }
00164
00165 fclose(f);
00166 }
00167
00168
00169 void PlaintextWriter::writePlaintextGrayF32(const Image<float>& img,
00170 const std::string& fname)
00171 {
00172 FILE* f = fopen(fname.c_str(), "w");
00173 if (f == 0)
00174 LFATAL("couldn't open %s for writing", fname.c_str());
00175
00176 const int w = img.getWidth();
00177 const int h = img.getHeight();
00178 Image<float>::const_iterator itr = img.begin();
00179 for (int y = 0; y < h; ++y)
00180 {
00181 const char* sep = "";
00182 for (int x = 0; x < w; ++x)
00183 {
00184 fprintf(f, "%s%.9e", sep, *itr);
00185 sep = " ";
00186 ++itr;
00187 }
00188 fprintf(f, "\n");
00189 }
00190
00191 fclose(f);
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201 #endif // RASTER_PLAINTEXTWRITER_C_DEFINED