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_READ_DATA_HH
00029 # define OLENA_IO_PNM_READ_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, reader_id R>
00050 struct pnm_read_data
00051 {
00052 template <class I>
00053 static bool
00054 read(std::istream&, I&, const pnm2d_info&)
00055 {
00056 return false;
00057 }
00058 };
00059
00060
00061
00062
00063
00072 template <>
00073 struct pnm_read_data<PnmBinary, ReadPnmPlain>
00074 {
00075 template <class I>
00076 static bool
00077 read(std::istream& in, I& output, const pnm2d_info&)
00078 {
00079
00080 oln_iter_type(I) it (output);
00081
00082 for (it = begin; it != end; ++it)
00083 {
00084 unsigned char c;
00085 do
00086 {
00087 c = in.get();
00088 }
00089 while ((c != '0') && (c != '1'));
00090 output[it] = (c == '0');
00091 }
00092 return true;
00093 }
00094 };
00095
00102 template <>
00103 struct pnm_read_data<PnmBinary, ReadPnmRaw>
00104 {
00105 template <class I>
00106 static bool
00107 read(std::istream& in, I& output, const pnm2d_info& info)
00108 {
00109 oln_iter_type(I) it(output);
00110
00111 coord cols = 0;
00112 unsigned bits = 0;
00113 unsigned char c = 0;
00114
00115 for (it = begin; it != end; ++it)
00116 {
00117 if (bits == 0)
00118 {
00119 c = in.get();
00120 bits = 8;
00121 }
00122 bool b = (c & (1 << --bits)) ? false : true;
00123 output[it] = b;
00124 if (++cols >= info.cols)
00125 c = cols = bits = 0;
00126 }
00127 return true;
00128 }
00129 };
00130
00131
00132
00133
00134
00144 template <>
00145 struct pnm_read_data<PnmInteger, ReadPnmPlain>
00146 {
00147 template <class I>
00148 static bool
00149 read(std::istream& in, I& output, const pnm2d_info&)
00150 {
00151
00152 oln_iter_type(I) it (output);
00153
00154 int c;
00155 for (it = begin; it != end; ++it)
00156 {
00157 in >> c;
00158 output[it] = c;
00159 }
00160 return true;
00161 }
00162 };
00163
00164
00173 template <>
00174 struct pnm_read_data<PnmInteger, ReadPnmRaw>
00175 {
00176 template <class I>
00177 static bool
00178 read(std::istream& in, I& output, const pnm2d_info& info)
00179 {
00180
00181 oln_iter_type(I) it (output);
00182
00183 for (it = begin; it != end; ++it)
00184 {
00185 if (info.max_val <= 255)
00186 {
00187 unsigned char c;
00188 in.read((char*) &c, 1);
00189 output[it] = c;
00190 }
00191 else
00192 {
00193 unsigned short i;
00194 in.read((char*) &i, 2);
00195 output[it] = i;
00196 }
00197 }
00198 return true;
00199 }
00200 };
00201
00202
00203
00204
00205
00206
00215 template <>
00216 struct pnm_read_data<PnmVectorial, ReadPnmPlain>
00217 {
00218 template <class I>
00219 static bool
00220 read(std::istream& in, I& output, const pnm2d_info&)
00221 {
00222
00223 oln_iter_type(I) it (output);
00224
00225 for (it = begin; it != end; ++it)
00226 {
00227 int tmp;
00228 in >> tmp; output[it][0] = tmp;
00229 in >> tmp; output[it][1] = tmp;
00230 in >> tmp; output[it][2] = tmp;
00231 }
00232 return true;
00233 }
00234 };
00235
00236
00245 template <>
00246 struct pnm_read_data<PnmVectorial, ReadPnmRaw>
00247 {
00248 template <class I>
00249 static bool
00250 read(std::istream& in, I& output, const pnm2d_info& info)
00251 {
00252
00253 oln_iter_type(I) it (output);
00254
00255 for (it = begin; it != end; ++it)
00256 {
00257 if (info.max_val <= 255)
00258 {
00259 unsigned char c[3];
00260 in.read((char*) c, 3);
00261 output[it][0] = c[0];
00262 output[it][1] = c[1];
00263 output[it][2] = c[2];
00264 }
00265 else
00266 {
00267 precondition(sizeof(unsigned short) == 2);
00268 unsigned short i[3];
00269 in.read((char*) i, 3 * sizeof(unsigned short));
00270 output[it][0] = i[0];
00271 output[it][1] = i[1];
00272 output[it][2] = i[2];
00273 }
00274 }
00275 return true;
00276 }
00277 };
00278
00279 }
00280
00281 }
00282
00283 }
00284
00285 #endif // ! OLENA_IO_PNM_READ_DATA_HH