pnm_write_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_WRITE_DATA_HH
00029 # define OLENA_IO_PNM_WRITE_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, writer_id R>
00050       struct pnm_write_data
00051       {
00052         template <class I>
00053         static bool
00054         write(std::ostream&, const I&, const pnm2d_info&)
00055         { 
00056           return false; 
00057         }
00058       };
00059 
00060       /*----------------------.
00061       | pnm write data binary |
00062       `----------------------*/
00063 
00064              
00073       template <>
00074       struct pnm_write_data<PnmBinary, WritePnmPlain>
00075       {
00076         template <class I>
00077         static bool
00078         write(std::ostream& out, const I& input, const pnm2d_info&)
00079         {
00080           // FIXME: implement an iterator over data
00081           oln_iter_type(I) it(input);
00082           
00083           unsigned stride = 0;
00084           for (it = begin; it != end; ++it)
00085             {
00086               bool b = input[it];
00087               
00088               out.put(b == true ? '0' : '1');
00089               if (++stride >= 70)
00090                 {
00091                   out << std::endl;
00092                   stride = 0;
00093                 }
00094             }
00095           out << std::endl;
00096           return true;
00097         }
00098       };
00099 
00108       template <>
00109       struct pnm_write_data<PnmBinary, WritePnmRaw>
00110       {
00111         template <class I>
00112         static bool
00113         write(std::ostream& out, const I& input, const pnm2d_info& info)
00114         {
00115           // FIXME: implement an iterator over data
00116           oln_iter_type(I) it(input);
00117           
00118           coord cols = 0;
00119           unsigned char c = 0;
00120           coord stride = 0;
00121           for (it = begin; it != end; ++it)
00122             {
00123               c <<= 1;
00124               c += (input[it] == true ? 0 : 1);
00125               if (++cols >= info.cols)
00126                 {
00127                   c <<= (8 - stride - 1);
00128                   out << c;
00129                   c = cols = stride = 0;
00130                 }
00131               else
00132                 if (++stride >= 8)
00133                   {
00134                     out << c;
00135                     c = stride = 0;
00136                   }
00137             }
00138           postcondition(stride == 0);
00139           return true;
00140         }
00141       };
00142 
00143       /*-----------------------.
00144       | pnm write data integer |
00145       `-----------------------*/
00146 
00155       template <>
00156       struct pnm_write_data<PnmInteger, WritePnmPlain>
00157       {
00158         template <class I>
00159         static bool
00160         write(std::ostream& out, const I& input, const pnm2d_info&)
00161         {
00162           // FIXME: implement an iterator over data
00163           oln_iter_type(I) it(input);
00164           
00165           int stride = 0;
00166           for (it = begin; it != end; ++it)
00167             {
00168               int i = input[it];
00169               out << i;
00170               if (++stride >= 70)
00171                 {
00172                   out << std::endl;
00173                   stride = 0;
00174                 }
00175               else
00176                 out << ' ';
00177             }
00178           return true;
00179         }
00180       };
00181 
00190       template <>
00191       struct pnm_write_data<PnmInteger, WritePnmRaw>
00192       {
00193         template <class I>
00194         static bool
00195         write(std::ostream& out, const I& input, const pnm2d_info& info)
00196         {
00197           // FIXME: implement an iterator over data
00198           oln_iter_type(I) it (input);
00199           
00200           for (it = begin; it != end; ++it)
00201             {
00202               if (info.max_val <= 255)
00203                 {
00204                   unsigned char c = input[it];
00205                   out.write((char*) &c, 1);
00206                 }
00207               else
00208                 {
00209                   unsigned short i = input[it];
00210                   out.write((char*) &i, 2);
00211                 }
00212             }
00213           return true;
00214         }
00215       };
00216 
00217       /*-------------------------.
00218       | pnm write data vectorial |
00219       `-------------------------*/
00220 
00229       template <>
00230       struct pnm_write_data<PnmVectorial, WritePnmPlain>
00231       {
00232         template <class I>
00233         static bool
00234         write(std::ostream& out, const I& input, const pnm2d_info&)
00235         {
00236           // FIXME: implement an iterator over data
00237          oln_iter_type(I) it(input);
00238           
00239           int stride = 0;
00240           for (it = begin; it != end; ++it)
00241             {
00242               int tmp;
00243               tmp = input[it][0]; out << tmp << ' ';
00244               tmp = input[it][1]; out << tmp << ' ';
00245               tmp = input[it][2]; out << tmp << ' ';
00246               
00247               if (++stride >= 70)
00248                 {
00249                   out << std::endl;
00250                   stride = 0;
00251                 }
00252               else
00253                 out << ' ';
00254             }
00255           return true;
00256         }
00257       };        
00258       
00267       template <>
00268       struct pnm_write_data<PnmVectorial, WritePnmRaw>
00269       {
00270         template <class I>
00271         static bool
00272         write(std::ostream& out, const I& input, const pnm2d_info& info)
00273         {
00274           // FIXME: implement an iterator over data
00275           oln_iter_type(I) it (input);
00276           
00277           for (it = begin; it != end; ++it)
00278             {
00279               if (info.max_val <= 255)
00280                 {
00281                   unsigned char c[3];
00282                   c[0] = input[it][0];
00283                   c[1] = input[it][1];
00284                   c[2] = input[it][2];
00285                   out.write((char*) c, 3);
00286                 }
00287               else
00288                 {
00289                   precondition(sizeof(unsigned short) == 2);
00290                   unsigned short i[3];
00291                   i[0] = input[it][0];
00292                   i[1] = input[it][1];
00293                   i[2] = input[it][2];
00294                   out.write((char*) i, 3 * sizeof(unsigned short));
00295                 }
00296             }
00297           return true;
00298         }
00299       }; 
00300       
00301     } // end of namespace internal
00302     
00303   } // end of namespace io
00304   
00305 } // end of namespace oln
00306 
00307 #endif // ! OLENA_IO_PNM_WRITE_DATA_HH

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