| 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 <cstdlib> |
| 14 |
#include <string> |
| 15 |
#include <iostream> |
| 16 |
|
| 17 |
using namespace std; |
| 18 |
|
| 19 |
using namespace Magick; |
| 20 |
|
| 21 |
int main( int /*argc*/, char ** argv) |
| 22 |
{ |
| 23 |
|
| 24 |
// Initialize ImageMagick install location for Windows |
| 25 |
MagickPlusPlusGenesis genesis(*argv); |
| 26 |
|
| 27 |
try { |
| 28 |
|
| 29 |
string srcdir(""); |
| 30 |
if(getenv("SRCDIR") != 0) |
| 31 |
srcdir = getenv("SRCDIR"); |
| 32 |
|
| 33 |
// |
| 34 |
// Create a 300x300 white canvas. |
| 35 |
// |
| 36 |
Image image( "300x300", "white" ); |
| 37 |
|
| 38 |
// Drawing list |
| 39 |
std::vector<Magick::Drawable> drawList; |
| 40 |
|
| 41 |
// Start drawing by pushing a drawing context with specified |
| 42 |
// viewbox size |
| 43 |
drawList.push_back(DrawablePushGraphicContext()); |
| 44 |
drawList.push_back(DrawableViewbox(0,0,(ssize_t) image.columns(), |
| 45 |
(ssize_t) 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::vector<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(DrawableStrokeDashArray(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(DrawableStrokeDashArray((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::vector<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 pentagram. |
| 119 |
// |
| 120 |
{ |
| 121 |
drawList.push_back(DrawableStrokeColor("red")); |
| 122 |
drawList.push_back(DrawableFillColor("LimeGreen")); |
| 123 |
drawList.push_back(DrawableStrokeWidth(3)); |
| 124 |
|
| 125 |
std::vector<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 |
#if defined(MAGICKCORE_FREETYPE_DELEGATE) |
| 152 |
if (getenv("MAGICK_FONT") != 0) |
| 153 |
drawList.push_back(DrawableFont(string(getenv("MAGICK_FONT")))); |
| 154 |
drawList.push_back(DrawableFillColor("green")); |
| 155 |
drawList.push_back(DrawableStrokeColor(Color())); // unset color |
| 156 |
drawList.push_back(DrawablePointSize(24)); |
| 157 |
drawList.push_back(DrawableTranslation(30,140)); |
| 158 |
drawList.push_back(DrawableRotation(45.0)); |
| 159 |
drawList.push_back(DrawableText(0,0,"This is a test!")); |
| 160 |
#endif |
| 161 |
|
| 162 |
// Finish drawing by popping back to base context. |
| 163 |
drawList.push_back(DrawablePopGraphicContext()); |
| 164 |
|
| 165 |
// Draw everything using completed drawing list |
| 166 |
// image.debug(true); |
| 167 |
image.draw(drawList); |
| 168 |
|
| 169 |
// image.write( "piddle.mvg" ); |
| 170 |
|
| 171 |
cout << "Writing image \"piddle_out.miff\" ..." << endl; |
| 172 |
image.depth( 8 ); |
| 173 |
image.compressType( RLECompression ); |
| 174 |
image.write( "piddle_out.miff" ); |
| 175 |
cout << "Writing MVG metafile \"piddle_out.mvg\" ..." << endl; |
| 176 |
image.write( "mvg:piddle_out.mvg" ); |
| 177 |
|
| 178 |
// cout << "Display image..." << endl; |
| 179 |
// image.display( ); |
| 180 |
|
| 181 |
} |
| 182 |
catch( exception &error_ ) |
| 183 |
{ |
| 184 |
cout << "Caught exception: " << error_.what() << endl; |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |