]> git.imager.perl.org - imager.git/blob - lib/Imager/ImageTypes.pod
8e679e6c06cdf965d5c552117dcfdc39ef91eb81
[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   
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]) {
61       my @res = $_->rgba();
62       print "(", join(", ", @res[0..$img->getchannels()-1]), ")\n";
63     }
64   }
65   
66   my @tags = $img->tags();
67   if (@tags) {
68     print "Tags:\n";
69     for(@tags) {
70       print shift @$_, ": ", join " ", @$_, "\n";
71     }
72   } else {
73     print "No tags in image\n";
74   }
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 To create paletted images, set the 'type' parameter to 'paletted':
116
117   $img = Imager->new(xsize=>200, ysize=>200, type=>'paletted');
118
119 which creates an image with a maxiumum of 256 colors, which you can
120 change by supplying the C<maxcolors> parameter.
121
122 For improved color precision you can use the bits parameter to specify
123 16 bit per channel:
124
125   $img = Imager->new(xsize=>200, ysize=>200,
126                      channels=>3, bits=>16);
127
128 or for even more precision:
129
130   $img = Imager->new(xsize=>200, ysize=>200,
131                      channels=>3, bits=>'double');
132
133 to get an image that uses a double for each channel.
134
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.
138
139 Currently only 8-bit, 16-bit, and double per channel image types are
140 available, this may change later.
141
142 =item img_set
143
144 If you have an existing image, use img_set() to change it's dimensions
145 - this will destroy any existing image data:
146
147   $img->img_set(xsize=>500, ysize=>500, channels=>4);
148
149 =back
150
151 =head2 Getting Information About an Imager Object
152
153 =over
154
155 =item getwidth
156
157   print "Image width: ", $img->getwidth(), "\n";
158
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-&gt;new()> returns, the return value of
163 C<getwidth()> is undef.
164
165 =item getheight
166
167   print "Image height: ", $img->getheight(), "\n";
168
169 Same details apply as for L<getwidth>.
170
171 =item getchannels
172
173   print "Image has ",$img->getchannels(), " channels\n";
174
175 To get the number of channels in an image C<getchannels()> is used.
176
177
178 =item getcolorcount
179
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:
186
187   if (!defined($img->getcolorcount(maxcolors=>512)) {
188     print "Less than 512 colors in image\n";
189   }
190
191
192 =item bits
193
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.
197
198 =item type
199
200 The type() method returns either 'direct' for truecolor images or
201 'paletted' for paletted images.  
202
203 =item virtual
204
205 The virtual() method returns non-zero if the image contains no actual
206 pixels, for example masked images.
207
208 =back
209
210
211 =head2 Direct Type Images
212
213 =over
214
215 =item getmask
216
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<<$_;
222   }
223
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.
228
229 =item setmask
230
231   $mask = $img->getmask();
232   $img->setmask(mask=>8);     # modify alpha only
233
234     ...
235
236   $img->setmask(mask=>$mask); # restore previous mask
237
238 C<setmask()> is used to set the channel mask of the image.  See
239 L<getmask> for details.
240
241 =back
242
243 =head2 Palette Type Images
244
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.
250
251 Palette management takes place through C<addcolors()>, C<setcolors()>,
252 C<getcolors()> and C<findcolor()>:
253
254 =over
255
256 =item addcolors
257
258 You can add colors to a paletted image with the addcolors() method:
259
260    my @colors = ( Imager::Color->new(255, 0, 0), 
261                   Imager::Color->new(0, 255, 0) );
262    my $index = $img->addcolors(colors=>\@colors);
263
264 The return value is the index of the first color added, or undef if
265 adding the colors would overflow the palette.
266
267 =item setcolors
268
269   $img->setcolors(start=>$start, colors=>\@colors);
270
271 Once you have colors in the palette you can overwrite them with the
272 C<setcolors()> method:  C<sercolors()> returns true on success.
273
274 =item getcolors
275
276 To retrieve existing colors from the palette use the getcolors() method:
277
278   # get the whole palette
279   my @colors = $img->getcolors();
280   # get a single color
281   my $color = $img->getcolors(start=>$index);
282   # get a range of colors
283   my @colors = $img->getcolors(start=>$index, count=>$count);
284
285 =item findcolor
286
287 To quickly find a color in the palette use findcolor():
288
289   my $index = $img->findcolor(color=>$color);
290
291 which returns undef on failure, or the index of the color.
292
293 You can get the current palette size with $img->colorcount, and the
294 maximum size of the palette with $img->maxcolors.
295
296 =back
297
298 =head2 Conversion Between Image Types
299
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.
302
303 =over
304
305 =item to_paletted
306
307 You can create a new paletted image from an existing image using the
308 to_paletted() method:
309
310  $palimg = $img->to_paletted(\%opts)
311
312 where %opts contains the options specified under L<Quantization options>.
313
314 =item to_rgb8
315
316 You can convert a paletted image (or any image) to an 8-bit/channel
317 RGB image with:
318
319   $rgbimg = $img->to_rgb8;
320
321
322 =head2 Masked Images
323
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.
327
328 For example, given a base image called $img:
329
330   my $mask = Imager->new(xsize=>$img->getwidth, ysize=>getheight,
331                          channels=>1);
332   # ... draw something on the mask
333   my $maskedimg = $img->masked(mask=>$mask);
334
335 You can specifiy the region of the underlying image that is masked
336 using the left, top, right and bottom options.
337
338 If you just want a subset of the image, without masking, just specify
339 the region without specifying a mask.
340
341
342
343
344
345
346
347 =head2 Tags
348
349 Image tags contain meta-data about the image, ie. information not
350 stored as pixels of the image.
351
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.
355
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:
359
360   my @alltags = $img->tags;
361
362 or you can get all tags that have a given name:
363
364   my @namedtags = $img->tags(name=>$name);
365
366 or a given code:
367
368   my @tags = $img->tags(code=>$code);
369
370 You can add tags using the addtag() method, either by name:
371
372   my $index = $img->addtag(name=>$name, value=>$value);
373
374 or by code:
375
376   my $index = $img->addtag(code=>$code, value=>$value);
377
378 You can remove tags with the deltag() method, either by index:
379
380   $img->deltag(index=>$index);
381
382 or by name:
383
384   $img->deltag(name=>$name);
385
386 or by code:
387
388   $img->deltag(code=>$code);
389
390 In each case deltag() returns the number of tags deleted.
391
392
393 =head2 Common Tags
394
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>.
399
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.
403
404 =over
405
406 =item i_xres
407
408 =item i_yres
409
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.
413
414 =item i_aspect_only
415
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.
419
420 =item i_incomplete
421
422 If this tag is present then the whole image could not be read.  This
423 isn't implemented for all images yet.
424
425 =back
426
427
428
429
430
431 =head2 Quantization options
432
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.
436
437 =over
438
439 =item colors
440
441 A arrayref of colors that are fixed.  Note that some color generators
442 will ignore this.
443
444 =item transp
445
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:
449
450 =over
451
452 =item none
453
454 No transparency processing is done. (default)
455
456 =item threshold
457
458 Pixels more transparent that tr_threshold are rendered as transparent.
459
460 =item errdiff
461
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.
465
466 =item ordered
467
468 The ordered dither specified by tr_orddith is performed on the alpha
469 channel.
470
471 =back
472
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.
475
476 =item tr_threshold
477
478 The highest alpha value at which a pixel will be made transparent when
479 transp is 'threshold'. (0-255, default 127)
480
481 =item tr_errdiff
482
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).
486
487 =item tr_orddith
488
489 The type of ordered dither to perform on the alpha channel when transp
490 is 'ordered'.  Possible values are:
491
492 =over
493
494 =item random
495
496 A semi-random map is used.  The map is the same each time.
497
498 =item dot8
499
500 8x8 dot dither.
501
502 =item dot4
503
504 4x4 dot dither
505
506 =item hline
507
508 horizontal line dither.
509
510 =item vline
511
512 vertical line dither.
513
514 =item "/line"
515
516 =item slashline
517
518 diagonal line dither
519
520 =item '\line'
521
522 =item backline
523
524 diagonal line dither
525
526 =item tiny
527
528 dot matrix dither (currently the default).  This is probably the best
529 for displays (like web pages).
530
531 =item custom
532
533 A custom dither matrix is used - see tr_map
534
535 =back
536
537 =item tr_map
538
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
543 betweern 0 and 255.
544
545 =item make_colors
546
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:
550
551 =over
552
553 =item none
554
555 Only colors supplied in 'colors' are used.
556
557 =item webmap
558
559 The web color map is used (need url here.)
560
561 =item addi
562
563 The original code for generating the color map (Addi's code) is used.
564
565 =item mediancut
566
567 Uses a mediancut algorithm, faster than 'addi', but not as good a
568 result.
569
570 =back
571
572 Other methods may be added in the future.
573
574 =item colors
575
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.)
580
581 =item max_colors
582
583 The maximum number of colors to use in the image.
584
585 =item translate
586
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'.
590
591 Possible values are:
592
593 =over
594
595 =item giflib
596
597 The giflib native quantization function is used.
598
599 =item closest
600
601 The closest color available is used.
602
603 =item perturb
604
605 The pixel color is modified by perturb, and the closest color is chosen.
606
607 =item errdiff
608
609 An error diffusion dither is performed.
610
611 =back
612
613 It's possible other transate values will be added.
614
615 =item errdiff
616
617 The type of error diffusion dither to perform.  These values (except
618 for custom) can also be used in tr_errdif.
619
620 =over
621
622 =item floyd
623
624 Floyd-Steinberg dither
625
626 =item jarvis
627
628 Jarvis, Judice and Ninke dither
629
630 =item stucki
631
632 Stucki dither
633
634 =item custom
635
636 Custom.  If you use this you must also set errdiff_width,
637 errdiff_height and errdiff_map.
638
639 =back
640
641 =item errdiff_width
642
643 =item errdiff_height
644
645 =item errdiff_orig
646
647 =item errdiff_map
648
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
653 of the map.
654
655 =item perturb
656
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
659 color table.
660
661 =back
662
663
664 =cut