milena r1107: add fits + pfm handling

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2007-09-12 Matthieu Garrigues <garrigues@lrde.epita.fr> add fits + pfm handling * mln/convert/to_image.hh: . * mln/io/fits/load.hh: New. * mln/io/fits: New. * mln/io/pfm/load.hh: New. * mln/io/pfm/save.hh: New. * mln/io/pfm: New. * mln/io/pgm/save.hh: . * tests/io_fits.cc: New. * tests/io_pgm.cc: . * tests/io_pgm16.cc: . --- mln/convert/to_image.hh | 6 -- mln/io/fits/load.hh | 110 +++++++++++++++++++++++++++++++++++++ mln/io/pfm/load.hh | 139 ++++++++++++++++++++++++++++++++++++++++++++++++ mln/io/pfm/save.hh | 107 ++++++++++++++++++++++++++++++++++++ mln/io/pgm/save.hh | 36 ++++++------ tests/io_fits.cc | 67 +++++++++++++++++++++++ tests/io_pgm.cc | 4 - tests/io_pgm16.cc | 2 8 files changed, 448 insertions(+), 23 deletions(-) Index: trunk/milena/tests/io_pgm.cc =================================================================== --- trunk/milena/tests/io_pgm.cc (revision 1106) +++ trunk/milena/tests/io_pgm.cc (revision 1107) @@ -49,12 +49,12 @@ image2d_b<rgb8> lena = io::pgm::load<rgb8>("../img/lena.pgm"); - io::ppm::save(lena, "out.pgm"); + //io::pgm::save(lena, "out.pgm"); } { image2d_b<int_u8> lena = io::pgm::load<int_u8>("../img/lena.pgm"); - io::ppm::save(lena, "out.pgm"); + io::pgm::save(lena, "out.pgm"); } } Index: trunk/milena/tests/io_fits.cc =================================================================== --- trunk/milena/tests/io_fits.cc (revision 0) +++ trunk/milena/tests/io_fits.cc (revision 1107) @@ -0,0 +1,67 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +/*! \file tests/fits_load.cc + * + * \brief Test on mln::io::fits::load. + */ + +#include <mln/core/image2d_b.hh> + +#include <mln/level/compare.hh> + +#include <mln/io/fits/load.hh> +#include <mln/io/pfm/save.hh> +#include <mln/io/pfm/load.hh> + +int main() +{ + using namespace mln; + { + image2d_b<float> + fits_in = io::fits::load("../img/test.fits"); + + io::pfm::save(fits_in, "out.pfm"); + + image2d_b<float> + pfm = io::pfm::load("out.pfm"); + + io::pfm::save(fits_in, "out2.pfm"); + + image2d_b<float> + pfm2 = io::pfm::load("out2.pfm"); + + mln_assertion(pfm == pfm2); + +// } +// { +// image2d_b<int_u8> +// lena = io::fits::load<int_u8>("../img/lena.fits"); + +// io::fits::save(lena, "out.fits"); + } +} Index: trunk/milena/tests/io_pgm16.cc =================================================================== --- trunk/milena/tests/io_pgm16.cc (revision 1106) +++ trunk/milena/tests/io_pgm16.cc (revision 1107) @@ -84,7 +84,7 @@ io::pgm::save(out, "out16.pgm"); image2d_b<int_u16> - lena2 = io::pgm::load<int_u16>("out.pgm"); + lena2 = io::pgm::load<int_u16>("out16.pgm"); image2d_b<int_u8> out2(lena.domain()); level::transform(lena2, to8bits(), out2); Index: trunk/milena/mln/convert/to_image.hh =================================================================== --- trunk/milena/mln/convert/to_image.hh (revision 1106) +++ trunk/milena/mln/convert/to_image.hh (revision 1107) @@ -112,11 +112,9 @@ template <typename W> mln_image_from(W, mln_weight(W)) to_image(const Weighted_Window<W>& w_win); - /// Convert an histo \p h into an image1d_b. + /// Convert an histo \p h into an image1d_b<std::size_t>. template <typename S> - image1d_b<std::size_t> - to_image(const - mln::histo::data<S>& h); + image1d_b<std::size_t> to_image(const mln::histo::data<S>& h); # ifndef MLN_INCLUDE_ONLY Index: trunk/milena/mln/io/pfm/save.hh =================================================================== --- trunk/milena/mln/io/pfm/save.hh (revision 0) +++ trunk/milena/mln/io/pfm/save.hh (revision 1107) @@ -0,0 +1,107 @@ +// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 EPITA +// Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_IO_PFM_SAVE_HH +# define MLN_IO_PFM_SAVE_HH + +# include <iostream> +# include <fstream> + +# include <mln/geom/size2d.hh> +# include <mln/metal/equal.hh> +# include <mln/metal/bexpr.hh> + + +namespace mln +{ + + namespace io + { + + namespace pfm + { + + template <typename I> + void save(const Image<I>& ima, const std::string& filename); + + + namespace impl + { + + template <typename I> + void save_header_(const I& ima, const std::string& filename, + std::ofstream& file) + { + file << geom::nrows(ima) << ' ' << geom::nrows(ima) << std::endl + << "float" << std::endl; + } + + template <typename I> + void save_(const Image<I>& ima_, const std::string& filename) + { + const I& ima = exact(ima_); + std::ofstream file(filename.c_str()); + save_header_(ima, filename, file); + + unsigned int + ncols = geom::ncols(ima); + + const mln_coord(I) + min_row = geom::min_row(ima), + max_row = geom::max_row(ima); + + //image2d<float>& ima_ = const_cast< image2d<float>& >(ima); + point2d p(make::point2d(0, 0)); + for (p.row() = min_row; p.row() < max_row; ++p.row()) + file.write((const char*)(&(ima(p))), + sizeof(float) * ncols); + file.close(); + + } + + } // end of namespace mln::io::impl + +# ifndef MLN_INCLUDE_ONLY + + template <typename I> + void save(const Image<I>& ima, const std::string& filename) + { + mln::metal::equal<mln_value(I), float >::check(); + impl::save_(exact(ima), filename); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::pfm + + } // end of namespace mln::io + +} // end of namespace mln + + +#endif // ! MLN_IO_PFM_SAVE_HH Index: trunk/milena/mln/io/pfm/load.hh =================================================================== --- trunk/milena/mln/io/pfm/load.hh (revision 0) +++ trunk/milena/mln/io/pfm/load.hh (revision 1107) @@ -0,0 +1,139 @@ +// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 EPITA +// Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_IO_PFM_LOAD_HH +# define MLN_IO_PFM_LOAD_HH + +// # include <iostream> +// # include <fstream> +// # include <string> + +# include <mln/core/image2d_b.hh> +# include <mln/value/int_u8.hh> + + +namespace mln +{ + + namespace io + { + + namespace pfm + { + + namespace internal + { + + void abort() + { + std::cerr << " aborting." << std::endl; + exit(0); + } + + bool read_pfm_header(std::istream& file, + int& nrows, int& ncols, + bool test = false) + { + std::string tag; + + // get size + file >> nrows; + file >> ncols; + + if (file.get() != '\n') + goto err; + + // check tag == float + file >> tag; + if (tag != "float") + goto err; + + if (file.get() != '\n') + goto err; + + return true; + + err: + if (! test) + { + std::cerr << "error: badly formed header!" + << std::endl; + abort(); + } + return false; + } + + + /// load_raw_2d. + template <typename I> + void load_raw_2d(std::ifstream& file, I& ima) + { + point2d p = make::point2d(0, 0); + + const mln_coord(I) + min_row = geom::min_row(ima), + max_row = geom::max_row(ima); + + unsigned int + ncols = geom::ncols(ima); + + for (p.row() = min_row; p.row() < max_row; ++p.row()) + file.read((char*)(&(ima(p))), + sizeof(float) * ncols); + + } + + + } // end of namespace mln::io::internal + + image2d_b<float> load(const std::string& filename) + { + std::ifstream file(filename.c_str()); + if (! file) + { + std::cerr << "error: file '" << filename + << "' not found!"; + abort(); + } + char type; + int nrows, ncols; + internal::read_pfm_header(file, nrows, ncols); + + image2d_b<float> ima(nrows, ncols); + internal::load_raw_2d(file, ima); + return ima; + } + + } // end of namespace mln::io::pfm + + } // end of namespace mln::io + +} // end of namespace mln + + +#endif // ! MLN_IO_PFM_LOAD_HH Index: trunk/milena/mln/io/pgm/save.hh =================================================================== --- trunk/milena/mln/io/pgm/save.hh (revision 1106) +++ trunk/milena/mln/io/pgm/save.hh (revision 1107) @@ -78,22 +78,25 @@ file << mln_max(mln_value(I)) << std::endl; } -// template <typename I> -// void save_(const Fast_Image<I>& ima_, const std::string& filename) -// { -// const I& ima = exact(ima_); -// std::ofstream file(filename.c_str()); -// save_header_(ima, filename, file); -// const int -// min_row = geom::min_row(ima), -// max_row = geom::max_row(ima); -// point2d p; -// if (sizeof(mln_value(I)) == 1) + template <typename I> + void save_(const Fast_Image<I>& ima_, const std::string& filename) + { + std::cout << "testestse" ; + const I& ima = exact(ima_); + std::ofstream file(filename.c_str()); + save_header_(ima, filename, file); + const int + min_row = geom::min_row(ima), + max_row = geom::max_row(ima); + point2d p; + +// FIXME : ask theo if I can remove this else +// if (sizeof(mln_value(I)) <= 2) // { -// p.col() = geom::min_col(ima); -// size_t len = geom::ncols(ima); -// for (p.row() = min_row; p.row() <= max_row; ++p.row()) -// file.write((char*)(& ima(p)), len); + p.col() = geom::min_col(ima); + size_t len = geom::ncols(ima) * sizeof(mln_value(I)); + for (p.row() = min_row; p.row() <= max_row; ++p.row()) + file.write((char*)(& ima(p)), len); // } // else // { @@ -108,11 +111,12 @@ // file.write((char*)(&c), 1); // } // } -// } + } template <typename I> void save_(const Image<I>& ima_, const std::string& filename) { + typedef typename I::value::enc T; const I& ima = exact(ima_); std::ofstream file(filename.c_str()); Index: trunk/milena/mln/io/fits/load.hh =================================================================== --- trunk/milena/mln/io/fits/load.hh (revision 0) +++ trunk/milena/mln/io/fits/load.hh (revision 1107) @@ -0,0 +1,110 @@ +// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 EPITA +// Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_IO_FITS_LOAD_HH +# define MLN_IO_FITS_LOAD_HH + +# include <iostream> +# include <fstream> +# include <string> + +# include <mln/core/image2d_b.hh> +# include <mln/value/int_u8.hh> +# include <mln/io/fitsio.h> + +#include <iostream> +#include <fstream> +#include <string> + +namespace mln +{ + + namespace io + { + + namespace fits + { + void fits_exit(int status) + { + if (status) + { + fits_report_error(stderr, status); + exit(status); + } + return; + } + + image2d_b<float> load(const std::string& filename) + { + fitsfile *fptr; + int status, nfound, anynull; + long naxes[2]; + float nullval; + + status = 0; + if (fits_open_file(&fptr, filename.c_str(), READONLY, &status)) + fits_exit(status); + + if (fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxes, &nfound, &status)) + fits_exit(status); + + const int ncols = naxes[0], nrows = naxes[1]; + + image2d_b<float> output(nrows, ncols); + + nullval = 0; // don't check null values + + point2d p(make::point2d(0, 0)); + + for (p.row() = 0; p.row() < nrows; ++p.row()) + { + if (fits_read_img(fptr, + TFLOAT, + 1 + p.row() * ncols, + ncols, + &nullval, + &(output(p)), + &anynull, + &status)) + fits_exit(status); + } + + if (fits_close_file(fptr, &status)) + fits_exit(status); + + return output; + } + + } // end of namespace mln::io::fits + + } // end of namespace mln::io + +} // end of namespace mln + + +#endif // ! MLN_IO_FITS_LOAD_HH
participants (1)
-
Matthieu Garrigues