Magick++  6.9.3
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 int main( int /*argc*/, char ** argv)
23 {
24 
25  // Initialize ImageMagick install location for Windows
26  InitializeMagick(*argv);
27 
28  try {
29 
30  string srcdir("");
31  if(getenv("SRCDIR") != 0)
32  srcdir = getenv("SRCDIR");
33 
34  list<Image> montage;
35 
36  {
37  //
38  // Read model & smile image.
39  //
40  cout << "Read images ..." << endl;
41 
42  Image model( srcdir + "model.miff" );
43  model.label( "Magick++" );
44  model.borderColor( "black" );
45  model.backgroundColor( "black" );
46 
47  Image smile( srcdir + "smile.miff" );
48  smile.label( "Smile" );
49  smile.borderColor( "black" );
50 
51  //
52  // Create image stack.
53  //
54  cout << "Creating thumbnails..." << endl;
55 
56  // Construct initial list containing seven copies of a null image
57  Image null;
58  null.size( Geometry(70,70) );
59  null.read( "NULL:black" );
60  list<Image> images( 7, null );
61 
62  Image example = model;
63 
64  // Each of the following follow the pattern
65  // 1. obtain reference to (own copy of) image
66  // 2. apply label to image
67  // 3. apply operation to image
68  // 4. append image to container
69 
70  cout << " add noise ..." << endl;
71  example.label( "Add Noise" );
72  example.addNoise( LaplacianNoise );
73  images.push_back( example );
74 
75  cout << " add noise (blue) ..." << endl;
76  example.label( "Add Noise\n(Blue Channel)" );
77  example.addNoiseChannel( BlueChannel, PoissonNoise );
78  images.push_back( example );
79 
80  cout << " annotate ..." << endl;
81  example = model;
82  example.label( "Annotate" );
83  example.density( "72x72" );
84  example.fontPointsize( 18 );
85  if (getenv("MAGICK_FONT") != 0)
86  example.font(string(getenv("MAGICK_FONT")));
87  example.strokeColor( Color() );
88  example.fillColor( "gold" );
89  example.annotate( "Magick++", "+0+20", NorthGravity );
90  images.push_back( example );
91 
92  cout << " blur ..." << endl;
93  example = model;
94  example.label( "Blur" );
95  example.blur( 0, 1.5 );
96  images.push_back( example );
97 
98  cout << " blur red channel ..." << endl;
99  example = model;
100  example.label( "Blur Channel\n(Red Channel)" );
101  example.blurChannel( RedChannel, 0, 3.0 );
102  images.push_back( example );
103 
104  cout << " border ..." << endl;
105  example = model;
106  example.label( "Border" );
107  example.borderColor( "gold" );
108  example.border( Geometry(6,6) );
109  images.push_back( example );
110 
111  cout << " channel ..." << endl;
112  example = model;
113  example.label( "Channel\n(Red Channel)" );
114  example.channel( RedChannel );
115  images.push_back( example );
116 
117  cout << " charcoal ..." << endl;
118  example = model;
119  example.label( "Charcoal" );
120  example.charcoal( );
121  images.push_back( example );
122 
123  cout << " composite ..." << endl;
124  example = model;
125  example.label( "Composite" );
126  example.composite( smile, "+35+65", OverCompositeOp);
127  images.push_back( example );
128 
129  cout << " contrast ..." << endl;
130  example = model;
131  example.label( "Contrast" );
132  example.contrast( false );
133  images.push_back( example );
134 
135  cout << " convolve ..." << endl;
136  example = model;
137  example.label( "Convolve" );
138  {
139  // 3x3 matrix
140  const double kernel[] = { 1, 1, 1, 1, 4, 1, 1, 1, 1 };
141  example.convolve( 3, kernel );
142  }
143  images.push_back( example );
144 
145  cout << " crop ..." << endl;
146  example = model;
147  example.label( "Crop" );
148  example.crop( "80x80+25+50" );
149  images.push_back( example );
150 
151  cout << " despeckle ..." << endl;
152  example = model;
153  example.label( "Despeckle" );
154  example.despeckle( );
155  images.push_back( example );
156 
157  cout << " draw ..." << endl;
158  example = model;
159  example.label( "Draw" );
160  example.fillColor(Color());
161  example.strokeColor( "gold" );
162  example.strokeWidth( 2 );
163  example.draw( DrawableCircle( 60,90, 60,120 ) );
164  images.push_back( example );
165 
166  cout << " edge ..." << endl;
167  example = model;
168  example.label( "Detect Edges" );
169  example.edge( );
170  images.push_back( example );
171 
172  cout << " emboss ..." << endl;
173  example = model;
174  example.label( "Emboss" );
175  example.emboss( );
176  images.push_back( example );
177 
178  cout << " equalize ..." << endl;
179  example = model;
180  example.label( "Equalize" );
181  example.equalize( );
182  images.push_back( example );
183 
184  cout << " explode ..." << endl;
185  example = model;
186  example.label( "Explode" );
187  example.backgroundColor( "#000000FF" );
188  example.implode( -1 );
189  images.push_back( example );
190 
191  cout << " flip ..." << endl;
192  example = model;
193  example.label( "Flip" );
194  example.flip( );
195  images.push_back( example );
196 
197  cout << " flop ..." << endl;
198  example = model;
199  example.label( "Flop" );
200  example.flop();
201  images.push_back( example );
202 
203  cout << " frame ..." << endl;
204  example = model;
205  example.label( "Frame" );
206  example.frame( );
207  images.push_back( example );
208 
209  cout << " gamma ..." << endl;
210  example = model;
211  example.label( "Gamma" );
212  example.gamma( 1.6 );
213  images.push_back( example );
214 
215  cout << " gaussian blur ..." << endl;
216  example = model;
217  example.label( "Gaussian Blur" );
218  example.gaussianBlur( 0.0, 1.5 );
219  images.push_back( example );
220 
221  cout << " gaussian blur channel ..." << endl;
222  example = model;
223  example.label( "Gaussian Blur\n(Green Channel)" );
224  example.gaussianBlurChannel( GreenChannel, 0.0, 1.5 );
225  images.push_back( example );
226 
227  cout << " gradient ..." << endl;
228  Image gradient;
229  gradient.size( "130x194" );
230  gradient.read( "gradient:#20a0ff-#ffff00" );
231  gradient.label( "Gradient" );
232  images.push_back( gradient );
233 
234  cout << " grayscale ..." << endl;
235  example = model;
236  example.label( "Grayscale" );
237  example.quantizeColorSpace( GRAYColorspace );
238  example.quantize( );
239  images.push_back( example );
240 
241  cout << " implode ..." << endl;
242  example = model;
243  example.label( "Implode" );
244  example.implode( 0.5 );
245  images.push_back( example );
246 
247  cout << " level ..." << endl;
248  example = model;
249  example.label( "Level" );
250  example.level( 0.20*QuantumRange, 0.90*QuantumRange, 1.20 );
251  images.push_back( example );
252 
253  cout << " level red channel ..." << endl;
254  example = model;
255  example.label( "Level Channel\n(Red Channel)" );
256  example.levelChannel( RedChannel, 0.20*QuantumRange, 0.90*QuantumRange, 1.20 );
257  images.push_back( example );
258 
259  cout << " median filter ..." << endl;
260  example = model;
261  example.label( "Median Filter" );
262  example.medianFilter( );
263  images.push_back( example );
264 
265  cout << " modulate ..." << endl;
266  example = model;
267  example.label( "Modulate" );
268  example.modulate( 110, 110, 110 );
269  images.push_back( example );
270 
271  cout << " monochrome ..." << endl;
272  example = model;
273  example.label( "Monochrome" );
274  example.quantizeColorSpace( GRAYColorspace );
275  example.quantizeColors( 2 );
276  example.quantizeDither( false );
277  example.quantize( );
278  images.push_back( example );
279 
280  cout << " motion blur ..." << endl;
281  example = model;
282  example.label( "Motion Blur" );
283  example.motionBlur( 0.0, 7.0,45 );
284  images.push_back( example );
285 
286  cout << " negate ..." << endl;
287  example = model;
288  example.label( "Negate" );
289  example.negate( );
290  images.push_back( example );
291 
292  cout << " normalize ..." << endl;
293  example = model;
294  example.label( "Normalize" );
295  example.normalize( );
296  images.push_back( example );
297 
298  cout << " oil paint ..." << endl;
299  example = model;
300  example.label( "Oil Paint" );
301  example.oilPaint( );
302  images.push_back( example );
303 
304  cout << " ordered dither 2x2 ..." << endl;
305  example = model;
306  example.label( "Ordered Dither\n(2x2)" );
307  example.randomThreshold( Geometry(2,2) );
308  images.push_back( example );
309 
310  cout << " ordered dither 3x3..." << endl;
311  example = model;
312  example.label( "Ordered Dither\n(3x3)" );
313  example.randomThreshold( Geometry(3,3) );
314  images.push_back( example );
315 
316  cout << " ordered dither 4x4..." << endl;
317  example = model;
318  example.label( "Ordered Dither\n(4x4)" );
319  example.randomThreshold( Geometry(4,4) );
320  images.push_back( example );
321 
322  cout << " ordered dither red 4x4..." << endl;
323  example = model;
324  example.label( "Ordered Dither\n(Red 4x4)" );
325  example.randomThresholdChannel( Geometry(4,4), RedChannel);
326  images.push_back( example );
327 
328  cout << " plasma ..." << endl;
329  Image plasma;
330  plasma.size( "130x194" );
331  plasma.read( "plasma:fractal" );
332  plasma.label( "Plasma" );
333  images.push_back( plasma );
334 
335  cout << " quantize ..." << endl;
336  example = model;
337  example.label( "Quantize" );
338  example.quantize( );
339  images.push_back( example );
340 
341  cout << " quantum operator ..." << endl;
342  example = model;
343  example.label( "Quantum Operator\nRed * 0.4" );
344  example.quantumOperator( RedChannel,MultiplyEvaluateOperator,0.40 );
345  images.push_back( example );
346 
347  cout << " raise ..." << endl;
348  example = model;
349  example.label( "Raise" );
350  example.raise( );
351  images.push_back( example );
352 
353  cout << " reduce noise ..." << endl;
354  example = model;
355  example.label( "Reduce Noise" );
356  example.reduceNoise( 1.0 );
357  images.push_back( example );
358 
359  cout << " resize ..." << endl;
360  example = model;
361  example.label( "Resize" );
362  example.zoom( "50%" );
363  images.push_back( example );
364 
365  cout << " roll ..." << endl;
366  example = model;
367  example.label( "Roll" );
368  example.roll( "+20+10" );
369  images.push_back( example );
370 
371  cout << " rotate ..." << endl;
372  example = model;
373  example.label( "Rotate" );
374  example.rotate( 45 );
375  example.transparent( "black" );
376  images.push_back( example );
377 
378  cout << " scale ..." << endl;
379  example = model;
380  example.label( "Scale" );
381  example.scale( "60%" );
382  images.push_back( example );
383 
384  cout << " segment ..." << endl;
385  example = model;
386  example.label( "Segment" );
387  example.segment( 0.5, 0.25 );
388  images.push_back( example );
389 
390  cout << " shade ..." << endl;
391  example = model;
392  example.label( "Shade" );
393  example.shade( 30, 30, false );
394  images.push_back( example );
395 
396  cout << " sharpen ..." << endl;
397  example = model;
398  example.label("Sharpen");
399  example.sharpen( 0.0, 1.0 );
400  images.push_back( example );
401 
402  cout << " shave ..." << endl;
403  example = model;
404  example.label("Shave");
405  example.shave( Geometry( 10, 10) );
406  images.push_back( example );
407 
408  cout << " shear ..." << endl;
409  example = model;
410  example.label( "Shear" );
411  example.shear( 45, 45 );
412  example.transparent( "black" );
413  images.push_back( example );
414 
415  cout << " spread ..." << endl;
416  example = model;
417  example.label( "Spread" );
418  example.spread( 3 );
419  images.push_back( example );
420 
421  cout << " solarize ..." << endl;
422  example = model;
423  example.label( "Solarize" );
424  example.solarize( );
425  images.push_back( example );
426 
427  cout << " swirl ..." << endl;
428  example = model;
429  example.backgroundColor( "#000000FF" );
430  example.label( "Swirl" );
431  example.swirl( 90 );
432  images.push_back( example );
433 
434  cout << " threshold ..." << endl;
435  example = model;
436  example.label( "Threshold" );
437  example.threshold( QuantumRange/2.0 );
438  images.push_back( example );
439 
440  cout << " threshold random ..." << endl;
441  example = model;
442  example.label( "Random\nThreshold" );
443  example.randomThreshold( Geometry((size_t) (0.3*QuantumRange),
444  (size_t) (0.85*QuantumRange)) );
445  images.push_back( example );
446 
447  cout << " unsharp mask ..." << endl;
448  example = model;
449  example.label( "Unsharp Mask" );
450  // radius_, sigma_, amount_, threshold_
451  example.unsharpmask( 0.0, 1.0, 1.0, 0.05);
452  images.push_back( example );
453 
454  cout << " wave ..." << endl;
455  example = model;
456  example.label( "Wave" );
457  example.matte( true );
458  example.backgroundColor( "#000000FF" );
459  example.wave( 25, 150 );
460  images.push_back( example );
461 
462  //
463  // Create image montage.
464  //
465  cout << "Montage images..." << endl;
466 
467  for_each( images.begin(), images.end(), strokeColorImage( Color("#600") ) );
468 
469  MontageFramed montageOpts;
470  montageOpts.geometry( "130x194+10+5>" );
471  montageOpts.gravity( CenterGravity );
472  montageOpts.borderColor( "green" );
473  montageOpts.borderWidth( 1 );
474  montageOpts.tile( "7x4" );
475  montageOpts.compose( OverCompositeOp );
476  montageOpts.backgroundColor( "#ffffff" );
477  montageOpts.pointSize( 18 );
478  montageOpts.fillColor( "#600" );
479  montageOpts.strokeColor( Color() );
480  montageOpts.compose(OverCompositeOp);
481  montageOpts.fileName( "Magick++ Demo" );
482  montageImages( &montage, images.begin(), images.end(), montageOpts );
483  }
484 
485  Image& montage_image = montage.front();
486  {
487  // Create logo image
488  cout << "Adding logo image ..." << endl;
489  Image logo( "logo:" );
490  logo.zoom( "45%" );
491 
492  // Composite logo into montage image
493  Geometry placement(0,0,(montage_image.columns()/2)-(logo.columns()/2),0);
494  montage_image.composite( logo, placement, OverCompositeOp );
495  }
496 
497  for_each( montage.begin(), montage.end(), depthImage(8) );
498  for_each( montage.begin(), montage.end(), matteImage( false ) );
499  for_each( montage.begin(), montage.end(), compressTypeImage( RLECompression) );
500 
501  cout << "Writing image \"demo_out.miff\" ..." << endl;
502  writeImages(montage.begin(),montage.end(),"demo_out_%d.miff");
503 
504  // Uncomment following lines to display image to screen
505  // cout << "Display image..." << endl;
506  // montage_image.display();
507 
508  }
509  catch( exception &error_ )
510  {
511  cout << "Caught exception: " << error_.what() << endl;
512  return 1;
513  }
514 
515  return 0;
516 }
void swirl(const double degrees_)
Definition: Image.cpp:4645
class MagickPPExport Color
Definition: Color.h:15
void modulate(const double brightness_, const double saturation_, const double hue_)
Definition: Image.cpp:3574
void annotate(const std::string &text_, const Geometry &location_)
Definition: Image.cpp:1904
void levelChannel(const ChannelType channel, const double black_point, const double white_point, const double mid_point=1.0)
Definition: Image.cpp:3386
void addNoise(const NoiseType noiseType_)
Definition: Image.cpp:1852
void charcoal(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2228
void blur(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2127
void medianFilter(const double radius_=0.0)
Definition: Image.cpp:3540
void composite(const Image &compositeImage_, const Geometry &offset_, const CompositeOperator compose_=InCompositeOp)
Definition: Image.cpp:2506
void unsharpmask(const double radius_, const double sigma_, const double amount_, const double threshold_)
Definition: Image.cpp:4837
void negate(const bool grayscale_=false)
Definition: Image.cpp:3699
void raise(const Geometry &geometry_=raiseGeometryDefault, const bool raisedFlag_=false)
Definition: Image.cpp:3993
double gamma(void) const
Definition: Image.cpp:919
void crop(const Geometry &geometry_)
Definition: Image.cpp:2616
void shade(const double azimuth_=30, const double elevation_=30, const bool colorShading_=false)
Definition: Image.cpp:4370
void equalize(void)
Definition: Image.cpp:2844
void zoom(const Geometry &geometry_)
Definition: Image.cpp:5010
void randomThresholdChannel(const Geometry &thresholds_, const ChannelType channel_)
Definition: Image.cpp:4014
void strokeWidth(const double strokeWidth_)
Definition: Image.cpp:1554
void blurChannel(const ChannelType channel_, const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2138
void strokeColor(const Color &strokeColor_)
Definition: Image.cpp:1451
int main(int argc, char **argv)
Definition: analyze.cpp:16
void level(const double black_point, const double white_point, const double mid_point=1.0)
Definition: Image.cpp:3373
void motionBlur(const double radius_, const double sigma_, const double angle_)
Definition: Image.cpp:3687
void emboss(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2814
void segment(const double clusterThreshold_=1.0, const double smoothingThreshold_=1.5)
Definition: Image.cpp:4294
void reduceNoise(void)
Definition: Image.h:1637
void quantumOperator(const ChannelType channel_, const MagickEvaluateOperator operator_, double rvalue_)
Definition: Image.cpp:3952
void sharpen(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:4395
void montageImages(Container *montageImages_, InputIterator first_, InputIterator last_, const Montage &options_)
Definition: STL.h:2497
void solarize(const double factor_=50.0)
Definition: Image.cpp:4482
size_t columns(void) const
Definition: Image.h:1623
void geometry(const Geometry &geometry_)
Definition: Montage.h:218
void quantize(const bool measureError_=false)
Definition: Image.cpp:3938
void read(const Blob &blob_)
Definition: Image.cpp:4024
void frame(const Geometry &geometry_=frameGeometryDefault)
Definition: Image.cpp:3126
void flip(void)
Definition: Image.cpp:2905
void shave(const Geometry &geometry_)
Definition: Image.cpp:4419
void borderColor(const Color &color_)
Definition: Image.cpp:426
void font(const std::string &font_)
Definition: Image.cpp:827
void fontPointsize(const double pointSize_)
Definition: Image.cpp:850
class MagickPPExport Geometry
Definition: Geometry.h:19
void writeImages(InputIterator first_, InputIterator last_, const std::string &imageSpec_, bool adjoin_=true)
Definition: STL.h:2805
void spread(const size_t amount_=3)
Definition: Image.cpp:4532
void edge(const double radius_=0.0)
Definition: Image.cpp:2803
void fillColor(const Color &fillColor_)
Definition: Image.cpp:760
void scale(const Geometry &geometry_)
Definition: Image.cpp:4272
void size(const Geometry &geometry_)
Definition: Image.cpp:1427
void draw(const Drawable &drawable_)
Definition: Image.cpp:2753
void label(const std::string &label_)
Definition: Image.cpp:1055
void contrast(const size_t sharpen_)
Definition: Image.cpp:2565
void implode(const double factor_)
Definition: Image.cpp:3320
void gaussianBlur(const double width_, const double sigma_)
Definition: Image.cpp:3219
void density(const Geometry &geomery_)
Definition: Image.cpp:630
void wave(const double amplitude_=25.0, const double wavelength_=150.0)
Definition: Image.cpp:4876
void threshold(const double threshold_)
Definition: Image.cpp:4670
void convolve(const size_t order_, const double *kernel_)
Definition: Image.cpp:2589
void roll(const Geometry &roll_)
Definition: Image.cpp:4183
void randomThreshold(const Geometry &thresholds_)
Definition: Image.cpp:4005
void border(const Geometry &geometry_=borderGeometryDefault)
Definition: Image.cpp:2151
void gaussianBlurChannel(const ChannelType channel_, const double width_, const double sigma_)
Definition: Image.cpp:3230
void quantizeColors(const size_t colors_)
Definition: Image.cpp:1323
void flop(void)
Definition: Image.cpp:3090
void despeckle(void)
Definition: Image.cpp:2713
void channel(const ChannelType channel_)
Definition: Image.cpp:2202
MagickPPExport void InitializeMagick(const char *path_)
Definition: Functions.cpp:54
void normalize(void)
Definition: Image.cpp:3714
void backgroundColor(const Color &color_)
Definition: Image.cpp:373
void quantizeDither(const bool ditherFlag_)
Definition: Image.cpp:1346
void oilPaint(const double radius_=3.0)
Definition: Image.cpp:3721
void addNoiseChannel(const ChannelType channel_, const NoiseType noiseType_)
Definition: Image.cpp:1863
void quantizeColorSpace(const ColorspaceType colorSpace_)
Definition: Image.cpp:1334
void matte(const bool matteFlag_)
Definition: Image.cpp:1137
void shear(const double xShearAngle_, const double yShearAngle_)
Definition: Image.cpp:4433
void transparent(const Color &color_)
Definition: Image.cpp:4745
void rotate(const double degrees_)
Definition: Image.cpp:4215