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 <string> 11 #include <iostream> 12 13 using namespace std; 14 15 using namespace Magick; 16 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 // 30 // Create a 300x300 white canvas. 31 // 32 Image image( "300x300", "white" ); 33 34 // Drawing list 35 std::vector<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::vector<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 { 84 const double dash_array[] = {4.0, 3.0, 0.0}; 85 drawList.push_back(DrawableStrokeDashArray(dash_array)); 86 drawList.push_back(DrawableStrokeColor("red")); 87 drawList.push_back(DrawableStrokeWidth(1)); 88 drawList.push_back(DrawableLine(10,200, 54,182)); 89 drawList.push_back(DrawableStrokeDashArray((double *) 0)); 90 } 91 92 // 93 // Draw arc within a circle. 94 // 95 drawList.push_back(DrawableStrokeColor("black")); 96 drawList.push_back(DrawableFillColor("yellow")); 97 drawList.push_back(DrawableStrokeWidth(4)); 98 drawList.push_back(DrawableCircle(160,70, 200,70)); 99 100 drawList.push_back(DrawableStrokeColor("black")); 101 drawList.push_back(DrawableFillColor("blue")); 102 drawList.push_back(DrawableStrokeWidth(4)); 103 { 104 std::vector<VPath> path; 105 path.push_back(PathMovetoAbs(Coordinate(160,70))); 106 path.push_back(PathLinetoVerticalRel(-40)); 107 path.push_back(PathArcRel(PathArcArgs(40,40, 0, 0, 0, -40,40))); 108 path.push_back(PathClosePath()); 109 drawList.push_back(DrawablePath(path)); 110 } 111 112 // 113 // Draw pentogram. 114 // 115 { 116 drawList.push_back(DrawableStrokeColor("red")); 117 drawList.push_back(DrawableFillColor("LimeGreen")); 118 drawList.push_back(DrawableStrokeWidth(3)); 119 120 std::vector<Magick::Coordinate> points; 121 points.push_back(Coordinate(160,120)); 122 points.push_back(Coordinate(130,190)); 123 points.push_back(Coordinate(210,145)); 124 points.push_back(Coordinate(110,145)); 125 points.push_back(Coordinate(190,190)); 126 points.push_back(Coordinate(160,120)); 127 drawList.push_back(DrawablePolygon(points)); 128 } 129 130 // 131 // Draw rectangle. 132 // 133 drawList.push_back(DrawableStrokeWidth(5)); 134 drawList.push_back(DrawableFillColor(Color())); // No fill 135 drawList.push_back(DrawableStrokeColor("yellow")); 136 drawList.push_back(DrawableLine(200,260, 200,200)); 137 drawList.push_back(DrawableLine(200,200, 260,200)); 138 drawList.push_back(DrawableStrokeColor("red")); 139 drawList.push_back(DrawableLine(260,200, 260,260)); 140 drawList.push_back(DrawableStrokeColor("green")); 141 drawList.push_back(DrawableLine(200,260, 260,260)); 142 143 // 144 // Draw text. 145 // 146 if (getenv("MAGICK_FONT") != 0) 147 drawList.push_back(DrawableFont(string(getenv("MAGICK_FONT")))); 148 drawList.push_back(DrawableFillColor("green")); 149 drawList.push_back(DrawableStrokeColor(Color())); // unset color 150 drawList.push_back(DrawablePointSize(24)); 151 drawList.push_back(DrawableTranslation(30,140)); 152 drawList.push_back(DrawableRotation(45.0)); 153 drawList.push_back(DrawableText(0,0,"This is a test!")); 154 155 // Finish drawing by popping back to base context. 156 drawList.push_back(DrawablePopGraphicContext()); 157 158 // Draw everything using completed drawing list 159 // image.debug(true); 160 image.draw(drawList); 161 162 // image.write( "piddle.mvg" ); 163 164 cout << "Writing image \"piddle_out.miff\" ..." << endl; 165 image.depth( 8 ); 166 image.compressType( RLECompression ); 167 image.write( "piddle_out.miff" ); 168 cout << "Writing MVG metafile \"piddle_out.mvg\" ..." << endl; 169 image.write( "piddle_out.mvg" ); 170 171 // cout << "Display image..." << endl; 172 // image.display( ); 173 174 } 175 catch( exception &error_ ) 176 { 177 cout << "Caught exception: " << error_.what() << endl; 178 return 1; 179 } 180 181 return 0; 182 } 183