pnm_read_3d.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_3D_HH
00029 # define OLENA_IO_PNM_READ_3D_HH
00030 
00031 # include <ntg/bin.hh>
00032 
00033 # include <oln/core/image2d.hh>
00034 # include <oln/io/pnm_read_2d.hh>
00035 
00036 # include <list>
00037 # include <iostream>
00038 # include <cstdio>
00039 
00040 namespace oln {
00041 
00042   namespace io {
00043 
00044     namespace internal {      
00045 
00046       /*-------------------.
00047       | pnm_reader3d (Any) |
00048       `-------------------*/
00049       
00058       template <pnm_type P, class I>
00059       struct pnm_reader<ReadPnmRaw, 3, P, I>
00060       {
00061         typedef pnm_reader<ReadPnmRaw, 2, P, image2d<oln_value_type(I)> > 
00062                 reader_2d;
00063         typedef image2d<oln_value_type(I)> image2d_type;
00064 
00065         static std::string 
00066         name()
00067         { 
00068           return reader_2d::name();
00069         }
00070 
00071         static bool 
00072         knows_ext(const std::string& ext)
00073         { 
00074           return reader_2d::knows_ext(ext);
00075         }
00076 
00077 
00082         static bool 
00083         read(std::istream& in, I& im)
00084         {
00085           typedef typename std::list<image2d_type*>::iterator iterator_type;
00086           std::list<image2d_type*> image2d_list;
00087           bool fail = false;      
00088           coord ncols = -1, nrows = -1;
00089           while (in.peek() != EOF)
00090             {
00091               image2d_type* new_ima = new image2d_type();
00092               image2d_list.push_back(new_ima);
00093               if (!reader_2d::read(in, *new_ima))
00094                 {
00095                   fail = true;
00096                   break;
00097                 }
00098               if (ncols < 0)
00099                 {
00100                   ncols = new_ima->ncols();
00101                   nrows = new_ima->nrows();
00102                 }
00103               else 
00104                 if (new_ima->ncols() != ncols
00105                     || new_ima->nrows() != nrows)
00106                   {
00107                     fail = true;
00108                     break;
00109                   }
00110             }
00111 
00112           if (image2d_list.size() == 0)
00113             fail = true;
00114 
00115           if (!fail)
00116             to_image3d_(image2d_list, im);
00117 
00118           for (iterator_type it = image2d_list.begin(); 
00119                it != image2d_list.end();
00120                ++it)
00121             delete (*it);
00122           return !fail;
00123         }
00124 
00125       private:
00126         
00128         static void 
00129         to_image3d_(std::list<image2d_type*>& image2d_list, I& output)
00130         {
00131           typedef typename std::list<image2d_type*>::iterator iterator_type;
00132           unsigned slice = 0;
00133           output = I(image2d_list.size(),
00134                      image2d_list.front()->nrows(),
00135                      image2d_list.front()->ncols());
00136           for (iterator_type it = image2d_list.begin();
00137                it != image2d_list.end();
00138                ++it, ++slice)
00139             {
00140               oln_iter_type(image2d_type) p(*(*it));
00141               for_all(p)
00142                 output(slice, p.row(), p.col()) = (*(*it))[p];
00143             }
00144         }
00145       };
00146 
00147     } // end of namespace internal
00148 
00149   } // end of namespace io
00150 
00151 } // end of namespace oln
00152 
00153 
00154 #endif // ! OLENA_IO_PNM_READ_3D_HH

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