Magick++  7.0.7
Convert, Edit, Or Compose Bitmap Images
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  example.strokeColor( Color() );
86  example.fillColor( "gold" );
87  example.annotate( "Magick++", "+0+20", NorthGravity );
88  images.push_back( example );
89 
90  cout << " blur ..." << endl;
91  example = model;
92  example.label( "Blur" );
93  example.blur( 0, 1.5 );
94  images.push_back( example );
95 
96  cout << " blur red channel ..." << endl;
97  example = model;
98  example.label( "Blur Channel\n(Red Channel)" );
99  example.blurChannel( RedChannel, 0, 3.0 );
100  images.push_back( example );
101 
102  cout << " border ..." << endl;
103  example = model;
104  example.label( "Border" );
105  example.borderColor( "gold" );
106  example.border( Geometry(6,6) );
107  images.push_back( example );
108 
109  cout << " channel ..." << endl;
110  example = model;
111  example.label( "Channel\n(Red Channel)" );
112  example.channel( RedChannel );
113  images.push_back( example );
114 
115  cout << " charcoal ..." << endl;
116  example = model;
117  example.label( "Charcoal" );
118  example.charcoal( );
119  images.push_back( example );
120 
121  cout << " composite ..." << endl;
122  example = model;
123  example.label( "Composite" );
124  example.composite( smile, "+35+65", OverCompositeOp);
125  images.push_back( example );
126 
127  cout << " contrast ..." << endl;
128  example = model;
129  example.label( "Contrast" );
130  example.contrast( false );
131  images.push_back( example );
132 
133  cout << " convolve ..." << endl;
134  example = model;
135  example.label( "Convolve" );
136  {
137  // 3x3 matrix
138  const double kernel[] = { 1, 1, 1, 1, 4, 1, 1, 1, 1 };
139  example.convolve( 3, kernel );
140  }
141  images.push_back( example );
142 
143  cout << " crop ..." << endl;
144  example = model;
145  example.label( "Crop" );
146  example.crop( "80x80+25+50" );
147  images.push_back( example );
148 
149  cout << " despeckle ..." << endl;
150  example = model;
151  example.label( "Despeckle" );
152  example.despeckle( );
153  images.push_back( example );
154 
155  cout << " draw ..." << endl;
156  example = model;
157  example.label( "Draw" );
158  example.fillColor(Color());
159  example.strokeColor( "gold" );
160  example.strokeWidth( 2 );
161  example.draw( DrawableCircle( 60,90, 60,120 ) );
162  images.push_back( example );
163 
164  cout << " edge ..." << endl;
165  example = model;
166  example.label( "Detect Edges" );
167  example.edge( );
168  images.push_back( example );
169 
170  cout << " emboss ..." << endl;
171  example = model;
172  example.label( "Emboss" );
173  example.emboss( );
174  images.push_back( example );
175 
176  cout << " equalize ..." << endl;
177  example = model;
178  example.label( "Equalize" );
179  example.equalize( );
180  images.push_back( example );
181 
182  cout << " explode ..." << endl;
183  example = model;
184  example.label( "Explode" );
185  example.backgroundColor( "#000000FF" );
186  example.implode( -1 );
187  images.push_back( example );
188 
189  cout << " flip ..." << endl;
190  example = model;
191  example.label( "Flip" );
192  example.flip( );
193  images.push_back( example );
194 
195  cout << " flop ..." << endl;
196  example = model;
197  example.label( "Flop" );
198  example.flop();
199  images.push_back( example );
200 
201  cout << " frame ..." << endl;
202  example = model;
203  example.label( "Frame" );
204  example.frame( );
205  images.push_back( example );
206 
207  cout << " gamma ..." << endl;
208  example = model;
209  example.label( "Gamma" );
210  example.gamma( 1.6 );
211  images.push_back( example );
212 
213  cout << " gaussian blur ..." << endl;
214  example = model;
215  example.label( "Gaussian Blur" );
216  example.gaussianBlur( 0.0, 1.5 );
217  images.push_back( example );
218 
219  cout << " gaussian blur channel ..." << endl;
220  example = model;
221  example.label( "Gaussian Blur\n(Green Channel)" );
222  example.gaussianBlurChannel( GreenChannel, 0.0, 1.5 );
223  images.push_back( example );
224 
225  cout << " gradient ..." << endl;
226  Image gradient;
227  gradient.size( "130x194" );
228  gradient.read( "gradient:#20a0ff-#ffff00" );
229  gradient.label( "Gradient" );
230  images.push_back( gradient );
231 
232  cout << " grayscale ..." << endl;
233  example = model;
234  example.label( "Grayscale" );
235  example.quantizeColorSpace( GRAYColorspace );
236  example.quantize( );
237  images.push_back( example );
238 
239  cout << " implode ..." << endl;
240  example = model;
241  example.label( "Implode" );
242  example.implode( 0.5 );
243  images.push_back( example );
244 
245  cout << " level ..." << endl;
246  example = model;
247  example.label( "Level" );
248  example.level( 0.20*QuantumRange, 0.90*QuantumRange, 1.20 );
249  images.push_back( example );
250 
251  cout << " level red channel ..." << endl;
252  example = model;
253  example.label( "Level Channel\n(Red Channel)" );
254  example.levelChannel( RedChannel, 0.20*QuantumRange, 0.90*QuantumRange, 1.20 );
255  images.push_back( example );
256 
257  cout << " median filter ..." << endl;
258  example = model;
259  example.label( "Median Filter" );
260  example.medianFilter( );
261  images.push_back( example );
262 
263  cout << " modulate ..." << endl;
264  example = model;
265  example.label( "Modulate" );
266  example.modulate( 110, 110, 110 );
267  images.push_back( example );
268 
269  cout << " monochrome ..." << endl;
270  example = model;
271  example.label( "Monochrome" );
272  example.quantizeColorSpace( GRAYColorspace );
273  example.quantizeColors( 2 );
274  example.quantizeDither( false );
275  example.quantize( );
276  images.push_back( example );
277 
278  cout << " motion blur ..." << endl;
279  example = model;
280  example.label( "Motion Blur" );
281  example.motionBlur( 0.0, 7.0,45 );
282  images.push_back( example );
283 
284  cout << " negate ..." << endl;
285  example = model;
286  example.label( "Negate" );
287  example.negate( );
288  images.push_back( example );
289 
290  cout << " normalize ..." << endl;
291  example = model;
292  example.label( "Normalize" );
293  example.normalize( );
294  images.push_back( example );
295 
296  cout << " oil paint ..." << endl;
297  example = model;
298  example.label( "Oil Paint" );
299  example.oilPaint( );
300  images.push_back( example );
301 
302  cout << " ordered dither 2x2 ..." << endl;
303  example = model;
304  example.label( "Ordered Dither\n(2x2)" );
305  example.randomThreshold(2,2);
306  images.push_back( example );
307 
308  cout << " ordered dither 3x3..." << endl;
309  example = model;
310  example.label( "Ordered Dither\n(3x3)" );
311  example.randomThreshold(3,3);
312  images.push_back( example );
313 
314  cout << " ordered dither 4x4..." << endl;
315  example = model;
316  example.label( "Ordered Dither\n(4x4)" );
317  example.randomThreshold(4,4);
318  images.push_back( example );
319 
320  cout << " ordered dither red 4x4..." << endl;
321  example = model;
322  example.label( "Ordered Dither\n(Red 4x4)" );
323  example.randomThresholdChannel(RedChannel,4,4);
324  images.push_back( example );
325 
326  cout << " plasma ..." << endl;
327  Image plasma;
328  plasma.size( "130x194" );
329  plasma.read( "plasma:fractal" );
330  plasma.label( "Plasma" );
331  images.push_back( plasma );
332 
333  cout << " quantize ..." << endl;
334  example = model;
335  example.label( "Quantize" );
336  example.quantize( );
337  images.push_back( example );
338 
339  cout << " quantum operator ..." << endl;
340  example = model;
341  example.label( "Quantum Operator\nRed * 0.4" );
342  example.evaluate( RedChannel,MultiplyEvaluateOperator,0.40 );
343  images.push_back( example );
344 
345  cout << " raise ..." << endl;
346  example = model;
347  example.label( "Raise" );
348  example.raise( );
349  images.push_back( example );
350 
351  cout << " reduce noise ..." << endl;
352  example = model;
353  example.label( "Reduce Noise" );
354  example.reduceNoise( 1.0 );
355  images.push_back( example );
356 
357  cout << " resize ..." << endl;
358  example = model;
359  example.label( "Resize" );
360  example.zoom( "50%" );
361  images.push_back( example );
362 
363  cout << " roll ..." << endl;
364  example = model;
365  example.label( "Roll" );
366  example.roll( "+20+10" );
367  images.push_back( example );
368 
369  cout << " rotate ..." << endl;
370  example = model;
371  example.label( "Rotate" );
372  example.rotate( 45 );
373  example.transparent( "black" );
374  images.push_back( example );
375 
376  cout << " scale ..." << endl;
377  example = model;
378  example.label( "Scale" );
379  example.scale( "60%" );
380  images.push_back( example );
381 
382  cout << " segment ..." << endl;
383  example = model;
384  example.label( "Segment" );
385  example.segment( 0.5, 0.25 );
386  images.push_back( example );
387 
388  cout << " shade ..." << endl;
389  example = model;
390  example.label( "Shade" );
391  example.shade( 30, 30, false );
392  images.push_back( example );
393 
394  cout << " sharpen ..." << endl;
395  example = model;
396  example.label("Sharpen");
397  example.sharpen( 0.0, 1.0 );
398  images.push_back( example );
399 
400  cout << " shave ..." << endl;
401  example = model;
402  example.label("Shave");
403  example.shave( Geometry( 10, 10) );
404  images.push_back( example );
405 
406  cout << " shear ..." << endl;
407  example = model;
408  example.label( "Shear" );
409  example.shear( 45, 45 );
410  example.transparent( "black" );
411  images.push_back( example );
412 
413  cout << " spread ..." << endl;
414  example = model;
415  example.label( "Spread" );
416  example.spread( 3 );
417  images.push_back( example );
418 
419  cout << " solarize ..." << endl;
420  example = model;
421  example.label( "Solarize" );
422  example.solarize( );
423  images.push_back( example );
424 
425  cout << " swirl ..." << endl;
426  example = model;
427  example.backgroundColor( "#000000FF" );
428  example.label( "Swirl" );
429  example.swirl( 90 );
430  images.push_back( example );
431 
432  cout << " threshold ..." << endl;
433  example = model;
434  example.label( "Threshold" );
435  example.threshold( QuantumRange/2.0 );
436  images.push_back( example );
437 
438  cout << " threshold random ..." << endl;
439  example = model;
440  example.label( "Random\nThreshold" );
441  example.randomThreshold( (0.3*QuantumRange),
442  (0.85*QuantumRange) );
443  images.push_back( example );
444 
445  cout << " unsharp mask ..." << endl;
446  example = model;
447  example.label( "Unsharp Mask" );
448  // radius_, sigma_, amount_, threshold_
449  example.unsharpmask( 0.0, 1.0, 1.0, 0.05);
450  images.push_back( example );
451 
452  cout << " wave ..." << endl;
453  example = model;
454  example.label( "Wave" );
455  example.alpha( true );
456  example.backgroundColor( "#000000FF" );
457  example.wave( 25, 150 );
458  images.push_back( example );
459 
460  //
461  // Create image montage.
462  //
463  cout << "Montage images..." << endl;
464 
465  for_each( images.begin(), images.end(), strokeColorImage( Color("#600") ) );
466 
467  MontageFramed montageOpts;
468  montageOpts.geometry( "130x194+10+5>" );
469  montageOpts.gravity( CenterGravity );
470  montageOpts.borderColor( "green" );
471  montageOpts.borderWidth( 1 );
472  montageOpts.tile( "7x4" );
473  montageOpts.backgroundColor( "#ffffff" );
474  montageOpts.pointSize( 18 );
475  montageOpts.fillColor( "#600" );
476  montageOpts.strokeColor( Color() );
477  montageOpts.fileName( "Magick++ Demo" );
478  montageImages( &montage, images.begin(), images.end(), montageOpts );
479  }
480 
481  Image& montage_image = montage.front();
482  {
483  // Create logo image
484  cout << "Adding logo image ..." << endl;
485  Image logo( "logo:" );
486  logo.zoom( "45%" );
487 
488  // Composite logo into montage image
489  Geometry placement(0,0,(montage_image.columns()/2)-(logo.columns()/2),0);
490  montage_image.composite( logo, placement, OverCompositeOp );
491  }
492 
493  for_each( montage.begin(), montage.end(), depthImage(8) );
494  for_each( montage.begin(), montage.end(), alphaImage( false ) );
495  for_each( montage.begin(), montage.end(), compressTypeImage( RLECompression) );
496 
497  cout << "Writing image \"demo_out.miff\" ..." << endl;
498  writeImages(montage.begin(),montage.end(),"demo_out_%d.miff");
499 
500  // Uncomment following lines to display image to screen
501  // cout << "Display image..." << endl;
502  // montage_image.display();
503 
504  }
505  catch( exception &error_ )
506  {
507  cout << "Caught exception: " << error_.what() << endl;
508  return 1;
509  }
510 
511  return 0;
512 }
void gaussianBlur(const double radius_, const double sigma_)
Definition: Image.cpp:3231
void swirl(const double degrees_)
Definition: Image.cpp:4587
class MagickPPExport Color
Definition: Color.h:16
void modulate(const double brightness_, const double saturation_, const double hue_)
Definition: Image.cpp:3580
void annotate(const std::string &text_, const Geometry &location_)
Definition: Image.cpp:1839
void charcoal(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2181
void blur(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2088
void contrast(const bool sharpen_)
Definition: Image.cpp:2578
void medianFilter(const double radius_=0.0)
Definition: Image.cpp:3557
void composite(const Image &compositeImage_, const Geometry &offset_, const CompositeOperator compose_=InCompositeOp)
Definition: Image.cpp:2516
void unsharpmask(const double radius_, const double sigma_, const double amount_, const double threshold_)
Definition: Image.cpp:4775
void negate(const bool grayscale_=false)
Definition: Image.cpp:3709
void raise(const Geometry &geometry_=raiseGeometryDefault, const bool raisedFlag_=false)
Definition: Image.cpp:3972
void crop(const Geometry &geometry_)
Definition: Image.cpp:2648
void density(const Point &density_)
Definition: Image.cpp:652
void shade(const double azimuth_=30, const double elevation_=30, const bool colorShading_=false)
Definition: Image.cpp:4363
STL namespace.
void equalize(void)
Definition: Image.cpp:2869
void zoom(const Geometry &geometry_)
Definition: Image.cpp:4955
void strokeWidth(const double strokeWidth_)
Definition: Image.cpp:1488
void blurChannel(const ChannelType channel_, const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2099
void strokeColor(const Color &strokeColor_)
Definition: Image.cpp:1384
void oilPaint(const double radius_=0.0, const double sigma=1.0)
Definition: Image.cpp:3736
void spread(const double amount_=3.0)
Definition: Image.cpp:4519
void randomThresholdChannel(const ChannelType channel_, const double low_, const double high_)
Definition: Image.cpp:3991
void motionBlur(const double radius_, const double sigma_, const double angle_)
Definition: Image.cpp:3697
void emboss(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:2839
void segment(const double clusterThreshold_=1.0, const double smoothingThreshold_=1.5)
Definition: Image.cpp:4269
void reduceNoise(void)
Definition: Image.cpp:4102
void sharpen(const double radius_=0.0, const double sigma_=1.0)
Definition: Image.cpp:4389
void montageImages(Container *montageImages_, InputIterator first_, InputIterator last_, const Montage &options_)
Definition: STL.h:2463
void solarize(const double factor_=50.0)
Definition: Image.cpp:4466
void geometry(const Geometry &geometry_)
Definition: Montage.cpp:80
void quantize(const bool measureError_=false)
Definition: Image.cpp:3958
void read(const Blob &blob_)
Definition: Image.cpp:4002
void frame(const Geometry &geometry_=frameGeometryDefault)
Definition: Image.cpp:3140
void flip(void)
Definition: Image.cpp:2978
void shave(const Geometry &geometry_)
Definition: Image.cpp:4414
void borderColor(const Color &color_)
Definition: Image.cpp:428
void fontPointsize(const double pointSize_)
Definition: Image.cpp:866
class MagickPPExport Geometry
Definition: Geometry.h:19
void writeImages(InputIterator first_, InputIterator last_, const std::string &imageSpec_, bool adjoin_=true)
Definition: STL.h:2746
void edge(const double radius_=0.0)
Definition: Image.cpp:2828
void fillColor(const Color &fillColor_)
Definition: Image.cpp:778
void scale(const Geometry &geometry_)
Definition: Image.cpp:4247
void size(const Geometry &geometry_)
Definition: Image.cpp:1360
void draw(const Drawable &drawable_)
Definition: Image.cpp:2779
void randomThreshold(const double low_, const double high_)
Definition: Image.cpp:3984
void label(const std::string &label_)
Definition: Image.cpp:1089
void evaluate(const ChannelType channel_, const MagickEvaluateOperator operator_, double rvalue_)
Definition: Image.cpp:2885
void implode(const double factor_)
Definition: Image.cpp:3350
void addNoiseChannel(const ChannelType channel_, const NoiseType noiseType_, const double attenuate_=1.0)
Definition: Image.cpp:1788
void wave(const double amplitude_=25.0, const double wavelength_=150.0)
Definition: Image.cpp:4816
double gamma(void) const
Definition: Image.cpp:939
void threshold(const double threshold_)
Definition: Image.cpp:4614
void convolve(const size_t order_, const double *kernel_)
Definition: Image.cpp:2606
void roll(const Geometry &roll_)
Definition: Image.cpp:4166
void gaussianBlurChannel(const ChannelType channel_, const double radius_, const double sigma_)
Definition: Image.cpp:3242
void transparent(const Color &color_, const bool inverse_=false)
Definition: Image.cpp:4677
void alpha(const bool alphaFlag_)
Definition: Image.cpp:305
void border(const Geometry &geometry_=borderGeometryDefault)
Definition: Image.cpp:2113
void addNoise(const NoiseType noiseType_, const double attenuate_=1.0)
Definition: Image.cpp:1777
size_t columns(void) const
Definition: Image.cpp:589
void quantizeColors(const size_t colors_)
Definition: Image.cpp:1252
Definition: Blob.h:15
void flop(void)
Definition: Image.cpp:3101
void levelChannel(const ChannelType channel_, const double blackPoint_, const double whitePoint_, const double gamma_=1.0)
Definition: Image.cpp:3414
void despeckle(void)
Definition: Image.cpp:2748
void channel(const ChannelType channel_)
Definition: Image.cpp:2170
MagickPPExport void InitializeMagick(const char *path_)
Definition: Functions.cpp:43
void normalize(void)
Definition: Image.cpp:3728
int main(int, char **argv)
Definition: demo.cpp:22
void backgroundColor(const Color &color_)
Definition: Image.cpp:375
void quantizeDither(const bool ditherFlag_)
Definition: Image.cpp:1275
void level(const double blackPoint_, const double whitePoint_, const double gamma_=1.0)
Definition: Image.cpp:3405
void quantizeColorSpace(const ColorspaceType colorSpace_)
Definition: Image.cpp:1263
void shear(const double xShearAngle_, const double yShearAngle_)
Definition: Image.cpp:4428
void rotate(const double degrees_)
Definition: Image.cpp:4189