pnm_write_2d.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
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       | pnm_write_header2d |
00048       `------------------*/
00049 
00055       // FIXME: should be in a .cc file ?
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       | pnm_writer (Binary) |
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             ; // std::clog << "Unable to write data!" << std::endl;
00114           return true;
00115         }
00116       };
00117 
00118       /*---------------------.
00119       | pnm_writer (Integer) |
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             ; // std::clog << "Unable to write data!" << std::endl;
00167           return true;
00168         }
00169       };
00170 
00171       /*-----------------------.
00172       | pnm_writer (Vectorial) |
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             ; // std::clog << "Unable to write data!" << std::endl;
00228           return true;
00229         }
00230       };
00231 
00232     } // end of namespace internal
00233 
00234   } // end of namespace io
00235 
00236 } // end of namespace oln
00237 
00238 #endif // ! OLENA_IO_PNM_WRITE_2D_HH

Generated on Tue Feb 20 20:20:16 2007 for Olena by  doxygen 1.5.1