Image is very easy to use. For example, here is a the source to a program which reads an image, crops it, and writes it to a new file (the exception handling is optional):
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
try {
// Create an image object
and read an image
Image image( "girl.gif"
);
// Crop the image to specified
size
image.crop("100x100+100+100"
); // Geometry implicitly initialized by char *
// Write the image to a
file
image.write( "x.gif" );
}
catch( Exception error_ )
{
cout <<
"Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
The following is the source to a program which illustrates the use of Magick++'s efficient reference-counted assignment and copy-constructor operation which minimizes use of memory and eliminates unncessary copy operations. The program accomplishes the following:
During the entire operation, a maximum of three images exists in memory and the image data is never copied.
The following is the source for another simple program which creates a 100 by 100 pixel white image with a red pixel in the center and writes it to a file:
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
Image image( "100x100",
"xc:white" );
image.pixelColor( 49,
49, "red" );
image.write( "red_pixel.png"
);
return 0;
}
If you wanted to change the color image to grayscale, you could simply add the lines:
image.quantizeColorSpace(
GRAYColorspace );
image.quantize( options
);
prior to writing the image.
Image supports access to all the single-image (versus image-list)
manipulation operations provided by the ImageMagick library. These operations
are shown in the following table:
Method | Signature(s) | Description |
|
NoiseType noiseType_ | Add noise to image with specified noise type. |
|
string text_, const Geometry &location_, unsigned int gravity_ = NorthWestGravity | Annotate image (render text on image) at specified location and influenced by gravity. |
const std::string &text_, unsigned int gravity_ = NorthWestGravity | Annotate image (render text on image) at location implied by gravity. | |
|
double factor_ | Blur image with specified blur factor |
|
const Geometry &geometry_ = "6x6+0+0" | Border image (add border to image). The color of the border is specified by the borderColor attribute. |
|
double factor_ = 50 | Charcoal effect image (looks like charcoal sketch) |
|
const Geometry &geometry_ | Chop image (remove vertical or horizontal subregion of image) |
|
const Color &opaqueColor_, const Color &penColor_ | Colorize opaque color in image using pen color |
|
const string &comment_ | Comment image (add comment string to image). By default, each image is commented with its file name. Use this method to assign a specific comment to the image. Optionally you can include the image filename, type, width, height, or other image attributes by embedding special format characters. |
|
const Image &compositeImage_, int xOffset_, int yOffset_, CompositeOperator compose_ = InCompositeOp | Compose an image onto another at specified offset and using specified algorithm |
const Image &compositeImage_, const Geometry &offset_, CompositeOperator compose_ = InCompositeOp | ||
|
void | Condense image (Re-run-length encode image in memory). |
|
unsigned int sharpen_ | Contrast image (enhance intensity differences in image) |
|
const Geometry &geometry_ | Crop image (subregion of original image) |
|
int amount_ | Cycle image colormap |
|
void | Despeckle image (reduce speckle noise) |
|
void | Display image on screen.
Caution: if an image format is is not compatable with the display visual (e.g. JPEG on a colormapped display) then the original image will be altered. Use a copy of the original if this is a problem. |
|
const Drawable &drawable_ | Draw shape or text on image. |
const std::list<Drawable> &drawable_ | Draw shapes or text on image using a set of Drawable objects contained in an STL list. Use of this method improves drawing performance and allows batching draw objects together in a list for repeated use. | |
|
double factor_ | Edge image (hilight edges in image) |
|
void | Emboss image (hilight edges with 3D effect) |
|
void | Enhance image (minimize noise) |
|
void | Equalize image (histogram equalization) |
|
void | Flip image (reflect each scanline in the vertical direction) |
Color |
int x_, int y_, const Color &fillColor_ | Flood-fill color across pixels that match the color of the target pixel and are neighbors of the target pixel. Uses current fuzz setting when determining color match. |
const Geometry &point_, const Color &fillColor_ | ||
int x_, int y_, const Color &fillColor_, const Color &borderColor_ | Flood-fill color across pixels starting at target-pixel and stopping at pixels matching specified border color. Uses current fuzz setting when determining color match. | |
const Geometry &point_, const Color &fillColor_, const Color &borderColor_ | ||
Texture |
int x_, int y_, const Image &texture_ | Flood-fill texture across pixels that match the color of the target pixel and are neighbors of the target pixel. Uses current fuzz setting when determining color match. |
const Geometry &point_, const Image &texture_ | ||
int x_, int y_, const Image &texture_, const Color &borderColor_ | Flood-fill texture across pixels starting at target-pixel and stopping at pixels matching specified border color. Uses current fuzz setting when determining color match. | |
const Geometry &point_, const Image &texture_, const Color &borderColor_ | ||
|
void | Flop image (reflect each scanline in the horizontal direction) |
|
const Geometry &geometry_ = "25x25+6+6" | Add decorative frame around image |
unsigned int width_, unsigned int height_, int x_, int y_, int innerBevel_ = 0, int outerBevel_ = 0 | ||
|
double gamma_ | Gamma correct image (uniform red, green, and blue correction). |
double gammaRed_, double gammaGreen_, double gammaBlue_ | Gamma correct red, green, and blue channels of image. | |
|
double factor_ | Implode image (special effect) |
|
const string &label_ | Assign a label to an image. Use this option to assign a specific label to the image. Optionally you can include the image filename, type, width, height, or scene number in the label by embedding special format characters. If the first character of string is @, the image label is read from a file titled by the remaining characters in the string. When converting to Postscript, use this option to specify a header string to print above the image. |
|
LayerType layer_ | Extract layer from image. Use this option to extract a particular layer from the image. MatteLayer, for example, is useful for extracting the opacity values from an image. |
|
void | Magnify image by integral size |
|
const Image &mapImage_ , bool dither_ = false | Remap image colors with closest color from reference image. Set dither_ to true in to apply Floyd/Steinberg error diffusion to the image. By default, color reduction chooses an optimal set of colors that best represent the original image. Alternatively, you can choose a particular set of colors from an image file with this option. |
|
const Color &target_, unsigned int matte_, int x_, int y_, PaintMethod method_ | Floodfill designated area with a matte value |
|
void | Reduce image by integral size |
|
double brightness_, double saturation_, double hue_ | Modulate percent hue, saturation, and brightness of an image |
|
bool grayscale_ = false | Negate colors in image. Replace every pixel with its complementary color (white becomes black, yellow becomes blue, etc.). Set grayscale to only negate grayscale values in image. |
|
void | Normalize image (increase contrast by normalizing the pixel values to span the full range of color values). |
|
unsigned int radius_ = 3 | Oilpaint image (image looks like oil painting) |
|
const Color &opaqueColor_, const Color &penColor_ | Change color of pixels matching opaqueColor_ to specified penColor_. |
|
bool measureError_ = false | Quantize image (reduce number of colors). Set measureError_ to true in order to calculate error attributes. |
|
const Geometry &geometry_ = "6x6+0+0", bool raisedFlag_ = false | Raise image (lighten or darken the edges of an image to give a 3-D raised or lowered effect) |
|
const string &imageSpec_ | Read image into current object |
const Geometry &size_, const std::string &imageSpec_ | Read image of specified size into current object. This form is useful for images that do not specifiy their size or to specify a size hint for decoding an image. For example, when reading a Photo CD, JBIG, or JPEG image, a size request causes the library to return an image which is the next resolution greater or equal to the specified size. This may result in memory and time savings. | |
|
void | Reduce noise in image using a noise peak elimination filter. |
|
int columns_, int rows_ | Roll image (rolls image vertically and horizontally) by specified number of columnms and rows) |
|
double degrees_, bool crop_ = false, unsigned int sharpen_ = false | Rotate image counter-clockwise by specified number of degrees. Optionally crop image to original size and sharpen image. |
|
const Geometry &geometry_ | Resize image by using pixel sampling algorithm |
|
const Geometry &geometry_ | Resize image by using simple ratio algorithm |
|
double clusterThreshold_ = 1.0,
double smoothingThreshold_ = 1.5 |
Segment (coalesce similar image components) by analyzing the histograms of the color components and identifying units that are homogeneous with the fuzzy c-means technique. Also uses quantizeColorSpace and verbose image attributes. Specify clusterThreshold_, as the number of pixels each cluster must exceed the the cluster threshold to be considered valid. SmoothingThreshold_ eliminates noise in the second derivative of the histogram. As the value is increased, you can expect a smoother second derivative. The default is 1.5. |
|
double azimuth_ = 30, double elevation_ = 30,
bool colorShading_ = false |
Shade image using distant light source. Specify azimuth_ and elevation_ as the position of the light source. By default, the shading results as a grayscale image.. Set colorShading_ to true to shade the red, green, and blue components of the image. |
|
double factor_ | Sharpen pixels in image. Specify factor as the percent enhancement (0.0 - 99.9%). |
|
double xShearAngle_, double yShearAngle_,
bool crop_ = false |
Shear image (create parallelogram by sliding image by X or Y axis). Shearing slides one edge of an image along the X or Y axis, creating a parallelogram. An X direction shear slides an edge along the X axis, while a Y direction shear slides an edge along the Y axis. The amount of the shear is controlled by a shear angle. For X direction shears, x degrees is measured relative to the Y axis, and similarly, for Y direction shears y degrees is measured relative to the X axis. Empty triangles left over from shearing the image are filled with the color defined as borderColor. Specify crop_ as true_ to crop the sheared image to the original size. |
|
double factor_ = 50.0 | Solarize image (similar to effect seen when exposing a photographic film to light during the development process) |
|
unsigned int amount_ = 3 | Spread pixels randomly within image by specified amount |
|
const Image &watermark_ | Add a digital watermark to the image (based on second image) |
|
const Image &rightImage_ | Create an image which appears in stereo when viewed with red-blue glasses (Red image on left, blue on right) |
|
double degrees_ | Swirl image (image pixels are rotated by degrees) |
|
const Image &texture_ | Layer a texture on image background |
|
double threshold_ | Threshold image |
|
const Geometry &imageGeometry_ | Transform image based on image and crop geometries. Crop geometry is optional. |
const Geometry &imageGeometry_, const Geometry &cropGeometry_ | ||
|
const Color &color_ | Add matte image to image, setting pixels matching color to transparent. |
|
void | Trim edges that are the background color from the image. |
|
double amplitude_ = 25.0, double wavelength_ = 150.0 | Alter an image along a sine wave. |
|
const string &imageSpec_ | Write image to a file using filename imageSpec_.
Caution: if an image format is selected which is capable of supporting fewer colors than the original image or quantization has been requested, the original image will be quantized to fewer colors. Use a copy of the original if this is a problem. |
|
const Geometry &geometry_ | Zoom image to specified size. |
The supported image attributes and the method arguments required to
obtain them are shown in the following table:
|
Of |
|
|
|
|
|
Options | bool | void | bool flag_ | Control antialiasing of rendered Postscript and Postscript or TrueType fonts. Enabled by default. |
Delay |
Image &
Options |
unsigned int (0 to 65535) | void | unsigned int delay_ | Time in 1/100ths of a second (0 to 65535) which must expire before displaying the next image in an animated sequence. This option is useful for regulating the animation of a sequence of GIF images within Netscape. |
Iterations |
Image &
Options |
unsigned int | void | unsigned int iterations_ | Number of iterations to loop an animation (e.g. Netscape loop extension) for. |
Color |
Image &
Options |
Color | void | const Color &color_ | Image background color |
Texture |
Options | string | void | const string &texture_ | Image to use as background texture. |
|
Image | unsigned int | void | Base image width (before transformations) | |
|
Image | string | void | Base image filename (before transformations) | |
|
Image | unsigned int | void | Base image height (before transformations) | |
|
Image &
Options |
Color | void | const Color &color_ | Image border color |
|
Options | Color | void | const Color &boxColor_ | Base color that annotation text is rendered on. |
BluePrimary |
Image | float x & y | float *x_, float *y_ | float x_, float y_ | Chromaticity blue primary point (e.g. x=0.15, y=0.06) |
GreenPrimary |
Image | float x & y | float *x_, float *y_ | float x_, float y_ | Chromaticity green primary point (e.g. x=0.3, y=0.6) |
RedPrimary |
Image | float x & y | float *x_, float *y_ | float x_, float y_ | Chromaticity red primary point (e.g. x=0.64, y=0.33) |
WhitePoint |
Image | float x & y | float *x_, float *y_ | float x_, float y_ | Chromaticity white point (e.g. x=0.3127, y=0.329) |
|
Image | ClassType | void | Image class | |
|
Image &
Options |
unsigned int | void | unsigned int fuzz_ | Colors within this distance are considered equal. A number of algorithms search for a target color. By default the color must be exact. Use this option to match colors that are close to the target color in RGB space. |
|
Image | Color | unsigned int index_ | unsigned int index_, const Color &color_ | Color at color-pallet index. |
|
Image | unsigned int | void | Image width | |
|
Image | string | void | Image comment | |
Type |
Image &
Options |
CompressionType | void | CompressionType compressType_ | Image compresion type. The default is the compression type of the specified image file. |
|
Options | Geometry (default 72x72) | void | const Geometry &density_ | Vertical and horizontal resolution in pixels of the image. This option specifies an image density when decoding a Postscript or Portable Document page. Often used with psPageSize. |
|
Image &
Options |
unsigned int (8 or 16) | void | unsigned int depth_ | Image depth. Used to specify the bit depth when reading or writing raw images. Defaults to the quantum depth that ImageMagick is compiled with. |
|
Image | string | void | Tile names from within an image montage | |
|
Image &
Options |
string | void | const string &fileName_ | Image file name. |
|
Image | unsigned int | void | Number of bytes of the image on disk | |
|
Image &
Options |
FilterType | void | FilterType filterType_ | Filter to use when resizing image. The reduction filter employed has a sigificant effect on the time required to resize an image and the resulting quality. The default filter is Mitchell (highest quality). |
|
Options | string | void | const string &font_ | Text rendering font. If the font is a fully qualified X server font name, the font is obtained from an X server. To use a TrueType font, precede the TrueType filename with an @. Otherwise, specify a Postscript font name (e.g. "helvetica"). |
|
Options | unsigned int | void | unsigned int pointSize_ | Text rendering font point size |
|
Image | string | void | Long form image format description. | |
|
Image | double (typical range 0.8 to 2.3) | void | Gamma level of the image. The same color image displayed on two different workstations may look different due to differences in the display monitor. Use gamma correction to adjust for this color difference. | |
|
Image | Geometry | void | Preferred size of the image when encoding. | |
Method |
Image &
Options |
unsigned int
{ 0 = Disposal not specified, 1 = Do not dispose of graphic, 3 = Overwrite graphic with background color, 4 = Overwrite graphic with previous graphic. } |
void | unsigned int disposeMethod_ | GIF disposal method. This option is used to control how successive frames are rendered (how the preceding frame is disposed of) when creating a GIF animation. |
Type |
Image &
Options |
InterlaceType | void | InterlaceType interlace_ | The type of interlacing scheme (default NoInterlace). This option is used to specify the type of interlacing scheme for raw image formats such as RGB or YUV. NoInterlace means do not interlace, LineInterlace uses scanline interlacing, and PlaneInterlace uses plane interlacing. PartitionInterlace is like PlaneInterlace except the different planes are saved to individual files (e.g. image.R, image.G, and image.B). Use LineInterlace or PlaneInterlace to create an interlaced GIF or progressive JPEG image. |
|
Image | string | void | const string &label_ | Image label |
|
Options | unsigned int | void | unsigned int lineWidth_ | Line width for drawing lines, circles, ellipses, etc. See Drawable. |
|
Image &
Options |
string | void | const string &magick_ | Get image format (e.g. "GIF") |
|
Image | bool | void | bool matteFlag_ | True if the image has transparency. If set True, store matte channel if the image has one otherwise create an opaque one. |
|
Image &
Options |
Color | void | const Color &matteColor_ | Image matte (transparent) color |
PerPixel |
Image | double | void | The mean error per pixel computed when an image is color reduced. This parameter is only valid if verbose is set to true and the image has just been quantized. | |
|
Options | bool | void | bool flag_ | Transform the image to black and white |
Geometry |
Image | string | void | Tile size and offset within an image montage. Only valid for montage images. | |
MaxError |
Image | double | void | The normalized max error per pixel computed when an image is color reduced. This parameter is only valid if verbose is set to true and the image has just been quantized. | |
MeanError |
Image | double | void | The normalized mean error per pixel computed when an image is color reduced. This parameter is only valid if verbose is set to true and the image has just been quantized. | |
|
Image | unsigned int | void | The number of runlength-encoded packets in
the image |
|
|
Image | unsigned int | void | The number of bytes in each pixel packet | |
|
Options | Color | void | const Color &penColor_ | Pen color to use when annotating on or drawing on image. |
|
Options | Image | void | const Image & penTexture_ | Texture image to paint with (similar to penColor). |
|
Image | Color | unsigned int x_, unsigned int y_ | unsigned int x_, unsigned int y_, const Color &color_ | Get/set pixel color at location x & y. |
|
Image &
Options |
Geometry | void | const Geometry &pageSize_ | Postscript page size. Use this option to specify the dimensions of the Postscript page in dots per inch or a TEXT page in pixels. This option is typically used in concert with density. |
|
Options | unsigned int (0 to 100) | void | unsigned int quality_ | JPEG/MIFF/PNG compression level (default 75). |
Colors |
Options | unsigned int | void | unsigned int colors_ | Preferred number of colors in the image. The actual number of colors in the image may be less than your request, but never more. Images with less unique colors than specified with this option will have any duplicate or unused colors removed. |
ColorSpace |
Options | ColorspaceType | void | ColorspaceType colorSpace_ | Colorspace to quantize colors in (default RGB). Empirical evidence suggests that distances in color spaces such as YUV or YIQ correspond to perceptual color differences more closely than do distances in RGB space. These color spaces may give better results when color reducing an image. |
Dither |
Options | bool | void | bool flag_ | Apply Floyd/Steinberg error diffusion to the image. The basic strategy of dithering is to trade intensity resolution for spatial resolution by averaging the intensities of several neighboring pixels. Images which suffer from severe contouring when reducing colors can be improved with this option. The quantizeColors or monochrome option must be set for this option to take effect. |
Error |
Image | unsigned int | void | Quantization error. Only valid if verbose is set to true prior to executing quantize and the value is read back immediately. | |
TreeDepth |
Options | unsigned int (0 to 8) | void | unsigned int treeDepth_ | Depth of the quantization color classification tree. Values of 0 or 1 allow selection of the optimal tree depth for the color reduction algorithm. Values between 2 and 8 may be used to manually adjust the tree depth. |
Intent |
Image | RenderingIntent | void | RenderingIntent render_ | The type of rendering intent |
Units |
Image &
Options |
ResolutionType | void | ResolutionType units_ | Units of image resolution |
|
Image | unsigned int | void | The number of pixel rows in the image | |
|
Image | unsigned int | void | unsigned int scene_ | Image scene number |
|
Image | string | bool force_ = false | Image MD5 signature. Set force_ to 'true' to force re-computation of signature. | |
|
Options | Geometry | void | const Geometry &geometry_ | Width and height of a raw image (an image which does not support width and height information). Size may also be used to affect the image size read from a multi-resolution format (e.g. Photo CD, JBIG, or JPEG. |
|
Options | unsigned int | void | unsigned int subImage_ | Subimage of an image sequence |
|
Options | unsigned int | void | unsigned int subRange_ | Number of images relative to the base image |
|
Image | string | void | Any text associated with the image | |
|
Options | string | void | const string &tileName_ | Tile name |
|
Image | unsigned long | void | Number of colors in the image | |
|
Image | ImageType | void | Image type | |
|
Options | bool | void | bool verboseFlag_ | Print detailed information about the image |
|
Options | string | void | const string &view_ | FlashPix viewing parameters. |
|
Options | string (e.g. "hostname:0.0") | void | const string &display_ | X11 display to display to, obtain fonts from, or to capture image from |
|
Image | double | void | x resolution of the image | |
|
Image | double | void | y resolution of the image |