Magick++  7.0.10
demo.cpp
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003
4 //
5 // Simple demo program for Magick++
6 //
7 // Concept and algorithms lifted from PerlMagick demo script written
8 // by John Christy.
9 //
10 // Max run-time size 60MB (as compared with 95MB for PerlMagick) under SPARC Solaris
11 //
12 
13 #include <Magick++.h>
14 #include <string>
15 #include <iostream>
16 #include <list>
17 
18 using namespace std;
19 
20 using namespace Magick;
21 
22 #if MAGICKCORE_FREETYPE_DELEGATE
23  #define MakeLabel(image, text) image.label( (text) )
24 #else
25  #define MakeLabel(image, text)
26 #endif
27 
28 int main( int /*argc*/, char ** argv)
29 {
30 
31  // Initialize ImageMagick install location for Windows
32  InitializeMagick(*argv);
33 
34  const char *const p = getenv("MAGICK_FONT");
35  const string MAGICK_FONT(p ? p : "");
36 
37  try {
38 
39  string srcdir("");
40  if(getenv("SRCDIR") != 0)
41  srcdir = getenv("SRCDIR");
42 
43  list<Image> montage;
44 
45  {
46  //
47  // Read model & smile image.
48  //
49  cout << "Read images ..." << endl;
50 
51  Image model( srcdir + "model.miff" );
52  MakeLabel(model, "Magick++");
53  model.borderColor( "black" );
54  model.backgroundColor( "black" );
55 
56  Image smile( srcdir + "smile.miff" );
57  MakeLabel(smile, "Smile");
58  smile.borderColor( "black" );
59 
60  //
61  // Create image stack.
62  //
63  cout << "Creating thumbnails..." << endl;
64 
65  // Construct initial list containing seven copies of a null image
66  Image null;
67  null.size( Geometry(70,70) );
68  null.read( "NULL:black" );
69  list<Image> images( 7, null );
70 
71  Image example = model;
72 
73  // Each of the following follow the pattern
74  // 1. obtain reference to (own copy of) image
75  // 2. apply label to image
76  // 3. apply operation to image
77  // 4. append image to container
78 
79  cout << " add noise ..." << endl;
80  MakeLabel(example, "Add Noise");
81  example.addNoise( LaplacianNoise );
82  images.push_back( example );
83 
84  cout << " add noise (blue) ..." << endl;
85  MakeLabel(example, "Add Noise\n(Blue Channel)");
86  example.addNoiseChannel( BlueChannel, PoissonNoise );
87  images.push_back( example );
88 
89 #if MAGICKCORE_FREETYPE_DELEGATE
90  cout << " annotate ..." << endl;
91  example = model;
92  MakeLabel(example, "Annotate");
93  example.density( "72x72" );
94  example.fontPointsize( 18 );
95  example.font(MAGICK_FONT);
96  example.strokeColor( Color() );
97  example.fillColor( "gold" );
98  example.annotate( "Magick++", "+0+20", NorthGravity );
99  images.push_back( example );
100 #endif
101 
102  cout << " blur ..." << endl;
103  example = model;
104  MakeLabel(example, "Blur");
105  example.blur( 0, 1.5 );
106  images.push_back( example );
107 
108  cout << " blur red channel ..." << endl;
109  example = model;
110  MakeLabel(example, "Blur Channel\n(Red Channel)");
111  example.blurChannel( RedChannel, 0, 3.0 );
112  images.push_back( example );
113 
114  cout << " border ..." << endl;
115  example = model;
116  MakeLabel(example, "Border");
117  example.borderColor( "gold" );
118  example.border( Geometry(6,6) );
119  images.push_back( example );
120 
121  cout << " channel ..." << endl;
122  example = model;
123  MakeLabel(example, "Channel\n(Red Channel)");
124  example.channel( RedChannel );
125  images.push_back( example );
126 
127  cout << " charcoal ..." << endl;
128  example = model;
129  MakeLabel(example, "Charcoal");
130  example.charcoal( );
131  images.push_back( example );
132 
133  cout << " composite ..." << endl;
134  example = model;
135  MakeLabel(example, "Composite");
136  example.composite( smile, "+35+65", OverCompositeOp);
137  images.push_back( example );
138 
139  cout << " contrast ..." << endl;
140  example = model;
141  MakeLabel(example, "Contrast");
142  example.contrast( false );
143  images.push_back( example );
144 
145  cout << " convolve ..." << endl;
146  example = model;
147  MakeLabel(example, "Convolve");
148  {
149  // 3x3 matrix
150  const double kernel[] = { 1, 1, 1, 1, 4, 1, 1, 1, 1 };
151  example.convolve( 3, kernel );
152  }
153  images.push_back( example );
154 
155  cout << " crop ..." << endl;
156  example = model;
157  MakeLabel(example, "Crop");
158  example.crop( "80x80+25+50" );
159  images.push_back( example );
160 
161  cout << " despeckle ..." << endl;
162  example = model;
163  MakeLabel(example, "Despeckle");
164  example.despeckle( );
165  images.push_back( example );
166 
167  cout << " draw ..." << endl;
168  example = model;
169  MakeLabel(example, "Draw");
170  example.fillColor(Color());
171  example.strokeColor( "gold" );
172  example.strokeWidth( 2 );
173  example.draw( DrawableCircle( 60,90, 60,120 ) );
174  images.push_back( example );
175 
176  cout << " edge ..." << endl;
177  example = model;
178  MakeLabel(example, "Detect Edges");
179  example.edge( );
180  images.push_back( example );
181 
182  cout << " emboss ..." << endl;
183  example = model;
184  MakeLabel(example, "Emboss");
185  example.emboss( );
186  images.push_back( example );
187 
188  cout << " equalize ..." << endl;
189  example = model;
190  MakeLabel(example, "Equalize");
191  example.equalize( );
192  images.push_back( example );
193 
194  cout << " explode ..." << endl;
195  example = model;
196  MakeLabel(example, "Explode");
197  example.backgroundColor( "#000000FF" );
198  example.implode( -1 );
199  images.push_back( example );
200 
201  cout << " flip ..." << endl;
202  example = model;
203  MakeLabel(example, "Flip");
204  example.flip( );
205  images.push_back( example );
206 
207  cout << " flop ..." << endl;
208  example = model;
209  MakeLabel(example, "Flop");
210  example.flop();
211  images.push_back( example );
212 
213  cout << " frame ..." << endl;
214  example = model;
215  MakeLabel(example, "Frame");
216  example.frame( );
217  images.push_back( example );
218 
219  cout << " gamma ..." << endl;
220  example = model;
221  MakeLabel(example, "Gamma");
222  example.gamma( 1.6 );
223  images.push_back( example );
224 
225  cout << " gaussian blur ..." << endl;
226  example = model;
227  MakeLabel(example, "Gaussian Blur");
228  example.gaussianBlur( 0.0, 1.5 );
229  images.push_back( example );
230 
231  cout << " gaussian blur channel ..." << endl;
232  example = model;
233  MakeLabel(example, "Gaussian Blur\n(Green Channel)");
234  example.gaussianBlurChannel( GreenChannel, 0.0, 1.5 );
235  images.push_back( example );
236 
237  cout << " gradient ..." << endl;
238  Image gradient;
239  gradient.size( "130x194" );
240  gradient.read( "gradient:#20a0ff-#ffff00" );
241  MakeLabel(gradient, "Gradient");
242  images.push_back( gradient );
243 
244  cout << " grayscale ..." << endl;
245  example = model;
246  MakeLabel(example, "Grayscale");
247  example.quantizeColorSpace( GRAYColorspace );
248  example.quantize( );
249  images.push_back( example );
250 
251  cout << " implode ..." << endl;
252  example = model;
253  MakeLabel(example, "Implode");
254  example.implode( 0.5 );
255  images.push_back( example );
256 
257  cout << " level ..." << endl;
258  example = model;
259  MakeLabel(example, "Level");
260  example.level( 0.20*QuantumRange, 0.90*QuantumRange, 1.20 );
261  images.push_back( example );
262 
263  cout << " level red channel ..." << endl;
264  example = model;
265  MakeLabel(example, "Level Channel\n(Red Channel)");
266  example.levelChannel( RedChannel, 0.20*QuantumRange, 0.90*QuantumRange, 1.20 );
267  images.push_back( example );
268 
269  cout << " median filter ..." << endl;
270  example = model;
271  MakeLabel(example, "Median Filter");
272  example.medianFilter( );
273  images.push_back( example );
274 
275  cout << " modulate ..." << endl;
276  example = model;
277  MakeLabel(example, "Modulate");
278  example.modulate( 110, 110, 110 );
279  images.push_back( example );
280 
281  cout << " monochrome ..." << endl;
282  example = model;
283  MakeLabel(example, "Monochrome");
284  example.quantizeColorSpace( GRAYColorspace );
285  example.quantizeColors( 2 );
286  example.quantizeDither( false );
287  example.quantize( );
288  images.push_back( example );
289 
290  cout << " motion blur ..." << endl;
291  example = model;
292  MakeLabel(example, "Motion Blur");
293  example.motionBlur( 0.0, 7.0,45 );
294  images.push_back( example );
295 
296  cout << " negate ..." << endl;
297  example = model;
298  MakeLabel(example, "Negate");
299  example.negate( );
300  images.push_back( example );
301 
302  cout << " normalize ..." << endl;
303  example = model;
304  MakeLabel(example, "Normalize");
305  example.normalize( );
306  images.push_back( example );
307 
308  cout << " oil paint ..." << endl;
309  example = model;
310  MakeLabel(example, "Oil Paint");
311  example.oilPaint( );
312  images.push_back( example );
313 
314  cout << " ordered dither 2x2 ..." << endl;
315  example = model;
316  MakeLabel(example, "Ordered Dither\n(2x2)");
317  example.randomThreshold(2,2);
318  images.push_back( example );
319 
320  cout << " ordered dither 3x3..." << endl;
321  example = model;
322  MakeLabel(example, "Ordered Dither\n(3x3)");
323  example.randomThreshold(3,3);
324  images.push_back( example );
325 
326  cout << " ordered dither 4x4..." << endl;
327  example = model;
328  MakeLabel(example, "Ordered Dither\n(4x4)");
329  example.randomThreshold(4,4);
330  images.push_back( example );
331 
332  cout << " ordered dither red 4x4..." << endl;
333  example = model;
334  MakeLabel(example, "Ordered Dither\n(Red 4x4)");
335  example.randomThresholdChannel(RedChannel,4,4);
336  images.push_back( example );
337 
338  cout << " plasma ..." << endl;
339  Image plasma;
340  plasma.size( "130x194" );
341  plasma.read( "plasma:fractal" );
342  MakeLabel(plasma, "Plasma");
343  images.push_back( plasma );
344 
345  cout << " quantize ..." << endl;
346  example = model;
347  MakeLabel(example, "Quantize");
348  example.quantize( );
349  images.push_back( example );
350 
351  cout << " quantum operator ..." << endl;
352  example = model;
353  MakeLabel(example, "Quantum Operator\nRed * 0.4");
354  example.evaluate( RedChannel,MultiplyEvaluateOperator,0.40 );
355  images.push_back( example );
356 
357  cout << " raise ..." << endl;
358  example = model;
359  MakeLabel(example, "Raise");
360  example.raise( );
361  images.push_back( example );
362 
363  cout << " reduce noise ..." << endl;
364  example = model;
365  MakeLabel(example, "Reduce Noise");
366  example.reduceNoise( 1.0 );
367  images.push_back( example );
368 
369  cout << " resize ..." << endl;
370  example = model;
371  MakeLabel(example, "Resize");
372  example.zoom( "50%" );
373  images.push_back( example );
374 
375  cout << " roll ..." << endl;
376  example = model;
377  MakeLabel(example, "Roll");
378  example.roll( "+20+10" );
379  images.push_back( example );
380 
381  cout << " rotate ..." << endl;
382  example = model;
383  MakeLabel(example, "Rotate");
384  example.rotate( 45 );
385  example.transparent( "black" );
386  images.push_back( example );
387 
388  cout << " scale ..." << endl;
389  example = model;
390  MakeLabel(example, "Scale");
391  example.scale( "60%" );
392  images.push_back( example );
393 
394  cout << " segment ..." << endl;
395  example = model;
396  MakeLabel(example, "Segment");
397  example.segment( 0.5, 0.25 );
398  images.push_back( example );
399 
400  cout << " shade ..." << endl;
401  example = model;
402  MakeLabel(example, "Shade");
403  example.shade( 30, 30, false );
404  images.push_back( example );
405 
406  cout << " sharpen ..." << endl;
407  example = model;
408  MakeLabel(example, "Sharpen");
409  example.sharpen( 0.0, 1.0 );
410  images.push_back( example );
411 
412  cout << " shave ..." << endl;
413  example = model;
414  MakeLabel(example, "Shave");
415  example.shave( Geometry( 10, 10) );
416  images.push_back( example );
417 
418  cout << " shear ..." << endl;
419  example = model;
420  MakeLabel(example, "Shear");
421  example.shear( 45, 45 );
422  example.transparent( "black" );
423  images.push_back( example );
424 
425  cout << " spread ..." << endl;
426  example = model;
427  MakeLabel(example, "Spread");
428  example.spread( 3 );
429  images.push_back( example );
430 
431  cout << " solarize ..." << endl;
432  example = model;
433  MakeLabel(example, "Solarize");
434  example.solarize( );
435  images.push_back( example );
436 
437  cout << " swirl ..." << endl;
438  example = model;
439  example.backgroundColor( "#000000FF" );
440  MakeLabel(example, "Swirl");
441  example.swirl( 90 );
442  images.push_back( example );
443 
444  cout << " threshold ..." << endl;
445  example = model;
446  MakeLabel(example, "Threshold");
447  example.threshold( QuantumRange/2.0 );
448  images.push_back( example );
449 
450  cout << " threshold random ..." << endl;
451  example = model;
452  MakeLabel(example, "Random\nThreshold");
453  example.randomThreshold( (0.3*QuantumRange),
454  (0.85*QuantumRange) );
455  images.push_back( example );
456 
457  cout << " unsharp mask ..." << endl;
458  example = model;
459  MakeLabel(example, "Unsharp Mask");
460  // radius_, sigma_, amount_, threshold_
461  example.unsharpmask( 0.0, 1.0, 1.0, 0.05);
462  images.push_back( example );
463 
464  cout << " wave ..." << endl;
465  example = model;
466  MakeLabel(example, "Wave");
467  example.alpha( true );
468  example.backgroundColor( "#000000FF" );
469  example.wave( 25, 150 );
470  images.push_back( example );
471 
472  //
473  // Create image montage.
474  //
475  cout << "Montage images..." << endl;
476 
477  for_each( images.begin(), images.end(), strokeColorImage( Color("#600") ) );
478 
479  MontageFramed montageOpts;
480  montageOpts.geometry( "130x194+10+5>" );
481  montageOpts.gravity( CenterGravity );
482  montageOpts.borderColor( "green" );
483  montageOpts.borderWidth( 1 );
484  montageOpts.tile( "7x4" );
485  montageOpts.backgroundColor( "#ffffff" );
486  montageOpts.pointSize( 18 );
487  montageOpts.font(MAGICK_FONT);
488  montageOpts.fillColor( "#600" );
489  montageOpts.strokeColor( Color() );
490  montageOpts.fileName( "Magick++ Demo" );
491  montageImages( &montage, images.begin(), images.end(), montageOpts );
492  }
493 
494  Image& montage_image = montage.front();
495  {
496  // Create logo image
497  cout << "Adding logo image ..." << endl;
498  Image logo( "logo:" );
499  logo.zoom( "45%" );
500 
501  // Composite logo into montage image
502  Geometry placement(0,0,(montage_image.columns()/2)-(logo.columns()/2),0);
503  montage_image.composite( logo, placement, OverCompositeOp );
504  }
505 
506  for_each( montage.begin(), montage.end(), depthImage(8) );
507  for_each( montage.begin(), montage.end(), alphaImage( false ) );
508  for_each( montage.begin(), montage.end(), compressTypeImage( RLECompression) );
509 
510  cout << "Writing image \"demo_out.miff\" ..." << endl;
511  writeImages(montage.begin(),montage.end(),"demo_out_%d.miff");
512 
513  // Uncomment following lines to display image to screen
514  // cout << "Display image..." << endl;
515  // montage_image.display();
516 
517  }
518  catch( exception &error_ )
519  {
520  cout << "Caught exception: " << error_.what() << endl;
521  return 1;
522  }
523 
524  return 0;
525 }
void gaussianBlur(const double radius_, const double sigma_)
Definition: Image.cpp:3237
void swirl(const double degrees_)
Definition: Image.cpp:4607
class MagickPPExport Color
Definition: Color.h:16
void modulate(const double brightness_, const double saturation_, const double hue_)
Definition: Image.cpp:3600
void annotate(const std::string &text_, const Geometry &location_)
Definition: Image.cpp:1845
void charcoal(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2187
void blur(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2094
void contrast(const bool sharpen_)
Definition: Image.cpp:2584
void medianFilter(const double radius_=0.0)
Definition: Image.cpp:3577
void composite(const Image &compositeImage_, const Geometry &offset_, const CompositeOperator compose_=InCompositeOp)
Definition: Image.cpp:2522
void unsharpmask(const double radius_, const double sigma_, const double amount_, const double threshold_)
Definition: Image.cpp:4795
void negate(const bool grayscale_=false)
Definition: Image.cpp:3729
#define MakeLabel(image, text)
Definition: demo.cpp:25
void raise(const Geometry &geometry_=raiseGeometryDefault, const bool raisedFlag_=false)
Definition: Image.cpp:3992
void crop(const Geometry &geometry_)
Definition: Image.cpp:2654
void density(const Point &density_)
Definition: Image.cpp:651
void shade(const double azimuth_=30, const double elevation_=30, const bool colorShading_=false)
Definition: Image.cpp:4383
STL namespace.
void equalize(void)
Definition: Image.cpp:2875
void zoom(const Geometry &geometry_)
Definition: Image.cpp:4981
void strokeWidth(const double strokeWidth_)
Definition: Image.cpp:1494
void blurChannel(const ChannelType channel_, const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2105
void strokeColor(const Color &strokeColor_)
Definition: Image.cpp:1390
void oilPaint(const double radius_=0.0, const double sigma=1.0)
Definition: Image.cpp:3756
void spread(const double amount_=3.0)
Definition: Image.cpp:4539
void randomThresholdChannel(const ChannelType channel_, const double low_, const double high_)
Definition: Image.cpp:4011
void motionBlur(const double radius_, const double sigma_, const double angle_)
Definition: Image.cpp:3717
void emboss(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2845
void segment(const double clusterThreshold_=1.0, const double smoothingThreshold_=1.5)
Definition: Image.cpp:4289
void reduceNoise(void)
Definition: Image.cpp:4122
void sharpen(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:4409
void montageImages(Container *montageImages_, InputIterator first_, InputIterator last_, const Montage &options_)
Definition: STL.h:2479
void solarize(const double factor_=50.0)
Definition: Image.cpp:4486
void geometry(const Geometry &geometry_)
Definition: Montage.cpp:80
void quantize(const bool measureError_=false)
Definition: Image.cpp:3978
void read(const Blob &blob_)
Definition: Image.cpp:4022
void frame(const Geometry &geometry_=frameGeometryDefault)
Definition: Image.cpp:3146
void flip(void)
Definition: Image.cpp:2984
void shave(const Geometry &geometry_)
Definition: Image.cpp:4434
void borderColor(const Color &color_)
Definition: Image.cpp:427
void font(const std::string &font_)
Definition: Image.cpp:850
void fontPointsize(const double pointSize_)
Definition: Image.cpp:872
class MagickPPExport Geometry
Definition: Geometry.h:19
void writeImages(InputIterator first_, InputIterator last_, const std::string &imageSpec_, bool adjoin_=true)
Definition: STL.h:2818
void edge(const double radius_=0.0)
Definition: Image.cpp:2834
void fillColor(const Color &fillColor_)
Definition: Image.cpp:784
void scale(const Geometry &geometry_)
Definition: Image.cpp:4267
void size(const Geometry &geometry_)
Definition: Image.cpp:1366
void draw(const Drawable &drawable_)
Definition: Image.cpp:2785
void randomThreshold(const double low_, const double high_)
Definition: Image.cpp:4004
void evaluate(const ChannelType channel_, const MagickEvaluateOperator operator_, double rvalue_)
Definition: Image.cpp:2891
void implode(const double factor_)
Definition: Image.cpp:3356
void addNoiseChannel(const ChannelType channel_, const NoiseType noiseType_, const double attenuate_=1.0)
Definition: Image.cpp:1794
void wave(const double amplitude_=25.0, const double wavelength_=150.0)
Definition: Image.cpp:4836
double gamma(void) const
Definition: Image.cpp:945
void threshold(const double threshold_)
Definition: Image.cpp:4634
void convolve(const size_t order_, const double *kernel_)
Definition: Image.cpp:2612
void roll(const Geometry &roll_)
Definition: Image.cpp:4186
void gaussianBlurChannel(const ChannelType channel_, const double radius_, const double sigma_)
Definition: Image.cpp:3248
void transparent(const Color &color_, const bool inverse_=false)
Definition: Image.cpp:4697
void alpha(const bool alphaFlag_)
Definition: Image.cpp:305
void border(const Geometry &geometry_=borderGeometryDefault)
Definition: Image.cpp:2119
void addNoise(const NoiseType noiseType_, const double attenuate_=1.0)
Definition: Image.cpp:1783
size_t columns(void) const
Definition: Image.cpp:588
void quantizeColors(const size_t colors_)
Definition: Image.cpp:1258
Definition: Blob.h:15
void flop(void)
Definition: Image.cpp:3107
void levelChannel(const ChannelType channel_, const double blackPoint_, const double whitePoint_, const double gamma_=1.0)
Definition: Image.cpp:3420
void despeckle(void)
Definition: Image.cpp:2754
void channel(const ChannelType channel_)
Definition: Image.cpp:2176
MagickPPExport void InitializeMagick(const char *path_)
Definition: Functions.cpp:43
void normalize(void)
Definition: Image.cpp:3748
int main(int, char **argv)
Definition: demo.cpp:28
void backgroundColor(const Color &color_)
Definition: Image.cpp:374
void quantizeDither(const bool ditherFlag_)
Definition: Image.cpp:1281
void level(const double blackPoint_, const double whitePoint_, const double gamma_=1.0)
Definition: Image.cpp:3411
void quantizeColorSpace(const ColorspaceType colorSpace_)
Definition: Image.cpp:1269
void shear(const double xShearAngle_, const double yShearAngle_)
Definition: Image.cpp:4448
void rotate(const double degrees_)
Definition: Image.cpp:4209