blob: 4a1558721910ad0a749975ed3d0fb70b3717248e [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001// 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//
cristye86f2f32014-01-08 17:30:27 +00007// Concept and algorithms lifted from PerlMagick demo script written
8// by John Christy.
9//
cristy3ed852e2009-09-05 21:47:34 +000010// 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
18using namespace std;
19
20using namespace Magick;
21
22int 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
cristy3ed852e2009-09-05 21:47:34 +000034 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 );
cristy3ed852e2009-09-05 21:47:34 +000085 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
cristye86f2f32014-01-08 17:30:27 +0000251 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
cristy3ed852e2009-09-05 21:47:34 +0000257 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)" );
dirke03b8c42016-02-01 23:23:43 +0100305 example.randomThreshold(2,2);
cristy3ed852e2009-09-05 21:47:34 +0000306 images.push_back( example );
307
308 cout << " ordered dither 3x3..." << endl;
309 example = model;
310 example.label( "Ordered Dither\n(3x3)" );
dirke03b8c42016-02-01 23:23:43 +0100311 example.randomThreshold(3,3);
cristy3ed852e2009-09-05 21:47:34 +0000312 images.push_back( example );
313
314 cout << " ordered dither 4x4..." << endl;
315 example = model;
316 example.label( "Ordered Dither\n(4x4)" );
dirke03b8c42016-02-01 23:23:43 +0100317 example.randomThreshold(4,4);
cristy3ed852e2009-09-05 21:47:34 +0000318 images.push_back( example );
319
320 cout << " ordered dither red 4x4..." << endl;
321 example = model;
322 example.label( "Ordered Dither\n(Red 4x4)" );
dirke03b8c42016-02-01 23:23:43 +0100323 example.randomThresholdChannel(RedChannel,4,4);
cristy3ed852e2009-09-05 21:47:34 +0000324 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" );
dirk17435e62015-08-31 12:50:49 +0200342 example.evaluate( RedChannel,MultiplyEvaluateOperator,0.40 );
cristy3ed852e2009-09-05 21:47:34 +0000343 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" );
dirka39b01d2016-02-01 23:26:00 +0100441 example.randomThreshold( (0.3*QuantumRange),
442 (0.85*QuantumRange) );
cristy3ed852e2009-09-05 21:47:34 +0000443 images.push_back( example );
444
445 cout << " unsharp mask ..." << endl;
446 example = model;
447 example.label( "Unsharp Mask" );
cristy3afd4012013-03-25 11:30:44 +0000448 // radius_, sigma_, amount_, threshold_
449 example.unsharpmask( 0.0, 1.0, 1.0, 0.05);
cristy3ed852e2009-09-05 21:47:34 +0000450 images.push_back( example );
451
452 cout << " wave ..." << endl;
453 example = model;
454 example.label( "Wave" );
cristy694da972014-01-10 12:51:58 +0000455 example.alpha( true );
cristy3ed852e2009-09-05 21:47:34 +0000456 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
cristy3ed852e2009-09-05 21:47:34 +0000465 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" );
cristy3ed852e2009-09-05 21:47:34 +0000473 montageOpts.backgroundColor( "#ffffff" );
cristy3ed852e2009-09-05 21:47:34 +0000474 montageOpts.pointSize( 18 );
475 montageOpts.fillColor( "#600" );
476 montageOpts.strokeColor( Color() );
cristy3ed852e2009-09-05 21:47:34 +0000477 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
cristy4a16cd52010-01-05 14:04:01 +0000493 for_each( montage.begin(), montage.end(), depthImage(8) );
cristy694da972014-01-10 12:51:58 +0000494 for_each( montage.begin(), montage.end(), alphaImage( false ) );
cristy4a16cd52010-01-05 14:04:01 +0000495 for_each( montage.begin(), montage.end(), compressTypeImage( RLECompression) );
496
cristy3ed852e2009-09-05 21:47:34 +0000497 cout << "Writing image \"demo_out.miff\" ..." << endl;
cristy508d9312010-02-10 21:10:30 +0000498 writeImages(montage.begin(),montage.end(),"demo_out_%d.miff");
cristy3ed852e2009-09-05 21:47:34 +0000499
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}