DGtal  1.2.0
MagickWriter.h
1 
17 #pragma once
18 
31 #if defined(MagickWriter_RECURSES)
32 #error Recursive header files inclusion detected in MagickWriter.h
33 #else // defined(MagickWriter_RECURSES)
35 #define MagickWriter_RECURSES
36 
37 #if !defined MagickWriter_h
39 #define MagickWriter_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <string>
45 #include "DGtal/base/CUnaryFunctor.h"
46 #include "DGtal/base/BasicFunctors.h"
47 #include "DGtal/io/writers/MagickWriter.h"
48 #include "DGtal/io/Color.h"
49 
50 #ifndef WITH_MAGICK
51 #pragma error "You must activate imagemagick (-DWITH_MAGICK=true) to include this file"
52 #endif
53 
54 // Specific inclusion method to fix warning with GraphicsMagic 1.3.31 and Clang.
55 // The warning comes from two "diagnostic pop" with missing corresponding push.
56 // "MagickLibAddendum" is defined in ImageMagick since 9 years but not in GraphicsMagic.
57 #if !defined(MagickLibAddendum) && defined(__clang__)
58 # pragma clang diagnostic push
59 # pragma clang diagnostic push
60 # include <Magick++.h>
61 # if MagickLibVersion != 0x221900
62 # pragma clang diagnostic pop
63 # pragma clang diagnostic pop
64 # endif
65 #else
66 # include <Magick++.h>
67 #endif
68 
70 
71 namespace DGtal
72 {
73 
75  // template class MagickWriter
92  template <typename TImage, typename TFunctor = functors::Identity>
93  struct MagickWriter
94  {
95  // ----------------------- Standard services ------------------------------
96  typedef TImage Image;
97  typedef typename TImage::Value Value;
98  typedef TFunctor Functor;
99 
101  BOOST_STATIC_ASSERT( (TImage::Domain::dimension == 2) );
102 
113  static bool exportMagick(const std::string & filename, const Image &anImage,
114  const Functor & aFunctor = Functor())
115  {
116  Magick::InitializeMagick(NULL);
117  int w = (anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0])+1;
118  int h = (anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1])+1;
119  Magick::Geometry geom(w,h);
120  Magick::Image image(geom,"white");
121  for(auto point: anImage.domain())
122  {
123  Color c = aFunctor( anImage(point) );
124  Magick::ColorRGB magickc( c.red()/255., c.green()/255., c.blue()/255.) ;
125  image.pixelColor( point[0] + anImage.domain().lowerBound()[0],
126  (h-1) - (point[1] + anImage.domain().lowerBound()[1]),
127  magickc );
128  }
129  image.write(filename);
130  return true;
131  }
132 
133 
134 
135  };
136 }//namespace
137 
138 // //
140 
141 #endif // !defined MagickWriter_h
142 
143 #undef MagickWriter_RECURSES
144 #endif // else defined(MagickWriter_RECURSES)
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: Export image using Imagemagick backend.
Definition: MagickWriter.h:94
BOOST_CONCEPT_ASSERT((concepts::CUnaryFunctor< TFunctor, Value, Color >))
BOOST_STATIC_ASSERT((TImage::Domain::dimension==2))
TImage::Value Value
Definition: MagickWriter.h:97
static bool exportMagick(const std::string &filename, const Image &anImage, const Functor &aFunctor=Functor())
Definition: MagickWriter.h:113
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:90
Image image(domain)
ImageContainerBySTLVector< Domain, Value > Image