Imager::ImageTypes - Internal image representation information
use Imager;
$img = Imager->new(); # Empty image (size is 0 by 0) $img->open(file=>'lena.png',type=>'png'); # Read image from file
$img = Imager->new(xsize=>400, ysize=>300); # RGB data
$img = Imager->new(xsize=>400, ysize=>300, # Grayscale channels=>1); #
$img = Imager->new(xsize=>400, ysize=>300, # RGB with alpha channels=>4); # $img = Imager->new(xsize=>200, ysize=>200, type=>'paletted'); # paletted image $img = Imager->new(xsize=>200, ysize=>200, bits=>16); # 16 bits/channel rgb $img = Imager->new(xsize=>200, ysize=>200, bits=>'double'); # 'double' floating point # per channel
$img->img_set(xsize=>500, ysize=>500, # reset the image object channels=>4);
# Example getting information about an Imager object
print "Image information:\n"; print "Width: ", $img->getwidth(), "\n"; print "Height: ", $img->getheight(), "\n"; print "Channels: ", $img->getchannels(), "\n"; print "Bits/Channel: ", $img->bits(), "\n"; print "Virtual: ", $img->virtual() ? "Yes" : "No", "\n"; my $colorcount = $img->getcolorcount(maxcolors=>512); print "Actual number of colors in image: "; print defined($colorcount) ? $colorcount : ">512", "\n"; print "Type: ", $img->type(), "\n";
if ($img->type() eq 'direct') { print "Modifiable Channels: "; print join " ", map { ($img->getmask() & 1<<$_) ? $_ : () } 0..$img->getchannels(); print "\n"; } else { @colors = $img->getcolors(); print "Palette size: ".@colors."\n"; my $mx = @colors > 4 ? 4 : 0+@colors; print "First $mx entries:\n"; for (@colors[0..$mx-1]) { my @res = $_->rgba(); print "(", join(", ", @res[0..$img->getchannels()-1]), ")\n"; } } my @tags = $img->tags(); if (@tags) { print "Tags:\n"; for(@tags) { print shift @$_, ": ", join " ", @$_, "\n"; } } else { print "No tags in image\n"; }
Imager supports various internal image representations of images. The two major classes are direct mode and paletted mode. In paletted mode an image has a numbered list of colors and the color of each pixel is determined by an index into the table. In direct mode there is no color palette and each pixel has a seperate value for red green and blue for RGB images. To complicate matters it's possible to have other color spaces than RGB, for example, gray, gray and alpha, or red, green, blue and alpha.
In addition it's possible to have direct type images with 8 bits/channel 16 bits/channel or double/channel (64 bits on many systems).
To query an existing image about it's parameters see the bits()
,
type()
, getwidth()
, getheight()
, getchannels()
and
virtual()
methods.
The coordinate system in Imager has the origin in the upper left corner, see Imager::Draw for details.
$img = Imager->new(); $img->read(file=>"alligator.ppm") or die $img->errstr;
Here new()
creates an empty image with width and height of zero.
It's only useful for creating an Imager object to call the read()
method on later.
%opts = (xsize=>300, ysize=>200); $img = Imager->new(%opts); # create direct mode RGBA image $img = Imager->new(%opts, channels=>4); # create direct mode RGBA image
To create paletted images, set the 'type' parameter to 'paletted':
$img = Imager->new(xsize=>200, ysize=>200, type=>'paletted');
which creates an image with a maxiumum of 256 colors, which you can
change by supplying the maxcolors
parameter.
For improved color precision you can use the bits parameter to specify 16 bit per channel:
$img = Imager->new(xsize=>200, ysize=>200, channels=>3, bits=>16);
or for even more precision:
$img = Imager->new(xsize=>200, ysize=>200, channels=>3, bits=>'double');
to get an image that uses a double for each channel.
Note that as of this writing all functions should work on images with more than 8-bits/channel, but many will only work at only 8-bit/channel precision.
Currently only 8-bit, 16-bit, and double per channel image types are available, this may change later.
If you have an existing image, use img_set() to change it's dimensions - this will destroy any existing image data:
$img->img_set(xsize=>500, ysize=>500, channels=>4);
print "Image width: ", $img->getwidth(), "\n";
The getwidth()
method returns the width of the image. This value
comes either from new()
with xsize,ysize parameters or from reading
data from a file with read()
. If called on an image that has no
valid data in it like Imager->new()
returns, the return value of
getwidth()
is undef.
print "Image height: ", $img->getheight(), "\n";
Same details apply as for getwidth.
print "Image has ",$img->getchannels(), " channels\n";
To get the number of channels in an image getchannels()
is used.
It is possible to have Imager find the number of colors in an image by
with the getcolorcount()
method. It requires memory proportionally
to the number of colors in the image so it is possible to have it stop
sooner if you only need to know if there are more than a certain
number of colors in the image. If there are more colors than asked
for the function return undef. Examples:
if (!defined($img->getcolorcount(maxcolors=>512)) { print "Less than 512 colors in image\n"; }
@rgbanames = qw( red green blue alpha ); my $mask = $img->getmask(); print "Modifiable channels:\n"; for (0..$img->getchannels()-1) { print $rgbanames[$_],"\n" if $mask & 1<<$_; }
getmask()
is used to fetch the current channel mask. The mask
determines what channels are currently modifiable in the image. The
channel mask is an integer value, if the i-th lsb is set the i-th
channel is modifiable.
$mask = $img->getmask(); $img->setmask(mask=>8); # modify alpha only
...
$img->setmask(mask=>$mask); # restore previous mask
setmask()
is used to set the channel mask of the image. See
getmask for details.
In general you can work with paletted images in the same way as RGB images, except that if you attempt to draw to a paletted image with a color that is not in the image's palette, the image will be converted to an RGB image. This means that drawing on a paletted image with anti-aliasing enabled will almost certainly convert the image to RGB.
Palette management takes place through addcolors()
, setcolors()
,
getcolors()
and findcolor()
:
You can add colors to a paletted image with the addcolors() method:
my @colors = ( Imager::Color->new(255, 0, 0), Imager::Color->new(0, 255, 0) ); my $index = $img->addcolors(colors=>\@colors);
The return value is the index of the first color added, or undef if adding the colors would overflow the palette.
$img->setcolors(start=>$start, colors=>\@colors);
Once you have colors in the palette you can overwrite them with the
setcolors()
method: sercolors()
returns true on success.
To retrieve existing colors from the palette use the getcolors() method:
# get the whole palette my @colors = $img->getcolors(); # get a single color my $color = $img->getcolors(start=>$index); # get a range of colors my @colors = $img->getcolors(start=>$index, count=>$count);
To quickly find a color in the palette use findcolor():
my $index = $img->findcolor(color=>$color);
which returns undef on failure, or the index of the color.
You can get the current palette size with $img->colorcount, and the maximum size of the palette with $img->maxcolors.
Warning: if you draw on a paletted image with colors that aren't in the palette, the image will be internally converted to a normal image.
You can create a new paletted image from an existing image using the to_paletted() method:
$palimg = $img->to_paletted(\%opts)
where %opts contains the options specified under Quantization options.
You can convert a paletted image (or any image) to an 8-bit/channel RGB image with:
$rgbimg = $img->to_rgb8;
Masked images let you control which pixels are modified in an underlying image. Where the first channel is completely black in the mask image, writes to the underlying image are ignored.
For example, given a base image called $img:
my $mask = Imager->new(xsize=>$img->getwidth, ysize=>getheight, channels=>1); # ... draw something on the mask my $maskedimg = $img->masked(mask=>$mask);
You can specifiy the region of the underlying image that is masked using the left, top, right and bottom options.
If you just want a subset of the image, without masking, just specify the region without specifying a mask.
Image tags contain meta-data about the image, ie. information not stored as pixels of the image.
At the perl level each tag has a name or code and a value, which is an integer or an arbitrary string. An image can contain more than one tag with the same name or code.
You can retrieve tags from an image using the tags() method, you can get all of the tags in an image, as a list of array references, with the code or name of the tag followed by the value of the tag:
my @alltags = $img->tags;
or you can get all tags that have a given name:
my @namedtags = $img->tags(name=>$name);
or a given code:
my @tags = $img->tags(code=>$code);
You can add tags using the addtag() method, either by name:
my $index = $img->addtag(name=>$name, value=>$value);
or by code:
my $index = $img->addtag(code=>$code, value=>$value);
You can remove tags with the deltag() method, either by index:
$img->deltag(index=>$index);
or by name:
$img->deltag(name=>$name);
or by code:
$img->deltag(code=>$code);
In each case deltag() returns the number of tags deleted.
Many tags are only meaningful for one format. GIF looping information is pretty useless for JPEG for example. Thus, many tags are set by only a single reader or used by a single writer. For a complete list of format specific tags see Imager::Files.
Since tags are a relatively new addition their use is not wide spread but eventually we hope to have all the readers for various formats set some standard information.
These options can be specified when calling write_multi() for gif files, when writing a single image with the gifquant option set to 'gen', or for direct calls to i_writegif_gen and i_writegif_callback.
The type of transparency processing to perform for images with an alpha channel where the output format does not have a proper alpha channel (eg. gif). This can be any of:
This will only be used if the image has an alpha channel, and if there is space in the palette for a transparency colour.
The type of ordered dither to perform on the alpha channel when transp is 'ordered'. Possible values are:
Defines how the quantization engine will build the palette(s). Currently this is ignored if 'translate' is 'giflib', but that may change. Possible values are:
Other methods may be added in the future.
The method used to translate the RGB values in the source image into the colors selected by make_colors. Note that make_colors is ignored whene translate is 'giflib'.
Possible values are:
It's possible other transate values will be added.
The type of error diffusion dither to perform. These values (except for custom) can also be used in tr_errdif.