]> git.imager.perl.org - imager.git/blob - lib/Imager/ImageTypes.pod
bfc7cd470c8723065eafa5df18c14dc52185dc3a
[imager.git] / lib / Imager / ImageTypes.pod
1 =head1 NAME
2
3 Imager::ImageTypes - Internal image representation information
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 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.
87
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).
90
91 To query an existing image about it's parameters see the C<bits()>,
92 C<type()>, C<getwidth()>, C<getheight()>, C<getchannels()> and
93 C<virtual()> methods.
94
95 The coordinate system in Imager has the origin in the upper left
96 corner, see L<Imager::Draw> for details.
97
98 =head2 Creating Imager Objects
99
100 =over
101
102 =item new
103
104   $img = Imager->new();
105   $img->read(file=>"alligator.ppm") or die $img->errstr;
106
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()
109 method on later.
110
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
114
115 The parameters for new are:
116
117 =over
118
119 =item *
120
121 C<xsize>, C<ysize> - Defines the width and height in pixels of the
122 image.  These must be positive.
123
124 If not supplied then only placeholder object is created, which can be
125 supplied to the C<read()> or C<img_set()> methods.
126
127 =item *
128
129 C<channels> - The number of channels for the image.  Default 3.  Valid
130 values are from 1 to 4.
131
132 =item *
133
134 C<bits> - The storage type for samples in the image.  Default: 8.
135 Valid values are:
136
137 =over
138
139 =item *
140
141 C<8> - One byte per sample.  256 discrete values.
142
143 =item *
144
145 C<16> - 16-bits per sample, 65536 discrete values.
146
147 =item *
148
149 C<double> - one C double per sample.
150
151 =back
152
153 Note: you can use any Imager function on any sample size image.
154
155 Paletted images always use 8 bits/sample.
156
157 =item *
158
159 C<type> - either C<'direct'> or C<'paletted'>.  Default: C<'direct'>.
160
161 Direct images store color values for each pixel.  
162
163 Paletted images keep a table of up to 256 colors called the palette,
164 each pixel is represented as an index into that table.
165
166 In most cases when working with Imager you will want to use the
167 C<direct> image type.
168
169 If you draw on a C<paletted> image with a color not in the image's
170 palette then Imager will transparently convert it to a C<direct>
171 image.
172
173 =item *
174
175 C<maxcolors> - the maximum number of colors in a paletted image.
176 Default: 256.  This must be in the range 1 through 256.
177
178 =back
179
180 In the simplest case just supply the width and height of the image:
181
182   # 8 bit/sample, RGB image
183   my $img = Imager->new(xsize => $width, ysize => $height);
184
185 or if you want an alpha channel:
186
187   # 8 bits/sample, RGBA image
188   my $img = Imager->new(xsize => $width, ysize => $height, channels=>4);
189
190 Note that it I<is> possible for image creation to fail, for example if
191 channels is out of range, or if the image would take too much memory.
192
193 To create paletted images, set the 'type' parameter to 'paletted':
194
195   $img = Imager->new(xsize=>200, ysize=>200, type=>'paletted');
196
197 which creates an image with a maxiumum of 256 colors, which you can
198 change by supplying the C<maxcolors> parameter.
199
200 For improved color precision you can use the bits parameter to specify
201 16 bit per channel:
202
203   $img = Imager->new(xsize=>200, ysize=>200,
204                      channels=>3, bits=>16);
205
206 or for even more precision:
207
208   $img = Imager->new(xsize=>200, ysize=>200,
209                      channels=>3, bits=>'double');
210
211 to get an image that uses a double for each channel.
212
213 Note that as of this writing all functions should work on images with
214 more than 8-bits/channel, but many will only work at only
215 8-bit/channel precision.
216
217 If you want an empty Imager object to call the read() method on, just
218 call new() with no parameters:
219
220   my $img = Imager->new;
221   $img->read(file=>$filename)
222     or die $img->errstr;
223
224 =item img_set
225
226 img_set destroys the image data in the object and creates a new one
227 with the given dimensions and channels.  For a way to convert image
228 data between formats see the C<convert()> method.
229
230   $img->img_set(xsize=>500, ysize=>500, channels=>4);
231
232 This takes exactly the same parameters as the new() method.
233
234 =back
235
236 =head2 Getting Information About an Imager Object
237
238 =over
239
240 =item getwidth
241
242   print "Image width: ", $img->getwidth(), "\n";
243
244 The C<getwidth()> method returns the width of the image.  This value
245 comes either from C<new()> with xsize,ysize parameters or from reading
246 data from a file with C<read()>.  If called on an image that has no
247 valid data in it like C<Imager-E<gt>new()> returns, the return value
248 of C<getwidth()> is undef.
249
250 =item getheight
251
252   print "Image height: ", $img->getheight(), "\n";
253
254 Same details apply as for L<getwidth>.
255
256 =item getchannels
257
258   print "Image has ",$img->getchannels(), " channels\n";
259
260 To get the number of channels in an image C<getchannels()> is used.
261
262
263 =item getcolorcount
264
265 It is possible to have Imager find the number of colors in an image by
266 with the C<getcolorcount()> method. It requires memory proportionally
267 to the number of colors in the image so it is possible to have it stop
268 sooner if you only need to know if there are more than a certain
269 number of colors in the image.  If there are more colors than asked
270 for the function return undef.  Examples:
271
272   if (defined($img->getcolorcount(maxcolors=>512)) {
273     print "Less than 512 colors in image\n";
274   }
275
276
277 =item bits
278
279 The bits() method retrieves the number of bits used to represent each
280 channel in a pixel, 8 for a normal image, 16 for 16-bit image and
281 'double' for a double/channel image.
282
283   if ($img->bits eq 8) {
284     # fast but limited to 8-bits/sample
285   }
286   else {
287     # slower but more precise
288   }
289
290 =item type
291
292 The type() method returns either 'direct' for truecolor images or
293 'paletted' for paletted images.
294
295   if ($img->type eq 'paletted') {
296     # print the palette
297     for my $color ($img->getcolors) {
298       print join(",", $color->rgba), "\n";
299     }
300   }
301
302 =item virtual
303
304 The virtual() method returns non-zero if the image contains no actual
305 pixels, for example masked images.
306
307 This may also be used for non-native Imager images in the future, for
308 example, for an Imager object that draws on an SDL surface.
309
310 =back
311
312 =head2 Direct Type Images
313
314 Direct images store the color value directly for each pixel in the
315 image.
316
317 =over
318
319 =item getmask
320
321   @rgbanames = qw( red green blue alpha );
322   my $mask = $img->getmask();
323   print "Modifiable channels:\n";
324   for (0..$img->getchannels()-1) {
325     print $rgbanames[$_],"\n" if $mask & 1<<$_;
326   }
327
328 C<getmask()> is used to fetch the current channel mask.  The mask
329 determines what channels are currently modifiable in the image.  The
330 channel mask is an integer value, if the i-th lsb is set the i-th
331 channel is modifiable.  eg. a channel mask of 0x5 means only channels
332 0 and 2 are writable.
333
334 =item setmask
335
336   $mask = $img->getmask();
337   $img->setmask(mask=>8);     # modify alpha only
338
339     ...
340
341   $img->setmask(mask=>$mask); # restore previous mask
342
343 C<setmask()> is used to set the channel mask of the image.  See
344 L<getmask> for details.
345
346 =back
347
348 =head2 Palette Type Images
349
350 Paletted images keep an array of up to 256 colors, and each pixel is
351 stored as an index into that array.
352
353 In general you can work with paletted images in the same way as RGB
354 images, except that if you attempt to draw to a paletted image with a
355 color that is not in the image's palette, the image will be converted
356 to an RGB image.  This means that drawing on a paletted image with
357 anti-aliasing enabled will almost certainly convert the image to RGB.
358
359 Palette management takes place through C<addcolors()>, C<setcolors()>,
360 C<getcolors()> and C<findcolor()>:
361
362 =over
363
364 =item addcolors
365
366 You can add colors to a paletted image with the addcolors() method:
367
368    my @colors = ( Imager::Color->new(255, 0, 0), 
369                   Imager::Color->new(0, 255, 0) );
370    my $index = $img->addcolors(colors=>\@colors);
371
372 The return value is the index of the first color added, or undef if
373 adding the colors would overflow the palette.
374
375 The only parameter is C<colors> which must be a reference to an array
376 of Imager::Color objects.
377
378 =item setcolors
379
380   $img->setcolors(start=>$start, colors=>\@colors);
381
382 Once you have colors in the palette you can overwrite them with the
383 C<setcolors()> method:  C<setcolors()> returns true on success.
384
385 Parameters:
386
387 =over
388
389 =item *
390
391 start - the first index to be set.  Default: 0
392
393 =item *
394
395 colors - reference to an array of Imager::Color objects.
396
397 =back
398
399 =item getcolors
400
401 To retrieve existing colors from the palette use the getcolors() method:
402
403   # get the whole palette
404   my @colors = $img->getcolors();
405   # get a single color
406   my $color = $img->getcolors(start=>$index);
407   # get a range of colors
408   my @colors = $img->getcolors(start=>$index, count=>$count);
409
410 =item findcolor
411
412 To quickly find a color in the palette use findcolor():
413
414   my $index = $img->findcolor(color=>$color);
415
416 which returns undef on failure, or the index of the color.
417
418 Parameter:
419
420 =over
421
422 =item *
423
424 color - an Imager::Color object.
425
426 =back
427
428 =item colorcount
429
430 Returns the number of colors in the image's palette:
431
432   my $count = $img->colorcount;
433
434 =item maxcolors
435
436 Returns the maximum size of the image's palette.
437
438   my $maxcount = $img->maxcolors;
439
440 =back
441
442 =head2 Conversion Between Image Types
443
444 Warning: if you draw on a paletted image with colors that aren't in
445 the palette, the image will be internally converted to a normal image.
446
447 =over
448
449 =item to_paletted
450
451 You can create a new paletted image from an existing image using the
452 to_paletted() method:
453
454  $palimg = $img->to_paletted(\%opts)
455
456 where %opts contains the options specified under L<Quantization options>.
457
458   # convert to a paletted image using the web palette
459   # use the closest color to each pixel
460   my $webimg = $img->to_paletted({ make_colors => 'webmap' });
461
462   # convert to a paletted image using a fairly optimal palette
463   # use an error diffusion dither to try to reduce the average error
464   my $optimag = $img->to_paletted({ make_colors => 'mediancut',
465                                     translate => 'errdiff' });
466
467 =item to_rgb8
468
469 You can convert a paletted image (or any image) to an 8-bit/channel
470 RGB image with:
471
472   $rgbimg = $img->to_rgb8;
473
474 No parameters.
475
476 =item masked
477
478 Creates a masked image.  A masked image lets you create an image proxy
479 object that protects parts of the underlying target image.
480
481 In the discussion below there are 3 image objects involved:
482
483 =over
484
485 =item *
486
487 the masked image - the return value of the masked() method.  Any
488 writes to this image are written to the target image, assuming the
489 mask image allows it.
490
491 =item *
492
493 the mask image - the image that protects writes to the target image.
494 Supplied as the C<mask> parameter to the masked() method.
495
496 =item *
497
498 the target image - the image you called the masked() method on.  Any
499 writes to the masked image end up on this image.
500
501 =back
502
503 Parameters:
504
505 =over
506
507 =item *
508
509 mask - the mask image.  If not supplied then all pixels in the target
510 image are writable.  On each write to the masked image, only pixels
511 that have non-zero in chennel 0 of the mask image will be written to
512 the original image.  Default: none, if not supplied then no masking is
513 done, but the other parameters are still honored.
514
515 =item *
516
517 left, top - the offset of writes to the target image.  eg. if you
518 attempt to set pixel (x,y) in the masked image, then pixel (x+left,
519 y+top) will be written to in the original image.
520
521 =item *
522
523 bottom, right - the bottom right of the area in the target available
524 from the masked image.
525
526 =back
527
528 Masked images let you control which pixels are modified in an
529 underlying image.  Where the first channel is completely black in the
530 mask image, writes to the underlying image are ignored.
531
532 For example, given a base image called $img:
533
534   my $mask = Imager->new(xsize=>$img->getwidth, ysize=>$img->getheight,
535                          channels=>1);
536   # ... draw something on the mask
537   my $maskedimg = $img->masked(mask=>$mask);
538
539   # now draw on $maskedimg and it will only draw on areas of $img 
540   # where $mask is non-zero in channel 0.
541
542 You can specifiy the region of the underlying image that is masked
543 using the left, top, right and bottom options.
544
545 If you just want a subset of the image, without masking, just specify
546 the region without specifying a mask.  For example:
547
548   # just work with a 100x100 region of $img
549   my $maskedimg = $img->masked(left => 100, top=>100,
550                                right=>200, bottom=>200);
551
552 =back
553
554 =head2 Tags
555
556 Image tags contain meta-data about the image, ie. information not
557 stored as pixels of the image.
558
559 At the perl level each tag has a name or code and a value, which is an
560 integer or an arbitrary string.  An image can contain more than one
561 tag with the same name or code, but having more than one tag with the
562 same name is discouraged.
563
564 You can retrieve tags from an image using the tags() method, you can
565 get all of the tags in an image, as a list of array references, with
566 the code or name of the tag followed by the value of the tag.
567
568 =over
569
570 =item tags
571
572 Retrieve tags from the image.
573
574 With no parameters, retrieves a list array references, each containing
575 a name and value: all tags in the image:
576
577   # get a list of ( [ name1 => value1 ], [ name2 => value2 ] ... )
578   my @alltags = $img->tags;
579   print $_->[0], ":", $_->[1], "\n" for @all_tags;
580
581   # or put it in a hash, but this will lose duplicates
582   my %alltags = map @$_, $img->tags;
583
584 in scalar context this returns the number of tags:
585
586   my $num_tags = $img->tags;
587
588 or you can get all tags values for the given name:
589
590   my @namedtags = $img->tags(name => $name);
591
592 in scalar context this returns the first tag of that name:
593
594   my $firstnamed = $img->tags(name => $name);
595
596 or a given code:
597
598   my @tags = $img->tags(code=>$code);
599
600 =item addtag
601
602 You can add tags using the addtag() method, either by name:
603
604   my $index = $img->addtag(name=>$name, value=>$value);
605
606 or by code:
607
608   my $index = $img->addtag(code=>$code, value=>$value);
609
610 =item deltag
611
612 You can remove tags with the deltag() method, either by index:
613
614   $img->deltag(index=>$index);
615
616 or by name:
617
618   $img->deltag(name=>$name);
619
620 or by code:
621
622   $img->deltag(code=>$code);
623
624 In each case deltag() returns the number of tags deleted.
625
626 =back
627
628 =head2 Common Tags
629
630 Many tags are only meaningful for one format.  GIF looping information
631 is pretty useless for JPEG for example.  Thus, many tags are set by
632 only a single reader or used by a single writer.  For a complete list
633 of format specific tags see L<Imager::Files>.
634
635 Since tags are a relatively new addition their use is not wide spread
636 but eventually we hope to have all the readers for various formats set
637 some standard information.
638
639 =over
640
641 =item i_xres
642
643 =item i_yres
644
645 The spatial resolution of the image in pixels per inch.  If the image
646 format uses a different scale, eg. pixels per meter, then this value
647 is converted.  A floating point number stored as a string.
648
649 =item i_aspect_only
650
651 If this is non-zero then the values in i_xres and i_yres are treated
652 as a ratio only.  If the image format does not support aspect ratios
653 then this is scaled so the smaller value is 72dpi.
654
655 =item i_incomplete
656
657 If this tag is present then the whole image could not be read.  This
658 isn't implemented for all images yet, and may not be.
659
660 =item i_format
661
662 The file format this file was read from.
663
664 =back
665
666 =head2 Quantization options
667
668 These options can be specified when calling write_multi() for gif
669 files, when writing a single image with the gifquant option set to
670 'gen', or for direct calls to i_writegif_gen and i_writegif_callback.
671
672 =over
673
674 =item colors
675
676 A arrayref of colors that are fixed.  Note that some color generators
677 will ignore this.
678
679 =item transp
680
681 The type of transparency processing to perform for images with an
682 alpha channel where the output format does not have a proper alpha
683 channel (eg. gif).  This can be any of:
684
685 =over
686
687 =item none
688
689 No transparency processing is done. (default)
690
691 =item threshold
692
693 Pixels more transparent that tr_threshold are rendered as transparent.
694
695 =item errdiff
696
697 An error diffusion dither is done on the alpha channel.  Note that
698 this is independent of the translation performed on the colour
699 channels, so some combinations may cause undesired artifacts.
700
701 =item ordered
702
703 The ordered dither specified by tr_orddith is performed on the alpha
704 channel.
705
706 =back
707
708 This will only be used if the image has an alpha channel, and if there
709 is space in the palette for a transparency colour.
710
711 =item tr_threshold
712
713 The highest alpha value at which a pixel will be made transparent when
714 transp is 'threshold'. (0-255, default 127)
715
716 =item tr_errdiff
717
718 The type of error diffusion to perform on the alpha channel when
719 transp is 'errdiff'.  This can be any defined error diffusion type
720 except for custom (see errdiff below).
721
722 =item tr_orddith
723
724 The type of ordered dither to perform on the alpha channel when transp
725 is 'ordered'.  Possible values are:
726
727 =over
728
729 =item random
730
731 A semi-random map is used.  The map is the same each time.
732
733 =item dot8
734
735 8x8 dot dither.
736
737 =item dot4
738
739 4x4 dot dither
740
741 =item hline
742
743 horizontal line dither.
744
745 =item vline
746
747 vertical line dither.
748
749 =item "/line"
750
751 =item slashline
752
753 diagonal line dither
754
755 =item '\line'
756
757 =item backline
758
759 diagonal line dither
760
761 =item tiny
762
763 dot matrix dither (currently the default).  This is probably the best
764 for displays (like web pages).
765
766 =item custom
767
768 A custom dither matrix is used - see tr_map
769
770 =back
771
772 =item tr_map
773
774 When tr_orddith is custom this defines an 8 x 8 matrix of integers
775 representing the transparency threshold for pixels corresponding to
776 each position.  This should be a 64 element array where the first 8
777 entries correspond to the first row of the matrix.  Values should be
778 betweern 0 and 255.
779
780 =item make_colors
781
782 Defines how the quantization engine will build the palette(s).
783 Currently this is ignored if 'translate' is 'giflib', but that may
784 change.  Possible values are:
785
786 =over
787
788 =item none
789
790 Only colors supplied in 'colors' are used.
791
792 =item webmap
793
794 The web color map is used (need url here.)
795
796 =item addi
797
798 The original code for generating the color map (Addi's code) is used.
799
800 =item mediancut
801
802 Uses a mediancut algorithm, faster than 'addi', but not as good a
803 result.
804
805 =back
806
807 Other methods may be added in the future.
808
809 =item colors
810
811 A arrayref containing Imager::Color objects, which represents the
812 starting set of colors to use in translating the images.  webmap will
813 ignore this.  The final colors used are copied back into this array
814 (which is expanded if necessary.)
815
816 =item max_colors
817
818 The maximum number of colors to use in the image.
819
820 =item translate
821
822 The method used to translate the RGB values in the source image into
823 the colors selected by make_colors.  Note that make_colors is ignored
824 whene translate is 'giflib'.
825
826 Possible values are:
827
828 =over
829
830 =item giflib
831
832 The giflib native quantization function is used.
833
834 =item closest
835
836 The closest color available is used.
837
838 =item perturb
839
840 The pixel color is modified by perturb, and the closest color is chosen.
841
842 =item errdiff
843
844 An error diffusion dither is performed.
845
846 =back
847
848 It's possible other transate values will be added.
849
850 =item errdiff
851
852 The type of error diffusion dither to perform.  These values (except
853 for custom) can also be used in tr_errdif.
854
855 =over
856
857 =item floyd
858
859 Floyd-Steinberg dither
860
861 =item jarvis
862
863 Jarvis, Judice and Ninke dither
864
865 =item stucki
866
867 Stucki dither
868
869 =item custom
870
871 Custom.  If you use this you must also set errdiff_width,
872 errdiff_height and errdiff_map.
873
874 =back
875
876 =item errdiff_width
877
878 =item errdiff_height
879
880 =item errdiff_orig
881
882 =item errdiff_map
883
884 When translate is 'errdiff' and errdiff is 'custom' these define a
885 custom error diffusion map.  errdiff_width and errdiff_height define
886 the size of the map in the arrayref in errdiff_map.  errdiff_orig is
887 an integer which indicates the current pixel position in the top row
888 of the map.
889
890 =item perturb
891
892 When translate is 'perturb' this is the magnitude of the random bias
893 applied to each channel of the pixel before it is looked up in the
894 color table.
895
896 =back
897
898 =head1 REVISION
899
900 $Revision$
901
902 =head1 AUTHORS
903
904 Tony Cook, Arnar M. Hrafnkelsson
905
906 =head1 SEE ALSO
907
908 Imager(3), Imager::Files(3), Imager::Draw(3),
909 Imager::Color(3), Imager::Fill(3), Imager::Font(3),
910 Imager::Transformations(3), Imager::Engines(3), Imager::Filters(3),
911 Imager::Expr(3), Imager::Matrix2d(3), Imager::Fountain(3)
912
913 =cut