DGtal  1.2.0
MagickReader.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file MagickReader.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2010/07/25
23  *
24  * Implementation of inline methods defined in MagickReader.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // Interface - public :
36 
37 template <typename TImageContainer, typename TFunctor>
38 inline
39 TImageContainer
40 DGtal::MagickReader<TImageContainer, TFunctor>::importImage(const std::string & filename,
41  const Functor & aFunctor, bool topbotomOrder )
42 {
43  int w,h;
44  const Magick::PixelPacket *cacheRead;
45  DGtal::IOException dgtalio;
46 
47  Magick::InitializeMagick(NULL);
48 
49  Magick::Image img;
50 
51  try
52  {
53  img.read ( filename );
54  }
55  catch( ... )
56  {
57  std::cout << "MagickReader : can't open " << filename << std::endl;
58  throw dgtalio;
59  }
60 
61  w = img.size().width();
62  h = img.size().height();
63 
64  //@todo Check Magick::Types according to ValueType
65  img.type ( Magick::TrueColorType );
66  img.modifyImage();
67  cacheRead = img.getConstPixels ( 0, 0, w, h );
68 
69  //@todo check ImageMagick errors.
70  //trace.error() << "MagickReader : can't open "<< filename<<endl;
71 
72  typename TImageContainer::Point firstPoint;
73  typename TImageContainer::Point lastPoint;
74 
75  firstPoint = TImageContainer::Point::zero;
76  lastPoint[0] = w-1;
77  lastPoint[1] = h-1;
78 
79  typename TImageContainer::Domain domain(firstPoint,lastPoint);
80  TImageContainer image(domain);
81 
82  //We scan the file
83  typename TImageContainer::Domain::ConstIterator it = domain.begin(),
84  itend=domain.end();
85 
86  for(; it != itend; ++it)
87  {
88  const Magick::PixelPacket *pixel = cacheRead + w * ((topbotomOrder)? ( h - 1 - (*it)[1] ): (*it)[1] ) + (*it)[0];
89  Color col(pixel->red,pixel->green,pixel->blue);
90  image.setValue( (*it), aFunctor(col) );
91  }
92 
93  return image;
94 }