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 if the image contains no actual
206 pixels, for example masked images.
211 =head2 Direct Type Images
217 @rgbanames = qw( red green blue alpha );
218 my $mask = $img->getmask();
219 print "Modifiable channels:\n";
220 for (0..$img->getchannels()-1) {
221 print $rgbanames[$_],"\n" if $mask & 1<<$_;
224 C<getmask()> is used to fetch the current channel mask. The mask
225 determines what channels are currently modifiable in the image. The
226 channel mask is an integer value, if the i-th lsb is set the i-th
227 channel is modifiable.
231 $mask = $img->getmask();
232 $img->setmask(mask=>8); # modify alpha only
236 $img->setmask(mask=>$mask); # restore previous mask
238 C<setmask()> is used to set the channel mask of the image. See
239 L<getmask> for details.
243 =head2 Palette Type Images
245 In general you can work with paletted images in the same way as RGB
246 images, except that if you attempt to draw to a paletted image with a
247 color that is not in the image's palette, the image will be converted
248 to an RGB image. This means that drawing on a paletted image with
249 anti-aliasing enabled will almost certainly convert the image to RGB.
251 Palette management takes place through C<addcolors()>, C<setcolors()>,
252 C<getcolors()> and C<findcolor()>:
258 You can add colors to a paletted image with the addcolors() method:
260 my @colors = ( Imager::Color->new(255, 0, 0),
261 Imager::Color->new(0, 255, 0) );
262 my $index = $img->addcolors(colors=>\@colors);
264 The return value is the index of the first color added, or undef if
265 adding the colors would overflow the palette.
269 $img->setcolors(start=>$start, colors=>\@colors);
271 Once you have colors in the palette you can overwrite them with the
272 C<setcolors()> method: C<sercolors()> returns true on success.
276 To retrieve existing colors from the palette use the getcolors() method:
278 # get the whole palette
279 my @colors = $img->getcolors();
281 my $color = $img->getcolors(start=>$index);
282 # get a range of colors
283 my @colors = $img->getcolors(start=>$index, count=>$count);
287 To quickly find a color in the palette use findcolor():
289 my $index = $img->findcolor(color=>$color);
291 which returns undef on failure, or the index of the color.
293 You can get the current palette size with $img->colorcount, and the
294 maximum size of the palette with $img->maxcolors.
298 =head2 Conversion Between Image Types
300 Warning: if you draw on a paletted image with colors that aren't in
301 the palette, the image will be internally converted to a normal image.
307 You can create a new paletted image from an existing image using the
308 to_paletted() method:
310 $palimg = $img->to_paletted(\%opts)
312 where %opts contains the options specified under L<Quantization options>.
316 You can convert a paletted image (or any image) to an 8-bit/channel
319 $rgbimg = $img->to_rgb8;
324 Masked images let you control which pixels are modified in an
325 underlying image. Where the first channel is completely black in the
326 mask image, writes to the underlying image are ignored.
328 For example, given a base image called $img:
330 my $mask = Imager->new(xsize=>$img->getwidth, ysize=>getheight,
332 # ... draw something on the mask
333 my $maskedimg = $img->masked(mask=>$mask);
335 You can specifiy the region of the underlying image that is masked
336 using the left, top, right and bottom options.
338 If you just want a subset of the image, without masking, just specify
339 the region without specifying a mask.
349 Image tags contain meta-data about the image, ie. information not
350 stored as pixels of the image.
352 At the perl level each tag has a name or code and a value, which is an
353 integer or an arbitrary string. An image can contain more than one
354 tag with the same name or code.
356 You can retrieve tags from an image using the tags() method, you can
357 get all of the tags in an image, as a list of array references, with
358 the code or name of the tag followed by the value of the tag:
360 my @alltags = $img->tags;
362 or you can get all tags that have a given name:
364 my @namedtags = $img->tags(name=>$name);
368 my @tags = $img->tags(code=>$code);
370 You can add tags using the addtag() method, either by name:
372 my $index = $img->addtag(name=>$name, value=>$value);
376 my $index = $img->addtag(code=>$code, value=>$value);
378 You can remove tags with the deltag() method, either by index:
380 $img->deltag(index=>$index);
384 $img->deltag(name=>$name);
388 $img->deltag(code=>$code);
390 In each case deltag() returns the number of tags deleted.
395 Many tags are only meaningful for one format. GIF looping information
396 is pretty useless for JPEG for example. Thus, many tags are set by
397 only a single reader or used by a single writer. For a complete list
398 of format specific tags see L<Imager::Files>.
400 Since tags are a relatively new addition their use is not wide spread
401 but eventually we hope to have all the readers for various formats set
402 some standard information.
410 The spatial resolution of the image in pixels per inch. If the image
411 format uses a different scale, eg. pixels per meter, then this value
412 is converted. A floating point number stored as a string.
416 If this is non-zero then the values in i_xres and i_yres are treated
417 as a ratio only. If the image format does not support aspect ratios
418 then this is scaled so the smaller value is 72dpi.
422 If this tag is present then the whole image could not be read. This
423 isn't implemented for all images yet.
431 =head2 Quantization options
433 These options can be specified when calling write_multi() for gif
434 files, when writing a single image with the gifquant option set to
435 'gen', or for direct calls to i_writegif_gen and i_writegif_callback.
441 A arrayref of colors that are fixed. Note that some color generators
446 The type of transparency processing to perform for images with an
447 alpha channel where the output format does not have a proper alpha
448 channel (eg. gif). This can be any of:
454 No transparency processing is done. (default)
458 Pixels more transparent that tr_threshold are rendered as transparent.
462 An error diffusion dither is done on the alpha channel. Note that
463 this is independent of the translation performed on the colour
464 channels, so some combinations may cause undesired artifacts.
468 The ordered dither specified by tr_orddith is performed on the alpha
473 This will only be used if the image has an alpha channel, and if there
474 is space in the palette for a transparency colour.
478 The highest alpha value at which a pixel will be made transparent when
479 transp is 'threshold'. (0-255, default 127)
483 The type of error diffusion to perform on the alpha channel when
484 transp is 'errdiff'. This can be any defined error diffusion type
485 except for custom (see errdiff below).
489 The type of ordered dither to perform on the alpha channel when transp
490 is 'ordered'. Possible values are:
496 A semi-random map is used. The map is the same each time.
508 horizontal line dither.
512 vertical line dither.
528 dot matrix dither (currently the default). This is probably the best
529 for displays (like web pages).
533 A custom dither matrix is used - see tr_map
539 When tr_orddith is custom this defines an 8 x 8 matrix of integers
540 representing the transparency threshold for pixels corresponding to
541 each position. This should be a 64 element array where the first 8
542 entries correspond to the first row of the matrix. Values should be
547 Defines how the quantization engine will build the palette(s).
548 Currently this is ignored if 'translate' is 'giflib', but that may
549 change. Possible values are:
555 Only colors supplied in 'colors' are used.
559 The web color map is used (need url here.)
563 The original code for generating the color map (Addi's code) is used.
567 Uses a mediancut algorithm, faster than 'addi', but not as good a
572 Other methods may be added in the future.
576 A arrayref containing Imager::Color objects, which represents the
577 starting set of colors to use in translating the images. webmap will
578 ignore this. The final colors used are copied back into this array
579 (which is expanded if necessary.)
583 The maximum number of colors to use in the image.
587 The method used to translate the RGB values in the source image into
588 the colors selected by make_colors. Note that make_colors is ignored
589 whene translate is 'giflib'.
597 The giflib native quantization function is used.
601 The closest color available is used.
605 The pixel color is modified by perturb, and the closest color is chosen.
609 An error diffusion dither is performed.
613 It's possible other transate values will be added.
617 The type of error diffusion dither to perform. These values (except
618 for custom) can also be used in tr_errdif.
624 Floyd-Steinberg dither
628 Jarvis, Judice and Ninke dither
636 Custom. If you use this you must also set errdiff_width,
637 errdiff_height and errdiff_map.
649 When translate is 'errdiff' and errdiff is 'custom' these define a
650 custom error diffusion map. errdiff_width and errdiff_height define
651 the size of the map in the arrayref in errdiff_map. errdiff_orig is
652 an integer which indicates the current pixel position in the top row
657 When translate is 'perturb' this is the magnitude of the random bias
658 applied to each channel of the pixel before it is looked up in the