Debian Codegraph @

lib/Imager/ImageTypes.pod

raw
1 =head1 NAME
2
3 Imager::ImageTypes - image models for Imager
4
5 =head1 SYNOPSIS
6
7 use Imager;
8
9 $img = Imager->new(); # Empty image (size is 0 by 0)
10 $img->open(file=>'lena.png',type=>'png'); # Read image from file
11
12 $img = Imager->new(xsize=>400, ysize=>300); # RGB data
13
14 $img = Imager->new(xsize=>400, ysize=>300, # Grayscale
15 channels=>1); #
16
17 $img = Imager->new(xsize=>400, ysize=>300, # RGB with alpha
18 channels=>4); #
19
20 $img = Imager->new(xsize=>200, ysize=>200,
21 type=>'paletted'); # paletted image
22
23 $img = Imager->new(xsize=>200, ysize=>200,
24 bits=>16); # 16 bits/channel rgb
25
26 $img = Imager->new(xsize=>200, ysize=>200,
27 bits=>'double'); # 'double' floating point
28 # per channel
29
30 $img->img_set(xsize=>500, ysize=>500, # reset the image object
31 channels=>4);
32
33
34 # Example getting information about an Imager object
35
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";
46
47 if ($img->type() eq 'direct') {
48 print "Modifiable Channels: ";
49 print join " ", map {
50 ($img->getmask() & 1<<$_) ? $_ : ()
51 } 0..$img->getchannels();
52 print "\n";
53
54 } else {
55 # palette info
56 my $count = $img->colorcount;
57 @colors = $img->getcolors();
58 print "Palette size: $count\n";
59 my $mx = @colors > 4 ? 4 : 0+@colors;
60 print "First $mx entries:\n";
61 for (@colors[0..$mx-1]) {
62 my @res = $_->rgba();
63 print "(", join(", ", @res[0..$img->getchannels()-1]), ")\n";
64 }
65 }
66
67 my @tags = $img->tags();
68 if (@tags) {
69 print "Tags:\n";
70 for(@tags) {
71 print shift @$_, ": ", join " ", @$_, "\n";
72 }
73 } else {
74 print "No tags in image\n";
75 }
76
77 =head1 DESCRIPTION
78
79 Imager supports two basic models of image:
80
81 =over
82
83 =item *
84
85 direct color - all samples are stored for every pixel. eg. for an
86 8-bit/sample RGB image, 24 bits are stored for each pixel.
87
88 =item *
89
90 paletted - an index into a table of colors is stored for each pixel.
91
92 =back
93
94 Direct color or paletted images can have 1 to 4 samples per color
95 stored. Imager treats these as follows:
96
97 =over
98
99 =item *
100
101 1 sample per color - gray scale image.
102
103 =item *
104
105 2 samples per color - gray scale image with alpha channel, allowing
106 transparency.
107
108 =item *
109
110 3 samples per color - RGB image.
111
112 =item *
113
114 4 samples per color - RGB image with alpha channel, allowing
115 transparency.
116
117 =back
118
119 Direct color images can have sample sizes of 8-bits per sample,
120 16-bits per sample or a double precision floating point number per
121 sample (64-bits on many systems).
122
123 Paletted images are always 8-bits/sample.
124
125 To query an existing image about it's parameters see the C<bits()>,
126 C<type()>, C<getwidth()>, C<getheight()>, C<getchannels()> and
127 C<virtual()> methods.
128
129 The coordinate system in Imager has the origin in the upper left
130 corner, see L<Imager::Draw> for details.
131
132 The alpha channel when one is present is considered unassociated -
133 ie the color data has not been scaled by the alpha channel. Note
134 that not all code follows this (recent) rule, but will over time.
135
136 =head2 Creating Imager Objects
137
138 =over
139
140 =item new()
141 X<new(), Imager methods>
142
143 $img = Imager->new();
144 $img->read(file=>"alligator.ppm") or die $img->errstr;
145
146 Here C<new()> creates an empty image with width and height of zero.
147 It's only useful for creating an Imager object to call the read()
148 method on later.
149
150 %opts = (xsize=>300, ysize=>200);
151 $img = Imager->new(%opts); # create direct mode RGBA image
152 $img = Imager->new(%opts, channels=>4); # create direct mode RGBA image
153
154 You can also read a file from new():
155
156 $img = Imager->new(file => "someimage.png");
157
158 The parameters for new are:
159
160 =over
161
162 =item *
163
164 C<xsize>, C<ysize> - Defines the width and height in pixels of the
165 image. These must be positive.
166
167 If not supplied then only placeholder object is created, which can be
168 supplied to the C<read()> or C<img_set()> methods.
169
170 =item *
171
172 C<channels> - The number of channels for the image. Default 3. Valid
173 values are from 1 to 4.
174
175 =item *
176
177 C<model> - this overrides the value, if any, supplied for C<channels>.
178 This can be one of C<gray>, C<graya>, C<rgb> or C<rgba>.
179
180 =item *
181
182 C<bits> - The storage type for samples in the image. Default: 8.
183 Valid values are:
184
185 =over
186
187 =item *
188
189 C<8> - One byte per sample. 256 discrete values.
190
191 =item *
192
193 C<16> - 16-bits per sample, 65536 discrete values.
194
195 =item *
196
197 C<double> - one C double per sample.
198
199 =back
200
201 Note: you can use any Imager function on any sample size image.
202
203 Paletted images always use 8 bits/sample.
204
205 =item *
206
207 C<type> - either C<'direct'> or C<'paletted'>. Default: C<'direct'>.
208
209 Direct images store color values for each pixel.
210
211 Paletted images keep a table of up to 256 colors called the palette,
212 each pixel is represented as an index into that table.
213
214 In most cases when working with Imager you will want to use the
215 C<direct> image type.
216
217 If you draw on a C<paletted> image with a color not in the image's
218 palette then Imager will transparently convert it to a C<direct>
219 image.
220
221 =item *
222
223 C<maxcolors> - previously the maximum number of colors in a paletted
224 image, this is now ignored and paletted images always have a 256 color
225 palette allocated.
226
227 =item *
228
229 C<file>, C<fh>, C<fd>, C<callback>, C<readcb>, or C<io> - specify a
230 file name, filehandle, file descriptor or callback to read image data
231 from. See L<Imager::Files> for details. The typical use is:
232
233 my $im = Imager->new(file => $filename);
234
235 =item *
236
237 C<filetype> - treated as the file format parameter, as for C<type>
238 with the read() method, eg:
239
240 my $im = Imager->new(file => $filename, filetype => "gif");
241
242 In most cases Imager will detect the file's format itself.
243
244 =back
245
246 In the simplest case just supply the width and height of the image:
247
248 # 8 bit/sample, RGB image
249 my $img = Imager->new(xsize => $width, ysize => $height);
250
251 or if you want an alpha channel:
252
253 # 8 bits/sample, RGBA image
254 my $img = Imager->new(xsize => $width, ysize => $height, channels=>4);
255
256 Note that it I<is> possible for image creation to fail, for example if
257 channels is out of range, or if the image would take too much memory.
258
259 To create paletted images, set the 'type' parameter to 'paletted':
260
261 $img = Imager->new(xsize=>200, ysize=>200, type=>'paletted');
262
263 which creates an image with a maximum of 256 colors, which you can
264 change by supplying the C<maxcolors> parameter.
265
266 For improved color precision you can use the bits parameter to specify
267 16 bit per channel:
268
269 $img = Imager->new(xsize=>200, ysize=>200,
270 channels=>3, bits=>16);
271
272 or for even more precision:
273
274 $img = Imager->new(xsize=>200, ysize=>200,
275 channels=>3, bits=>'double');
276
277 to get an image that uses a double for each channel.
278
279 Note that as of this writing all functions should work on images with
280 more than 8-bits/channel, but many will only work at only
281 8-bit/channel precision.
282
283 If you want an empty Imager object to call the read() method on, just
284 call new() with no parameters:
285
286 my $img = Imager->new;
287 $img->read(file=>$filename)
288 or die $img->errstr;
289
290 Though it's much easier now to just call new() with a C<file>
291 parameter:
292
293 my $img = Imager->new(file => $filename)
294 or die Imager->errstr;
295
296 If none of C<xsize>, C<ysize>, C<file>, C<fh>, C<fd>, C<callback>,
297 C<readcb>, C<data>, C<io> is supplied, and other parameters I<are> supplied
298 C<< Imager->new >> will return failure rather than returning an empty
299 image object.
300
301 =item img_set()
302 X<img_set>
303
304 img_set destroys the image data in the object and creates a new one
305 with the given dimensions and channels. For a way to convert image
306 data between formats see the C<convert()> method.
307
308 $img->img_set(xsize=>500, ysize=>500, channels=>4);
309
310 This takes exactly the same parameters as the new() method, excluding
311 those for reading from files.
312
313 =back
314
315 =head2 Image Attribute functions
316
317 These return basic attributes of an image object.
318
319 =over
320
321 =item getwidth()
322
323 print "Image width: ", $img->getwidth(), "\n";
324
325 The C<getwidth()> method returns the width of the image. This value
326 comes either from C<new()> with C<xsize>, C<ysize> parameters or from
327 reading data from a file with C<read()>. If called on an image that
328 has no valid data in it like C<Imager-E<gt>new()> returns, the return
329 value of C<getwidth()> is undef.
330
331 =item getheight()
332
333 print "Image height: ", $img->getheight(), "\n";
334
335 Same details apply as for L</getwidth()>.
336
337 =item getchannels()
338 X<getchannels() method> X<methods, getchannels()>
339
340 print "Image has ",$img->getchannels(), " channels\n";
341
342 Returns the number of channels in an image.
343
344 Note: previously the number of channels in an image mapped directly to
345 the color model of the image, ie a 4 channel image was always RGBA.
346 This may change in a future release of Imager.
347
348 Returns an empty list if the image object is not initialized.
349
350 =item colorchannels()
351 X<colorchannels() method> X<methods, colorchannels()>
352
353 Returns the number of color channels.
354
355 Currently this is always 1 or 3, but may be 0 for some rare images in
356 a future version of Imager.
357
358 Returns an empty list if the image object is not initialized.
359
360 =item colormodel()
361 X<C<colormodel> method> X<methods, C<colormodel>>
362
363 Returns the color model of the image, including whether there is an
364 alpha channel.
365
366 By default this is returned as a string, one of C<unknown>, C<gray>,
367 C<graya>, C<rgb> or C<rgba>.
368
369 If you call C<colormodel()> with a true numeric parameter:
370
371 my $model = $img->colormodel(numeric => 1);
372
373 then the color model is returned as a number, mapped as follows:
374
375 Numeric String
376 ------- ------
377 0 unknown
378 1 gray
379 2 graya
380 3 rgb
381 4 rgba
382
383 =item alphachannel()
384 X<alphachannel() method> X<methods, alphachannel()>
385
386 Returns the channel index of the alpha channel of the image.
387
388 This is 1 for grayscale images with alpha, 3 for RGB images with alpha
389 and will return C<undef> for all other images.
390
391 Returns an empty list if the image object is not initialized.
392
393 =item bits()
394
395 The bits() method retrieves the number of bits used to represent each
396 channel in a pixel, 8 for a normal image, 16 for 16-bit image and
397 'double' for a double/channel image.
398
399 if ($img->bits eq 8) {
400 # fast but limited to 8-bits/sample
401 }
402 else {
403 # slower but more precise
404 }
405
406 Returns an empty list if the image object is not initialized.
407
408 =item type()
409
410 The type() method returns either 'direct' for direct color images or
411 'paletted' for paletted images.
412
413 if ($img->type eq 'paletted') {
414 # print the palette
415 for my $color ($img->getcolors) {
416 print join(",", $color->rgba), "\n";
417 }
418 }
419
420 Returns an empty list if the image object is not initialized.
421
422 =item virtual()
423
424 The virtual() method returns non-zero if the image contains no actual
425 pixels, for example masked images.
426
427 =for stopwords SDL
428
429 This may also be used for non-native Imager images in the future, for
430 example, for an Imager object that draws on an SDL surface.
431
432 =item is_bilevel()
433
434 Tests if the image will be written as a monochrome or bi-level image
435 for formats that support that image organization.
436
437 In scalar context, returns true if the image is bi-level.
438
439 In list context returns a list:
440
441 ($is_bilevel, $zero_is_white) = $img->is_bilevel;
442
443 An image is considered bi-level, if all of the following are true:
444
445 =over
446
447 =item *
448
449 the image is a paletted image
450
451 =item *
452
453 the image has 1 or 3 channels
454
455 =item *
456
457 the image has only 2 colors in the palette
458
459 =item *
460
461 those 2 colors are black and white, in either order.
462
463 =back
464
465 If a real bi-level organization image is ever added to Imager, this
466 function will return true for that too.
467
468 Returns an empty list if the image object is not initialized.
469
470 =back
471
472 =head2 Direct Type Images
473
474 Direct images store the color value directly for each pixel in the
475 image.
476
477 =over
478
479 =item getmask()
480
481 @rgbanames = qw( red green blue alpha );
482 my $mask = $img->getmask();
483 print "Modifiable channels:\n";
484 for (0..$img->getchannels()-1) {
485 print $rgbanames[$_],"\n" if $mask & 1<<$_;
486 }
487
488 =for stopwords th
489
490 C<getmask()> is used to fetch the current channel mask. The mask
491 determines what channels are currently modifiable in the image. The
492 channel mask is an integer value, if the C<i-th> least significant bit
493 is set the C<i-th> channel is modifiable. eg. a channel mask of 0x5
494 means only channels 0 and 2 are writable.
495
496 Channel masks are deprecated.
497
498 =item setmask()
499
500 $mask = $img->getmask();
501 $img->setmask(mask=>8); # modify alpha only
502
503 ...
504
505 $img->setmask(mask=>$mask); # restore previous mask
506
507 C<setmask()> is used to set the channel mask of the image. See
508 L</getmask()> for details.
509
510 Channel masks are deprecated.
511
512 =back
513
514 =head2 Palette Type Images
515
516 Paletted images keep an array of up to 256 colors, and each pixel is
517 stored as an index into that array.
518
519 In general you can work with paletted images in the same way as RGB
520 images, except that if you attempt to draw to a paletted image with a
521 color that is not in the image's palette, the image will be converted
522 to an RGB image. This means that drawing on a paletted image with
523 anti-aliasing enabled will almost certainly convert the image to RGB.
524
525 Palette management takes place through C<addcolors()>, C<setcolors()>,
526 C<getcolors()> and C<findcolor()>:
527
528 =over
529
530 =item addcolors()
531
532 You can add colors to a paletted image with the addcolors() method:
533
534 my @colors = ( Imager::Color->new(255, 0, 0),
535 Imager::Color->new(0, 255, 0) );
536 my $index = $img->addcolors(colors=>\@colors);
537
538 The return value is the index of the first color added, or undef if
539 adding the colors would overflow the palette.
540
541 The only parameter is C<colors> which must be a reference to an array
542 of Imager::Color objects.
543
544 =item setcolors()
545
546 $img->setcolors(start=>$start, colors=>\@colors);
547
548 Once you have colors in the palette you can overwrite them with the
549 C<setcolors()> method: C<setcolors()> returns true on success.
550
551 Parameters:
552
553 =over
554
555 =item *
556
557 start - the first index to be set. Default: 0
558
559 =item *
560
561 colors - reference to an array of Imager::Color objects.
562
563 =back
564
565 =item getcolors()
566
567 To retrieve existing colors from the palette use the getcolors() method:
568
569 # get the whole palette
570 my @colors = $img->getcolors();
571 # get a single color
572 my $color = $img->getcolors(start=>$index);
573 # get a range of colors
574 my @colors = $img->getcolors(start=>$index, count=>$count);
575
576 =item findcolor()
577
578 To quickly find a color in the palette use findcolor():
579
580 my $index = $img->findcolor(color=>$color);
581
582 which returns undef on failure, or the index of the color.
583
584 Parameter:
585
586 =over
587
588 =item *
589
590 color - an Imager::Color object.
591
592 =back
593
594 =item colorcount()
595
596 Returns the number of colors in the image's palette:
597
598 my $count = $img->colorcount;
599
600 =item maxcolors()
601
602 Returns the maximum size of the image's palette.
603
604 my $maxcount = $img->maxcolors;
605
606 =back
607
608 =head2 Color Distribution
609
610 =over
611
612 =item getcolorcount()
613
614 Calculates the number of colors in an image.
615
616 The amount of memory used by this is proportional to the number of
617 colors present in the image, so to avoid using too much memory you can
618 supply a maxcolors() parameter to limit the memory used.
619
620 Note: getcolorcount() treats the image as an 8-bit per sample image.
621
622 =over
623
624 =item *
625
626 X<C<maxcolors>!getcolorcount>C<maxcolors> - the maximum number of colors to
627 return. Default: unlimited.
628
629 =back
630
631 if (defined($img->getcolorcount(maxcolors=>512)) {
632 print "Less than 512 colors in image\n";
633 }
634
635 =item getcolorusagehash()
636
637 Calculates a histogram of colors used by the image.
638
639 =over
640
641 =item *
642
643 X<C<maxcolors>!getcolorusagehash>C<maxcolors> - the maximum number of colors
644 to return. Default: unlimited.
645
646 =back
647
648 Returns a reference to a hash where the keys are the raw color as
649 bytes, and the values are the counts for that color.
650
651 The alpha channel of the image is ignored. If the image is gray scale
652 then the hash keys will each be a single character.
653
654 my $colors = $img->getcolorusagehash;
655 my $blue_count = $colors->{pack("CCC", 0, 0, 255)} || 0;
656 print "#0000FF used $blue_count times\n";
657
658 =item getcolorusage()
659
660 Calculates color usage counts and returns just the counts.
661
662 =over
663
664 =item *
665
666 X<maxcolors!getcolorusage>C<maxcolors> - the maximum number of colors
667 to return. Default: unlimited.
668
669 =back
670
671 Returns a list of the color frequencies in ascending order.
672
673 my @counts = $img->getcolorusage;
674 print "The most common color is used $counts[0] times\n";
675
676 =back
677
678 =head2 Conversion Between Image Types
679
680 Warning: if you draw on a paletted image with colors that aren't in
681 the palette, the image will be internally converted to a normal image.
682
683 =over
684
685 =item to_paletted()
686
687 You can create a new paletted image from an existing image using the
688 to_paletted() method:
689
690 $palimg = $img->to_paletted(\%opts)
691
692 where %opts contains the options specified under L</Quantization options>.
693
694 # convert to a paletted image using the web palette
695 # use the closest color to each pixel
696 my $webimg = $img->to_paletted({ make_colors => 'webmap' });
697
698 # convert to a paletted image using a fairly optimal palette
699 # use an error diffusion dither to try to reduce the average error
700 my $optimag = $img->to_paletted({ make_colors => 'mediancut',
701 translate => 'errdiff' });
702
703 =item to_rgb8()
704
705 You can convert a paletted image (or any image) to an 8-bit/channel
706 RGB image with:
707
708 $rgbimg = $img->to_rgb8;
709
710 No parameters.
711
712 =item to_rgb16()
713
714 Convert a paletted image (or any image) to a 16-bit/channel RGB image.
715
716 $rgbimg = $img->to_rgb16;
717
718 No parameters.
719
720 =item to_rgb_double()
721
722 Convert a paletted image (or any image) to an double/channel direct
723 color image.
724
725 $rgbimg = $img->to_rgb_double;
726
727 No parameters.
728
729 =item masked()
730
731 Creates a masked image. A masked image lets you create an image proxy
732 object that protects parts of the underlying target image.
733
734 In the discussion below there are 3 image objects involved:
735
736 =over
737
738 =item *
739
740 the masked image - the return value of the masked() method. Any
741 writes to this image are written to the target image, assuming the
742 mask image allows it.
743
744 =item *
745
746 the mask image - the image that protects writes to the target image.
747 Supplied as the C<mask> parameter to the masked() method.
748
749 =item *
750
751 the target image - the image you called the masked() method on. Any
752 writes to the masked image end up on this image.
753
754 =back
755
756 In terms of code:
757
758 $masked = $target->masked(mask => $mask);
759
760 Parameters:
761
762 =over
763
764 =item *
765
766 mask - the mask image. If not supplied then all pixels in the target
767 image are writable. On each write to the masked image, only pixels
768 that have non-zero in channel 0 of the mask image will be written to
769 the original image. Default: none, if not supplied then no masking is
770 done, but the other parameters are still honored.
771
772 =item *
773
774 left, top - the offset of writes to the target image. eg. if you
775 attempt to set pixel (x,y) in the masked image, then pixel (x+left,
776 y+top) will be written to in the original image.
777
778 =item *
779
780 bottom, right - the bottom right of the area in the target available
781 from the masked image.
782
783 =back
784
785 If C<left> or C<right> is negative, they are counted from the right of
786 the target image.
787
788 If C<top> or C<bottom> is negative, they are counted from the bottom
789 of the target image.
790
791 Masked images let you control which pixels are modified in an
792 underlying image. Where the first channel is completely black in the
793 mask image, writes to the underlying image are ignored.
794
795 For example, given a base image called $img:
796
797 my $mask = Imager->new(xsize=>$img->getwidth, ysize=>$img->getheight,
798 channels=>1);
799 # ... draw something on the mask
800 my $maskedimg = $img->masked(mask=>$mask);
801
802 # now draw on $maskedimg and it will only draw on areas of $img
803 # where $mask is non-zero in channel 0.
804
805 You can specify the region of the underlying image that is masked
806 using the left, top, right and bottom options.
807
808 If you just want a subset of the image, without masking, just specify
809 the region without specifying a mask. For example:
810
811 # just work with a 100x100 region of $img
812 my $maskedimg = $img->masked(left => 100, top=>100,
813 right=>200, bottom=>200);
814
815 =item make_palette()
816
817 This doesn't perform an image conversion, but it can be used to
818 construct a common palette for use in several images:
819
820 my @colors = Imager->make_palette(\%opts, @images);
821
822 You must supply at least one image, even if the C<make_colors>
823 parameter produces a fixed palette.
824
825 On failure returns no colors and you can check C<< Imager->errstr >>.
826
827 =back
828
829 =head2 Tags
830
831 Image tags contain meta-data about the image, ie. information not
832 stored as pixels of the image.
833
834 At the perl level each tag has a name or code and a value, which is an
835 integer or an arbitrary string. An image can contain more than one
836 tag with the same name or code, but having more than one tag with the
837 same name is discouraged.
838
839 You can retrieve tags from an image using the tags() method, you can
840 get all of the tags in an image, as a list of array references, with
841 the code or name of the tag followed by the value of the tag.
842
843 Imager's support for fairly limited, for access to pretty much all
844 image metadata you may want to try L<Image::ExifTool>.
845
846 =over
847
848 =item tags()
849
850 Retrieve tags from the image.
851
852 With no parameters, retrieves a list array references, each containing
853 a name and value: all tags in the image:
854
855 # get a list of ( [ name1 => value1 ], [ name2 => value2 ] ... )
856 my @alltags = $img->tags;
857 print $_->[0], ":", $_->[1], "\n" for @all_tags;
858
859 # or put it in a hash, but this will lose duplicates
860 my %alltags = map @$_, $img->tags;
861
862 in scalar context this returns the number of tags:
863
864 my $num_tags = $img->tags;
865
866 or you can get all tags values for the given name:
867
868 my @namedtags = $img->tags(name => $name);
869
870 in scalar context this returns the first tag of that name:
871
872 my $firstnamed = $img->tags(name => $name);
873
874 or a given code:
875
876 my @tags = $img->tags(code=>$code);
877
878 =item addtag()
879
880 You can add tags using the addtag() method, either by name:
881
882 my $index = $img->addtag(name=>$name, value=>$value);
883
884 or by code:
885
886 my $index = $img->addtag(code=>$code, value=>$value);
887
888 Setting tags by C<code> is deprecated. If you have a use for this
889 please open an issue.
890
891 =item deltag()
892
893 You can remove tags with the deltag() method, either by index:
894
895 $img->deltag(index=>$index);
896
897 or by name:
898
899 $img->deltag(name=>$name);
900
901 or by code:
902
903 $img->deltag(code=>$code);
904
905 In each case deltag() returns the number of tags deleted.
906
907 Setting or deleting tags by C<code> is deprecated. If you have a use
908 for this please open an issue.
909
910 =item settag()
911
912 settag() replaces any existing tags with a new tag. This is
913 equivalent to calling deltag() then addtag().
914
915 =back
916
917 =head2 Common Tags
918
919 Many tags are only meaningful for one format. GIF looping information
920 is pretty useless for JPEG for example. Thus, many tags are set by
921 only a single reader or used by a single writer. For a complete list
922 of format specific tags see L<Imager::Files>.
923
924 Since tags are a relatively new addition their use is not wide spread
925 but eventually we hope to have all the readers for various formats set
926 some standard information.
927
928 =over
929
930 =item *
931
932 X<i_xres tag> X<i_yres tag> X<tags, i_xres> X<tags, i_yres> C<i_xres>, C<i_yres>
933 - The spatial resolution of the image in pixels per inch. If the
934 image format uses a different scale, eg. pixels per meter, then this
935 value is converted. A floating point number stored as a string.
936
937 # our image was generated as a 300 dpi image
938 $img->settag(name => 'i_xres', value => 300);
939 $img->settag(name => 'i_yres', value => 300);
940
941 # 100 pixel/cm for a TIFF image
942 $img->settag(name => 'tiff_resolutionunit', value => 3); # RESUNIT_CENTIMETER
943 # convert to pixels per inch, Imager will convert it back
944 $img->settag(name => 'i_xres', value => 100 * 2.54);
945 $img->settag(name => 'i_yres', value => 100 * 2.54);
946
947 =item *
948
949 X<i_aspect_only tag> X<tags, i_aspect_only> C<i_aspect_only> - If this is
950 non-zero then the values in i_xres and i_yres are treated as a ratio
951 only. If the image format does not support aspect ratios then this is
952 scaled so the smaller value is 72 DPI.
953
954 =item *
955
956 X<i_incomplete tag> X<tags, i_incomplete> C<i_incomplete> - If this tag is
957 present then the whole image could not be read. This isn't
958 implemented for all images yet, and may not be.
959
960 =item *
961
962 X<i_lines_read tag> X<tags, i_lines_read> C<i_lines_read> - If
963 C<i_incomplete> is set then this tag may be set to the number of
964 scan lines successfully read from the file. This can be used to decide
965 whether an image is worth processing.
966
967 =item *
968
969 X<i_format tag> X<tags, i_format>i_format - The file format this file
970 was read from.
971
972 =item *
973
974 X<i_background> X<tags, i_background> i_background - used when writing
975 an image with an alpha channel to a file format that doesn't support
976 alpha channels. The C<write> method will convert a normal color
977 specification like "#FF0000" into a color object for you, but if you
978 set this as a tag you will need to format it like
979 C<color(>I<red>C<,>I<green>C<,>I<blue>C<)>, eg color(255,0,0).
980
981 =item *
982
983 X<i_comment>C<i_comment> - used when reading or writing several image
984 formats. If the format has only one text field it will be read into
985 the C<i_comment> tag or written to the file.
986
987 =back
988
989 =head2 Quantization options
990
991 These options can be specified when calling
992 L<Imager::ImageTypes/to_paletted()>, write_multi() for GIF files, when
993 writing a single image with the C<gifquant> option set to C<gen>, or for
994 direct calls to i_writegif_gen() and i_writegif_callback().
995
996 =over
997
998 =item *
999
1000 C<colors> - An arrayref of colors that are fixed. Note that some
1001 color generators will ignore this. If this is supplied it will be
1002 filled with the color table generated for the image.
1003
1004 =item *
1005
1006 C<transp> - The type of transparency processing to perform for images
1007 with an alpha channel where the output format does not have a proper
1008 alpha channel (eg. GIF). This can be any of:
1009
1010 =over
1011
1012 =item *
1013
1014 C<none> - No transparency processing is done. (default)
1015
1016 =item *
1017
1018 C<threshold> - pixels more transparent than C<tr_threshold> are
1019 rendered as transparent.
1020
1021 =item *
1022
1023 C<errdiff> - An error diffusion dither is done on the alpha channel.
1024 Note that this is independent of the translation performed on the
1025 color channels, so some combinations may cause undesired artifacts.
1026
1027 =item *
1028
1029 C<ordered> - the ordered dither specified by tr_orddith is performed
1030 on the alpha channel.
1031
1032 =back
1033
1034 This will only be used if the image has an alpha channel, and if there
1035 is space in the palette for a transparency color.
1036
1037 =item *
1038
1039 C<tr_threshold> - the highest alpha value at which a pixel will be
1040 made transparent when C<transp> is 'threshold'. (0-255, default 127)
1041
1042 =item *
1043
1044 C<tr_errdiff> - The type of error diffusion to perform on the alpha
1045 channel when C<transp> is C<errdiff>. This can be any defined error
1046 diffusion type except for custom (see C<errdiff> below).
1047
1048 =item *
1049
1050 C<tr_orddith> - The type of ordered dither to perform on the alpha
1051 channel when C<transp> is 'ordered'. Possible values are:
1052
1053 =over
1054
1055 =item *
1056
1057 C<random> - A semi-random map is used. The map is the same each time.
1058
1059 =item *
1060
1061 C<dot8> - 8x8 dot dither.
1062
1063 =item *
1064
1065 C<dot4> - 4x4 dot dither
1066
1067 =item *
1068
1069 C<hline> - horizontal line dither.
1070
1071 =item *
1072
1073 C<vline> - vertical line dither.
1074
1075 =item *
1076
1077 C</line>, C<slashline> - diagonal line dither
1078
1079 =item *
1080
1081 C<\line>, C<backline> - diagonal line dither
1082
1083 =item *
1084
1085 C<tiny> - dot matrix dither (currently the default). This is probably
1086 the best for displays (like web pages).
1087
1088 =item *
1089
1090 C<custom> - A custom dither matrix is used - see C<tr_map>.
1091
1092 =back
1093
1094 =item *
1095
1096 C<tr_map> - When tr_orddith is custom this defines an 8 x 8 matrix of
1097 integers representing the transparency threshold for pixels
1098 corresponding to each position. This should be a 64 element array
1099 where the first 8 entries correspond to the first row of the matrix.
1100 Values should be between 0 and 255.
1101
1102 =item *
1103
1104 C<make_colors> - Defines how the quantization engine will build the
1105 palette(s). Currently this is ignored if C<translate> is C<giflib>,
1106 but that may change. Possible values are:
1107
1108 =over
1109
1110 =item *
1111
1112 C<none> - only colors supplied in 'colors' are used.
1113
1114 =item *
1115
1116 C<webmap> - the web color map is used (need URL here.)
1117
1118 =item *
1119
1120 C<addi> - The original code for generating the color map (Addi's code) is
1121 used.
1122
1123 =item *
1124
1125 C<mediancut> - Uses a median-cut algorithm, faster than C<addi>, but not
1126 as good a result.
1127
1128 =item *
1129
1130 C<mono>, C<monochrome> - a fixed black and white palette, suitable for
1131 producing bi-level images (eg. facsimile)
1132
1133 =item *
1134
1135 C<gray>, C<gray4>, C<gray16> - make fixed gray palette with 256, 4 or
1136 16 entries respectively.
1137
1138 =back
1139
1140 Other methods may be added in the future.
1141
1142 =item *
1143
1144 C<colors> - an arrayref containing Imager::Color objects, which
1145 represents the starting set of colors to use in translating the
1146 images. C<webmap> will ignore this. On return the final colors used
1147 are copied back into this array (which is expanded if necessary.)
1148
1149 =item *
1150
1151 C<max_colors> - the maximum number of colors to use in the image.
1152
1153 =item *
1154
1155 C<translate> - The method used to translate the RGB values in the
1156 source image into the colors selected by make_colors. Note that
1157 make_colors is ignored when C<translate> is C<giflib>.
1158
1159 Possible values are:
1160
1161 =over
1162
1163 =item *
1164
1165 C<giflib> - this is a historical equivalent for C<closest> that also
1166 forces C<make_colors> to C<mediancut>.
1167
1168 =item *
1169
1170 C<closest> - the closest color available is used.
1171
1172 =item *
1173
1174 C<perturb> - the pixel color is modified by C<perturb>, and the
1175 closest color is chosen.
1176
1177 =item *
1178
1179 C<errdiff> - an error diffusion dither is performed. If the supplied
1180 (or generated) palette contains only grays the source colors are
1181 converted to gray before error diffusion is performed.
1182
1183 =back
1184
1185 It's possible other C<translate> values will be added.
1186
1187 =item *
1188
1189 C<errdiff> - The type of error diffusion dither to perform. These
1190 values (except for custom) can also be used in tr_errdif.
1191
1192 =for stopwords Floyd-Steinberg Jarvis Judice Ninke Stucki
1193
1194 =over
1195
1196 =item *
1197
1198 C<floyd> - Floyd-Steinberg dither
1199
1200 =item *
1201
1202 C<jarvis> - Jarvis, Judice and Ninke dither
1203
1204 =item *
1205
1206 C<stucki> - Stucki dither
1207
1208 =item *
1209
1210 C<custom> - custom. If you use this you must also set C<errdiff_width>,
1211 C<errdiff_height> and C<errdiff_map>.
1212
1213 =back
1214
1215 =item *
1216
1217 C<errdiff_width>, C<errdiff_height>, C<errdiff_orig>, C<errdiff_map> -
1218 When C<translate> is C<errdiff> and C<errdiff> is C<custom> these
1219 define a custom error diffusion map. C<errdiff_width> and
1220 C<errdiff_height> define the size of the map in the arrayref in
1221 C<errdiff_map>. C<errdiff_orig> is an integer which indicates the
1222 current pixel position in the top row of the map.
1223
1224 =item *
1225
1226 C<perturb> - When translate is C<perturb> this is the magnitude of the
1227 random bias applied to each channel of the pixel before it is looked
1228 up in the color table.
1229
1230 =back
1231
1232 =head1 INITIALIZATION
1233
1234 This documents the Imager initialization function, which you will
1235 almost never need to call.
1236
1237 =over
1238
1239 =item init()
1240
1241 This is a function, not a method.
1242
1243 This function is a mess, it can take the following named parameters:
1244
1245 =over
1246
1247 =item *
1248
1249 C<log> - name of a log file to log Imager's actions to. Not all
1250 actions are logged, but the debugging memory allocator does log
1251 allocations here. Ignored if Imager has been built without logging
1252 support. Preferably use the open_log() method instead.
1253
1254 =item *
1255
1256 C<loglevel> - the maximum level of message to log. Default: 1.
1257
1258 =item *
1259
1260 C<warn_obsolete> - if this is non-zero then Imager will warn when you
1261 attempt to use obsoleted parameters or functionality. This currently
1262 only includes the old GIF output options instead of tags.
1263
1264 =item *
1265
1266 C<t1log> - if non-zero then T1lib will be configured to produce a log
1267 file. This will fail if there are any existing T1lib font objects.
1268
1269 =back
1270
1271 Example:
1272
1273 Imager::init(log => 'trace.log', loglevel => 9);
1274
1275 =back
1276
1277 =head1 LOGGING METHODS
1278
1279 Imager can open an internal log to send debugging information to.
1280 This log is extensively used in Imager's tests, but you're unlikely to
1281 use it otherwise.
1282
1283 If Imager has been built with logging disabled, the methods fail
1284 quietly.
1285
1286 =over
1287
1288 =item open_log()
1289
1290 Open the Imager debugging log file.
1291
1292 =over
1293
1294 =item *
1295
1296 C<log> - the file name to log to. If this is undef logging information
1297 is sent to the standard error stream.
1298
1299 =item *
1300
1301 C<loglevel> the level of logging to produce. Default: 1.
1302
1303 =back
1304
1305 Returns a true value if the log file was opened successfully.
1306
1307 # send debug output to test.log
1308 Imager->open_log(log => "test.log");
1309
1310 # send debug output to stderr
1311 Imager->open_log();
1312
1313 =item close_log()
1314
1315 Close the Imager debugging log file and disable debug logging.
1316
1317 No parameters.
1318
1319 Imager->close_log();
1320
1321 =item log()
1322
1323 Imager->log($message)
1324 Imager->log($message, $level)
1325
1326 This method does not use named parameters.
1327
1328 The default for C<$level> is 1.
1329
1330 Send a message to the debug log.
1331
1332 Imager->log("My code got here!");
1333
1334 =item is_logging()
1335
1336 Returns a true value if logging is enabled.
1337
1338 =back
1339
1340 =head1 AUTHOR
1341
1342 Tony Cook <[email protected]>, Arnar M. Hrafnkelsson
1343
1344 =head1 SEE ALSO
1345
1346 Imager(3), Imager::Files(3), Imager::Draw(3),
1347 Imager::Color(3), Imager::Fill(3), Imager::Font(3),
1348 Imager::Transformations(3), Imager::Engines(3), Imager::Filters(3),
1349 Imager::Expr(3), Imager::Matrix2d(3), Imager::Fountain(3)
1350
1351 =cut