Index: ChangeLog
from Damien Thivolle <damien(a)lrde.epita.fr>
* oln/canvas/io.hh: Contain New I/O Canvas. Two hierarchies are now
handling I/O. The first one fills the image. The second one is
dedicated to reading specific file formats. Two classes of those
hierarchies communicate to fill an image from a file.
io.hh | 203 ++++++++++++++++++++++++------------------------------------------
1 files changed, 75 insertions(+), 128 deletions(-)
Index: oln/canvas/io.hh
--- oln/canvas/io.hh (revision 244)
+++ oln/canvas/io.hh (working copy)
@@ -42,197 +42,144 @@
namespace canvas {
- template <typename I, typename E>
- struct write : public mlc::any<E>
+ template <typename I, typename F, typename E>
+ struct write_from_oln : public mlc::any<E>
{
+ typedef oln_type_of(I, value) value_type;
- void impl_extra_work()
- {
- }
-
- void work()
+ void write_point()
{
- this->exact().impl_work();
+ this->exact().impl_write_point();
}
- void extra_work()
+ void run()
{
- this->exact().impl_extra_work();
- }
+ unsigned npoints = input.size().npoints();
- void write_point()
+ writer.set_size(input.size());
+ if (writer.write_header() == false)
{
- this->exact().impl_write_point();
+ std::cerr << "io error: data type too large" << std::endl;
+ return;
}
- void run()
- {
- for (p.row() = 0; p.row() < input.size().nrows(); ++p.row())
+ for (unsigned i = 0; i < npoints; ++i)
{
- work();
- extra_work();
+ write_point();
+ writer.next_point();
}
}
+ write_from_oln(const abstract::image<I>& input, F& writer) :
+ writer(writer),
+ input(input) {}
- protected:
-
- write(const oln::abstract::image<I>& input,
- std::ostream& ostr) :
- input(input.exact()),
- ostr(ostr)
- {}
-
+ F& writer;
box<const I> input;
- std::ostream& ostr;
- oln_type_of(I, point) p;
-
};
-
- template <typename I, typename E>
- struct write_2d: public write<I, E>
- {
-
- void impl_work()
+ template <typename T, typename G, typename E>
+ struct write_to_file : public mlc::any<E>
{
- for (this->p.col() = 0;
- this->p.col() < this->input.size().ncols();
- ++this->p.col())
- this->write_point();
- }
- protected:
- typedef write<I, E> super_type;
+ typedef oln_grd_type_of(G, size) size_type;
+ typedef oln_grd_type_of(G, point) point_type;
- write_2d(const abstract::image<I>& input,
- std::ostream& ostr) :
- super_type(input, ostr)
- {}
- };
-
- template <typename I, typename E>
- struct write_1d: public write<I, E>
+ bool write_header()
{
-
- void impl_work()
- {
- this->write_point();
+ return this->exact().impl_write_header();
}
- protected:
- typedef write<I, E> super_type;
-
- write_1d(const abstract::image<I>& input,
- std::ostream& ostr) :
- super_type(input, ostr)
- {}
-
- };
-
-
- template <typename I, typename E>
- struct read : public mlc::any<E>
+ void set_size(const size_type& size)
{
+ this->exact().impl_set_size(size);
+ }
- void impl_extra_work() {}
-
- void extra_work()
+ void write_value(T v)
{
- this->exact().impl_extra_work();
+ this->exact().impl_write_value(v);
}
- void read_point()
+ void next_point()
{
- this->exact().impl_read_point();
+ this->exact().impl_next_point();
}
- void preconditions()
+ const point_type& point()
{
- this->exact().impl_preconditions();
+ return this->exact().impl_point();
}
- void work()
+ };
+
+
+ template <typename I, typename F, typename E>
+ struct read_to_oln : public mlc::any<E>
{
- this->exact().impl_work();
- }
+ typedef oln_type_of(I, value) value_type;
- void get_output(I& ima)
+ void read_point()
{
- ima = output.unbox();
+ this->exact().impl_read_point();
}
void run()
{
- preconditions();
-
- I tmp(hdr.rows, hdr.cols);
-
+ I tmp(reader.size());
output = tmp;
+ unsigned npoints = reader.npoints();
- for (p.row() = 0; p.row() < hdr.rows && !istr.eof(); ++p.row())
+ for (unsigned i = 0; i < npoints; ++i)
{
- extra_work();
- work();
+ read_point();
+ reader.next_point();
}
}
- protected:
+ void assign(abstract::image<I>& ima)
+ {
+ ima.exact() = output.unbox();
+ }
- read(std::istream& istr,
- const io::internal::pnm_header& hdr) :
- istr(istr),
- hdr(hdr)
- {}
+ read_to_oln(F& reader) : reader(reader) {}
+ F& reader;
box<I> output;
- std::istream& istr;
- const io::internal::pnm_header& hdr;
- oln_type_of(I, point) p;
-
};
- template <typename I, typename E>
- struct read_2d: public read<I, E>
+ template <typename T, typename G, typename E>
+ struct read_from_file : public mlc::any<E>
{
- void impl_work()
+ typedef oln_grd_type_of(G, size) size_type;
+ typedef oln_grd_type_of(G, point) point_type;
+
+ size_type size()
{
- for (this->p.col() = 0;
- this->p.col() < this->hdr.cols && !this->istr.eof();
- ++this->p.col())
- this->read_point();
+ return this->exact().impl_size();
}
- protected:
- typedef read<I, E> super_type;
-
- read_2d(std::istream& istr,
- const io::internal::pnm_header& hdr) :
- super_type(istr, hdr)
- {}
-
- };
-
-
- template <typename I, typename E>
- struct read_1d: public read<I, E>
+ T read_value()
{
+ return this->exact().impl_read_value();
+ }
- void impl_work()
+ void next_point()
{
- this->read_point();
+ this->exact().impl_next_point();
}
- protected:
- typedef read<I, E> super_type;
+ unsigned npoints()
+ {
+ return this->exact().impl_npoints();
+ }
- read_1d(std::istream& istr,
- const io::internal::pnm_header& hdr) :
- super_type(istr, hdr)
- {}
+ const point_type& point()
+ {
+ return this->exact().impl_point();
+ }
};