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_2D_HH
00029 # define OLENA_IO_PNM_READ_2D_HH
00030
00031 # include <ntg/bin.hh>
00032 # include <oln/io/pnm_read.hh>
00033
00034 # include <iostream>
00035
00036 namespace oln {
00037
00038 namespace io {
00039
00040 namespace internal {
00041
00042
00043
00044
00045
00052
00053 static bool
00054 pnm_read_header2d(std::istream& s, char type, pnm2d_info& info)
00055 {
00056
00057 if (s.get() != 'P' ) return false;
00058 if (s.get() != type) return false;
00059 if (s.get() != '\n') return false;
00060
00061
00062 while (s.peek() == '#')
00063 {
00064 std::string line;
00065 std::getline(s,line);
00066 }
00067
00068
00069 s >> info.cols >> info.rows;
00070 if (info.cols <= 0 || info.rows <= 0) return false;
00071
00072
00073
00074
00075 if (s.get() != '\n') return false;
00076 if (type != '1' && type != '4')
00077 {
00078 s >> info.max_val;
00079 if (info.max_val > 65535 || s.get() != '\n')
00080 return false;
00081 }
00082 return true;
00083 }
00084
00085
00086
00087
00088
00089
00095 template <reader_id R, class I>
00096 struct pnm_reader<R, 2, PnmBinary, I>
00097 {
00098 static const char pnm_id = (R == ReadPnmPlain) ? '1' : '4';
00099
00100 static std::string
00101 name()
00102 {
00103 static const std::string name_("pnm/P");
00104 return name_ + pnm_id;
00105 }
00106
00107 static bool
00108 knows_ext(const std::string& ext)
00109 {
00110 if (R == ReadPnmPlain)
00111 return (ext == "ppbm") || (ext == "pbm");
00112 return ext == "pbm";
00113 }
00114
00119 static bool
00120 read(std::istream& in, I& im)
00121 {
00122 pnm2d_info info;
00123 if (!pnm_read_header2d(in, pnm_id, info))
00124 return false;
00125
00126 im = I(info.rows, info.cols);
00127
00128 pnm_read_data<PnmBinary, R>::read(in, im, info);
00129 return true;
00130 }
00131 };
00132
00133
00134
00135
00136
00142 template <reader_id R, class I>
00143 struct pnm_reader<R, 2, PnmInteger, I>
00144 {
00145 static const char pnm_id = (R == ReadPnmPlain) ? '2' : '5';
00146
00147 static std::string
00148 name()
00149 {
00150 static const std::string name_("pnm/P");
00151 return name_ + pnm_id;
00152 }
00153
00154 static bool
00155 knows_ext(const std::string& ext)
00156 {
00157 if (R == ReadPnmPlain)
00158 return (ext == "ppgm") || (ext == "pgm");
00159 return ext == "pgm";
00160 }
00161
00167 static bool
00168 read(std::istream& in, I& im)
00169 {
00170 pnm2d_info info;
00171
00172 if (!pnm_read_header2d(in, pnm_id, info))
00173 return false;
00174
00175 im = I(info.rows, info.cols);
00176
00177
00178 if (ntg::to_ntg(info.max_val) > ntg_max_val(oln_value_type(I)))
00179 return false;
00180
00181 pnm_read_data<PnmInteger, R>::read(in, im, info);
00182 return true;
00183 }
00184 };
00185
00186
00187
00188
00189
00195 template <reader_id R, class I>
00196 struct pnm_reader<R, 2, PnmVectorial, I>
00197 {
00198 static const char pnm_id = (R == ReadPnmPlain) ? '3' : '6';
00199
00200 static std::string
00201 name()
00202 {
00203 static const std::string name_("pnm/P");
00204 return name_ + pnm_id;
00205 }
00206
00207 static bool
00208 knows_ext(const std::string& ext)
00209 {
00210 if (R == ReadPnmPlain)
00211 return (ext == "pppm") || (ext == "ppm");
00212 return ext == "ppm";
00213 }
00214
00219 static bool
00220 read(std::istream& in, I& im)
00221 {
00222 pnm2d_info info;
00223
00224
00225 if (!ntg_is_a(ntg_comp_type(oln_value_type(I)), ntg::unsigned_integer)::ret)
00226 return false;
00227
00228
00229 if (ntg_nb_comp(oln_value_type(I)) != 3)
00230 return false;
00231
00232 if (!pnm_read_header2d(in, pnm_id, info))
00233 return false;
00234
00235 im = I(info.rows, info.cols);
00236
00237
00238 if (ntg::to_ntg(info.max_val) > ntg_max_val(ntg_comp_type(oln_value_type(I))))
00239 return false;
00240
00241 pnm_read_data<PnmVectorial, R>::read(in, im, info);
00242 return true;
00243 }
00244 };
00245
00246 }
00247
00248 }
00249
00250 }
00251
00252 #endif // ! OLENA_IO_PNM_READ_2D_HH