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