blob: ec6007f9f6936806766d5069017d5e0aceeaf0e0 [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, 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
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::list<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::list<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 drawList.push_back(DrawableStrokeColor("red"));
84 drawList.push_back(DrawableStrokeWidth(1));
85 drawList.push_back(DrawableLine(10,200, 20,190));
86
87 //
88 // Draw arc within a circle.
89 //
90 drawList.push_back(DrawableStrokeColor("black"));
91 drawList.push_back(DrawableFillColor("yellow"));
92 drawList.push_back(DrawableStrokeWidth(4));
93 drawList.push_back(DrawableCircle(160,70, 200,70));
94
95 drawList.push_back(DrawableStrokeColor("black"));
96 drawList.push_back(DrawableFillColor("blue"));
97 drawList.push_back(DrawableStrokeWidth(4));
98 {
99 std::list<VPath> path;
100 path.push_back(PathMovetoAbs(Coordinate(160,70)));
101 path.push_back(PathLinetoVerticalRel(-40));
102 path.push_back(PathArcRel(PathArcArgs(40,40, 0, 0, 0, -40,40)));
103 path.push_back(PathClosePath());
104 drawList.push_back(DrawablePath(path));
105 }
106
107 //
108 // Draw pentogram.
109 //
110 {
111 drawList.push_back(DrawableStrokeColor("red"));
112 drawList.push_back(DrawableFillColor("LimeGreen"));
113 drawList.push_back(DrawableStrokeWidth(3));
114
115 std::list<Magick::Coordinate> points;
116 points.push_back(Coordinate(160,120));
117 points.push_back(Coordinate(130,190));
118 points.push_back(Coordinate(210,145));
119 points.push_back(Coordinate(110,145));
120 points.push_back(Coordinate(190,190));
121 points.push_back(Coordinate(160,120));
122 drawList.push_back(DrawablePolygon(points));
123 }
124
125 //
126 // Draw rectangle.
127 //
128 drawList.push_back(DrawableStrokeWidth(5));
129 drawList.push_back(DrawableFillColor(Color())); // No fill
130 drawList.push_back(DrawableStrokeColor("yellow"));
131 drawList.push_back(DrawableLine(200,260, 200,200));
132 drawList.push_back(DrawableLine(200,200, 260,200));
133 drawList.push_back(DrawableStrokeColor("red"));
134 drawList.push_back(DrawableLine(260,200, 260,260));
135 drawList.push_back(DrawableStrokeColor("green"));
136 drawList.push_back(DrawableLine(200,260, 260,260));
137
138 //
139 // Draw text.
140 //
141 drawList.push_back(DrawableFillColor("green"));
142 drawList.push_back(DrawableStrokeColor(Color())); // unset color
143 drawList.push_back(DrawablePointSize(24));
144 drawList.push_back(DrawableTranslation(30,140));
145 drawList.push_back(DrawableRotation(45.0));
146 drawList.push_back(DrawableText(0,0,"This is a test!"));
147
148 // Finish drawing by popping back to base context.
149 drawList.push_back(DrawablePopGraphicContext());
150
151 // Draw everything using completed drawing list
152 // image.debug(true);
153 image.draw(drawList);
154
155 // image.write( "piddle.mvg" );
156
157 cout << "Writing image \"piddle_out.miff\" ..." << endl;
cristy4a16cd52010-01-05 14:04:01 +0000158 image.depth( 8 );
cristy3ed852e2009-09-05 21:47:34 +0000159 image.compressType( RLECompression );
160 image.write( "piddle_out.miff" );
161 cout << "Writing MVG metafile \"piddle_out.mvg\" ..." << endl;
162 image.write( "piddle_out.mvg" );
163
164 // cout << "Display image..." << endl;
165 // image.display( );
166
167 }
168 catch( exception &error_ )
169 {
170 cout << "Caught exception: " << error_.what() << endl;
171 return 1;
172 }
173
174 return 0;
175}