Commit 59c73634 authored by Matthieu Garrigues's avatar Matthieu Garrigues
Browse files

Review the mln/io directory.

	* mln/io/abort.hh,
	* mln/io/fits/load.hh,
	* mln/io/pbm/load.hh,
	* mln/io/pbm/save.hh,
	* mln/io/pfm/load.hh,
	* mln/io/pfm/save.hh,
	* mln/io/pgm/load.hh,
	* mln/io/pgm/save.hh,
	* mln/io/pnm/load.hh,
	* mln/io/pnm/load_header.hh,
	* mln/io/pnm/max_component.hh,
	* mln/io/pnm/save.hh,
	* mln/io/pnm/save_header.hh,
	* mln/io/ppm/load.hh,
	* mln/io/ppm/save.hh,
	* tests/io/ppm/ppm.cc: Add documentation, review the code, put
          MLN_INCLUDE_ONLY gards, add missing function declaration (outside
          MLN_INCLUDE_ONLY).

git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@1568 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 51d1873e
2007-11-28 Matthieu Garrigues <garrigues@lrde.epita.fr>
Review the mln/io directory.
* mln/io/abort.hh,
* mln/io/fits/load.hh,
* mln/io/pbm/load.hh,
* mln/io/pbm/save.hh,
* mln/io/pfm/load.hh,
* mln/io/pfm/save.hh,
* mln/io/pgm/load.hh,
* mln/io/pgm/save.hh,
* mln/io/pnm/load.hh,
* mln/io/pnm/load_header.hh,
* mln/io/pnm/max_component.hh,
* mln/io/pnm/save.hh,
* mln/io/pnm/save_header.hh,
* mln/io/ppm/load.hh,
* mln/io/ppm/save.hh,
* tests/io/ppm/ppm.cc: Add documentation, review the code, put
MLN_INCLUDE_ONLY gards, add missing function declaration (outside
MLN_INCLUDE_ONLY).
2007-11-28 Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr>
Add full test for border::get.
......
......@@ -44,13 +44,19 @@ namespace mln
namespace internal
{
/// The way to abort when an error occur in io processing.
void abort()
# ifndef MLN_INCLUDE_ONLY
void abort()
{
std::cerr << " aborting." << std::endl;
std::cerr << "I/O error, aborting." << std::endl;
exit(0);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::internal
} // end of namespace mln::io
......
......@@ -51,8 +51,29 @@ namespace mln
namespace io
{
namespace fits
{
/*! Load a fits image in a milena image.
*
* \param[out] ima A reference to the image2d<float> which will receive
* data.
* \param[in] filename The source.
*/
void load(image2d<float>& ima,
const std::string& filename);
/*! Load a fits image in a image2d<float>.
*
* \param[in] filename The image source.
*
* \return An image2d<float> which contains loaded data.
*/
image2d<float> load(const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
void fits_exit(int status)
{
if (status)
......@@ -104,6 +125,14 @@ namespace mln
return output;
}
void load(image2d<float>& ima,
const std::string& filename)
{
ima = load(filename);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::fits
} // end of namespace mln::io
......
......@@ -54,6 +54,26 @@ namespace mln
namespace pbm
{
/*! Load a pbm image in a milena image.
*
* \param[out] ima A reference to the image2d<bool> which will receive
* data.
* \param[in] filename The source.
*/
void load(image2d<bool>& ima,
const std::string& filename);
/*! Load a pbm image in a image2d<float>.
*
* \param[in] filename The image source.
*
* \return An image2d<float> which contains loaded data.
*/
image2d<bool> load(const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
namespace internal
{
......@@ -84,53 +104,61 @@ namespace mln
max_row = geom::max_row(ima),
min_col = geom::min_col(ima),
max_col = geom::max_col(ima);
char c;
int i;
for (p.row() = min_row; p.row() <= max_row; ++p.row())
{
i = 0;
for (p.col() = min_col; p.col() <= max_col; ++p.col())
{
i = 0;
for (p.col() = min_col; p.col() <= max_col; ++p.col())
{
if (i % 8 == 0)
file.read((char*)(&c), 1);
ima(p) = c & 128;
c *= 2;
++i;
}
if (i % 8 == 0)
file.read((char*)(&c), 1);
ima(p) = c & 128;
c *= 2;
++i;
}
}
}
} // end of namespace mln::io::internal
} // end of namespace mln::io::internal
image2d<bool> load(const std::string& filename)
{
std::ifstream file(filename.c_str());
if (! file)
image2d<bool> load(const std::string& filename)
{
std::cerr << "error: file '" << filename
<< "' not found!";
abort();
std::ifstream file(filename.c_str());
if (! file)
{
std::cerr << "error: file '" << filename
<< "' not found!";
abort();
}
char type;
int nrows, ncols;
io::pnm::read_header('1', '4', file, type, nrows, ncols);
image2d<bool> ima(nrows, ncols);
if (type == '4')
internal::load_raw_2d(file, ima);
else
if (type == '1')
internal::load_ascii(file, ima);
return ima;
}
char type;
int nrows, ncols;
io::pnm::read_header('1', '4', file, type, nrows, ncols);
image2d<bool> ima(nrows, ncols);
if (type == '4')
internal::load_raw_2d(file, ima);
else
if (type == '1')
internal::load_ascii(file, ima);
return ima;
}
void load(image2d<bool>& ima,
const std::string& filename)
{
ima = load(filename);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::pbm
} // end of namespace mln::io::pbm
} // end of namespace mln::io
} // end of namespace mln::io
} // end of namespace mln
......
......@@ -63,6 +63,11 @@ namespace mln
namespace pbm
{
/*! Save a milena image as a pbm image.
*
* \param[in] ima The image to save.
* \param[in,out] filename the destination.
*/
template <typename I>
void save(const Image<I>& ima, const std::string& filename);
......@@ -79,7 +84,7 @@ namespace mln
std::ofstream file(filename.c_str());
io::pnm::save_header(PBM, ima, filename, file);
int ncols = geom::ncols(ima);
int col = 0;
int stride = 0;
......
......@@ -37,10 +37,6 @@
*
*/
// # include <iostream>
// # include <fstream>
// # include <string>
# include <mln/core/image2d.hh>
# include <mln/value/int_u8.hh>
......@@ -54,6 +50,25 @@ namespace mln
namespace pfm
{
/*! Load a pfm image in a milena image.
*
* \param[out] ima A reference to the image2d<float> which will receive
* data.
* \param[in] filename The source.
*/
void load(image2d<float>& ima,
const std::string& filename);
/*! Load a pfm image in a image2d<float>.
*
* \param[in] filename The image source.
*
* \return An image2d<float> which contains loaded data.
*/
image2d<float> load(const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
namespace internal
{
......@@ -136,6 +151,15 @@ namespace mln
return ima;
}
void load(image2d<float>& ima,
const std::string& filename)
{
ima = load(filename);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::pfm
} // end of namespace mln::io
......
......@@ -54,9 +54,15 @@ namespace mln
namespace pfm
{
/*! Save a milena image as a pfm image.
*
* \param[in] ima The image to save.
* \param[in,out] filename the destination.
*/
template <typename I>
void save(const Image<I>& ima, const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
namespace impl
{
......@@ -94,8 +100,6 @@ namespace mln
} // end of namespace mln::io::impl
# ifndef MLN_INCLUDE_ONLY
template <typename I>
void save(const Image<I>& ima, const std::string& filename)
{
......
......@@ -57,23 +57,45 @@ namespace mln
namespace pgm
{
/*! Load a pgm image in a milena image.
*
* \param[out] ima A reference to the image which will receive
* data.
* \param[in] filename The source.
*/
template <typename I>
void load(Image<I>& ima,
const std::string& filename);
/*! Load a pgm image in a milena image. To use this routine, you
* should specialize the template whith the value type of the
* image loaded. (ex : load<value::int_u8>("...") )
*
* \param[in] filename The image source.
*
* \return An image2d which contains loaded data.
*/
template <typename V>
image2d<V> load(const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
template <typename V>
image2d<V> load(const std::string& filename)
{
return io::pnm::load<V>(PGM, filename);
}
image2d<value::int_u8> load(const std::string& filename)
{
return load<value::int_u8>(filename);
}
template <typename I>
void load(Image<I>& ima,
const std::string& filename)
{
io::pnm::load<I>(PGM, ima, filename);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::pgm
} // end of namespace mln::io
......
......@@ -63,6 +63,11 @@ namespace mln
namespace pgm
{
/*! Save a milena image as a pgm image.
*
* \param[in] ima The image to save.
* \param[in,out] filename the destination.
*/
template <typename I>
void save(const Image<I>& ima, const std::string& filename);
......
......@@ -179,7 +179,7 @@ namespace mln
}
/// a new function to load pnm files :
/// An other way to load pnm files :
/// the destination is an argument to check if
/// the type match the file to load.
template <typename I>
......
......@@ -49,6 +49,7 @@ namespace mln
namespace pnm
{
# ifndef MLN_INCLUDE_ONLY
bool read_header(std::ifstream& istr,
char& type,
......@@ -125,6 +126,8 @@ namespace mln
nrows, ncols, maxval);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::pnm
} // end of namespace mln::io
......
......@@ -44,8 +44,10 @@ namespace mln
namespace pnm
{
/// Give the maximum value which can be stored as a component
/// value type V.
template <typename V>
unsigned int max_component();
unsigned int max_component(const V&);
# ifndef MLN_INCLUDE_ONLY
......
......@@ -71,7 +71,6 @@ namespace mln
* \param[in] ima_ The image to save.
* \param[in,out] filename the destination.
*/
template <typename I>
void save(const int type, const Image<I>& ima_, const std::string& filename);
......
......@@ -39,6 +39,8 @@
# include <iostream>
# include <fstream>
# include <mln/io/pnm/max_component.hh>
# include <mln/value/rgb.hh>
# include <mln/geom/nrows.hh>
# include <mln/geom/ncols.hh>
......@@ -57,21 +59,13 @@ namespace mln
template <typename V>
void save_max_val(V&, std::ofstream& file)
{
file << mln_max(V) << std::endl;
file << max_component(V()) << std::endl;
}
void save_max_val(bool&, std::ofstream& file)
{
}
template <unsigned int n>
void save_max_val(value::rgb<n>&, std::ofstream& file)
{
typedef typename value::int_u<n>::enc E;
file << unsigned(mln_max(E)) << std::endl;
}
template <typename I>
void save_header(const char type,
const I& ima, const std::string& filename,
......
......@@ -54,17 +54,36 @@ namespace mln
namespace ppm
{
/*! Load a ppm image in a milena image.
*
* \param[out] ima A reference to the image which will receive
* data.
* \param[in] filename The source.
*/
template <typename I>
void load(Image<I>& ima,
const std::string& filename);
/*! Load a ppm image in a milena image. To use this routine, you
* should specialize the template whith the value type of the
* image loaded. (ex : load<value::int_u8>("...") )
*
* \param[in] filename The image source.
*
* \return An image2d which contains loaded data.
*/
template <typename V>
image2d<V> load(const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
template <typename V>
image2d<V> load(const std::string& filename)
{
return io::pnm::load<V>(PPM, filename);
}
image2d<value::rgb8> load(const std::string& filename)
{
return load<value::rgb8>(filename);
}
template <typename I>
void load(Image<I>& ima,
const std::string& filename)
......@@ -72,6 +91,8 @@ namespace mln
io::pnm::load<I>(PPM, ima, filename);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::ppm
} // end of namespace mln::io
......
......@@ -52,6 +52,11 @@ namespace mln
namespace ppm
{
/*! Save a milena image as a ppm image.
*
* \param[in] ima The image to save.
* \param[in,out] filename the destination.
*/
template <typename I>
void save(const Image<I>& ima, const std::string& filename);
......
......@@ -46,8 +46,10 @@ int main()
using namespace mln;
using value::rgb8;
image2d<rgb8> lena = io::ppm::load("../../../img/lena.ppm");
image2d<rgb8> lena = io::ppm::load<rgb8>("../../../img/lena.ppm");
io::ppm::save(lena, "out.ppm");
image2d<rgb8> lena2 = io::ppm::load("out.ppm");
image2d<rgb8> lena2;
io::ppm::load(lena2, "out.ppm");
mln_assertion(lena2 == lena);
}
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