r4344: Improve tiled image format

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-08-12 Fabien Freling <fabien.freling@lrde.epita.fr> Improve tiled image format. * fabien/mln/core/image/tiled2d.hh: Create a tiled image based on basic I/O. * fabien/mln/io/pnm/load.hh: Add a method for loading a tiled image. * fabien/tests/core/image/tiled2d.cc: Update test file. --- mln/core/image/tiled2d.hh | 75 ++++++++++++++++++++++++++++++++++---------- mln/io/pnm/load.hh | 8 ++-- tests/core/image/tiled2d.cc | 35 +++++++++++++++----- 3 files changed, 89 insertions(+), 29 deletions(-) Index: trunk/milena/sandbox/fabien/tests/core/image/tiled2d.cc =================================================================== --- trunk/milena/sandbox/fabien/tests/core/image/tiled2d.cc (revision 4343) +++ trunk/milena/sandbox/fabien/tests/core/image/tiled2d.cc (revision 4344) @@ -1,3 +1,4 @@ +#include <mln/core/image/image2d.hh> #include <mln/core/image/tiled2d.hh> #include <mln/io/ppm/load.hh> @@ -19,19 +20,37 @@ return 1; } + //image2d<rgb8> ima; tiled2d<rgb8> tiled_ima; + //io::ppm::load(ima, argv[1]); io::ppm::load(tiled_ima, argv[1]); - std::cout << "bbox: " << tiled_ima.bbox() << std::endl; - /*mln_piter_(tiled2d<rgb8>) p(tiled_ima.domain()); + //std::cout << "bbox: " << tiled_ima.bbox() << std::endl; + //std::cout << "file: " << tiled_ima.file_() << std::endl; + + /*point2d pt0(0, 0); + mln_assertion(tiled_ima(pt0) == ima(pt0)); + + point2d pt(0, 1); + mln_assertion(tiled_ima(pt) == ima(pt)); + + point2d pt2(1, 0); + mln_assertion(tiled_ima(pt2) == ima(pt2)); + + point2d pt3(1, 1); + mln_assertion(tiled_ima(pt3) == ima(pt3));*/ + + mln_piter_(tiled2d<rgb8>) p(tiled_ima.domain()); for_all(p) - { - if (p.col() % 64 == 0) - { - tiled_ima(p) = literal::purple; - } - }*/ + if (p.col() % 16 == 0) + tiled_ima(p) = literal::green; + //std::cout << tiled_ima(p) << std::endl; + //mln_assertion(tiled_ima(p) == ima(p)); + + /*for_all(p) + if (p.col() % 16 == 0) + tiled_ima(p) = literal::purple;*/ return 0; } Index: trunk/milena/sandbox/fabien/mln/io/pnm/load.hh =================================================================== --- trunk/milena/sandbox/fabien/mln/io/pnm/load.hh (revision 4343) +++ trunk/milena/sandbox/fabien/mln/io/pnm/load.hh (revision 4344) @@ -159,7 +159,7 @@ // used in g++ > 2.95 template <typename I> inline - void load_raw_2d_contiguous(std::ifstream& file, I& ima, std::string& filename) + void load_raw_2d_contiguous(std::ifstream& file, I& ima, const std::string& filename) { (void) filename; point2d p = point2d(0, ima.domain().pmin().col()); @@ -176,7 +176,7 @@ // Load raw 2d for tiled images. template <typename T> inline - void load_raw_2d_contiguous(std::ifstream& file, tiled2d<T>& ima, std::string& filename) + void load_raw_2d_contiguous(std::ifstream& file, tiled2d<T>& ima, const std::string& filename) { ima.pos_() = file.tellg(); ima.file_() = filename; @@ -219,7 +219,7 @@ /// for all pnm 8/16 bits formats template <typename I> inline - void load_raw_2d(std::ifstream& file, I& ima, std::string& filename) + void load_raw_2d(std::ifstream& file, I& ima, const std::string& filename) { if (sizeof(value::int_u8) == 1) load_raw_2d_contiguous(file, ima, filename); @@ -314,7 +314,7 @@ ima.init_(make::box2d(nrows, ncols)); if (type == type_) - load_raw_2d(file, ima); + load_raw_2d(file, ima, filename); else if (type == (type_ - 3)) pnm::internal::load_ascii_dispatch(file, ima, mlc_is_a(mln_value(I), mln::Value)()); Index: trunk/milena/sandbox/fabien/mln/core/image/tiled2d.hh =================================================================== --- trunk/milena/sandbox/fabien/mln/core/image/tiled2d.hh (revision 4343) +++ trunk/milena/sandbox/fabien/mln/core/image/tiled2d.hh (revision 4344) @@ -61,6 +61,8 @@ std::fstream* f_; std::streampos pos_; + std::string file_; + bool loaded_; T value_; box2d b_; // theoretical box @@ -233,16 +235,22 @@ // Hooks /// Give a hook to the value buffer. - const std::fstream* buffer() const; + //const std::fstream* buffer() const; /// Give a hook to the value buffer. - std::fstream* buffer(); + //std::fstream* buffer(); /// Give a hook to the offset for accessing data. - const std::streampos pos_() const; + const std::streampos& pos_() const; /// Give a hook to the offset for accessing data. - std::streampos pos_(); + std::streampos& pos_(); + + /// Give a hook to the filename. + const std::string& file_() const; + + /// Give a hook to the filename. + std::string& file_(); /// Resize image border with new_border. @@ -348,8 +356,8 @@ void data< tiled2d<T> >::deallocate_() { -// if (f_) -// delete(f_); + if (this->loaded_) + this->f_->close(); } template <typename T> @@ -397,6 +405,7 @@ { mln_precondition(! this->is_valid()); this->data_ = new internal::data< tiled2d<T> >(b, bdr); + this->data_->loaded_ = false; } /*template <typename T> @@ -464,8 +473,18 @@ const T& tiled2d<T>::read_(const point2d& p) const { - std::cout << "setting at point " << p << std::endl; // DELETEME - return this->data_->value_; + mln::tiled2d<T>* this_ = const_cast<mln::tiled2d<T>* >(this); // Trust me, I have to do this(_). + if (!this_->data_->loaded_) + { + this_->data_->f_ = new std::fstream(this->data_->file_.c_str()); + this_->data_->loaded_ = true; + } + std::streampos offset = this_->data_->pos_; + offset += (this_->ncols() * p.row() + p.col()) * sizeof(T); + this_->data_->f_->seekg(offset); + this_->data_->f_->get((char*)(&this_->data_->value_), sizeof(T) + 1); // FIXME: I don't know why + // I have to add +1. + return this_->data_->value_; } template <typename T> @@ -473,11 +492,15 @@ void tiled2d<T>::write_(const point2d& p, const T& value) { - std::cout << "setting value " << value << " at point " << p << std::endl; // DELETEME - /*this->data_->pixel_cache = this->data_->buffer_.getPixels(p.col(), p.row(), p.col(), p.row()); - *(this->data_->pixel_cache) = Magick::ColorRGB(256 - value.red(), - 256 - value.green(), - 256 - value.blue());*/ + if (!this->data_->loaded_) + { + this->data_->f_ = new std::fstream(this->data_->file_.c_str()); + this->data_->loaded_ = true; + } + std::streampos offset = this->data_->pos_; + offset += (this->ncols() * p.row() + p.col()) * sizeof(T); + this->data_->f_->seekp(offset); + this->data_->f_->write((char*)(&value), sizeof(T)); } @@ -532,7 +555,7 @@ // Hooks. - template <typename T> + /*template <typename T> inline const std::fstream* tiled2d<T>::buffer() const @@ -548,11 +571,11 @@ { mln_precondition(this->is_valid()); return this->data_->f_; - } + }*/ template <typename T> inline - const std::streampos + const std::streampos& tiled2d<T>::pos_() const { mln_precondition(this->is_valid()); @@ -561,13 +584,31 @@ template <typename T> inline - std::streampos + std::streampos& tiled2d<T>::pos_() { mln_precondition(this->is_valid()); return this->data_->pos_; } + template <typename T> + inline + const std::string& + tiled2d<T>::file_() const + { + mln_precondition(this->is_valid()); + return this->data_->file_; + } + + template <typename T> + inline + std::string& + tiled2d<T>::file_() + { + mln_precondition(this->is_valid()); + return this->data_->file_; + } + # endif // ! MLN_INCLUDE_ONLY
participants (1)
-
Fabien Freling