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