The following is an example of using the Drawable subclasses with the one-by-one approach to draw the following figure:
#include <string>
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int /*argc*/,char **/*argv*/)
{
try {
// Create base image (white
image of 300 by 200 pixels)
Image image( Geometry(300,200),
Color("white") );
// Set draw options
image.strokeColor("red");
// Outline color
image.fillColor("green");
// Fill color
image.strokeWidth(5);
// Draw a circle
image.draw( DrawableCircle(100,100,
50,100) );
// Draw a rectangle
image.draw( DrawableRectangle(200,200,
270,170) );
// Display the result
image.display( );
}
catch( exception &error_ )
{
cout <<
"Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
Since Drawable is an object it may be saved in an array or a list for later (perhaps repeated) use. The following example shows how to draw the same figure using the list-based approach
#include <string>
#include <iostream>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int /*argc*/,char **/*argv*/)
{
try {
// Create base image (white
image of 300 by 200 pixels)
Image image( Geometry(300,200),
Color("white") );
// Construct drawing list
std::list<Magick::Drawable>
drawList;
// Add some drawing options
to drawing list
drawList.push_back(DrawableStrokeColor("red"));
// Outline color
drawList.push_back(DrawableStrokeWidth(5));
// Stroke width
drawList.push_back(DrawableFillColor("green"));
// Fill color
// Add a Circle to drawing
list
drawList.push_back(DrawableCircle(100,100,
50,100));
// Add a Rectangle to drawing
list
drawList.push_back(DrawableRectangle(200,100,
270,170));
// Draw everything using
completed drawing list
image.draw(drawList);
// Display the result
image.display( );
}
catch( exception &error_ )
{
cout <<
"Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
Drawable depends on the simple Coordinate structure
which represents a pair of x,y coodinates. The methods provided by
the Coordinate structure are shown in the following table:
|
|
|
|
void | Default Constructor |
double x_, double y_ | Constructor, setting first & second | |
|
double x_ | x coordinate member |
|
double y_ | y coordinate member |
The Drawable classes are shown in the following
table. Only constructor signatures are documented here. Each Drawable class
also provides methods by which each individual parameter may be adjusted.
|
|
|
|
double sx_, double sy_, double rx_, double ry_, double tx_, double ty_ | Specify a transformation matrix to adjust scaling, rotation, and translation (coordinate transformation) for subsequently drawn objects in the same or decendent drawing context. The sx_ & sy_ parameters represent the x & y scale factors, the rx_ & ry_ parameters represent the x & y rotation, and the tx_ & ty_ parameters represent the x & y translation. |
void | Specify a transformation matrix to adjust scaling, rotation, and translation (coordinate transformation) for subsequently drawn objects in the same or decendent drawing context. Initialized to unity (no effect) affine values. Use class methods (not currently documented) to adjust individual parameters from their unity values. | |
|
double angle_ | Set drawing angle |
|
double startX_, double startY_, double endX_, double endY_, double startDegrees, double endDegrees_ | Draw an arc using the stroke color and based on the circle starting at coordinates startX_,startY_, and ending with coordinates endX_,endY_, and bounded by the rotational arc startDegrees_,endDegrees_ |
|
const std::list<Magick::Coordinate> &coordinates_ | Draw a bezier curve using the stroke color and based on the coordinates specified by the coordinates_ list. |
|
double originX_, double originY_, double perimX_, double perimY_ | Draw a circle using the stroke color and thickness using specified origin and perimeter coordinates. If a fill color is specified, then the object is filled. |
|
double x_, double y_, PaintMethod paintMethod_ | Color image according to paintMethod. The point method recolors the target pixel. The replace method recolors any pixel that matches the color of the target pixel. Floodfill recolors any pixel that matches the color of the target pixel and is a neighbor, whereas filltoborder recolors any neighbor pixel that is not the border color. Finally, reset recolors all pixels. |
|
double x_, double y_, const std::string &filename_ | Composite current image with contents of specified image, at specified coordinates. If the matte attribute is set to true, then the image composition will consider an alpha channel, or transparency, present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. |
double x_, double y_, const Image &image_ | ||
double x_, double y_, double width_, double height_, const std::string &filename_ | Composite current image with contents of specified image, rendered with specified width and height, at specified coordinates. If the matte attribute is set to true, then the image composition will consider an alpha channel, or transparency, present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. If the specified width or height is zero, then the image is composited at its natural size, without enlargement or reduction. | |
double x_, double y_, double width_, double height_, const Image &image_ | ||
double x_, double y_, double width_, double height_, const std::string &filename_, CompositeOperator composition_ | Composite current image with contents of specified image, rendered with specified width and height, using specified composition algorithm, at specified coordinates. If the matte attribute is set to true, then the image composition will consider an alpha channel, or transparency, present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. If the specified width or height is zero, then the image is composited at its natural size, without enlargement or reduction. | |
double x_, double y_, double width_, double height_, const Image &image_, CompositeOperator composition_ | ||
|
DecorationType decoration_ | Specify decoration to apply to text. |
|
const double* dasharray_ | Specify the pattern of dashes and gaps used to stroke paths. The strokeDashArray represents a zero-terminated array of numbers that specify the lengths of alternating dashes and gaps in pixels. If an odd number of values is provided, then the list of values is repeated to yield an even number of values. A typical strokeDashArray_ array might contain the members 5 3 2 0, where the zero value indicates the end of the pattern array. |
|
double offset_ | Specify the distance into the dash pattern to start the dash. See documentation on SVG's stroke-dashoffset property for usage details. |
|
double originX_, double originY_, double radiusX_, double radiusY_, double arcStart_, double arcEnd_ | Draw an ellipse using the stroke color and thickness, specified origin, x & y radius, as well as specified start and end of arc in degrees. If a fill color is specified, then the object is filled. |
|
const Color &color_ | Specify drawing object fill color. |
|
FillRule fillRule_ | Specify the algorithm which is to be used to determine what parts of the canvas are included inside the shape. See documentation on SVG's fill-rule property for usage details. |
|
double opacity_ | Specify opacity to use when drawing using fill color. |
|
const std::string &font_ | Specify font name to use when drawing text. |
const std::string &family_,
StyleType style_, unsigned long weight_, StretchType stretch_ |
Specify font family, style, weight (one of the set { 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 } with 400 being the normal size), and stretch to be used to select the font used when drawing text. Wildcard matches may be applied to style via the AnyStyle enumeration, applied to weight if weight is zero, and applied to stretch via the AnyStretch enumeration. | |
|
GravityType gravity_ | Specify text positioning gravity. |
|
double startX_, double startY_, double endX_, double endY_ | Draw a line using stroke color and thickness using starting and ending coordinates |
|
double x_, double y_, PaintMethod paintMethod_ | Change the pixel matte value to transparent. The point method changes the matte value of the target pixel. The replace method changes the matte value of any pixel that matches the color of the target pixel. Floodfill changes the matte value of any pixel that matches the color of the target pixel and is a neighbor, whereas filltoborder changes the matte value of any neighbor pixel that is not the border color, Finally reset changes the matte value of all pixels. |
|
unsigned int miterLimit_ | Specify miter limit. When two line segments meet at a sharp angle and miter joins have been specified for 'lineJoin', it is possible for the miter to extend far beyond the thickness of the line stroking the path. The miterLimit' imposes a limit on the ratio of the miter length to the 'lineWidth'. The default value of this parameter is 4. |
|
const std::list<Magick::VPath> &path_ | Draw on image using vector path. |
|
double x_, double y_ | Draw a point using stroke color and thickness at coordinate |
|
double pointSize_ | Set font point size. |
|
const std::list<Magick::Coordinate> &coordinates_ | Draw an arbitrary polygon using stroke color and thickness consisting of three or more coordinates contained in an STL list. If a fill color is specified, then the object is filled. |
|
const std::list<Magick::Coordinate> &coordinates_ | Draw an arbitrary polyline using stroke color and thickness consisting of three or more coordinates contained in an STL list. If a fill color is specified, then the object is filled. |
|
void | Pop Graphic Context. Removing the current graphic context from the graphic context stack restores the options to the values they had prior to the preceding DrawablePushGraphicContext operation. |
|
void | Push Graphic Context. When a graphic context is pushed, options set after the context is pushed (such as coordinate transformations, color settings, etc.) are saved to a new graphic context. This allows related options to be saved on a graphic context "stack" in order to support heirarchical nesting of options. When DrawablePopGraphicContext is used to pop the current graphic context, the options in effect during the last DrawablePushGraphicContext operation are restored. |
|
std::string &id_, long x_, long y_, long width_, long height_ | Start a pattern definition with arbitrary pattern name specified by id_, pattern offset specified by x_ and y_, and pattern size specified by width_ and height_. The pattern is defined within the coordinate system defined by the specified offset and size. Arbitrary drawing objects (including DrawableCompositeImage) may be specified between DrawablePushPattern and DrawablePopPattern in order to draw the pattern. Normally the pair DrawablePushGraphicContext & DrawablePopGraphicContext are used to enclose a pattern definition. Pattern definitions are terminated by a DrawablePopPattern object. |
|
void | Terminate a pattern definition started via DrawablePushPattern. |
|
double upperLeftX_, double upperLeftY_, double lowerRightX_, double lowerRightY | Draw a rectangle using stroke color and thickness from upper-left coordinates to lower-right coordinates. If a fill color is specified, then the object is filled. |
|
double angle_ | Set rotation to use when drawing (coordinate transformation). |
|
double centerX_, double centerY_, double width_, double hight_, double cornerWidth_, double cornerHeight_ | Draw a rounded rectangle using stroke color and thickness, with specified center coordinate, specified width and height, and specified corner width and height. If a fill color is specified, then the object is filled. |
|
double x_, double y_ | Apply scaling in x and y direction while drawing objects (coordinate transformation). |
|
double angle_ | Apply Skew in X direction (coordinate transformation) |
|
double angle_ | Apply Skew in Y direction |
|
bool flag_ | Antialias while drawing lines or object outlines. |
|
const Color &color_ | Set color to use when drawing lines or object outlines. |
|
LineCap linecap_ | Specify the shape to be used at the end of open subpaths when they are stroked. Values of LineCap are UndefinedCap, ButtCap, RoundCap, and SquareCap. |
|
LineJoin linejoin_ | Specify the shape to be used at the corners of paths (or other vector shapes) when they are stroked. Values of LineJoin are UndefinedJoin, MiterJoin, RoundJoin, and BevelJoin. |
|
double opacity_ | Opacity to use when drawing lines or object outlines. |
|
double width_ | Set width to use when drawing lines or object outlines. |
|
double x_, double y_, std::string text_ | Annotate image with text using stroke color, font, font pointsize, and box color (text background color), at specified coordinates. If text contains special format characters the image filename, type, width, height, or other image attributes may be incorporated in the text (see label()). |
|
bool flag_ | Antialias while drawing text (default true). The main reason to disable text antialiasing is to avoid adding new colors to the image. |
|
const Color &color_ | Draw a box under rendered text using the specified color. |
|
double x_, double y_ | Apply coordinate translation (set new coordinate origin). |
|
unsigned long x1_, unsigned long y1_, unsigned long x2_, unsigned long y2_ | Dimensions of the output viewbox. If the image is to be written to a vector format (e.g. MVG or SVG), then a DrawablePushGraphicContext() object should be pushed to the head of the list, followed by a DrawableViewbox() statement to establish the output canvas size. A matching DrawablePopGraphicContext() object should be pushed to the tail of the list. |
PathCurvetoArgs( double x1_, double y1_,
double x2_, double y2_,
double x_, double y_ );
The commands are as follows:
PathQuadraticCurvetoArgs( double x1_, double y1_,
double x_, double y_ );
The quadratic Bézier commands are as follows:
PathArcArgs( double radiusX_, double radiusY_,
double xAxisRotation_, bool largeArcFlag_,
bool sweepFlag_, double x_, double y_ );
The elliptical arc commands are as follows: