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