]> git.imager.perl.org - imager.git/blob - lib/Imager/ImageTypes.pod
44190d148604fc2f5ad87c01b3f1f19c0ea11e2c
[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
206 if the image contains no actual pixels, for example masked images.
207
208
209 =back
210
211
212 =head2 Direct Type Images
213
214 =over
215
216 =item getmask
217
218   @rgbanames = qw( red green blue alpha );
219   my $mask = $img->getmask();
220   print "Modifiable channels:\n";
221   for (0..$img->getchannels()-1) {
222     print $rgbanames[$_],"\n" if $mask & 1<<$_;
223   }
224
225 C<getmask()> is used to fetch the current channel mask.  The mask
226 determines what channels are currently modifiable in the image.  The
227 channel mask is an integer value, if the i-th lsb is set the i-th
228 channel is modifiable.
229
230 =item setmask
231
232   $mask = $img->getmask();
233   $img->setmask(mask=>8);     # modify alpha only
234
235     ...
236
237   $img->setmask(mask=>$mask); # restore previous mask
238
239 C<setmask()> is used to set the channel mask of the image.  See
240 L<getmask> for details.
241
242 =back
243
244 =head2 Palette Type Images
245
246 In general you can work with paletted images in the same way as RGB
247 images, except that if you attempt to draw to a paletted image with a
248 color that is not in the image's palette, the image will be converted
249 to an RGB image.  This means that drawing on a paletted image with
250 anti-aliasing enabled will almost certainly convert the image to RGB.
251
252 Palette management takes place through C<addcolors()>, C<setcolors()>,
253 C<getcolors()> and C<findcolor()>:
254
255 =over
256
257 =item addcolors
258
259 You can add colors to a paletted image with the addcolors() method:
260
261    my @colors = ( Imager::Color->new(255, 0, 0), 
262                   Imager::Color->new(0, 255, 0) );
263    my $index = $img->addcolors(colors=>\@colors);
264
265 The return value is the index of the first color added, or undef if
266 adding the colors would overflow the palette.
267
268 =item setcolors
269
270   $img->setcolors(start=>$start, colors=>\@colors);
271
272 Once you have colors in the palette you can overwrite them with the
273 C<setcolors()> method:  C<sercolors()> returns true on success.
274
275 =item getcolors
276
277 To retrieve existing colors from the palette use the getcolors() method:
278
279   # get the whole palette
280   my @colors = $img->getcolors();
281   # get a single color
282   my $color = $img->getcolors(start=>$index);
283   # get a range of colors
284   my @colors = $img->getcolors(start=>$index, count=>$count);
285
286 =item findcolor
287
288 To quickly find a color in the palette use findcolor():
289
290   my $index = $img->findcolor(color=>$color);
291
292 which returns undef on failure, or the index of the color.
293
294 You can get the current palette size with $img->colorcount, and the
295 maximum size of the palette with $img->maxcolors.
296
297 =back
298
299 =head2 Conversion Between Image Types
300
301 Warning: if you draw on a paletted image with colors that aren't in
302 the palette, the image will be internally converted to a normal image.
303
304 =over
305
306 =item to_paletted
307
308 You can create a new paletted image from an existing image using the
309 to_paletted() method:
310
311  $palimg = $img->to_paletted(\%opts)
312
313 where %opts contains the options specified under L<Quantization options>.
314
315 =item to_rgb8
316
317 You can convert a paletted image (or any image) to an 8-bit/channel
318 RGB image with:
319
320   $rgbimg = $img->to_rgb8;
321
322
323 =head2 Masked Images
324
325 Masked images let you control which pixels are modified in an
326 underlying image.  Where the first channel is completely black in the
327 mask image, writes to the underlying image are ignored.
328
329 For example, given a base image called $img:
330
331   my $mask = Imager->new(xsize=>$img->getwidth, ysize=>getheight,
332                          channels=>1);
333   # ... draw something on the mask
334   my $maskedimg = $img->masked(mask=>$mask);
335
336 You can specifiy the region of the underlying image that is masked
337 using the left, top, right and bottom options.
338
339 If you just want a subset of the image, without masking, just specify
340 the region without specifying a mask.
341
342
343
344
345
346
347
348 =head2 Tags
349
350 Image tags contain meta-data about the image, ie. information not
351 stored as pixels of the image.
352
353 At the perl level each tag has a name or code and a value, which is an
354 integer or an arbitrary string.  An image can contain more than one
355 tag with the same name or code.
356
357 You can retrieve tags from an image using the tags() method, you can
358 get all of the tags in an image, as a list of array references, with
359 the code or name of the tag followed by the value of the tag:
360
361   my @alltags = $img->tags;
362
363 or you can get all tags that have a given name:
364
365   my @namedtags = $img->tags(name=>$name);
366
367 or a given code:
368
369   my @tags = $img->tags(code=>$code);
370
371 You can add tags using the addtag() method, either by name:
372
373   my $index = $img->addtag(name=>$name, value=>$value);
374
375 or by code:
376
377   my $index = $img->addtag(code=>$code, value=>$value);
378
379 You can remove tags with the deltag() method, either by index:
380
381   $img->deltag(index=>$index);
382
383 or by name:
384
385   $img->deltag(name=>$name);
386
387 or by code:
388
389   $img->deltag(code=>$code);
390
391 In each case deltag() returns the number of tags deleted.
392
393
394 =head2 Common Tags
395
396 Many tags are only meaningful for one format.  GIF looping information
397 is pretty useless for JPEG for example.  Thus, many tags are set by
398 only a single reader or used by a single writer.  For a complete list
399 of format specific tags see L<Imager::Files>.
400
401 Since tags are a relatively new addition their use is not wide spread
402 but eventually we hope to have all the readers for various formats set
403 some standard information.
404
405 =over
406
407 =item i_xres
408
409 =item i_yres
410
411 The spatial resolution of the image in pixels per inch.  If the image
412 format uses a different scale, eg. pixels per meter, then this value
413 is converted.  A floating point number stored as a string.
414
415 =item i_aspect_only
416
417 If this is non-zero then the values in i_xres and i_yres are treated
418 as a ratio only.  If the image format does not support aspect ratios
419 then this is scaled so the smaller value is 72dpi.
420
421 =item i_incomplete
422
423 If this tag is present then the whole image could not be read.  This
424 isn't implemented for all images yet.
425
426 =back
427
428
429
430
431
432 =head2 Quantization options
433
434 These options can be specified when calling write_multi() for gif
435 files, when writing a single image with the gifquant option set to
436 'gen', or for direct calls to i_writegif_gen and i_writegif_callback.
437
438 =over
439
440 =item colors
441
442 A arrayref of colors that are fixed.  Note that some color generators
443 will ignore this.
444
445 =item transp
446
447 The type of transparency processing to perform for images with an
448 alpha channel where the output format does not have a proper alpha
449 channel (eg. gif).  This can be any of:
450
451 =over
452
453 =item none
454
455 No transparency processing is done. (default)
456
457 =item threshold
458
459 Pixels more transparent that tr_threshold are rendered as transparent.
460
461 =item errdiff
462
463 An error diffusion dither is done on the alpha channel.  Note that
464 this is independent of the translation performed on the colour
465 channels, so some combinations may cause undesired artifacts.
466
467 =item ordered
468
469 The ordered dither specified by tr_orddith is performed on the alpha
470 channel.
471
472 =back
473
474 This will only be used if the image has an alpha channel, and if there
475 is space in the palette for a transparency colour.
476
477 =item tr_threshold
478
479 The highest alpha value at which a pixel will be made transparent when
480 transp is 'threshold'. (0-255, default 127)
481
482 =item tr_errdiff
483
484 The type of error diffusion to perform on the alpha channel when
485 transp is 'errdiff'.  This can be any defined error diffusion type
486 except for custom (see errdiff below).
487
488 =item tr_orddith
489
490 The type of ordered dither to perform on the alpha channel when transp
491 is 'ordered'.  Possible values are:
492
493 =over
494
495 =item random
496
497 A semi-random map is used.  The map is the same each time.
498
499 =item dot8
500
501 8x8 dot dither.
502
503 =item dot4
504
505 4x4 dot dither
506
507 =item hline
508
509 horizontal line dither.
510
511 =item vline
512
513 vertical line dither.
514
515 =item "/line"
516
517 =item slashline
518
519 diagonal line dither
520
521 =item '\line'
522
523 =item backline
524
525 diagonal line dither
526
527 =item tiny
528
529 dot matrix dither (currently the default).  This is probably the best
530 for displays (like web pages).
531
532 =item custom
533
534 A custom dither matrix is used - see tr_map
535
536 =back
537
538 =item tr_map
539
540 When tr_orddith is custom this defines an 8 x 8 matrix of integers
541 representing the transparency threshold for pixels corresponding to
542 each position.  This should be a 64 element array where the first 8
543 entries correspond to the first row of the matrix.  Values should be
544 betweern 0 and 255.
545
546 =item make_colors
547
548 Defines how the quantization engine will build the palette(s).
549 Currently this is ignored if 'translate' is 'giflib', but that may
550 change.  Possible values are:
551
552 =over
553
554 =item none
555
556 Only colors supplied in 'colors' are used.
557
558 =item webmap
559
560 The web color map is used (need url here.)
561
562 =item addi
563
564 The original code for generating the color map (Addi's code) is used.
565
566 =back
567
568 Other methods may be added in the future.
569
570 =item colors
571
572 A arrayref containing Imager::Color objects, which represents the
573 starting set of colors to use in translating the images.  webmap will
574 ignore this.  The final colors used are copied back into this array
575 (which is expanded if necessary.)
576
577 =item max_colors
578
579 The maximum number of colors to use in the image.
580
581 =item translate
582
583 The method used to translate the RGB values in the source image into
584 the colors selected by make_colors.  Note that make_colors is ignored
585 whene translate is 'giflib'.
586
587 Possible values are:
588
589 =over
590
591 =item giflib
592
593 The giflib native quantization function is used.
594
595 =item closest
596
597 The closest color available is used.
598
599 =item perturb
600
601 The pixel color is modified by perturb, and the closest color is chosen.
602
603 =item errdiff
604
605 An error diffusion dither is performed.
606
607 =back
608
609 It's possible other transate values will be added.
610
611 =item errdiff
612
613 The type of error diffusion dither to perform.  These values (except
614 for custom) can also be used in tr_errdif.
615
616 =over
617
618 =item floyd
619
620 Floyd-Steinberg dither
621
622 =item jarvis
623
624 Jarvis, Judice and Ninke dither
625
626 =item stucki
627
628 Stucki dither
629
630 =item custom
631
632 Custom.  If you use this you must also set errdiff_width,
633 errdiff_height and errdiff_map.
634
635 =back
636
637 =item errdiff_width
638
639 =item errdiff_height
640
641 =item errdiff_orig
642
643 =item errdiff_map
644
645 When translate is 'errdiff' and errdiff is 'custom' these define a
646 custom error diffusion map.  errdiff_width and errdiff_height define
647 the size of the map in the arrayref in errdiff_map.  errdiff_orig is
648 an integer which indicates the current pixel position in the top row
649 of the map.
650
651 =item perturb
652
653 When translate is 'perturb' this is the magnitude of the random bias
654 applied to each channel of the pixel before it is looked up in the
655 color table.
656
657 =back
658
659 =cut