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 #ifndef OLENA_IO_PNM_WRITE_DATA_HH
00029 # define OLENA_IO_PNM_WRITE_DATA_HH
00030
00031 # include <oln/core/image.hh>
00032 # include <oln/io/image_base.hh>
00033 # include <oln/io/pnm_common.hh>
00034
00035 # include <iostream>
00036
00037 namespace oln {
00038
00039 namespace io {
00040
00041 namespace internal {
00042
00043
00049 template<pnm_type V, writer_id R>
00050 struct pnm_write_data
00051 {
00052 template <class I>
00053 static bool
00054 write(std::ostream&, const I&, const pnm2d_info&)
00055 {
00056 return false;
00057 }
00058 };
00059
00060
00061
00062
00063
00064
00073 template <>
00074 struct pnm_write_data<PnmBinary, WritePnmPlain>
00075 {
00076 template <class I>
00077 static bool
00078 write(std::ostream& out, const I& input, const pnm2d_info&)
00079 {
00080
00081 oln_iter_type(I) it(input);
00082
00083 unsigned stride = 0;
00084 for (it = begin; it != end; ++it)
00085 {
00086 bool b = input[it];
00087
00088 out.put(b == true ? '0' : '1');
00089 if (++stride >= 70)
00090 {
00091 out << std::endl;
00092 stride = 0;
00093 }
00094 }
00095 out << std::endl;
00096 return true;
00097 }
00098 };
00099
00108 template <>
00109 struct pnm_write_data<PnmBinary, WritePnmRaw>
00110 {
00111 template <class I>
00112 static bool
00113 write(std::ostream& out, const I& input, const pnm2d_info& info)
00114 {
00115
00116 oln_iter_type(I) it(input);
00117
00118 coord cols = 0;
00119 unsigned char c = 0;
00120 coord stride = 0;
00121 for (it = begin; it != end; ++it)
00122 {
00123 c <<= 1;
00124 c += (input[it] == true ? 0 : 1);
00125 if (++cols >= info.cols)
00126 {
00127 c <<= (8 - stride - 1);
00128 out << c;
00129 c = cols = stride = 0;
00130 }
00131 else
00132 if (++stride >= 8)
00133 {
00134 out << c;
00135 c = stride = 0;
00136 }
00137 }
00138 postcondition(stride == 0);
00139 return true;
00140 }
00141 };
00142
00143
00144
00145
00146
00155 template <>
00156 struct pnm_write_data<PnmInteger, WritePnmPlain>
00157 {
00158 template <class I>
00159 static bool
00160 write(std::ostream& out, const I& input, const pnm2d_info&)
00161 {
00162
00163 oln_iter_type(I) it(input);
00164
00165 int stride = 0;
00166 for (it = begin; it != end; ++it)
00167 {
00168 int i = input[it];
00169 out << i;
00170 if (++stride >= 70)
00171 {
00172 out << std::endl;
00173 stride = 0;
00174 }
00175 else
00176 out << ' ';
00177 }
00178 return true;
00179 }
00180 };
00181
00190 template <>
00191 struct pnm_write_data<PnmInteger, WritePnmRaw>
00192 {
00193 template <class I>
00194 static bool
00195 write(std::ostream& out, const I& input, const pnm2d_info& info)
00196 {
00197
00198 oln_iter_type(I) it (input);
00199
00200 for (it = begin; it != end; ++it)
00201 {
00202 if (info.max_val <= 255)
00203 {
00204 unsigned char c = input[it];
00205 out.write((char*) &c, 1);
00206 }
00207 else
00208 {
00209 unsigned short i = input[it];
00210 out.write((char*) &i, 2);
00211 }
00212 }
00213 return true;
00214 }
00215 };
00216
00217
00218
00219
00220
00229 template <>
00230 struct pnm_write_data<PnmVectorial, WritePnmPlain>
00231 {
00232 template <class I>
00233 static bool
00234 write(std::ostream& out, const I& input, const pnm2d_info&)
00235 {
00236
00237 oln_iter_type(I) it(input);
00238
00239 int stride = 0;
00240 for (it = begin; it != end; ++it)
00241 {
00242 int tmp;
00243 tmp = input[it][0]; out << tmp << ' ';
00244 tmp = input[it][1]; out << tmp << ' ';
00245 tmp = input[it][2]; out << tmp << ' ';
00246
00247 if (++stride >= 70)
00248 {
00249 out << std::endl;
00250 stride = 0;
00251 }
00252 else
00253 out << ' ';
00254 }
00255 return true;
00256 }
00257 };
00258
00267 template <>
00268 struct pnm_write_data<PnmVectorial, WritePnmRaw>
00269 {
00270 template <class I>
00271 static bool
00272 write(std::ostream& out, const I& input, const pnm2d_info& info)
00273 {
00274
00275 oln_iter_type(I) it (input);
00276
00277 for (it = begin; it != end; ++it)
00278 {
00279 if (info.max_val <= 255)
00280 {
00281 unsigned char c[3];
00282 c[0] = input[it][0];
00283 c[1] = input[it][1];
00284 c[2] = input[it][2];
00285 out.write((char*) c, 3);
00286 }
00287 else
00288 {
00289 precondition(sizeof(unsigned short) == 2);
00290 unsigned short i[3];
00291 i[0] = input[it][0];
00292 i[1] = input[it][1];
00293 i[2] = input[it][2];
00294 out.write((char*) i, 3 * sizeof(unsigned short));
00295 }
00296 }
00297 return true;
00298 }
00299 };
00300
00301 }
00302
00303 }
00304
00305 }
00306
00307 #endif // ! OLENA_IO_PNM_WRITE_DATA_HH