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, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, 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 <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       | pnm_write_header2d |
00046       `------------------*/
00047 
00053       // FIXME: should be in a .cc file ?
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       | pnm_writer (Binary) |
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             ; // std::clog << "Unable to write data!" << std::endl;
00112           return true;
00113         }
00114       };
00115 
00116       /*---------------------.
00117       | pnm_writer (Integer) |
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             ; // std::clog << "Unable to write data!" << std::endl;
00165           return true;
00166         }
00167       };
00168 
00169       /*-----------------------.
00170       | pnm_writer (Vectorial) |
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             ; // std::clog << "Unable to write data!" << std::endl;
00226           return true;
00227         }
00228       };
00229 
00230     } // end of namespace internal
00231 
00232   } // end of namespace io
00233 
00234 } // end of namespace oln
00235 
00236 #endif // ! OLENA_IO_PNM_WRITE_2D_HH

Generated on Thu Apr 15 20:13:13 2004 for Olena by doxygen 1.3.6-20040222