pnm_read_data.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_DATA_HH
00029 # define OLENA_IO_PNM_READ_DATA_HH
00030 
00031 # include <oln/core/image.hh>
00032 # include <oln/io/image_base.hh>
00033 # include <oln/io/pnm_common.hh>
00034 
00035 # include <iostream>
00036 
00037 namespace oln {
00038 
00039   namespace io {
00040 
00041     namespace internal {
00042 
00043       
00049       template<pnm_type V, reader_id R>
00050       struct pnm_read_data
00051       {
00052         template <class I>
00053         static bool
00054         read(std::istream&, I&, const pnm2d_info&)
00055         { 
00056           return false; 
00057         }
00058       }; 
00059 
00060       /*---------------------.
00061       | pnm read data binary |
00062       `---------------------*/
00063 
00072       template <>
00073       struct pnm_read_data<PnmBinary, ReadPnmPlain>
00074       {
00075         template <class I>
00076         static bool
00077         read(std::istream& in, I& output, const pnm2d_info&)
00078         {
00079           // FIXME: implement an iterator over data
00080           oln_iter_type(I) it (output);
00081           
00082           for (it = begin; it != end; ++it)
00083             {
00084               unsigned char c;
00085               do
00086                 {
00087                   c = in.get();
00088                 }
00089               while ((c != '0') && (c != '1'));
00090               output[it] = (c == '0');
00091             }
00092           return true;
00093         }
00094       };
00095       
00102       template <>
00103       struct pnm_read_data<PnmBinary, ReadPnmRaw>
00104       {
00105         template <class I>
00106         static bool
00107         read(std::istream& in, I& output, const pnm2d_info& info)
00108         {
00109           oln_iter_type(I) it(output);
00110           
00111           coord cols = 0;
00112           unsigned bits = 0;
00113           unsigned char c = 0;
00114           
00115           for (it = begin; it != end; ++it)
00116             {
00117               if (bits == 0)
00118                 {
00119                   c = in.get();
00120                   bits = 8;
00121                 }
00122               bool b = (c & (1 << --bits)) ? false : true;
00123               output[it] = b;
00124               if (++cols >= info.cols)
00125                 c = cols = bits = 0;
00126             }
00127           return true;
00128         }
00129       };
00130 
00131       /*----------------------.
00132       | pnm read data integer |
00133       `----------------------*/
00134         
00144       template <>
00145       struct pnm_read_data<PnmInteger, ReadPnmPlain>
00146       {
00147         template <class I>
00148         static bool
00149         read(std::istream& in, I& output, const pnm2d_info&)
00150         {
00151           // FIXME: implement an iterator over data
00152           oln_iter_type(I) it (output);
00153           
00154           int c;
00155           for (it = begin; it != end; ++it)
00156             {
00157               in >> c;
00158               output[it] = c;
00159             }
00160           return true;
00161         }
00162       };
00163       
00164           
00173       template <>
00174       struct pnm_read_data<PnmInteger, ReadPnmRaw>
00175       {
00176         template <class I>
00177         static bool
00178         read(std::istream& in, I& output, const pnm2d_info& info)
00179         {
00180           // FIXME: implement an iterator over data
00181           oln_iter_type(I) it (output);
00182           
00183           for (it = begin; it != end; ++it)
00184             {
00185               if (info.max_val <= 255)
00186                 {
00187                   unsigned char c;
00188                   in.read((char*) &c, 1);
00189                   output[it] = c;
00190                 }
00191               else
00192                 {
00193                   unsigned short i;
00194                   in.read((char*) &i, 2);
00195                   output[it] = i;
00196                 }
00197             }
00198           return true;
00199         }
00200       };
00201 
00202       /*------------------------.
00203       | pnm read data vectorial |
00204       `------------------------*/
00205 
00206           
00215       template <>
00216       struct pnm_read_data<PnmVectorial, ReadPnmPlain>
00217       {
00218         template <class I>
00219         static bool
00220         read(std::istream& in, I& output, const pnm2d_info&)
00221         {
00222           // FIXME: implement an iterator over data
00223           oln_iter_type(I) it (output);
00224           
00225           for (it = begin; it != end; ++it)
00226             {
00227               int tmp;
00228               in >> tmp; output[it][0] = tmp;
00229               in >> tmp; output[it][1] = tmp;
00230               in >> tmp; output[it][2] = tmp;
00231             }     
00232           return true;
00233         }
00234       };
00235 
00236           
00245       template <>
00246       struct pnm_read_data<PnmVectorial, ReadPnmRaw>
00247       {
00248         template <class I>
00249         static bool
00250         read(std::istream& in, I& output, const pnm2d_info& info)
00251         {
00252           // FIXME: implement an iterator over data
00253           oln_iter_type(I) it (output);
00254           
00255           for (it = begin; it != end; ++it)
00256             {
00257               if (info.max_val <= 255)
00258                 {
00259                   unsigned char c[3];
00260                   in.read((char*) c, 3);
00261                   output[it][0] = c[0];
00262                   output[it][1] = c[1];
00263                   output[it][2] = c[2];
00264                 }
00265               else
00266                 {
00267                   precondition(sizeof(unsigned short) == 2);
00268                   unsigned short i[3];
00269                   in.read((char*) i, 3 * sizeof(unsigned short));
00270                   output[it][0] = i[0];
00271                   output[it][1] = i[1];
00272                   output[it][2] = i[2];
00273                 }
00274             }
00275           return true;
00276         }
00277       };      
00278 
00279     } // end of namespace internal
00280     
00281   } // end of namespace io
00282   
00283 } // end of namespace oln
00284 
00285 #endif // ! OLENA_IO_PNM_READ_DATA_HH

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