1// This may look like C code, but it is really -*- C++ -*-
2//
3// Copyright Bob Friesenhahn, 1999, 2000, 2002, 2003
4//
5// PerlMagick "piddle" demo re-implemented using Magick++ methods.
6// The PerlMagick "piddle" demo is written by Cristy
7//
8
9#include <Magick++.h>
10#include <string>
11#include <iostream>
12
13using namespace std;
14
15using namespace Magick;
16
17int main( int /*argc*/, char ** argv)
18{
19
20  // Initialize ImageMagick install location for Windows
21  InitializeMagick(*argv);
22
23  try {
24
25    string srcdir("");
26    if(getenv("SRCDIR") != 0)
27      srcdir = getenv("SRCDIR");
28
29    //
30    // Create a 300x300 white canvas.
31    //
32    Image image( "300x300", "white" );
33
34    // Drawing list
35    std::vector<Magick::Drawable> drawList;
36
37    // Start drawing by pushing a drawing context with specified
38    // viewbox size
39    drawList.push_back(DrawablePushGraphicContext());
40    drawList.push_back(DrawableViewbox(0,0,image.columns(),image.rows()));
41
42    //
43    // Draw blue grid
44    //
45    drawList.push_back(DrawableStrokeColor("#ccf"));
46    for ( int i=0; i < 300; i += 10 )
47      {
48        drawList.push_back(DrawableLine(i,0, i,300));
49        drawList.push_back(DrawableLine(0,i, 300,i));
50      }
51
52    //
53    // Draw rounded rectangle.
54    //
55    drawList.push_back(DrawableFillColor("blue"));
56    drawList.push_back(DrawableStrokeColor("red"));
57    drawList.push_back(DrawableRoundRectangle(15,15, 70,70, 10,10));
58
59    drawList.push_back(DrawableFillColor("blue"));
60    drawList.push_back(DrawableStrokeColor("maroon"));
61    drawList.push_back(DrawableStrokeWidth(4));
62    drawList.push_back(DrawableRoundRectangle(15,15, 70,70, 10,10));
63
64    //
65    // Draw curve.
66    //
67    {
68      drawList.push_back(DrawableStrokeColor("black"));
69      drawList.push_back(DrawableStrokeWidth(4));
70      drawList.push_back(DrawableFillColor(Color()));
71
72      std::vector<Magick::Coordinate> points;
73      points.push_back(Coordinate(20,20));
74      points.push_back(Coordinate(100,50));
75      points.push_back(Coordinate(50,100));
76      points.push_back(Coordinate(160,160));
77      drawList.push_back(DrawableBezier(points));
78    }
79
80    //
81    // Draw line
82    //
83    {
84      const double dash_array[] = {4.0, 3.0, 0.0};
85      drawList.push_back(DrawableStrokeDashArray(dash_array));
86      drawList.push_back(DrawableStrokeColor("red"));
87      drawList.push_back(DrawableStrokeWidth(1));
88      drawList.push_back(DrawableLine(10,200, 54,182));
89      drawList.push_back(DrawableStrokeDashArray((double *) 0));
90    }
91
92    //
93    // Draw arc within a circle.
94    //
95    drawList.push_back(DrawableStrokeColor("black"));
96    drawList.push_back(DrawableFillColor("yellow"));
97    drawList.push_back(DrawableStrokeWidth(4));
98    drawList.push_back(DrawableCircle(160,70, 200,70));
99
100    drawList.push_back(DrawableStrokeColor("black"));
101    drawList.push_back(DrawableFillColor("blue"));
102    drawList.push_back(DrawableStrokeWidth(4));
103    {
104      std::vector<VPath> path;
105      path.push_back(PathMovetoAbs(Coordinate(160,70)));
106      path.push_back(PathLinetoVerticalRel(-40));
107      path.push_back(PathArcRel(PathArcArgs(40,40, 0, 0, 0, -40,40)));
108      path.push_back(PathClosePath());
109      drawList.push_back(DrawablePath(path));
110    }
111
112    //
113    // Draw pentogram.
114    //
115    {
116      drawList.push_back(DrawableStrokeColor("red"));
117      drawList.push_back(DrawableFillColor("LimeGreen"));
118      drawList.push_back(DrawableStrokeWidth(3));
119
120      std::vector<Magick::Coordinate> points;
121      points.push_back(Coordinate(160,120));
122      points.push_back(Coordinate(130,190));
123      points.push_back(Coordinate(210,145));
124      points.push_back(Coordinate(110,145));
125      points.push_back(Coordinate(190,190));
126      points.push_back(Coordinate(160,120));
127      drawList.push_back(DrawablePolygon(points));
128    }
129
130    //
131    // Draw rectangle.
132    //
133    drawList.push_back(DrawableStrokeWidth(5));
134    drawList.push_back(DrawableFillColor(Color())); // No fill
135    drawList.push_back(DrawableStrokeColor("yellow"));
136    drawList.push_back(DrawableLine(200,260, 200,200));
137    drawList.push_back(DrawableLine(200,200, 260,200));
138    drawList.push_back(DrawableStrokeColor("red"));
139    drawList.push_back(DrawableLine(260,200, 260,260));
140    drawList.push_back(DrawableStrokeColor("green"));
141    drawList.push_back(DrawableLine(200,260, 260,260));
142
143    //
144    // Draw text.
145    //
146    drawList.push_back(DrawableFillColor("green"));
147    drawList.push_back(DrawableStrokeColor(Color())); // unset color
148    drawList.push_back(DrawablePointSize(24));
149    drawList.push_back(DrawableTranslation(30,140));
150    drawList.push_back(DrawableRotation(45.0));
151    drawList.push_back(DrawableText(0,0,"This is a test!"));
152
153    // Finish drawing by popping back to base context.
154    drawList.push_back(DrawablePopGraphicContext());
155
156    // Draw everything using completed drawing list
157    //    image.debug(true);
158    image.draw(drawList);
159
160    //     image.write( "piddle.mvg" );
161
162    cout << "Writing image \"piddle_out.miff\" ..." << endl;
163    image.depth( 8 );
164    image.compressType( RLECompression );
165    image.write( "piddle_out.miff" );
166    cout << "Writing MVG metafile \"piddle_out.mvg\" ..." << endl;
167    image.write( "piddle_out.mvg" );
168
169    //     cout << "Display image..." << endl;
170    //     image.display( );
171
172  }
173  catch( exception &error_ )
174    {
175      cout << "Caught exception: " << error_.what() << endl;
176      return 1;
177    }
178
179  return 0;
180}
181