3 Imager::ImageTypes - Internal image representation information
9 $img = Imager->new(); # Empty image (size is 0 by 0)
10 $img->open(file=>'lena.png',type=>'png'); # Read image from file
12 $img = Imager->new(xsize=>400, ysize=>300); # RGB data
14 $img = Imager->new(xsize=>400, ysize=>300, # Grayscale
17 $img = Imager->new(xsize=>400, ysize=>300, # RGB with alpha
20 $img = Imager->new(xsize=>200, ysize=>200,
21 type=>'paletted'); # paletted image
23 $img = Imager->new(xsize=>200, ysize=>200,
24 bits=>16); # 16 bits/channel rgb
26 $img = Imager->new(xsize=>200, ysize=>200,
27 bits=>'double'); # 'double' floating point
30 $img->img_set(xsize=>500, ysize=>500, # reset the image object
34 # Example getting information about an Imager object
36 print "Image information:\n";
37 print "Width: ", $img->getwidth(), "\n";
38 print "Height: ", $img->getheight(), "\n";
39 print "Channels: ", $img->getchannels(), "\n";
40 print "Bits/Channel: ", $img->bits(), "\n";
41 print "Virtual: ", $img->virtual() ? "Yes" : "No", "\n";
42 my $colorcount = $img->getcolorcount(maxcolors=>512);
43 print "Actual number of colors in image: ";
44 print defined($colorcount) ? $colorcount : ">512", "\n";
45 print "Type: ", $img->type(), "\n";
47 if ($img->type() eq 'direct') {
48 print "Modifiable Channels: ";
50 ($img->getmask() & 1<<$_) ? $_ : ()
51 } 0..$img->getchannels();
56 @colors = $img->getcolors();
57 print "Palette size: ".@colors."\n";
58 my $mx = @colors > 4 ? 4 : 0+@colors;
59 print "First $mx entries:\n";
60 for (@colors[0..$mx-1]) {
62 print "(", join(", ", @res[0..$img->getchannels()-1]), ")\n";
66 my @tags = $img->tags();
70 print shift @$_, ": ", join " ", @$_, "\n";
73 print "No tags in image\n";
79 Imager supports various internal image representations of images. The
80 two major classes are direct mode and paletted mode. In paletted mode
81 an image has a numbered list of colors and the color of each pixel is
82 determined by an index into the table. In direct mode there is no
83 color palette and each pixel has a seperate value for red green and
84 blue for RGB images. To complicate matters it's possible to have
85 other color spaces than RGB, for example, gray, gray and alpha, or
86 red, green, blue and alpha.
88 In addition it's possible to have direct type images with 8 bits/channel
89 16 bits/channel or double/channel (64 bits on many systems).
91 To query an existing image about it's parameters see the C<bits()>,
92 C<type()>, C<getwidth()>, C<getheight()>, C<getchannels()> and
95 The coordinate system in Imager has the origin in the upper left
96 corner, see L<Imager::Draw> for details.
98 =head2 Creating Imager Objects
104 $img = Imager->new();
105 $img->read(file=>"alligator.ppm") or die $img->errstr;
107 Here C<new()> creates an empty image with width and height of zero.
108 It's only useful for creating an Imager object to call the read()
111 %opts = (xsize=>300, ysize=>200);
112 $img = Imager->new(%opts); # create direct mode RGBA image
113 $img = Imager->new(%opts, channels=>4); # create direct mode RGBA image
115 To create paletted images, set the 'type' parameter to 'paletted':
117 $img = Imager->new(xsize=>200, ysize=>200, type=>'paletted');
119 which creates an image with a maxiumum of 256 colors, which you can
120 change by supplying the C<maxcolors> parameter.
122 For improved color precision you can use the bits parameter to specify
125 $img = Imager->new(xsize=>200, ysize=>200,
126 channels=>3, bits=>16);
128 or for even more precision:
130 $img = Imager->new(xsize=>200, ysize=>200,
131 channels=>3, bits=>'double');
133 to get an image that uses a double for each channel.
135 Note that as of this writing all functions should work on images with
136 more than 8-bits/channel, but many will only work at only
137 8-bit/channel precision.
139 Currently only 8-bit, 16-bit, and double per channel image types are
140 available, this may change later.
144 If you have an existing image, use img_set() to change it's dimensions
145 - this will destroy any existing image data:
147 $img->img_set(xsize=>500, ysize=>500, channels=>4);
151 =head2 Getting Information About an Imager Object
157 print "Image width: ", $img->getwidth(), "\n";
159 The C<getwidth()> method returns the width of the image. This value
160 comes either from C<new()> with xsize,ysize parameters or from reading
161 data from a file with C<read()>. If called on an image that has no
162 valid data in it like C<Imager->new()> returns, the return value of
163 C<getwidth()> is undef.
167 print "Image height: ", $img->getheight(), "\n";
169 Same details apply as for L<getwidth>.
173 print "Image has ",$img->getchannels(), " channels\n";
175 To get the number of channels in an image C<getchannels()> is used.
180 It is possible to have Imager find the number of colors in an image by
181 with the C<getcolorcount()> method. It requires memory proportionally
182 to the number of colors in the image so it is possible to have it stop
183 sooner if you only need to know if there are more than a certain
184 number of colors in the image. If there are more colors than asked
185 for the function return undef. Examples:
187 if (!defined($img->getcolorcount(maxcolors=>512)) {
188 print "Less than 512 colors in image\n";
194 The bits() method retrieves the number of bits used to represent each
195 channel in a pixel, 8 for a normal image, 16 for 16-bit image and
196 'double' for a double/channel image.
200 The type() method returns either 'direct' for truecolor images or
201 'paletted' for paletted images.
205 The virtual() method returns non-zero
206 if the image contains no actual pixels, for example masked images.
212 =head2 Direct Type Images
218 @rgbanames = qw( red green blue alpha );
219 my $mask = $img->getmask();
220 print "Modifiable channels:\n";
221 for (0..$img->getchannels()-1) {
222 print $rgbanames[$_],"\n" if $mask & 1<<$_;
225 C<getmask()> is used to fetch the current channel mask. The mask
226 determines what channels are currently modifiable in the image. The
227 channel mask is an integer value, if the i-th lsb is set the i-th
228 channel is modifiable.
232 $mask = $img->getmask();
233 $img->setmask(mask=>8); # modify alpha only
237 $img->setmask(mask=>$mask); # restore previous mask
239 C<setmask()> is used to set the channel mask of the image. See
240 L<getmask> for details.
244 =head2 Palette Type Images
246 In general you can work with paletted images in the same way as RGB
247 images, except that if you attempt to draw to a paletted image with a
248 color that is not in the image's palette, the image will be converted
249 to an RGB image. This means that drawing on a paletted image with
250 anti-aliasing enabled will almost certainly convert the image to RGB.
252 Palette management takes place through C<addcolors()>, C<setcolors()>,
253 C<getcolors()> and C<findcolor()>:
259 You can add colors to a paletted image with the addcolors() method:
261 my @colors = ( Imager::Color->new(255, 0, 0),
262 Imager::Color->new(0, 255, 0) );
263 my $index = $img->addcolors(colors=>\@colors);
265 The return value is the index of the first color added, or undef if
266 adding the colors would overflow the palette.
270 $img->setcolors(start=>$start, colors=>\@colors);
272 Once you have colors in the palette you can overwrite them with the
273 C<setcolors()> method: C<sercolors()> returns true on success.
277 To retrieve existing colors from the palette use the getcolors() method:
279 # get the whole palette
280 my @colors = $img->getcolors();
282 my $color = $img->getcolors(start=>$index);
283 # get a range of colors
284 my @colors = $img->getcolors(start=>$index, count=>$count);
288 To quickly find a color in the palette use findcolor():
290 my $index = $img->findcolor(color=>$color);
292 which returns undef on failure, or the index of the color.
294 You can get the current palette size with $img->colorcount, and the
295 maximum size of the palette with $img->maxcolors.
299 =head2 Conversion Between Image Types
301 Warning: if you draw on a paletted image with colors that aren't in
302 the palette, the image will be internally converted to a normal image.
308 You can create a new paletted image from an existing image using the
309 to_paletted() method:
311 $palimg = $img->to_paletted(\%opts)
313 where %opts contains the options specified under L<Quantization options>.
317 You can convert a paletted image (or any image) to an 8-bit/channel
320 $rgbimg = $img->to_rgb8;
325 Masked images let you control which pixels are modified in an
326 underlying image. Where the first channel is completely black in the
327 mask image, writes to the underlying image are ignored.
329 For example, given a base image called $img:
331 my $mask = Imager->new(xsize=>$img->getwidth, ysize=>getheight,
333 # ... draw something on the mask
334 my $maskedimg = $img->masked(mask=>$mask);
336 You can specifiy the region of the underlying image that is masked
337 using the left, top, right and bottom options.
339 If you just want a subset of the image, without masking, just specify
340 the region without specifying a mask.
350 Image tags contain meta-data about the image, ie. information not
351 stored as pixels of the image.
353 At the perl level each tag has a name or code and a value, which is an
354 integer or an arbitrary string. An image can contain more than one
355 tag with the same name or code.
357 You can retrieve tags from an image using the tags() method, you can
358 get all of the tags in an image, as a list of array references, with
359 the code or name of the tag followed by the value of the tag:
361 my @alltags = $img->tags;
363 or you can get all tags that have a given name:
365 my @namedtags = $img->tags(name=>$name);
369 my @tags = $img->tags(code=>$code);
371 You can add tags using the addtag() method, either by name:
373 my $index = $img->addtag(name=>$name, value=>$value);
377 my $index = $img->addtag(code=>$code, value=>$value);
379 You can remove tags with the deltag() method, either by index:
381 $img->deltag(index=>$index);
385 $img->deltag(name=>$name);
389 $img->deltag(code=>$code);
391 In each case deltag() returns the number of tags deleted.
396 Many tags are only meaningful for one format. GIF looping information
397 is pretty useless for JPEG for example. Thus, many tags are set by
398 only a single reader or used by a single writer. For a complete list
399 of format specific tags see L<Imager::Files>.
401 Since tags are a relatively new addition their use is not wide spread
402 but eventually we hope to have all the readers for various formats set
403 some standard information.
411 The spatial resolution of the image in pixels per inch. If the image
412 format uses a different scale, eg. pixels per meter, then this value
413 is converted. A floating point number stored as a string.
417 If this is non-zero then the values in i_xres and i_yres are treated
418 as a ratio only. If the image format does not support aspect ratios
419 then this is scaled so the smaller value is 72dpi.
423 If this tag is present then the whole image could not be read. This
424 isn't implemented for all images yet.
432 =head2 Quantization options
434 These options can be specified when calling write_multi() for gif
435 files, when writing a single image with the gifquant option set to
436 'gen', or for direct calls to i_writegif_gen and i_writegif_callback.
442 A arrayref of colors that are fixed. Note that some color generators
447 The type of transparency processing to perform for images with an
448 alpha channel where the output format does not have a proper alpha
449 channel (eg. gif). This can be any of:
455 No transparency processing is done. (default)
459 Pixels more transparent that tr_threshold are rendered as transparent.
463 An error diffusion dither is done on the alpha channel. Note that
464 this is independent of the translation performed on the colour
465 channels, so some combinations may cause undesired artifacts.
469 The ordered dither specified by tr_orddith is performed on the alpha
474 This will only be used if the image has an alpha channel, and if there
475 is space in the palette for a transparency colour.
479 The highest alpha value at which a pixel will be made transparent when
480 transp is 'threshold'. (0-255, default 127)
484 The type of error diffusion to perform on the alpha channel when
485 transp is 'errdiff'. This can be any defined error diffusion type
486 except for custom (see errdiff below).
490 The type of ordered dither to perform on the alpha channel when transp
491 is 'ordered'. Possible values are:
497 A semi-random map is used. The map is the same each time.
509 horizontal line dither.
513 vertical line dither.
529 dot matrix dither (currently the default). This is probably the best
530 for displays (like web pages).
534 A custom dither matrix is used - see tr_map
540 When tr_orddith is custom this defines an 8 x 8 matrix of integers
541 representing the transparency threshold for pixels corresponding to
542 each position. This should be a 64 element array where the first 8
543 entries correspond to the first row of the matrix. Values should be
548 Defines how the quantization engine will build the palette(s).
549 Currently this is ignored if 'translate' is 'giflib', but that may
550 change. Possible values are:
556 Only colors supplied in 'colors' are used.
560 The web color map is used (need url here.)
564 The original code for generating the color map (Addi's code) is used.
568 Other methods may be added in the future.
572 A arrayref containing Imager::Color objects, which represents the
573 starting set of colors to use in translating the images. webmap will
574 ignore this. The final colors used are copied back into this array
575 (which is expanded if necessary.)
579 The maximum number of colors to use in the image.
583 The method used to translate the RGB values in the source image into
584 the colors selected by make_colors. Note that make_colors is ignored
585 whene translate is 'giflib'.
593 The giflib native quantization function is used.
597 The closest color available is used.
601 The pixel color is modified by perturb, and the closest color is chosen.
605 An error diffusion dither is performed.
609 It's possible other transate values will be added.
613 The type of error diffusion dither to perform. These values (except
614 for custom) can also be used in tr_errdif.
620 Floyd-Steinberg dither
624 Jarvis, Judice and Ninke dither
632 Custom. If you use this you must also set errdiff_width,
633 errdiff_height and errdiff_map.
645 When translate is 'errdiff' and errdiff is 'custom' these define a
646 custom error diffusion map. errdiff_width and errdiff_height define
647 the size of the map in the arrayref in errdiff_map. errdiff_orig is
648 an integer which indicates the current pixel position in the top row
653 When translate is 'perturb' this is the magnitude of the random bias
654 applied to each channel of the pixel before it is looked up in the