Commit efdf6433 authored by Guillaume Lazzara's avatar Guillaume Lazzara
Browse files

Add io::raw::save and io::raw::load routines.

	* headers.mk: add new headers to distribution.

	* mln/io/raw/all.hh: include new headers.

	* mln/io/raw/load.hh,
	* mln/io/raw/save.hh: new io routines. Dump/load Olena images into a
	file.

	* tests/io/Makefile.am,
	* tests/io/raw/Makefile.am,
	* tests/io/raw/pbm.cc,
	* tests/io/raw/raw.cc: add new tests.

	* tests/unit_test/Makefile.am,
	* tests/unit_test/mln_io_raw_all.cc,
	* tests/unit_test/mln_io_raw_load.cc,
	* tests/unit_test/mln_io_raw_save.cc: add new unit tests.

git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@3194 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent befb6e64
2009-01-23 Guillaume Lazzara <z@lrde.epita.fr>
Add io::raw::save and io::raw::load routines.
* headers.mk: add new headers to distribution.
* mln/io/raw/all.hh: include new headers.
* mln/io/raw/load.hh,
* mln/io/raw/save.hh: new io routines. Dump/load Olena images into a
file.
* tests/io/Makefile.am,
* tests/io/raw/Makefile.am,
* tests/io/raw/pbm.cc,
* tests/io/raw/raw.cc: add new tests.
* tests/unit_test/Makefile.am,
* tests/unit_test/mln_io_raw_all.cc,
* tests/unit_test/mln_io_raw_load.cc,
* tests/unit_test/mln_io_raw_save.cc: add new unit tests.
2009-01-23 Guillaume Lazzara <z@lrde.epita.fr>
 
Use extract::* routines in tutorial examples.
......@@ -985,6 +985,9 @@ mln/io/ppm/save.hh \
mln/io/pfm/load.hh \
mln/io/pfm/all.hh \
mln/io/pfm/save.hh \
mln/io/raw/load.hh \
mln/io/raw/all.hh \
mln/io/raw/save.hh \
mln/io/all.hh \
mln/io/pbm/load.hh \
mln/io/pbm/all.hh \
......
// 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.
#ifndef MLN_IO_PGM_ALL_HH
# define MLN_IO_PGM_ALL_HH
/*! \file mln/io/pgm/all.hh
*
* \brief File that includes all pgm io materials.
*/
namespace mln
{
namespace io
{
/// Namespace of pgm input/output handling.
namespace pgm {}
}
}
# include <mln/io/pgm/load.hh>
# include <mln/io/pgm/save.hh>
#endif // ! MLN_IO_PGM_ALL_HH
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
// 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_RAW_LOAD_HH
# define MLN_IO_RAW_LOAD_HH
/// \file mln/io/raw/load.hh
///
/// Load a Milena image dumped into a file.
# include <iostream>
# include <fstream>
# include <mln/core/concept/image.hh>
# include <mln/core/routine/initialize.hh>
# include <mln/core/box_runstart_piter.hh>
# include <mln/core/pixel.hh>
# include <mln/data/memcpy_.hh>
namespace mln
{
namespace io
{
namespace raw
{
/// Load a Milena image by dumped into a file.
///
/// \param[in,out] ima_ The image to load.
/// \param[in] filename the destination.
template <typename I>
void load(Image<I>& ima_, const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
namespace internal
{
template <typename P>
inline
void read_point(std::ifstream& file, P& p)
{
char tmp[sizeof (P)];
file.read(tmp, sizeof (P));
p = *(P*)(void*)(&tmp);
}
template <typename I>
inline
void load_header(Image<I>& ima, std::ifstream& file)
{
unsigned dim;
file >> dim;
typedef mln_site(I) P;
mln_assertion(P::dim == dim);
P pmin, pmax;
read_point<P>(file, pmin);
read_point<P>(file, pmax);
mln_concrete(I) result(box<P>(pmin, pmax));
initialize(ima, result);
}
template <typename I>
inline
void load_data(Image<I>& ima_, std::ifstream& file)
{
I& ima = exact(ima_);
unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
mln_box_runstart_piter(I) p(ima.domain());
for_all(p)
{
pixel<I> src(ima, p);
file.read((char*) (&src.val()), p.run_length() * data_size);
}
}
} // end of namespace mln::io::raw::internal
template <typename I>
void load(Image<I>& ima, const std::string& filename)
{
trace::entering("mln::io::raw::load");
std::ifstream file(filename.c_str());
if (! file)
{
std::cerr << "error: cannot open file '" << filename << "'!";
abort();
}
internal::load_header(ima, file);
internal::load_data(ima, file);
trace::exiting("mln::io::raw::load");
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::raw
} // end of namespace mln::io
} // end of namespace mln
#endif // ! MLN_IO_RAW_LOAD_HH
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
// 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_RAW_SAVE_HH
# define MLN_IO_RAW_SAVE_HH
/// \file mln/io/raw/save.hh
///
/// Save a Milena image by dumping its data to a file.
# include <iostream>
# include <fstream>
# include <mln/core/concept/image.hh>
# include <mln/core/box_runstart_piter.hh>
# include <mln/core/pixel.hh>
# include <mln/data/memcpy_.hh>
namespace mln
{
namespace io
{
namespace raw
{
/// Save a Milena image by dumping its data to a file.
///
/// \param[in] ima_ The image to save.
/// \param[in] filename the destination.
template <typename I>
void save(const Image<I>& ima_, const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
namespace internal
{
template <typename I>
inline
void save_header(const I& ima,
std::ofstream& file)
{
typedef mln_site(I) P;
file << P::dim;
mln_site(I) p = ima.domain().pmin();
file.write((char*) (&p), sizeof (P));
p = ima.domain().pmax();
file.write((char*) (&p), sizeof (P));
}
template <typename I>
inline
void save_data(I& ima, std::ofstream& file)
{
unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
mln_box_runstart_piter(I) p(ima.domain());
for_all(p)
{
pixel<I> src(ima, p);
file.write((char*) (&src.val()), p.run_length() * data_size);
}
}
} // end of namespace mln::io::raw::internal
template <typename I>
void save(const Image<I>& ima_, const std::string& filename)
{
trace::entering("mln::io::raw::save");
const I& ima = exact(ima_);
std::ofstream file(filename.c_str());
if (! file)
{
std::cerr << "error: cannot open file '" << filename << "'!";
abort();
}
internal::save_header(ima, file);
internal::save_data(ima, file);
trace::exiting("mln::io::raw::save");
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::raw
} // end of namespace mln::io
} // end of namespace mln
#endif // ! MLN_IO_RAW_SAVE_HH
......@@ -9,4 +9,5 @@ SUBDIRS = \
pfm \
pgm \
pnm \
ppm
ppm \
raw
## Process this file through Automake to create Makefile.in -*- Makefile -*-
include $(top_srcdir)/milena/tests/tests.mk
check_PROGRAMS = \
raw
raw_SOURCES = raw.cc
TESTS = $(check_PROGRAMS)
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
// 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/io/raw/raw.cc
///
/// Test on mln::io::raw::.
#include <mln/core/image/image2d.hh>
#include <mln/io/pbm/load.hh>
#include <mln/io/raw/load.hh>
#include <mln/io/raw/save.hh>
#include <mln/level/compare.hh>
#include "tests/data.hh"
int main()
{
using namespace mln;
image2d<bool> pic = io::pbm::load(MLN_IMG_DIR "/picasso.pbm");
io::raw::save(pic, "pic.raw");
// image2d<bool> pic2 = io::raw::load("pic.raw");
// mln_assertion(pic == pic2);
}
// sample binary image to test with xv, imview, and display
// to bring into the fore the binary image bug with raw raw.
// | | | | -
// | | - - -
// | - - - -
// - - - - -
// - - - - -
// - - - - -
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
// 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/io/raw/raw.cc
///
/// Test on mln::io::raw::*.
#include <mln/core/image/image2d.hh>
#include <mln/io/raw/load.hh>
#include <mln/io/raw/save.hh>
#include <mln/level/compare.hh>
#include <mln/value/int_u8.hh>
#include <mln/value/rgb8.hh>
#include "tests/data.hh"
#include <mln/debug/println.hh>
int main()
{
using namespace mln;
/// Value: bool
{
bool data[4] = { 0, 1,
1, 0 };
image2d<bool> pic = make::image2d(data);
io::raw::save(pic, "pic.raw");
image2d<bool> pic2;
io::raw::load(pic2, "pic.raw");
mln_assertion(pic.domain() == pic2.domain());
mln_assertion(pic == pic2);
}
/// Value: int_u8
{
value::int_u8 data[4] = { 5, 1,
1, 9 };
image2d<value::int_u8> pic = make::image2d(data);
io::raw::save(pic, "pic.raw");
image2d<value::int_u8> pic2;
io::raw::load(pic2, "pic.raw");
mln_assertion(pic.domain() == pic2.domain());
mln_assertion(pic == pic2);
}
/// Value: rgb8
{
using value::rgb8;
value::rgb8 data[4] = { rgb8(2,4,5), rgb8(1,23,255),
rgb8(64,41,150), rgb8(23,53,49) };
image2d<value::rgb8> pic = make::image2d(data);
io::raw::save(pic, "pic.raw");
image2d<value::rgb8> pic2;
io::raw::load(pic2, "pic.raw");
mln_assertion(pic.domain() == pic2.domain());
mln_assertion(pic == pic2);
}
}
......@@ -951,6 +951,9 @@ mln_io_ppm_save \
mln_io_pfm_load \
mln_io_pfm_all \
mln_io_pfm_save \
mln_io_raw_load \
mln_io_raw_all \
mln_io_raw_save \
mln_io_all \
mln_io_pbm_load \
mln_io_pbm_all \
......@@ -1974,6 +1977,9 @@ mln_io_ppm_save_SOURCES = mln_io_ppm_save.cc
mln_io_pfm_load_SOURCES = mln_io_pfm_load.cc
mln_io_pfm_all_SOURCES = mln_io_pfm_all.cc
mln_io_pfm_save_SOURCES = mln_io_pfm_save.cc
mln_io_raw_load_SOURCES = mln_io_raw_load.cc
mln_io_raw_all_SOURCES = mln_io_raw_all.cc
mln_io_raw_save_SOURCES = mln_io_raw_save.cc
mln_io_all_SOURCES = mln_io_all.cc
mln_io_pbm_load_SOURCES = mln_io_pbm_load.cc
mln_io_pbm_all_SOURCES = mln_io_pbm_all.cc
......
// Unit test for mln/io/raw/all.hh.
// Generated by ./build_unit_test.sh, do not modify.
// Include the file twice, so we detect missing inclusion guards.
#include <mln/io/raw/all.hh>
#include <mln/io/raw/all.hh>
int main()
{
// Nothing.
}
// Unit test for mln/io/raw/load.hh.
// Generated by ./build_unit_test.sh, do not modify.
// Include the file twice, so we detect missing inclusion guards.
#include <mln/io/raw/load.hh>
#include <mln/io/raw/load.hh>
int main()
{
// Nothing.
}
// Unit test for mln/io/raw/save.hh.
// Generated by ./build_unit_test.sh, do not modify.
// Include the file twice, so we detect missing inclusion guards.
#include <mln/io/raw/save.hh>
#include <mln/io/raw/save.hh>
int main()
{
// Nothing.
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment