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_2D_HH
00029 # define OLENA_IO_PNM_WRITE_2D_HH
00030
00031 # include <ntg/basics.hh>
00032 # include <oln/core/traverse.hh>
00033 # include <oln/utils/stat.hh>
00034 # include <oln/io/pnm_common.hh>
00035 # include <oln/io/pnm_write_data.hh>
00036 # include <oln/io/pnm_write.hh>
00037
00038 namespace oln {
00039
00040 namespace io {
00041
00042 namespace internal {
00043
00044
00045
00046
00047
00053
00054 static bool
00055 pnm_write_header2d(std::ostream& s, char type, const pnm2d_info& info)
00056 {
00057 s.put('P'); s.put(type); s.put('\n');
00058 s << "# Creator: OLENA / Epita-LRDE" << std::endl
00059 << info.cols << ' ' << info.rows << std::endl;
00060
00061 if (info.max_val >= 0)
00062 s << info.max_val << std::endl;
00063 return true;
00064 }
00065
00066
00067
00068
00069
00075 template <writer_id W, class I>
00076 struct pnm_writer<W, 2, PnmBinary, I>
00077 {
00078 static const char pnm_id = (W == WritePnmPlain) ? '1' : '4';
00079
00080 static std::string
00081 name()
00082 {
00083 static const std::string name_("pnm/P");
00084 return name_ + pnm_id;
00085 }
00086
00087 static bool
00088 knows_ext(const std::string& ext)
00089 {
00090 if (W == WritePnmPlain)
00091 return (ext == "ppbm") || (ext == "pbm");
00092 return ext == "pbm";
00093 }
00094
00099 static bool
00100 write(std::ostream& out, const I& im)
00101 {
00102 pnm2d_info info;
00103 info.cols = im.ncols();
00104 info.rows = im.nrows();
00105 info.max_val = -1;
00106
00107 if (!pnm_write_header2d(out, pnm_id, info))
00108 return false;
00109
00110 if (!pnm_write_data<PnmBinary, W>::write(out, im, info))
00111 ;
00112 return true;
00113 }
00114 };
00115
00116
00117
00118
00119
00125 template <writer_id W, class I>
00126 struct pnm_writer<W, 2, PnmInteger, I>
00127 {
00128 static const char pnm_id = (W == WritePnmPlain) ? '2' : '5';
00129
00130 static std::string
00131 name()
00132 {
00133 static const std::string name_("pnm/P");
00134 return name_ + pnm_id;
00135 }
00136
00137 static bool
00138 knows_ext(const std::string& ext)
00139 {
00140 if (W == WritePnmPlain)
00141 return (ext == "ppgm") || (ext == "pgm");
00142 return ext == "pgm";
00143 }
00144
00149 static bool
00150 write(std::ostream& out, const I& im)
00151 {
00152 pnm2d_info info;
00153 info.cols = im.ncols();
00154 info.rows = im.nrows();
00155 info.max_val = ntg_max_val(oln_value_type(I));
00156
00157 if (info.max_val > ntg::to_ntg(65535U))
00158 return false;
00159
00160 if (!pnm_write_header2d(out, pnm_id, info))
00161 return false;
00162
00163 if (!pnm_write_data<PnmInteger, W>::write(out, im, info))
00164 ;
00165 return true;
00166 }
00167 };
00168
00169
00170
00171
00172
00178 template <writer_id W, class I>
00179 struct pnm_writer<W, 2, PnmVectorial, I>
00180 {
00181 static const char pnm_id = (W == WritePnmPlain) ? '3' : '6';
00182
00183 static std::string
00184 name()
00185 {
00186 static const std::string name_("pnm/P");
00187 return name_ + pnm_id;
00188 }
00189
00190 static bool
00191 knows_ext(const std::string& ext)
00192 {
00193 if (W == WritePnmPlain)
00194 return (ext == "pppm") || (ext == "ppm");
00195 return ext == "ppm";
00196 }
00197
00202 static bool
00203 write(std::ostream& out, const I& im)
00204 {
00205 typedef ntg_comp_type(oln_value_type(I)) comp_type;
00206
00207 if (!ntg_is_a(comp_type, ntg::unsigned_integer)::ret)
00208 return false;
00209
00210 if (ntg_nb_comp(oln_value_type(I)) != 3)
00211 return false;
00212
00213 if (ntg_max_val(comp_type) > ntg::to_ntg(65535U))
00214 return false;
00215
00216 pnm2d_info info;
00217 info.cols = im.ncols();
00218 info.rows = im.nrows();
00219 info.max_val = ntg_max_val(comp_type);
00220
00221 if (!pnm_write_header2d(out, pnm_id, info))
00222 return false;
00223
00224 if (!pnm_write_data<PnmVectorial, W>::write(out, im, info))
00225 ;
00226 return true;
00227 }
00228 };
00229
00230 }
00231
00232 }
00233
00234 }
00235
00236 #endif // ! OLENA_IO_PNM_WRITE_2D_HH