DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MagickReader.ih
1 
30 
31 #include <cstdlib>
33 
35 // Interface - public :
36 
37 template <typename TImageContainer>
38 inline
39 TImageContainer
40 DGtal::MagickReader<TImageContainer>::importImage(const std::string & filename, bool topbotomOrder ) throw(DGtal::IOException)
41 {
42  int w,h;
43  const Magick::PixelPacket *cacheRead;
44  DGtal::IOException dgtalio;
45 
46  Magick::InitializeMagick(NULL);
47 
48  Magick::Image img;
49 
50  try
51  {
52  img.read ( filename );
53  }
54  catch( ... )
55  {
56  std::cout << "MagickReader : can't open " << filename << endl;
57  throw dgtalio;
58  }
59 
60  w = img.size().width();
61  h = img.size().height();
62 
63  //@todo Check Magick::Types according to ValueType
64  img.type ( Magick::TrueColorType );
65  img.modifyImage();
66  cacheRead = img.getConstPixels ( 0, 0, w, h );
67 
68  //@todo check ImageMagick errors.
69  //trace.error() << "MagickReader : can't open "<< filename<<endl;
70 
71  typename TImageContainer::Point firstPoint;
72  typename TImageContainer::Point lastPoint;
73 
74  firstPoint = TImageContainer::Point::zero;
75  lastPoint[0] = w-1;
76  lastPoint[1] = h-1;
77 
78  typename TImageContainer::Domain domain(firstPoint,lastPoint);
79  TImageContainer image(domain);
80  typename TImageContainer::Value val;
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];
90  val = (pixel->red + pixel->green + pixel->blue) % 256;
91  // std::cout<<" valread= "<< (int)val << std::endl;
92  image.setValue( (*it), val );
93  }
94 
95  return image;
96 }
97