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