00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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 }
00148
00149 }
00150
00151 }
00152
00153
00154 #endif // ! OLENA_IO_PNM_READ_3D_HH