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