Commit 5c195b4c authored by Thierry Geraud's avatar Thierry Geraud
Browse files

2006-10-10 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr>

	Add i/o read for pbm and pgm formats.
	
	* tests/io_pnm.cc: New.
	* tests/Makefile.am (check_PROGRAMS): Update.
	* oln/io: New directory.
	* oln/io/pnm.hh: New.
	* oln/core/image_entry.hh (coord_type): New single_vtype.
	* oln/core/abstract/image.hh (decl): New comment.
	* oln/core/2d/image2d.hh (adr_at): New.
	* oln/basics2d.hh (include): Add fwd_piter.hh.
	* oln/Makefile.am (nobase_oln_HEADERS): Update.
	* img: New directory.
	* img/chien.pbm: New.
	* img/lena32.pgm: New.



git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@615 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 28d697de
2006-10-10 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr>
Add i/o read for pbm and pgm formats.
* tests/io_pnm.cc: New.
* tests/Makefile.am (check_PROGRAMS): Update.
* oln/io: New directory.
* oln/io/pnm.hh: New.
* oln/core/image_entry.hh (coord_type): New single_vtype.
* oln/core/abstract/image.hh (decl): New comment.
* oln/core/2d/image2d.hh (adr_at): New.
* oln/basics2d.hh (include): Add fwd_piter.hh.
* oln/Makefile.am (nobase_oln_HEADERS): Update.
* img: New directory.
* img/chien.pbm: New.
* img/lena32.pgm: New.
2006-10-09 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr>
* oln/core/internal/tracked_ptr.hh (operator bool):
......
P5
32 32
255
chmztsy{{`bgly~~~~vvw{Y3`fkx}~~~}wtuV40]dlw|ysrp02.rXbhsy{w}io@02<hkYbhsxrs{qhdB/1FyYkYbhtvovзo>yZ129uZkV`g{ynzѠSK12aaoYcfxlxO-F`pZacl|<4v_rZaavɿr.ScuYa`z~r[fU[|B4wfsX_a||zbF>H]|F/HfnX`ep|vX@C;PmlF32edl[citxT:6?SЄRE6=ck[ci}naD:6CypJO4T_d\`pUKI73jqVomEFU4n\]blnZJOU/CTHX<NSbrZSD>V;swvcFXMHMfhV\D9UVuGuV@^gIXb]NYYAVrIIw@>dA^f[DR_iQ5Jtd7@f7_hSDdcp56Aa~?8Dj1a^HAdotB548Cbf2>LmϷ;Ra>A]jqM935>d^AOo}~͊OwYZ8GKWse>59GoGqs}ŚTXspO1A:KkoI5<Uqirsac_tI1A4>foN:KewҠ}ͮgc`SzM295@XY<7_hĀ~\^Z[M075<^I,BakЀWP\W]OiE39B>UA.Od~Ϝa[iOU_FQ<9ANKJBFl|Ȱesr`T_X?
\ No newline at end of file
......@@ -132,6 +132,8 @@ nobase_oln_HEADERS = \
\
debug/print.hh \
\
io/pnm.hh \
\
level/fill.hh \
\
morpher/internal/image_extension.hh \
......
......@@ -50,5 +50,7 @@ namespace oln { template class bbox_<point2d>; }
# include <oln/core/2d/image2d.hh>
# include <oln/core/fwd_piter.hh>
#endif // ! OLN_BASICS2D_HH
......@@ -89,6 +89,9 @@ namespace oln
T& impl_op_readwrite(const point2d& p);
T* adr_at(int row, int col);
const T* adr_at(int row, int col) const;
private:
topo2d topo_;
......@@ -143,6 +146,22 @@ namespace oln
return data_->operator()(p.row(), p.col());
}
template <typename T>
T* image2d<T>::adr_at(int row, int col)
{
precondition(data_ != 0);
precondition(data_->has(row, col));
return &(data_->operator()(row, col));
}
template <typename T>
const T* image2d<T>::adr_at(int row, int col) const
{
precondition(data_ != 0);
precondition(data_->has(row, col));
return &(data_->operator()(row, col));
}
# endif
} // end of namespace oln
......
......@@ -66,6 +66,8 @@ namespace oln
// oln_virtual_typedef(topo);
// oln_virtual_typedef(grid);
// oln_virtual_typedef(coord);
// oln_virtual_typedef(psite);
// oln_virtual_typedef(point);
......
......@@ -67,6 +67,8 @@ namespace oln
typedef mlc::undefined topo_type;
typedef mlc::undefined grid_type;
// coord_type: see below.
// psite_type: see below.
typedef mlc::undefined point_type;
......@@ -82,6 +84,15 @@ namespace oln
};
// Coord.
template <typename E>
struct single_vtype< image_entry<E>, typedef_::coord_type >
{
typedef oln_deduce_type_of(E, point, coord) ret;
};
// Psite.
template <typename E>
struct single_vtype< image_entry<E>, typedef_::psite_type >
{
......@@ -89,6 +100,7 @@ namespace oln
};
// Piter.
template <typename E>
struct single_vtype< image_entry<E>, typedef_::piter_type >
{
......@@ -96,6 +108,7 @@ namespace oln
};
// Rvalue.
template <typename E>
struct single_vtype< image_entry<E>, typedef_::rvalue_type >
{
......
// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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 OLN_IO_PNM_HH
# define OLN_IO_PNM_HH
# include <iostream>
# include <fstream>
# include <string>
# include <oln/core/2d/image2d.hh>
namespace oln {
namespace io {
namespace internal {
void abort()
{
std::cerr << " aborting." << std::endl;
exit(0);
}
bool read_pnm_header(std::istream& istr,
char& type,
int& nrows, int& ncols,
bool test = false)
{
// check magic
if (istr.get() != 'P' )
goto err;
type = istr.get();
if (type < '1' or type > '6')
goto err;
if (istr.get() != '\n')
goto err;
// skip comments
while (istr.peek() == '#')
{
std::string line;
std::getline(istr, line);
}
// get size
istr >> ncols >> nrows;
if (nrows <= 0 or ncols <= 0)
goto err;
// skip maxvalue
if (istr.get() != '\n')
goto err;
if (type != '1' and type != '4')
{
std::string line;
std::getline(istr, line);
}
return true;
err:
if (not test)
{
std::cerr << "error: badly formed header!";
abort();
}
return false;
}
void read_pnm_header(char ascii, char raw,
std::istream& istr,
char& type,
int& nrows, int& ncols)
{
read_pnm_header(istr, type, nrows, ncols);
if (not (type == ascii or type == raw))
{
std::cerr << "error: bad pnm type; "
<< "expected P" << ascii
<< " or P" << raw
<< ", get P" << type << "!";
abort();
}
}
/// load_pbm_raw.
template <typename I>
void load_pbm_raw(std::ifstream& file, I& ima)
{
oln_type_of(I, coord) cols = 0;
unsigned bits = 0;
unsigned char c = 0;
oln_type_of(I, fwd_piter) p(ima.topo());
for_all(p)
{
if (bits == 0)
{
c = file.get();
bits = 8;
}
ima(p) = (c & (1 << --bits)) ? false : true;
if (++cols >= int(ima.bbox().ncols()))
cols = bits = 0;
}
}
/// load_pbm_ascii.
template <typename I>
void load_pbm_ascii(std::ifstream& file, I& ima)
{
oln_type_of(I, fwd_piter) p(ima.topo());
for_all(p)
ima(p) = (file.get() == '0');
}
/// load_ascii.
template <typename I>
void load_pnm_ascii(std::ifstream& file, I& ima)
{
oln_type_of(I, fwd_piter) p(ima.topo());
for_all(p)
{
oln_type_of(I, value) value;
file >> value;
ima(p) = value;
// FIXME: Test alt code below.
// file >> ima(p);
}
}
/// load_raw_2d.
template <typename I>
void load_pnm_raw_2d(std::ifstream& file, I& ima)
{
int col = ima.pmin().col();
size_t len = ima.bbox().ncols();
for (int row = ima.pmin().row(); row <= ima.pmax().row(); ++row)
file.read((char*)(ima.adr_at(row, col)),
len * sizeof(oln_type_of(I, value)));
}
} // end of namespace oln::io::internal
image2d<bool> load_pbm(const std::string& filename)
{
std::ifstream file(filename.c_str());
if (not file)
{
std::cerr << "error: file '" << filename
<< "' not found!";
abort();
}
char type;
int nrows, ncols;
internal::read_pnm_header('1', '4', file, type, nrows, ncols);
image2d<bool> ima(nrows, ncols);
if (type == '4')
internal::load_pbm_raw(file, ima);
else
// type == '1'
internal::load_pbm_ascii(file, ima);
return ima;
}
image2d<unsigned char> load_pgm(const std::string& filename)
{
std::ifstream file(filename.c_str());
if (not file)
{
std::cerr << "error: file '" << filename
<< "' not found!";
abort();
}
char type;
int nrows, ncols;
internal::read_pnm_header('2', '5', file, type, nrows, ncols);
image2d<unsigned char> ima(nrows, ncols);
if (type == '4')
internal::load_pnm_raw_2d(file, ima);
else
// type == '1'
internal::load_pnm_ascii(file, ima);
return ima;
}
} // end of namespace oln::io
} // end of namespace oln
#endif // ! OLN_IO_PNM_HH
......@@ -25,6 +25,7 @@ check_PROGRAMS = \
image1d \
image2d \
image3d \
io_pnm \
npoints \
\
identity_morpher \
......
// Copyright (C) 2006 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.
/// Test oln::io_pnm.cc.
#include <cassert>
#include <oln/basics2d.hh>
#include <oln/io/pnm.hh>
int
main()
{
// Fill a 2-d image using its iterator.
oln::image2d<bool> ima = oln::io::load_pbm("../img/chien.pbm");
oln::image2d<unsigned char> ima2 = oln::io::load_pgm("../img/lena32.pgm");
}
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