pnm_read_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_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       | pnm_read_header2d |
00044       `------------------*/
00045 
00052       // FIXME: should be in a .cc file ?
00053       static bool 
00054       pnm_read_header2d(std::istream& s, char type, pnm2d_info& info)
00055       {
00056         // check magic
00057         if (s.get() != 'P' ) return false;
00058         if (s.get() != type) return false;
00059         if (s.get() != '\n') return false;
00060 
00061         // skip comments
00062         while (s.peek() == '#')
00063           {
00064             std::string line;
00065             std::getline(s,line);
00066           }
00067 
00068         // read size
00069         s >> info.cols >> info.rows;
00070         if (info.cols <= 0 || info.rows <= 0) return false;
00071 
00072         // FIXME: it can be either '\n', 'whitespace', ..., not only '\n'!
00073 
00074         // extract or skip maxvalue
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       | pnm_reader (Binary) |
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       | pnm_reader (Integer) |
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           // Check that value type is large enough
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       | pnm_reader (Vectorial) |
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           // Components have to be integers
00225           if (!ntg_is_a(ntg_comp_type(oln_value_type(I)), ntg::unsigned_integer)::ret)
00226             return false;
00227 
00228           // Must have only 3 components
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           // Check that value type is large enough
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     } // end of namespace internal
00247 
00248   } // end of namespace io
00249 
00250 } // end of namespace oln
00251 
00252 #endif // ! OLENA_IO_PNM_READ_2D_HH

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