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