Commit 89cf4eee authored by Guillaume Lazzara's avatar Guillaume Lazzara
Browse files

First draft of raw format support.

	* milena/mln/io/raw/all.hh,
	* milena/mln/io/raw/get_header.hh,
	* milena/mln/io/raw/load.hh,
	* milena/mln/io/raw/save.hh: New.
parent a7dd8e81
2010-02-16 Guillaume Lazzara <z@lrde.epita.fr>
First draft of raw format support.
* milena/mln/io/raw/all.hh,
* milena/mln/io/raw/get_header.hh,
* milena/mln/io/raw/load.hh,
* milena/mln/io/raw/save.hh: New.
2010-02-16 Guillaume Lazzara <z@lrde.epita.fr>
 
Add new 3D neighborhoods and windows.
// Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
// (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project 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_ALL_HH
# define MLN_IO_RAW_ALL_HH
/// \file
/// \brief Inclusion of all raw I/O routines.
namespace mln
{
namespace io
{
/// Namespace of raw input/output handling.
namespace raw {}
}
}
# include <mln/io/raw/get_header.hh>
# include <mln/io/raw/load.hh>
# include <mln/io/raw/save.hh>
#endif // ! MLN_IO_RAW_ALL_HH
// Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project 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_GET_HEADER_HH
# define MLN_IO_RAW_GET_HEADER_HH
/// \file
///
/// Load a Milena image rawed 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
{
/// Store raw file header.
struct raw_header
{
unsigned dim;
std::string value_type;
util::array<unsigned> size;
};
/// Retrieve header in a raw file.
raw_header get_header(const std::string& filename);
# ifndef MLN_INCLUDE_ONLY
raw_header get_header(const std::string& filename)
{
trace::entering("mln::io::raw::get_header");
raw_header header;
std::string info_filename = filename + ".info";
std::ifstream info_file(info_filename.c_str());
if (! info_file)
{
std::cerr << "io::raw::get_header - Error: cannot open file '"
<< filename << "'!"
<< std::endl;
abort();
}
std::string file_type;
info_file >> file_type;
if (file_type != "milena/raw")
{
std::cerr << "io::raw::load - Error: invalid file type. '"
<< filename
<< "' is NOT a valid milena/raw info file!"
<< std::endl;
abort();
}
char dev_null[30];
// Dimension ?
// Reading line title "Dim: "
info_file.read(dev_null, 5);
info_file >> header.dim;
// Size information - Skip it, useless.
header.size.resize(header.dim);
for (unsigned i = 0; i < header.dim; ++i)
info_file >> header.size[i];
// Skipping endline.
char c;
info_file.get(c);
// Value type name ?
// Reading line title "data type: "
info_file.read(dev_null, 11);
// WARNING: value type name limited to 255 characters...
char value_type[255];
info_file.getline(value_type, 255);
header.value_type = value_type;
info_file.close();
trace::exiting("mln::io::raw::get_header");
return header;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::io::raw
} // end of namespace mln::io
} // end of namespace mln
#endif // ! MLN_IO_RAW_GET_HEADER_HH
// Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
// (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project 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
///
/// Load an image saved as a raw data 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 an image saved as a raw data file.
///
/// \param[in,out] ima_ The image to load.
/// \param[in] filename the destination.
///
/// This routine try to read two input files: 'filename' and
/// 'filename.info'.
/// 'filename' is the raw data.
/// 'filename.info' store various information about the image.
//
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)
{
for (unsigned i = 0; i < P::dim; ++i)
file >> p[i];
}
template <typename I>
inline
void load_header(Image<I>& ima, std::ifstream& info_file,
const std::string& filename)
{
// Milena's file type ?
std::string file_type;
info_file >> file_type;
if (file_type != "milena/raw")
{
std::cerr << "io::raw::load - Error: invalid file type. '"
<< filename
<< "' is NOT a valid milena/raw info file!"
<< std::endl;
abort();
}
char dev_null[255];
// Dimension ?
// Reading line title "Dim: "
info_file.read(dev_null, 5);
unsigned dim;
info_file >> dim;
typedef mln_site(I) P;
if (P::dim != dim)
{
std::cerr << "io::raw::load - Error: invalid image dimension. '"
<< filename << "' is a " << dim << "-D image "
<< "but you try to load it into a " << P::dim
<< "-D image!"
<< std::endl;
abort();
}
// Size information - Skip it, useless.
std::string tmp;
for (unsigned i = 0; i < dim; ++i)
info_file >> tmp;
// Skipping endline.
char c;
info_file.get(c);
// Value type name ?
// Reading line title "data type: "
info_file.read(dev_null, 11);
// WARNING: value type name limited to 255 characters...
char value_type[255];
info_file.getline(value_type, 255);
if (mln_trait_value_name(mln_value(I)) != std::string(value_type))
{
std::cerr << "io::raw::load - Error: invalid image value type. '"
<< filename << "' is an image of '" << value_type
<< "' but you try to load it into an image of '"
<< mln_trait_value_name(mln_value(I)) << "'!"
<< std::endl;
abort();
}
// Pmin
// Reading line title "top left: "
info_file.read(dev_null, 10);
P pmin;
read_point<P>(info_file, pmin);
// Pmax
// Reading line title "bottom right: "
info_file.read(dev_null, 14);
P pmax;
read_point<P>(info_file, pmax);
std::cout << pmax << std::endl;
// Initialize the image buffer.
mln_concrete(I) result(box<P>(pmin, pmax));
initialize(ima, result);
mln_assertion(exact(ima).is_valid());
}
template <typename I>
inline
void load_data(Image<I>& ima_, std::ifstream& file)
{
I& ima = exact(ima_);
// Handle padding.
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 << "io::raw::load - error: cannot open file '"
<< filename << "'!";
abort();
}
std::string info_filename = filename + ".info";
std::ifstream info_file(info_filename.c_str());
if (! info_file)
{
std::cerr << "io::raw::load - error: cannot open file '"
<< info_filename << "'!";
abort();
}
internal::load_header(ima, info_file, info_filename);
internal::load_data(ima, file);
mln_postcondition(exact(ima).is_valid());
file.close();
info_file.close();
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 Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project 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
///
/// Save a Milena image as a raw data file.
///
/// \todo handle endianness.
# 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>
# include <mln/trait/value_.hh>
namespace mln
{
namespace io
{
namespace raw
{
/// Save a Milena image as a raw data file.
///
/// \param[in] ima_ The image to save.
/// \param[in] filename the destination.
///
/// This routine produce two output files: 'filename' and
/// 'filename.info'.
/// 'filename' is the raw data.
/// 'filename.info' store various information about the image.
//
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)
{
// Milena's file type
file << "milena/raw" << std::endl;
// Dimension
typedef mln_site(I) P;
file << "dim: " << P::dim << std::endl;
// Image size.
typedef algebra::vec<P::dim, unsigned> vec_t;
vec_t size = ima.domain().pmax() - ima.domain().pmin();
for (unsigned i = 0; i < P::dim - 1; ++i)
file << size[i] + 1 << " ";
file << size[P::dim - 1] + 1 << std::endl;
// Value type name
// WARNING: value type name limited to 255 characters...
file << "data type: " << mln_trait_value_name(mln_value(I))
<< std::endl;
// Pmin
file << "top left: ";
for (unsigned i = 0; i < P::dim - 1; ++i)
file << ima.domain().pmin()[i] << " ";
file << ima.domain().pmin()[P::dim - 1] << std::endl;
// Pmax
file << "bottom right: ";
for (unsigned i = 0; i < P::dim - 1; ++i)
file << ima.domain().pmax()[i] << " ";
file << ima.domain().pmax()[P::dim - 1] << std::endl;
}
template <typename I>
inline
void save_data(I& ima, std::ofstream& file)
{
// Handle padding.
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
// Facade
template <typename I>
void save(const Image<I>& ima_, const std::string& filename)
{
trace::entering("mln::io::raw::save");
mlc_bool(mln_site_(I)::dim == 2 || mln_site_(I)::dim == 3)::check();
const I& ima = exact(ima_);
std::ofstream file(filename.c_str());
if (! file)
{
std::cerr << "error: cannot open file '" << filename << "'!";
abort();
}
std::string info_filename = filename + ".info";
std::ofstream info_file(info_filename.c_str());
if (! info_file)
{
std::cerr << "error: cannot open file '" << info_filename << "'!";
abort();
}
internal::save_header(ima, info_file);
internal::save_data(ima, file);
info_file.close();
file.close();
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
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