Class GD2::Image
In: lib/gd2/image.rb
Parent: Object

Introduction

Image is the abstract base class for Image::IndexedColor and Image::TrueColor.

Creating and Importing

Image objects are created either as a blank array of pixels:

  image = Image::IndexedColor.new(width, height)
  image = Image::TrueColor.new(width, height)

or by loading image data from a file or a string containing one of the supported image formats:

  image = Image.load(file)
  image = Image.load(string)

or by importing image data from a file given by its pathname:

   image = Image.import(filename)

Exporting

After manipulating an image, it can be exported to a string in one of the supported image formats:

  image.jpeg(quality = nil)
  image.png(level = nil)
  image.gif
  image.wbmp(fgcolor)
  image.gd
  image.gd2(fmt = FMT_COMPRESSED)

or to a file in a format determined by the filename extension:

  image.export(filename, options = {})

Methods

Included Modules

Enumerable

Classes and Modules

Class GD2::Image::IndexedColor
Class GD2::Image::TrueColor
Class GD2::Image::UnrecognizedImageTypeError

External Aliases

new -> []

Attributes

palette  [R]  The Palette object for this image

Public Class methods

Import an image from a file with the given filename. The :format option or the file extension is used to determine the image type (jpeg, png, gif, wbmp, gd, gd2, xbm, or xpm). The resulting image will be either of class Image::TrueColor or Image::IndexedColor.

If the file format is gd2, it is optionally possible to extract only a part of the image. Use options :x, :y, :width, and :height to specify the part of the image to import.

Load an image from a file or a string. The image type is detected automatically (JPEG, PNG, GIF, WBMP, or GD2). The resulting image will be either of class Image::TrueColor or Image::IndexedColor.

Create a new image of the specified dimensions. The default image class is Image::TrueColor; call this method on Image::IndexedColor instead if a palette image is desired.

Public Instance methods

Compare this image with another image. Returns false if the images are not identical.

Return the color of the pixel at image location (x, y).

Set the color of the pixel at image location (x, y).

Set whether colors should be alpha blended with existing colors when pixels are modified. Alpha blending is not available for IndexedColor images.

Return true if colors will be alpha blended into the image when pixels are modified. Returns false if colors will be copied verbatim into the image without alpha blending when pixels are modified.

Return the aspect ratio of this image, as a floating point ratio of the width to the height.

Return the current clipping rectangle. Use Image#with_clipping to temporarily modify the clipping rectangle.

Return true if the current clipping rectangle excludes the given point.

Return a pixel value for the given color object.

Compare this image with another image. Returns 0 if the images are identical, otherwise a bit field indicating the differences. See the GD2::CMP_* constants for individual bit flags.

Copy a portion of another image to this image. If src_w and src_h are specified, the indicated portion of the source image will be resized (and resampled) to fit the indicated dimensions of the destination.

Copy a portion of another image to this image, rotating the source portion first by the indicated angle (in radians). The dst_x and dst_y arguments indicate the center of the desired destination, and may be floating point.

Like Image#crop! except a new image is returned.

Crop this image to the specified dimensions, such that (x, y) becomes (0, 0).

Provide a drawing environment for a block. See GD2::Canvas.

Duplicate this image, copying all pixels to a new image. Contrast with Image#clone which produces a shallow copy and shares internal pixel data.

Iterate over each row of pixels in the image, returning an array of pixel values.

Export this image to a file with the given filename. The image format is determined by the :format option, or by the file extension (jpeg, png, gif, wbmp, gd, or gd2). Returns the size of the written image data. Additional options are as arguments for the Image#jpeg, Image#png, Image#wbmp, or Image#gd2 methods.

Encode and return data for this image in “.gd” format. This is an internal format used by the gd library to quickly read and write images.

Encode and return data for this image in “.gd2” format. This is an internal format used by the gd library to quickly read and write images. The specified fmt may be either GD2::FMT_RAW or GD2::FMT_COMPRESSED.

Return the pixel value at image location (x, y).

Encode and return data for this image in GIF format. Note that GIF only supports palette images; TrueColor images will be automatically converted to IndexedColor internally in order to create the GIF. Use Image#to_indexed_color to control this conversion more precisely.

h()

Alias for height

Return the height of this image, in pixels.

Set whether this image will be stored in interlaced form when output as PNG or JPEG.

Return true if this image will be stored in interlaced form when output as PNG or JPEG.

Encode and return data for this image in JPEG format. The quality argument should be in the range 0–95, with higher quality values usually implying both higher quality and larger sizes.

Merge a portion of another image into this one by the amount specified as pct (a percentage). A percentage of 1.0 is identical to Image#copy_from; a percentage of 0.0 is a no-op. Note that alpha channel information from the source image is ignored.

Consolidate duplicate colors in this image, and eliminate all unused palette entries. This only has an effect on IndexedColor images, and is rather expensive. Returns the number of palette entries deallocated.

pixel(x, y)

Alias for get_pixel

Return a Color object for the given pixel value.

Encode and return data for this image in PNG format. The level argument should be in the range 0–9 indicating the level of lossless compression (0 = none, 1 = minimal but fast, 9 = best but slow).

Like Image#polar_transform! except a new image is returned.

Transform this image into a new image of width and height radius × 2, in which the X axis of the original has been remapped to θ (angle) and the Y axis of the original has been remapped to ρ (distance from center). Note that the original image must be square.

Like Image#resize! except a new image is returned.

Resize this image to the given dimensions. If resample is true, the image pixels will be resampled; otherwise they will be stretched or shrunk as necessary without resampling.

Rotate this image by the given angle (in radians) about the given axis coordinates. Note that some of the edges of the image may be lost.

Set whether this image will be stored with full alpha channel information when output as PNG.

Return true if this image will be stored with full alpha channel information when output as PNG.

Set the pixel value at image location (x, y).

Sharpen this image by pct (a percentage) which can be greater than 1.0. Transparency/alpha channel are not altered. This has no effect on IndexedColor images.

Return the size of this image as an array [width, height], in pixels.

Return this image as an IndexedColor image, creating a copy if necessary. colors indicates the maximum number of palette colors to use, and dither controls whether dithering is used.

Return this image as a TrueColor image, creating a copy if necessary.

Return the transparent color for this image, or nil if none has been set.

Set or unset the transparent color for this image.

Return true if this image is a TrueColor image.

Like Image#uncrop! except a new image is returned.

Expand the left, top, right, and bottom borders of this image by the given number of pixels.

w()

Alias for width

Encode and return data for this image in WBMP format. WBMP currently supports only black and white images; the specified fgcolor will be used as the foreground color (black), and all other colors will be considered “background” (white).

Return the width of this image, in pixels.

Temporarily set the clipping rectangle during the execution of a block. Pixels outside this rectangle will not be modified by drawing or copying operations.

[Validate]