Home | History | Annotate | Download | only in demo
      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(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(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(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(RedChannel,4,4);
    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.evaluate( 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( (0.3*QuantumRange),
    444         (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.alpha( 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.backgroundColor( "#ffffff" );
    476       montageOpts.pointSize( 18 );
    477       montageOpts.fillColor( "#600" );
    478       montageOpts.strokeColor( Color() );
    479       montageOpts.fileName( "Magick++ Demo" );
    480       montageImages( &montage, images.begin(), images.end(), montageOpts );
    481     }
    482 
    483     Image& montage_image = montage.front();
    484     {
    485       // Create logo image
    486       cout << "Adding logo image ..." << endl;
    487       Image logo( "logo:" );
    488       logo.zoom( "45%" );
    489 
    490       // Composite logo into montage image
    491       Geometry placement(0,0,(montage_image.columns()/2)-(logo.columns()/2),0);
    492       montage_image.composite( logo, placement, OverCompositeOp );
    493     }
    494 
    495     for_each( montage.begin(), montage.end(), depthImage(8) );
    496     for_each( montage.begin(), montage.end(), alphaImage( false ) );
    497     for_each( montage.begin(), montage.end(), compressTypeImage( RLECompression) );
    498 
    499     cout << "Writing image \"demo_out.miff\" ..." << endl;
    500     writeImages(montage.begin(),montage.end(),"demo_out_%d.miff");
    501 
    502     // Uncomment following lines to display image to screen
    503     //    cout <<  "Display image..." << endl;
    504     //    montage_image.display();
    505 
    506   }
    507   catch( exception &error_ )
    508     {
    509       cout << "Caught exception: " << error_.what() << endl;
    510       return 1;
    511     }
    512 
    513   return 0;
    514 }
    515