]> git.imager.perl.org - imager.git/blob - lib/Imager/Files.pod
he unpack code for ICO/CUR file handling could extend 32-bit unsigned values to 64...
[imager.git] / lib / Imager / Files.pod
1 =head1 NAME
2
3 Imager::Files - working with image files
4
5 =head1 SYNOPSIS
6
7   use Imager;
8   my $img = ...;
9   $img->write(file=>$filename, type=>$type)
10     or die "Cannot write: ",$img->errstr;
11
12   # type is optional if we can guess the format from the filename
13   $img->write(file => "foo.png")
14     or die "Cannot write: ",$img->errstr;
15
16   $img = Imager->new;
17   $img->read(file=>$filename, type=>$type)
18     or die "Cannot read: ", $img->errstr;
19
20   # type is optional if we can guess the type from the file data
21   # and we normally can guess
22   $img->read(file => $filename)
23     or die "Cannot read: ", $img->errstr;
24
25   Imager->write_multi({ file=> $filename, ... }, @images)
26     or die "Cannot write: ", Imager->errstr;
27
28   my @imgs = Imager->read_multi(file=>$filename)
29     or die "Cannot read: ", Imager->errstr;
30
31   Imager->set_file_limits(width=>$max_width, height=>$max_height)
32
33   my @read_types = Imager->read_types;
34   my @write_types = Imager->write_types;
35
36   # we can write/write_multi to things other than filenames
37   my $data;
38   $img->write(data => \$data, type => $type) or die;
39
40   my $fh = ... ; # eg. IO::File
41   $img->write(fh => $fh, type => $type) or die;
42
43   $img->write(fd => fileno($fh), type => $type) or die;
44
45   # some file types need seek callbacks too
46   $img->write(callback => \&write_callback, type => $type) or die;
47
48   # and similarly for read/read_multi
49   $img->read(data => $data) or die;
50   $img->read(fh => $fh) or die;
51   $img->read(fd => fileno($fh)) or die;
52   $img->read(callback => \&read_callback) or die;
53
54   use Imager 0.68;
55   my $img = Imager->new(file => $filename)
56     or die Imager->errstr;
57
58 =head1 DESCRIPTION
59
60 You can read and write a variety of images formats, assuming you have
61 the appropriate libraries, and images can be read or written to/from
62 files, file handles, file descriptors, scalars, or through callbacks.
63
64 To see which image formats Imager is compiled to support the following
65 code snippet is sufficient:
66
67   use Imager;
68   print join " ", keys %Imager::formats;
69
70 This will include some other information identifying libraries rather
71 than file formats.  For new code you might find the L</read_types()>
72 or L</write_types()> methods useful.
73
74 =over 
75
76 =item read()
77
78 Reading writing to and from files is simple, use the C<read()>
79 method to read an image:
80
81   my $img = Imager->new;
82   $img->read(file=>$filename, type=>$type)
83     or die "Cannot read $filename: ", $img->errstr;
84
85 In most cases Imager can auto-detect the file type, so you can just
86 supply the file name:
87
88   $img->read(file => $filename)
89     or die "Cannot read $filename: ", $img->errstr;
90
91 The read() method accepts the C<allow_incomplete> parameter.  If this
92 is non-zero then read() can return true on an incomplete image and set
93 the C<i_incomplete> tag.
94
95 From Imager 0.68 you can supply most read() parameters to the new()
96 method to read the image file on creation.  If the read fails, check
97 Imager->errstr() for the cause:
98
99   use Imager 0.68;
100   my $img = Imager->new(file => $filename)
101     or die "Cannot read $filename: ", Imager->errstr;
102
103 =item write()
104
105 and the C<write()> method to write an image:
106
107   $img->write(file=>$filename, type=>$type)
108     or die "Cannot write $filename: ", $img->errstr;
109
110 =item read_multi()
111
112 If you're reading from a format that supports multiple images per
113 file, use the C<read_multi()> method:
114
115   my @imgs = Imager->read_multi(file=>$filename, type=>$type)
116     or die "Cannot read $filename: ", Imager->errstr;
117
118 As with the read() method, Imager will normally detect the C<type>
119 automatically.
120
121 =item write_multi()
122
123 and if you want to write multiple images to a single file use the
124 C<write_multi()> method:
125
126   Imager->write_multi({ file=> $filename, type=>$type }, @images)
127     or die "Cannot write $filename: ", Imager->errstr;
128
129 =item read_types()
130
131 This is a class method that returns a list of the image file types
132 that Imager can read.
133
134   my @types = Imager->read_types;
135
136 These types are the possible values for the C<type> parameter, not
137 necessarily the extension of the files you're reading.
138
139 It is possible for extra file read handlers to be loaded when
140 attempting to read a file, which may modify the list of available read
141 types.
142
143 =item write_types()
144
145 This is a class method that returns a list of the image file types
146 that Imager can write.
147
148   my @types = Imager->write_types;
149
150 Note that these are the possible values for the C<type> parameter, not
151 necessarily the extension of the files you're writing.
152
153 It is possible for extra file write handlers to be loaded when
154 attempting to write a file, which may modify the list of available
155 write types.
156
157 =back
158
159 When writing, if the C<filename> includes an extension that Imager
160 recognizes, then you don't need the C<type>, but you may want to
161 provide one anyway.  See L</Guessing types> for information on
162 controlling this recognition.
163
164 The C<type> parameter is a lowercase representation of the file type,
165 and can be any of the following:
166
167   bmp   Windows BitMaP (BMP)
168   gif   Graphics Interchange Format (GIF)
169   jpeg  JPEG/JFIF
170   png   Portable Network Graphics (PNG)
171   pnm   Portable aNyMap (PNM)
172   raw   Raw
173   sgi   SGI .rgb files
174   tga   TARGA
175   tiff  Tagged Image File Format (TIFF)
176
177 When you read an image, Imager may set some tags, possibly including
178 information about the spatial resolution, textual information, and
179 animation information.  See L<Imager::ImageTypes/Tags> for specifics.
180
181 The open() method is a historical alias for the read() method.
182
183 =head2 Input and output
184
185 When reading or writing you can specify one of a variety of sources or
186 targets:
187
188 =over
189
190 =item *
191
192 C<file> - The C<file> parameter is the name of the image file to be
193 written to or read from.  If Imager recognizes the extension of the
194 file you do not need to supply a C<type>.
195
196   # write in tiff format
197   $image->write(file => "example.tif")
198     or die $image->errstr;
199
200   $image->write(file => 'foo.tmp', type => 'tiff')
201     or die $image->errstr;
202
203   my $image = Imager->new;
204   $image->read(file => 'example.tif')
205     or die $image->errstr;
206
207 =item *
208
209 C<fh> - C<fh> is a file handle, typically either returned from
210 C<<IO::File->new()>>, or a glob from an C<open> call.  You should call
211 C<binmode> on the handle before passing it to Imager.
212
213 Imager will set the handle to autoflush to make sure any buffered data
214 is flushed , since Imager will write to the file descriptor (from
215 fileno()) rather than writing at the perl level.
216
217   $image->write(fh => \*STDOUT, type => 'gif')
218     or die $image->errstr;
219
220   # for example, a file uploaded via CGI.pm
221   $image->read(fd => $cgi->param('file')) 
222     or die $image->errstr;
223
224 =item *
225
226 C<fd> - C<fd> is a file descriptor.  You can get this by calling the
227 C<fileno()> function on a file handle, or by using one of the standard
228 file descriptor numbers.
229
230 If you get this from a perl file handle, you may need to flush any
231 buffered output, otherwise it may appear in the output stream after
232 the image.
233
234   $image->write(fd => file(STDOUT), type => 'gif')
235     or die $image->errstr;
236
237 =item *
238
239 C<data> - When reading data, C<data> is a scalar containing the image
240 file data, or a reference to such a scalar.  When writing, C<data> is
241 a reference to the scalar to save the image file data to.
242
243   my $data;
244   $image->write(data => \$data, type => 'tiff')
245     or die $image->errstr;
246
247   my $data = $row->{someblob}; # eg. from a database
248   my @images = Imager->read_multi(data => $data)
249     or die Imager->errstr;
250
251   # from Imager 0.99
252   my @images = Imager->read_multi(data => \$data)
253     or die Imager->errstr;
254
255 =item *
256
257 C<callback>, C<readcb>, C<writecb>, C<seekcb>, C<closecb> - Imager
258 will make calls back to your supplied coderefs to read, write and seek
259 from/to/through the image file.  See L</"I/O Callbacks"> below for details.
260
261 =item *
262
263 C<io> - an L<Imager::IO> object.
264
265 =back
266
267 X<buffering>X<unbuffered>By default Imager will use buffered I/O when
268 reading or writing an image.  You can disabled buffering for output by
269 supplying a C<< buffered => 0 >> parameter to C<write()> or
270 C<write_multi()>.
271
272 =head2 I/O Callbacks
273
274 When reading from a file you can use either C<callback> or C<readcb>
275 to supply the read callback, and when writing C<callback> or
276 C<writecb> to supply the write callback.
277
278 Whether reading or writing a C<TIFF> image, C<seekcb> and C<readcb>
279 are required.
280
281 If a file handler attempts to use C<readcb>, C<writecb> or C<seekcb>
282 and you haven't supplied one, the call will fail, failing the image
283 read or write, returning an error message indicating that the callback
284 is missing:
285
286   # attempting to read a TIFF image without a seekcb
287   open my $fh, "<", $filename or die;
288   my $rcb = sub {
289     my $val;
290     read($fh, $val, $_[0]) or return "";
291     return $val;
292   };
293   my $im = Imager->new(callback => $rcb)
294     or die Imager->errstr
295   # dies with (wrapped here):
296   # Error opening file: (Iolayer): Failed to read directory at offset 0:
297   # (Iolayer): Seek error accessing TIFF directory: seek callback called
298   # but no seekcb supplied
299
300 You can also provide a C<closecb> parameter called when writing the
301 file is complete.  If no C<closecb> is supplied the default will
302 succeed silently.
303
304   # contrived
305   my $data;
306   sub mywrite {
307     $data .= unpack("H*", shift);
308     1;
309   }
310   Imager->write_multi({ callback => \&mywrite, type => 'gif'}, @images)
311     or die Imager->errstr;
312
313 =head3 C<readcb>
314
315 The read callback is called with 2 parameters:
316
317 =over
318
319 =item *
320
321 C<size> - the minimum amount of data required.
322
323 =item *
324
325 C<maxsize> - previously this was the maximum amount of data returnable
326 - currently it's always the same as C<size>
327
328 =back
329
330 Your read callback should return the data as a scalar:
331
332 =over
333
334 =item *
335
336 on success, a string containing the bytes read.
337
338 =item *
339
340 on end of file, an empty string
341
342 =item *
343
344 on error, C<undef>.
345
346 =back
347
348 If your return value contains more data than C<size> Imager will
349 panic.
350
351 Your return value must not contain any characters over C<\xFF> or
352 Imager will panic.
353
354 =head3 C<writecb>
355
356 Your write callback takes exactly one parameter, a scalar containing
357 the data to be written.
358
359 Return true for success.
360
361 =head3 C<seekcb>
362
363 The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
364 defined in the same way as perl's seek function.
365
366 Previously you always needed a C<seekcb> callback if you called
367 Imager's L</read()> or L</read_multi()> without a C<type> parameter,
368 but this is no longer necessary unless the file handler requires
369 seeking, such as for TIFF files.
370
371 Returns the new position in the file, or -1 on failure.
372
373 =head3 C<closecb>
374
375 You can also supply a C<closecb> which is called with no parameters
376 when there is no more data to be written.  This could be used to flush
377 buffered data.
378
379 Return true on success.
380
381 =head2 Guessing types
382 X<FORMATGUESS>
383
384 When writing to a file, if you don't supply a C<type> parameter Imager
385 will attempt to guess it from the file name.  This is done by calling
386 the code reference stored in C<$Imager::FORMATGUESS>.  This is only
387 done when write() or write_multi() is called with a C<file> parameter,
388 or if read() or read_multi() can't determine the type from the file's
389 header.
390
391 The default function value of C<$Imager::FORMATGUESS> is
392 C<\&Imager::def_guess_type>.
393
394 =over
395
396 =item def_guess_type()
397 X<methods, def_guess_type()>
398
399 This is the default function Imager uses to derive a file type from a
400 file name.  This is a function, not a method.
401
402 Accepts a single parameter, the file name and returns the type or
403 undef.
404
405 =back
406
407 You can replace function with your own implementation if you have some
408 specialized need.  The function takes a single parameter, the name of
409 the file, and should return either a file type or under.
410
411   # I'm writing jpegs to weird filenames
412   local $Imager::FORMATGUESS = sub { 'jpeg' };
413
414 When reading a file Imager examines beginning of the file for
415 identifying information.  The current implementation attempts to
416 detect the following image types beyond those supported by Imager:
417
418 =for stopwords Photoshop
419
420 =over
421
422 C<xpm>, C<mng>, C<jng>, C<ilbm>, C<pcx>, C<fits>, C<psd> (Photoshop), C<eps>, Utah
423 C<RLE>.
424
425 =back
426
427 =head2 Limiting the sizes of images you read
428
429 =over
430
431 =item set_file_limits()
432
433 In some cases you will be receiving images from an untested source,
434 such as submissions via CGI.  To prevent such images from consuming
435 large amounts of memory, you can set limits on the dimensions of
436 images you read from files:
437
438 =over
439
440 =item *
441
442 width - limit the width in pixels of the image
443
444 =item *
445
446 height - limit the height in pixels of the image
447
448 =item *
449
450 bytes - limits the amount of storage used by the image.  This depends
451 on the width, height, channels and sample size of the image.  For
452 paletted images this is calculated as if the image was expanded to a
453 direct color image.
454
455 =back
456
457 To set the limits, call the class method set_file_limits:
458
459   Imager->set_file_limits(width=>$max_width, height=>$max_height);
460
461 You can pass any or all of the limits above, any limits you do not
462 pass are left as they were.
463
464 Any limit of zero for width or height is treated as unlimited.
465
466 A limit of zero for bytes is treated as one gigabyte, but higher bytes
467 limits can be set explicitly.
468
469 By default, the width and height limits are zero, or unlimited.  The
470 default memory size limit is one gigabyte.
471
472 You can reset all limits to their defaults with the reset parameter:
473
474   # no limits
475   Imager->set_file_limits(reset=>1);
476
477 This can be used with the other limits to reset all but the limit you
478 pass:
479
480   # only width is limited
481   Imager->set_file_limits(reset=>1, width=>100);
482
483   # only bytes is limited
484   Imager->set_file_limits(reset=>1, bytes=>10_000_000);
485
486 =item get_file_limits()
487
488 You can get the current limits with the get_file_limits() method:
489
490   my ($max_width, $max_height, $max_bytes) =
491      Imager->get_file_limits();
492
493 =item check_file_limits()
494 X<class methods, check_file_limits()>X<check_file_limits()>
495
496 Intended for use by file handlers to check that the size of a file is
497 within the limits set by C<set_file_limits()>.
498
499 Parameters:
500
501 =over
502
503 =item *
504
505 C<width>, C<height> - the width and height of the image in pixels.
506 Must be a positive integer. Required.
507
508 =item *
509
510 C<channels> - the number of channels in the image, including the alpha
511 channel if any.  Must be a positive integer between 1 and 4
512 inclusive.  Default: 3.
513
514 =item *
515
516 C<sample_size> - the number of bytes stored per sample.  Must be a
517 positive integer or C<"float">.  Note that this should be the sample
518 size of the Imager image you will be creating, not the sample size in
519 the source, eg. if the source has 32-bit samples this should be
520 C<"float"> since Imager doesn't have 32-bit/sample images.
521
522 =back
523
524 =back
525
526 =head1 TYPE SPECIFIC INFORMATION
527
528 The different image formats can write different image type, and some have
529 different options to control how the images are written.
530
531 When you call C<write()> or C<write_multi()> with an option that has
532 the same name as a tag for the image format you're writing, then the
533 value supplied to that option will be used to set the corresponding
534 tag in the image.  Depending on the image format, these values will be
535 used when writing the image.
536
537 This replaces the previous options that were used when writing GIF
538 images.  Currently if you use an obsolete option, it will be converted
539 to the equivalent tag and Imager will produced a warning.  You can
540 suppress these warnings by calling the C<Imager::init()> function with
541 the C<warn_obsolete> option set to false:
542
543   Imager::init(warn_obsolete=>0);
544
545 At some point in the future these obsolete options will no longer be
546 supported.
547
548 =for stopwords aNy PixMaps BitMap
549
550 =head2 PNM (Portable aNy Map)
551
552 Imager can write C<PGM> (Portable Gray Map) and C<PPM> (Portable
553 PixMaps) files, depending on the number of channels in the image.
554 Currently the images are written in binary formats.  Only 1 and 3
555 channel images can be written, including 1 and 3 channel paletted
556 images.
557
558   $img->write(file=>'foo.ppm') or die $img->errstr;
559
560 Imager can read both the ASCII and binary versions of each of the
561 C<PBM> (Portable BitMap), C<PGM> and C<PPM> formats.
562
563   $img->read(file=>'foo.ppm') or die $img->errstr;
564
565 PNM does not support the spatial resolution tags.
566
567 The following tags are set when reading a PNM file:
568
569 =over
570
571 =item *
572
573 X<pnm_maxval>C<pnm_maxval> - the C<maxvals> number from the PGM/PPM header.
574 Always set to 2 for a C<PBM> file.
575
576 =item *
577
578 X<pnm_type>C<pnm_type> - the type number from the C<PNM> header, 1 for ASCII
579 C<PBM> files, 2 for ASCII C<PGM> files, 3 for ASCII c<PPM> files, 4 for binary
580 C<PBM> files, 5 for binary C<PGM> files, 6 for binary C<PPM> files.
581
582 =back
583
584 The following tag is checked when writing an image with more than
585 8-bits/sample:
586
587 =over
588
589 =item *
590
591 X<pnm_write_wide_data>pnm_write_wide_data - if this is non-zero then
592 write() can write C<PGM>/C<PPM> files with 16-bits/sample.  Some
593 applications, for example GIMP 2.2, and tools can only read
594 8-bit/sample binary PNM files, so Imager will only write a 16-bit
595 image when this tag is non-zero.
596
597 =back
598
599 =head2 JPEG
600
601 You can supply a C<jpegquality> parameter ranging from 0 (worst
602 quality) to 100 (best quality) when writing a JPEG file, which
603 defaults to 75.
604
605   $img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
606
607 If you write an image with an alpha channel to a JPEG file then it
608 will be composed against the background set by the C<i_background>
609 parameter (or tag), or black if not supplied.
610
611 Imager will read a gray scale JPEG as a 1 channel image and a color
612 JPEG as a 3 channel image.
613
614   $img->read(file=>'foo.jpg') or die $img->errstr;
615
616 The following tags are set in a JPEG image when read, and can be set
617 to control output:
618
619 =over
620
621 =item *
622
623 C<jpeg_density_unit> - The value of the density unit field in the
624 C<JFIF> header.  This is ignored on writing if the C<i_aspect_only>
625 tag is non-zero.
626
627 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
628 matter the value of this tag, they will be converted to/from the value
629 stored in the JPEG file.
630
631 =item *
632
633 C<jpeg_density_unit_name> - This is set when reading a JPEG file to
634 the name of the unit given by C<jpeg_density_unit>.  Possible results
635 include C<inch>, C<centimeter>, C<none> (the C<i_aspect_only> tag is
636 also set reading these files).  If the value of C<jpeg_density_unit>
637 is unknown then this tag isn't set.
638
639 =item *
640
641 C<jpeg_comment> - Text comment.
642
643 =item *
644
645 C<jpeg_progressive> - Whether the JPEG file is a progressive
646 file. (Imager 0.84)
647
648 =back
649
650 JPEG supports the spatial resolution tags C<i_xres>, C<i_yres> and
651 C<i_aspect_only>.
652
653 You can also set the following tags when writing to an image, they are
654 not set in the image when reading:
655
656 =over
657
658 C<jpeg_optimize> - set to a non-zero integer to compute optimal
659 Huffman coding tables for the image.  This will increase memory usage
660 and processing time (about 12% in my simple tests) but can
661 significantly reduce file size without a loss of quality.
662
663 =back
664
665 =for stopwords EXIF
666
667 If an C<APP1> block containing EXIF information is found, then any of the
668 following tags can be set when reading a JPEG image:
669
670 =over
671
672 exif_aperture exif_artist exif_brightness exif_color_space
673 exif_contrast exif_copyright exif_custom_rendered exif_date_time
674 exif_date_time_digitized exif_date_time_original
675 exif_digital_zoom_ratio exif_exposure_bias exif_exposure_index
676 exif_exposure_mode exif_exposure_program exif_exposure_time
677 exif_f_number exif_flash exif_flash_energy exif_flashpix_version
678 exif_focal_length exif_focal_length_in_35mm_film
679 exif_focal_plane_resolution_unit exif_focal_plane_x_resolution
680 exif_focal_plane_y_resolution exif_gain_control exif_image_description
681 exif_image_unique_id exif_iso_speed_rating exif_make exif_max_aperture
682 exif_metering_mode exif_model exif_orientation exif_related_sound_file
683 exif_resolution_unit exif_saturation exif_scene_capture_type
684 exif_sensing_method exif_sharpness exif_shutter_speed exif_software
685 exif_spectral_sensitivity exif_sub_sec_time
686 exif_sub_sec_time_digitized exif_sub_sec_time_original
687 exif_subject_distance exif_subject_distance_range
688 exif_subject_location exif_tag_light_source exif_user_comment
689 exif_version exif_white_balance exif_x_resolution exif_y_resolution
690
691 =back
692
693 The following derived tags can also be set when reading a JPEG image:
694
695 =over
696
697 exif_color_space_name exif_contrast_name exif_custom_rendered_name
698 exif_exposure_mode_name exif_exposure_program_name exif_flash_name
699 exif_focal_plane_resolution_unit_name exif_gain_control_name
700 exif_light_source_name exif_metering_mode_name
701 exif_resolution_unit_name exif_saturation_name
702 exif_scene_capture_type_name exif_sensing_method_name
703 exif_sharpness_name exif_subject_distance_range_name
704 exif_white_balance_name
705
706 =back
707
708 The derived tags are for enumerated fields, when the value for the
709 base field is valid then the text that appears in the EXIF
710 specification for that value appears in the derived field.  So for
711 example if C<exf_metering_mode> is C<5> then
712 C<exif_metering_mode_name> is set to C<Pattern>.
713
714 eg.
715
716   my $image = Imager->new;
717   $image->read(file => 'exiftest.jpg')
718     or die "Cannot load image: ", $image->errstr;
719   print $image->tags(name => "exif_image_description"), "\n";
720   print $image->tags(name => "exif_exposure_mode"), "\n";
721   print $image->tags(name => "exif_exposure_mode_name"), "\n";
722
723   # for the exiftest.jpg in the Imager distribution the output would be:
724   Imager Development Notes
725   0
726   Auto exposure
727
728 Imager will not write EXIF tags to any type of image, if you need more
729 advanced EXIF handling, consider L<Image::ExifTool>.
730
731 =for stopwords IPTC
732
733 =over
734
735 =item parseiptc()
736
737 Historically, Imager saves IPTC data when reading a JPEG image, the
738 parseiptc() method returns a list of key/value pairs resulting from a
739 simple decoding of that data.
740
741 Any future IPTC data decoding is likely to go into tags.
742
743 =back
744
745 =head2 GIF
746
747 When writing one of more GIF images you can use the same
748 L<Quantization Options|Imager::ImageTypes> as you can when converting
749 an RGB image into a paletted image.
750
751 When reading a GIF all of the sub-images are combined using the screen
752 size and image positions into one big image, producing an RGB image.
753 This may change in the future to produce a paletted image where possible.
754
755 When you read a single GIF with C<$img-E<gt>read()> you can supply a
756 reference to a scalar in the C<colors> parameter, if the image is read
757 the scalar will be filled with a reference to an anonymous array of
758 L<Imager::Color> objects, representing the palette of the image.  This
759 will be the first palette found in the image.  If you want the
760 palettes for each of the images in the file, use C<read_multi()> and
761 use the C<getcolors()> method on each image.
762
763 GIF does not support the spatial resolution tags.
764
765 Imager will set the following tags in each image when reading, and can
766 use most of them when writing to GIF:
767
768 =over
769
770 =item *
771
772 gif_left - the offset of the image from the left of the "screen"
773 ("Image Left Position")
774
775 =item *
776
777 gif_top - the offset of the image from the top of the "screen" ("Image
778 Top Position")
779
780 =item *
781
782 gif_interlace - non-zero if the image was interlaced ("Interlace
783 Flag")
784
785 =item *
786
787 gif_screen_width, gif_screen_height - the size of the logical
788 screen. When writing this is used as the minimum.  If any image being
789 written would extend beyond this then the screen size is extended.
790 ("Logical Screen Width", "Logical Screen Height").
791
792 =item *
793
794 gif_local_map - Non-zero if this image had a local color map.  If set
795 for an image when writing the image is quantized separately from the
796 other images in the file.
797
798 =item *
799
800 gif_background - The index in the global color map of the logical
801 screen's background color.  This is only set if the current image uses
802 the global color map.  You can set this on write too, but for it to
803 choose the color you want, you will need to supply only paletted
804 images and set the C<gif_eliminate_unused> tag to 0.
805
806 =item *
807
808 gif_trans_index - The index of the color in the color map used for
809 transparency.  If the image has a transparency then it is returned as
810 a 4 channel image with the alpha set to zero in this palette entry.
811 This value is not used when writing. ("Transparent Color Index")
812
813 =item *
814
815 gif_trans_color - A reference to an Imager::Color object, which is the
816 color to use for the palette entry used to represent transparency in
817 the palette.  You need to set the C<transp> option (see
818 L<Imager::ImageTypes/"Quantization options">) for this value to be
819 used.
820
821 =item *
822
823 gif_delay - The delay until the next frame is displayed, in 1/100 of a
824 second.  ("Delay Time").
825
826 =item *
827
828 gif_user_input - whether or not a user input is expected before
829 continuing (view dependent) ("User Input Flag").
830
831 =item *
832
833 gif_disposal - how the next frame is displayed ("Disposal Method")
834
835 =item *
836
837 gif_loop - the number of loops from the Netscape Loop extension.  This
838 may be zero to loop forever.
839
840 =item *
841
842 gif_comment - the first block of the first GIF comment before each
843 image.
844
845 =item *
846
847 gif_eliminate_unused - If this is true, when you write a paletted
848 image any unused colors will be eliminated from its palette.  This is
849 set by default.
850
851 =item *
852
853 gif_colormap_size - the original size of the color map for the image.
854 The color map of the image may have been expanded to include out of
855 range color indexes.
856
857 =back
858
859 Where applicable, the ("name") is the name of that field from the C<GIF89>
860 standard.
861
862 The following GIF writing options are obsolete, you should set the
863 corresponding tag in the image, either by using the tags functions, or
864 by supplying the tag and value as options.
865
866 =over
867
868 =item *
869
870 gif_each_palette - Each image in the GIF file has it's own palette if
871 this is non-zero.  All but the first image has a local color table
872 (the first uses the global color table.
873
874 Use C<gif_local_map> in new code.
875
876 =item *
877
878 interlace - The images are written interlaced if this is non-zero.
879
880 Use C<gif_interlace> in new code.
881
882 =item *
883
884 gif_delays - A reference to an array containing the delays between
885 images, in 1/100 seconds.
886
887 Use C<gif_delay> in new code.
888
889 =item *
890
891 gif_positions - A reference to an array of references to arrays which
892 represent screen positions for each image.
893
894 New code should use the C<gif_left> and C<gif_top> tags.
895
896 =item *
897
898 gif_loop_count - If this is non-zero the Netscape loop extension block
899 is generated, which makes the animation of the images repeat.
900
901 This is currently unimplemented due to some limitations in C<giflib>.
902
903 =back
904
905 You can supply a C<page> parameter to the C<read()> method to read
906 some page other than the first.  The page is 0 based:
907
908   # read the second image in the file
909   $image->read(file=>"example.gif", page=>1)
910     or die "Cannot read second page: ",$image->errstr,"\n";
911
912 Before release 0.46, Imager would read multiple image GIF image files
913 into a single image, overlaying each of the images onto the virtual
914 GIF screen.
915
916 As of 0.46 the default is to read the first image from the file, as if
917 called with C<< page => 0 >>.
918
919 You can return to the previous behavior by calling read with the
920 C<gif_consolidate> parameter set to a true value:
921
922   $img->read(file=>$some_gif_file, gif_consolidate=>1);
923
924 As with the to_paletted() method, if you supply a colors parameter as
925 a reference to an array, this will be filled with Imager::Color
926 objects of the color table generated for the image file.
927
928 =head2 TIFF (Tagged Image File Format)
929
930 Imager can write images to either paletted or RGB TIFF images,
931 depending on the type of the source image.
932
933 When writing direct color images to TIFF the sample size of the
934 output file depends on the input:
935
936 =over
937
938 =item *
939
940 double/sample - written as 32-bit/sample TIFF
941
942 =item *
943
944 16-bit/sample - written as 16-bit/sample TIFF
945
946 =item *
947
948 8-bit/sample - written as 8-bit/sample TIFF
949
950 =back
951
952 For paletted images:
953
954 =over
955
956 =item *
957
958 C<< $img->is_bilevel >> is true - the image is written as bi-level
959
960 =item *
961
962 otherwise - image is written as paletted.
963
964 =back
965
966 If you are creating images for faxing you can set the I<class>
967 parameter set to C<fax>.  By default the image is written in fine
968 mode, but this can be overridden by setting the I<fax_fine> parameter
969 to zero.  Since a fax image is bi-level, Imager uses a threshold to
970 decide if a given pixel is black or white, based on a single channel.
971 For gray scale images channel 0 is used, for color images channel 1
972 (green) is used.  If you want more control over the conversion you can
973 use $img->to_paletted() to product a bi-level image.  This way you can
974 use dithering:
975
976   my $bilevel = $img->to_paletted(make_colors => 'mono',
977                                   translate => 'errdiff',
978                                   errdiff => 'stucki');
979
980 =over
981
982 =item *
983
984 C<class> - If set to 'fax' the image will be written as a bi-level fax
985 image.
986
987 =item *
988
989 C<fax_fine> - By default when C<class> is set to 'fax' the image is
990 written in fine mode, you can select normal mode by setting
991 C<fax_fine> to 0.
992
993 =back
994
995 Imager should be able to read any TIFF image you supply.  Paletted
996 TIFF images are read as paletted Imager images, since paletted TIFF
997 images have 16-bits/sample (48-bits/color) this means the bottom
998 8-bits are lost, but this shouldn't be a big deal.
999
1000 TIFF supports the spatial resolution tags.  See the
1001 C<tiff_resolutionunit> tag for some extra options.
1002
1003 As of Imager 0.62 Imager reads:
1004
1005 =over
1006
1007 =item *
1008
1009 8-bit/sample gray, RGB or CMYK images, including a possible alpha
1010 channel as an 8-bit/sample image.
1011
1012 =item *
1013
1014 16-bit gray, RGB, or CMYK image, including a possible alpha channel as
1015 a 16-bit/sample image.
1016
1017 =item *
1018
1019 32-bit gray, RGB image, including a possible alpha channel as a
1020 double/sample image.
1021
1022 =item *
1023
1024 bi-level images as paletted images containing only black and white,
1025 which other formats will also write as bi-level.
1026
1027 =item *
1028
1029 tiled paletted images are now handled correctly
1030
1031 =item *
1032
1033 other images are read using C<tifflib>'s RGBA interface as
1034 8-bit/sample images.
1035
1036 =back
1037
1038 The following tags are set in a TIFF image when read, and can be set
1039 to control output:
1040
1041 =over
1042
1043 =item *
1044
1045 C<tiff_compression> - When reading an image this is set to the numeric
1046 value of the TIFF compression tag.
1047
1048 On writing you can set this to either a numeric compression tag value,
1049 or one of the following values:
1050
1051   Ident     Number  Description
1052   none         1    No compression
1053   packbits   32773  Macintosh RLE
1054   ccittrle     2    CCITT RLE
1055   fax3         3    CCITT Group 3 fax encoding (T.4)
1056   t4           3    As above
1057   fax4         4    CCITT Group 4 fax encoding (T.6)
1058   t6           4    As above
1059   lzw          5    LZW
1060   jpeg         7    JPEG
1061   zip          8    Deflate (GZIP) Non-standard
1062   deflate      8    As above.
1063   oldzip     32946  Deflate with an older code.
1064   ccittrlew  32771  Word aligned CCITT RLE
1065
1066 In general a compression setting will be ignored where it doesn't make
1067 sense, eg. C<jpeg> will be ignored for compression if the image is
1068 being written as bilevel.
1069
1070 =for stopwords LZW
1071
1072 Imager attempts to check that your build of C<libtiff> supports the
1073 given compression, and will fallback to C<packbits> if it isn't
1074 enabled.  eg. older distributions didn't include LZW compression, and
1075 JPEG compression is only available if C<libtiff> is configured with
1076 C<libjpeg>'s location.
1077
1078   $im->write(file => 'foo.tif', tiff_compression => 'lzw')
1079     or die $im->errstr;
1080
1081 =item *
1082
1083 C<tags, tiff_jpegquality>C<tiff_jpegquality> - If C<tiff_compression>
1084 is C<jpeg> then this can be a number from 1 to 100 giving the JPEG
1085 compression quality.  High values are better quality and larger files.
1086
1087 =item *
1088
1089 X<tags, tiff_resolutionunit>C<tiff_resolutionunit> - The value of the
1090 C<ResolutionUnit> tag.  This is ignored on writing if the
1091 i_aspect_only tag is non-zero.
1092
1093 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
1094 matter the value of this tag, they will be converted to/from the value
1095 stored in the TIFF file.
1096
1097 =item *
1098
1099 X<tags, tiff_resolutionunit_name>C<tiff_resolutionunit_name> - This is
1100 set when reading a TIFF file to the name of the unit given by
1101 C<tiff_resolutionunit>.  Possible results include C<inch>,
1102 C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
1103 these files) or C<unknown>.
1104
1105 =item *
1106
1107 X<tags, tiff_bitspersample>C<tiff_bitspersample> - Bits per sample
1108 from the image.  This value is not used when writing an image, it is
1109 only set on a read image.
1110
1111 =item *
1112
1113 X<tags, tiff_photometric>C<tiff_photometric> - Value of the
1114 C<PhotometricInterpretation> tag from the image.  This value is not
1115 used when writing an image, it is only set on a read image.
1116
1117 =item *
1118
1119 C<tiff_documentname>, C<tiff_imagedescription>, C<tiff_make>,
1120 C<tiff_model>, C<tiff_pagename>, C<tiff_software>, C<tiff_datetime>,
1121 C<tiff_artist>, C<tiff_hostcomputer> - Various strings describing the
1122 image.  C<tiff_datetime> must be formatted as "YYYY:MM:DD HH:MM:SS".
1123 These correspond directly to the mixed case names in the TIFF
1124 specification.  These are set in images read from a TIFF and saved
1125 when writing a TIFF image.
1126
1127 =back
1128
1129 You can supply a C<page> parameter to the C<read()> method to read
1130 some page other than the first.  The page is 0 based:
1131
1132   # read the second image in the file
1133   $image->read(file=>"example.tif", page=>1)
1134     or die "Cannot read second page: ",$image->errstr,"\n";
1135
1136 If you read an image with multiple alpha channels, then only the first
1137 alpha channel will be read.
1138
1139 When reading a C<TIFF> image with callbacks, the C<seekcb> callback
1140 parameter is also required.
1141
1142 When writing a C<TIFF> image with callbacks, the C<seekcb> and
1143 C<readcb> parameters are also required.
1144
1145 C<TIFF> is a random access file format, it cannot be read from or
1146 written to unseekable streams such as pipes or sockets.
1147
1148 =head2 BMP (Windows Bitmap)
1149
1150 Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
1151 Windows BMP files.  Currently you cannot write compressed BMP files
1152 with Imager.
1153
1154 Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
1155 Windows BMP files.  There is some support for reading 16-bit per pixel
1156 images, but I haven't found any for testing.
1157
1158 BMP has no support for multiple image files.
1159
1160 BMP files support the spatial resolution tags, but since BMP has no
1161 support for storing only an aspect ratio, if C<i_aspect_only> is set
1162 when you write the C<i_xres> and C<i_yres> values are scaled so the
1163 smaller is 72 DPI.
1164
1165 The following tags are set when you read an image from a BMP file:
1166
1167 =over
1168
1169 =item bmp_compression
1170
1171 The type of compression, if any.  This can be any of the following
1172 values:
1173
1174 =for stopwords RLE
1175
1176 =over
1177
1178 =item BI_RGB (0)
1179
1180 Uncompressed.
1181
1182 =item BI_RLE8 (1)
1183
1184 8-bits/pixel paletted value RLE compression.
1185
1186 =item BI_RLE4 (2)
1187
1188 4-bits/pixel paletted value RLE compression.
1189
1190 =item BI_BITFIELDS (3)
1191
1192 Packed RGB values.
1193
1194 =back
1195
1196 =item bmp_compression_name
1197
1198 The bmp_compression value as a BI_* string
1199
1200 =item bmp_important_colors
1201
1202 The number of important colors as defined by the writer of the image.
1203
1204 =item bmp_used_colors
1205
1206 Number of color used from the BMP header
1207
1208 =item bmp_filesize
1209
1210 The file size from the BMP header
1211
1212 =item bmp_bit_count
1213
1214 Number of bits stored per pixel. (24, 8, 4 or 1)
1215
1216 =back
1217
1218 =for stopwords Targa
1219
1220 =head2 TGA (Targa)
1221
1222 When storing Targa images RLE compression can be activated with the
1223 C<compress> parameter, the C<idstring> parameter can be used to set the
1224 Targa comment field and the C<wierdpack> option can be used to use the
1225 15 and 16 bit Targa formats for RGB and RGBA data.  The 15 bit format
1226 has 5 of each red, green and blue.  The 16 bit format in addition
1227 allows 1 bit of alpha.  The most significant bits are used for each
1228 channel.
1229
1230 Tags:
1231
1232 =over
1233
1234 =item tga_idstring
1235
1236 =item tga_bitspp
1237
1238 =item compressed
1239
1240 =back
1241
1242 =head2 RAW
1243
1244 When reading raw images you need to supply the width and height of the
1245 image in the C<xsize> and C<ysize> options:
1246
1247   $img->read(file=>'foo.raw', xsize=>100, ysize=>100)
1248     or die "Cannot read raw image\n";
1249
1250 If your input file has more channels than you want, or (as is common),
1251 junk in the fourth channel, you can use the C<raw_datachannels> and
1252 C<raw_storechannels> options to control the number of channels in your input
1253 file and the resulting channels in your image.  For example, if your
1254 input image uses 32-bits per pixel with red, green, blue and junk
1255 values for each pixel you could do:
1256
1257   $img->read(file=>'foo.raw', xsize => 100, ysize => 100,
1258              raw_datachannels => 4, raw_storechannels => 3,
1259              raw_interleave => 0)
1260     or die "Cannot read raw image\n";
1261
1262 In general, if you supply C<raw_storechannels> you should also supply
1263 C<raw_datachannels>
1264
1265 Read parameters:
1266
1267 =over
1268
1269 =item *
1270
1271 C<raw_interleave> - controls the ordering of samples within the image.
1272 Default: 1.  Alternatively and historically spelled C<interleave>.
1273 Possible values:
1274
1275 =over
1276
1277 =item *
1278
1279 0 - samples are pixel by pixel, so all samples for the first pixel,
1280 then all samples for the second pixel and so on.  eg. for a four pixel
1281 scan line the channels would be laid out as:
1282
1283   012012012012
1284
1285 =item *
1286
1287 1 - samples are line by line, so channel 0 for the entire scan line is
1288 followed by channel 1 for the entire scan line and so on.  eg. for a
1289 four pixel scan line the channels would be laid out as:
1290
1291   000011112222
1292
1293 This is the default.
1294
1295 =back
1296
1297 Unfortunately, historically, the default C<raw_interleave> for read
1298 has been 1, while writing only supports the C<raw_interleave> = 0
1299 format.
1300
1301 For future compatibility, you should always supply the
1302 C<raw_interleave> (or C<interleave>) parameter.  As of 0.68, Imager
1303 will warn if you attempt to read a raw image without a
1304 C<raw_interleave> parameter.
1305
1306 =item *
1307
1308 C<raw_storechannels> - the number of channels to store in the image.
1309 Range: 1 to 4.  Default: 3.  Alternatively and historically spelled
1310 C<storechannels>.
1311
1312 =item *
1313
1314 C<raw_datachannels> - the number of channels to read from the file.
1315 Range: 1 or more.  Default: 3.  Alternatively and historically spelled
1316 C<datachannels>.
1317
1318 =back
1319
1320   $img->read(file=>'foo.raw', xsize=100, ysize=>100, raw_interleave=>1)
1321     or die "Cannot read raw image\n";
1322
1323 =head2 PNG
1324
1325 =head3 PNG Image modes
1326
1327 PNG files can be read and written in the following modes:
1328
1329 =over
1330
1331 =item *
1332
1333 bi-level - written as a 1-bit per sample gray scale image
1334
1335 =item *
1336
1337 paletted - Imager gray scale paletted images are written as RGB
1338 paletted images.  PNG palettes can include alpha values for each entry
1339 and this is honored as an Imager four channel paletted image.
1340
1341 =item *
1342
1343 8 and 16-bit per sample gray scale, optionally with an alpha channel.
1344
1345 =item *
1346
1347 8 and 16-bit per sample RGB, optionally with an alpha channel.
1348
1349 =back
1350
1351 Unlike GIF, there is no automatic conversion to a paletted image,
1352 since PNG supports direct color.
1353
1354 =head3 PNG Text tags
1355
1356 Text tags are retrieved from and written to PNG C<tEXT> or C<zTXT>
1357 chunks.  The following standard tags from the PNG specification are
1358 directly supported:
1359
1360 =over
1361
1362 =item *
1363
1364 C<i_comment>X<tags,i_comment> - keyword of "Comment".
1365
1366 =item *
1367
1368 C<png_author>X<tags,PNG,png_author> - keyword "Author".
1369
1370 =item *
1371
1372 C<png_copyright>X<tags,PNG,png_copyright> - keyword "Copyright".
1373
1374 =item *
1375
1376 C<png_creation_time>X<tags,PNG,png_creation_time> - keyword "Creation Time".
1377
1378 =item *
1379
1380 C<png_description>X<tags,PNG,png_description> - keyword "Description".
1381
1382 =item *
1383
1384 C<png_disclaimer>X<tags,PNG,png_disclaimer> - keyword "Disclaimer".
1385
1386 =item *
1387
1388 C<png_software>X<tags,PNG,png_software> - keyword "Software".
1389
1390 =item *
1391
1392 C<png_title>X<tags,PNG,png_title> - keyword "Title".
1393
1394 =item *
1395
1396 C<png_warning>X<tags,PNG,png_warning> - keyword "Warning".
1397
1398 =back
1399
1400 Each of these tags has a corresponding C<< I<base-tag-name>_compressed
1401 >> tag, eg. C<png_comment_compressed>.  When reading, if the PNG chunk
1402 is compressed this tag will be set to 1, but is otherwise unset.  When
1403 writing, Imager will honor the compression tag if set and non-zero,
1404 otherwise the chunk text will be compressed if the value is longer
1405 than 1000 characters, as recommended by the C<libpng> documentation.
1406
1407 PNG C<tEXT> or C<zTXT> chunks outside of those above are read into or
1408 written from Imager tags named like:
1409
1410 =over
1411
1412 =item *
1413
1414 C<< png_textI<N>_key >> - the key for the text chunk.  This can be 1
1415 to 79 characters, may not contain any leading, trailing or consecutive
1416 spaces, and may contain only Latin-1 characters from 32-126, 161-255.
1417
1418 =item *
1419
1420 C<< png_textI<N>_text >> - the text for the text chunk.  This may not
1421 contain any C<NUL> characters.
1422
1423 =item *
1424
1425 C<< png_textI<N>_compressed >> - whether or not the text chunk is
1426 compressed.  This behaves similarly to the C<<
1427 I<base-tag-name>_compressed >> tags described above.
1428
1429 =back
1430
1431 Where I<N> starts from 0.  When writing both the C<..._key> and
1432 C<..._text> tags must be present or the write will fail.  If the key
1433 or text do not satisfy the requirements above the write will fail.
1434
1435 =head3 Other PNG metadata tags
1436
1437 =over
1438
1439 =item *
1440
1441 X<tags, png_interlace>C<png_interlace>, C<png_interlace_name> - only
1442 set when reading, C<png_interlace> is set to the type of interlacing
1443 used by the file, 0 for one, 1 for Adam7.  C<png_interlace_name> is
1444 set to a keyword describing the interlacing, either C<none> or
1445 C<adam7>.
1446
1447 =item *
1448
1449 X<tags, png_srgb_intent>C<png_srgb_intent> - the sRGB rendering intent
1450 for the image. an integer from 0 to 3, per the PNG specification.  If
1451 this chunk is found in the PNG file the C<gAMA> and C<cHRM> are
1452 ignored and the C<png_gamma> and C<png_chroma_...> tags are not set.
1453 Similarly when writing if C<png_srgb_intent> is set the C<gAMA> and
1454 C<cHRM> chunks are not written.
1455
1456 =item *
1457
1458 X<tags, png_gamma>C<png_gamma> - the gamma of the image. This value is
1459 not currently used by Imager when processing the image, but this may
1460 change in the future.
1461
1462 =item *
1463
1464 X<tags, png_chroma_...>C<png_chroma_white_x>, C<png_chroma_white_y>,
1465 C<png_chroma_red_x>, C<png_chroma_red_y>, C<png_chroma_green_x>,
1466 C<png_chroma_green_y>, C<png_chroma_blue_x>, C<png_chroma_blue_y> -
1467 the primary chromaticities of the image, defining the color model.
1468 This is currently not used by Imager when processing the image, but
1469 this may change in the future.
1470
1471 =item *
1472
1473 C<i_xres>, C<i_yres>, C<i_aspect_only> - processed per
1474 I<Imager::ImageTypes/CommonTags>.
1475
1476 =item *
1477
1478 X<tags, png_bits>C<png_bits> - the number of bits per sample in the
1479 representation.  Ignored when writing.
1480
1481 =item *
1482
1483 X<tags, png_time>C<png_time> - the creation time of the file formatted
1484 as C<< I<year>-I<month>-I<day>TI<hour>:I<minute>:I<second> >>.  This
1485 is stored as time data structure in the file, not a string.  If you
1486 set C<png_time> and it cannot be parsed as above, writing the PNG file
1487 will fail.
1488
1489 =item *
1490
1491 C<i_background> - set from the C<sBKG> when reading an image file.
1492
1493 =back
1494
1495 X<compression>X<png_compression_level>You can control the level of
1496 F<zlib> compression used when writing with the
1497 C<png_compression_level> parameter.  This can be an integer between 0
1498 (uncompressed) and 9 (best compression).
1499
1500 =for stopwords
1501 CRC
1502
1503 X<png_ignore_benign_errors>If you're using F<libpng> 1.6 or later, or
1504 an earlier release configured with C<PNG_BENIGN_ERRORS_SUPPORTED>, you
1505 can choose to ignore file format errors the authors of F<libpng>
1506 consider I<benign>, this includes at least CRC errors and palette
1507 index overflows.  Do this by supplying a true value for the
1508 C<png_ignore_benign_errors> parameter to the read() method:
1509
1510   $im->read(file => "foo.png", png_ignore_benign_errors => 1)
1511     or die $im->errstr;
1512
1513 =head2 ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)
1514
1515 Icon and Cursor files are very similar, the only differences being a
1516 number in the header and the storage of the cursor hot spot.  I've
1517 treated them separately so that you're not messing with tags to
1518 distinguish between them.
1519
1520 The following tags are set when reading an icon image and are used
1521 when writing it:
1522
1523 =over
1524
1525 =item ico_mask
1526
1527 This is the AND mask of the icon.  When used as an icon in Windows 1
1528 bits in the mask correspond to pixels that are modified by the source
1529 image rather than simply replaced by the source image.
1530
1531 Rather than requiring a binary bitmap this is accepted in a specific format:
1532
1533 =over
1534
1535 =item *
1536
1537 first line consisting of the 0 placeholder, the 1 placeholder and a
1538 newline.
1539
1540 =item *
1541
1542 following lines which contain 0 and 1 placeholders for each scan line
1543 of the image, starting from the top of the image.
1544
1545 =back
1546
1547 When reading an image, '.' is used as the 0 placeholder and '*' as the
1548 1 placeholder.  An example:
1549
1550   .*
1551   ..........................******
1552   ..........................******
1553   ..........................******
1554   ..........................******
1555   ...........................*****
1556   ............................****
1557   ............................****
1558   .............................***
1559   .............................***
1560   .............................***
1561   .............................***
1562   ..............................**
1563   ..............................**
1564   ...............................*
1565   ...............................*
1566   ................................
1567   ................................
1568   ................................
1569   ................................
1570   ................................
1571   ................................
1572   *...............................
1573   **..............................
1574   **..............................
1575   ***.............................
1576   ***.............................
1577   ****............................
1578   ****............................
1579   *****...........................
1580   *****...........................
1581   *****...........................
1582   *****...........................
1583
1584 =back
1585
1586 The following tags are set when reading an icon:
1587
1588 =over
1589
1590 =item ico_bits
1591
1592 The number of bits per pixel used to store the image.
1593
1594 =back
1595
1596 For cursor files the following tags are set and read when reading and
1597 writing:
1598
1599 =over
1600
1601 =item cur_mask
1602
1603 This is the same as the ico_mask above.
1604
1605 =item cur_hotspotx
1606
1607 =item cur_hotspoty
1608
1609 The "hot" spot of the cursor image.  This is the spot on the cursor
1610 that you click with.  If you set these to out of range values they are
1611 clipped to the size of the image when written to the file.
1612
1613 =back
1614
1615 The following parameters can be supplied to read() or read_multi() to
1616 control reading of ICO/CUR files:
1617
1618 =over
1619
1620 =item *
1621
1622 C<ico_masked> - if true, the default, then the icon/cursors mask is
1623 applied as an alpha channel to the image, unless that image already
1624 has an alpha channel.  This may result in a paletted image being
1625 returned as a direct color image.  Default: 1
1626
1627   # retrieve the image as stored, without using the mask as an alpha
1628   # channel
1629   $img->read(file => 'foo.ico', ico_masked => 0)
1630     or die $img->errstr;
1631
1632 This was introduced in Imager 0.60.  Previously reading ICO images
1633 acted as if C<ico_masked =E<gt> 0>.
1634
1635 =item *
1636
1637 C<ico_alpha_masked> - if true, then the icon/cursor mask is applied as
1638 an alpha channel to images that already have an alpha mask.  Note that
1639 this will only make pixels transparent, not opaque.  Default: 0.
1640
1641 Note: If you get different results between C<ico_alpha_masked> being
1642 set to 0 and 1, your mask may break when used with the Win32 API.
1643
1644 =back
1645
1646 C<cur_bits> is set when reading a cursor.
1647
1648 Examples:
1649
1650   my $img = Imager->new(xsize => 32, ysize => 32, channels => 4);
1651   $im->box(color => 'FF0000');
1652   $im->write(file => 'box.ico');
1653
1654   $im->settag(name => 'cur_hotspotx', value => 16);
1655   $im->settag(name => 'cur_hotspoty', value => 16);
1656   $im->write(file => 'box.cur');
1657
1658 =for stopwords BW
1659
1660 =head2 SGI (RGB, BW)
1661
1662 SGI images, often called by the extensions, RGB or BW, can be stored
1663 either uncompressed or compressed using an RLE compression.
1664
1665 By default, when saving to an extension of C<rgb>, C<bw>, C<sgi>,
1666 C<rgba> the file will be saved in SGI format.  The file extension is
1667 otherwise ignored, so saving a 3-channel image to a C<.bw> file will
1668 result in a 3-channel image on disk.
1669
1670 The following tags are set when reading a SGI image:
1671
1672 =over
1673
1674 =item *
1675
1676 i_comment - the C<IMAGENAME> field from the image.  Also written to
1677 the file when writing.
1678
1679 =item *
1680
1681 sgi_pixmin, sgi_pixmax - the C<PIXMIN> and C<PIXMAX> fields from the
1682 image.  On reading image data is expanded from this range to the full
1683 range of samples in the image.
1684
1685 =item *
1686
1687 sgi_bpc - the number of bytes per sample for the image.  Ignored when
1688 writing.
1689
1690 =item *
1691
1692 sgi_rle - whether or not the image is compressed.  If this is non-zero
1693 when writing the image will be compressed.
1694
1695 =back
1696
1697 =head1 ADDING NEW FORMATS
1698
1699 To support a new format for reading, call the register_reader() class
1700 method:
1701
1702 =over
1703
1704 =item register_reader()
1705
1706 Registers single or multiple image read functions.
1707
1708 Parameters:
1709
1710 =over
1711
1712 =item *
1713
1714 type - the identifier of the file format, if Imager's
1715 i_test_format_probe() can identify the format then this value should
1716 match i_test_format_probe()'s result.
1717
1718 This parameter is required.
1719
1720 =item *
1721
1722 single - a code ref to read a single image from a file.  This is
1723 supplied:
1724
1725 =over
1726
1727 =item *
1728
1729 the object that read() was called on,
1730
1731 =item *
1732
1733 an Imager::IO object that should be used to read the file, and
1734
1735 =item *
1736
1737 all the parameters supplied to the read() method.
1738
1739 =back
1740
1741 The single parameter is required.
1742
1743 =item *
1744
1745 multiple - a code ref which is called to read multiple images from a
1746 file. This is supplied:
1747
1748 =over
1749
1750 =item *
1751
1752 an Imager::IO object that should be used to read the file, and
1753
1754 =item *
1755
1756 all the parameters supplied to the read_multi() method.
1757
1758 =back
1759
1760 =back
1761
1762 Example:
1763
1764   # from Imager::File::ICO
1765   Imager->register_reader
1766     (
1767      type=>'ico',
1768      single => 
1769      sub { 
1770        my ($im, $io, %hsh) = @_;
1771        $im->{IMG} = i_readico_single($io, $hsh{page} || 0);
1772
1773        unless ($im->{IMG}) {
1774          $im->_set_error(Imager->_error_as_msg);
1775          return;
1776        }
1777        return $im;
1778      },
1779      multiple =>
1780      sub {
1781        my ($io, %hsh) = @_;
1782      
1783        my @imgs = i_readico_multi($io);
1784        unless (@imgs) {
1785          Imager->_set_error(Imager->_error_as_msg);
1786          return;
1787        }
1788        return map { 
1789          bless { IMG => $_, DEBUG => $Imager::DEBUG, ERRSTR => undef }, 'Imager'
1790        } @imgs;
1791      },
1792     );
1793
1794 =item register_writer()
1795
1796 Registers single or multiple image write functions.
1797
1798 Parameters:
1799
1800 =over
1801
1802 =item *
1803
1804 type - the identifier of the file format.  This is typically the
1805 extension in lowercase.
1806
1807 This parameter is required.
1808
1809 =item *
1810
1811 single - a code ref to write a single image to a file.  This is
1812 supplied:
1813
1814 =over
1815
1816 =item *
1817
1818 the object that write() was called on,
1819
1820 =item *
1821
1822 an Imager::IO object that should be used to write the file, and
1823
1824 =item *
1825
1826 all the parameters supplied to the write() method.
1827
1828 =back
1829
1830 The single parameter is required.
1831
1832 =item *
1833
1834 multiple - a code ref which is called to write multiple images to a
1835 file. This is supplied:
1836
1837 =over
1838
1839 =item *
1840
1841 the class name write_multi() was called on, this is typically
1842 C<Imager>.
1843
1844 =item *
1845
1846 an Imager::IO object that should be used to write the file, and
1847
1848 =item *
1849
1850 all the parameters supplied to the read_multi() method.
1851
1852 =back
1853
1854 =back
1855
1856 =item add_type_extensions($type, $ext, ...)
1857
1858 This class method can be used to add extensions to the map used by
1859 C<def_guess_type> when working out the file type a filename extension.
1860
1861   Imager->add_type_extension(mytype => "mytype", "mytypish");
1862   ...
1863   $im->write(file => "foo.mytypish") # use the mytype handler
1864
1865 =back
1866
1867 If you name the reader module C<Imager::File::>I<your-format-name>
1868 where I<your-format-name> is a fully upper case version of the type
1869 value you would pass to read(), read_multi(), write() or write_multi()
1870 then Imager will attempt to load that module if it has no other way to
1871 read or write that format.
1872
1873 For example, if you create a module Imager::File::GIF and the user has
1874 built Imager without it's normal GIF support then an attempt to read a
1875 GIF image will attempt to load Imager::File::GIF.
1876
1877 If your module can only handle reading then you can name your module
1878 C<Imager::File::>I<your-format-name>C<Reader> and Imager will attempt
1879 to autoload it.
1880
1881 If your module can only handle writing then you can name your module 
1882 C<Imager::File::>I<your-format-name>C<Writer> and Imager will attempt
1883 to autoload it.
1884
1885 =head1 PRELOADING FILE MODULES
1886
1887 =over
1888
1889 =item preload()
1890
1891 This preloads the file support modules included with or that have been
1892 included with Imager in the past.  This is intended for use in forking
1893 servers such as mod_perl.
1894
1895 If the module is not available no error occurs.
1896
1897 Preserves $@.
1898
1899   use Imager;
1900   Imager->preload;
1901
1902 =back
1903
1904 =head1 EXAMPLES
1905
1906 =head2 Producing an image from a CGI script
1907
1908 Once you have an image the basic mechanism is:
1909
1910 =for stopwords STDOUT
1911
1912 =over
1913
1914 =item 1.
1915
1916 set STDOUT to autoflush
1917
1918 =item 2.
1919
1920 output a content-type header, and optionally a content-length header
1921
1922 =item 3.
1923
1924 put STDOUT into binmode
1925
1926 =item 4.
1927
1928 call write() with the C<fd> or C<fh> parameter.  You will need to
1929 provide the C<type> parameter since Imager can't use the extension to
1930 guess the file format you want.
1931
1932 =back
1933
1934   # write an image from a CGI script
1935   # using CGI.pm
1936   use CGI qw(:standard);
1937   $| = 1;
1938   binmode STDOUT;
1939   print header(-type=>'image/gif');
1940   $img->write(type=>'gif', fd=>fileno(STDOUT))
1941     or die $img->errstr;
1942
1943 If you want to send a content length you can send the output to a
1944 scalar to get the length:
1945
1946   my $data;
1947   $img->write(type=>'gif', data=>\$data)
1948     or die $img->errstr;
1949   binmode STDOUT;
1950   print header(-type=>'image/gif', -content_length=>length($data));
1951   print $data;
1952
1953 =head2 Writing an animated GIF
1954
1955 The basic idea is simple, just use write_multi():
1956
1957   my @imgs = ...;
1958   Imager->write_multi({ file=>$filename, type=>'gif' }, @imgs);
1959
1960 If your images are RGB images the default quantization mechanism will
1961 produce a very good result, but can take a long time to execute.  You
1962 could either use the standard web color map:
1963
1964   Imager->write_multi({ file=>$filename, 
1965                         type=>'gif',
1966                         make_colors=>'webmap' },
1967                       @imgs);
1968
1969 or use a median cut algorithm to built a fairly optimal color map:
1970
1971   Imager->write_multi({ file=>$filename,
1972                         type=>'gif',
1973                         make_colors=>'mediancut' },
1974                       @imgs);
1975
1976 By default all of the images will use the same global color map, which
1977 will produce a smaller image.  If your images have significant color
1978 differences, you may want to generate a new palette for each image:
1979
1980   Imager->write_multi({ file=>$filename,
1981                         type=>'gif',
1982                         make_colors=>'mediancut',
1983                         gif_local_map => 1 },
1984                       @imgs);
1985
1986 which will set the C<gif_local_map> tag in each image to 1.
1987 Alternatively, if you know only some images have different colors, you
1988 can set the tag just for those images:
1989
1990   $imgs[2]->settag(name=>'gif_local_map', value=>1);
1991   $imgs[4]->settag(name=>'gif_local_map', value=>1);
1992
1993 and call write_multi() without a C<gif_local_map> parameter, or supply
1994 an arrayref of values for the tag:
1995
1996   Imager->write_multi({ file=>$filename,
1997                         type=>'gif',
1998                         make_colors=>'mediancut',
1999                         gif_local_map => [ 0, 0, 1, 0, 1 ] },
2000                       @imgs);
2001
2002 Other useful parameters include C<gif_delay> to control the delay
2003 between frames and C<transp> to control transparency.
2004
2005 =head2 Reading tags after reading an image
2006
2007 This is pretty simple:
2008
2009   # print the author of a TIFF, if any
2010   my $img = Imager->new;
2011   $img->read(file=>$filename, type='tiff') or die $img->errstr;
2012   my $author = $img->tags(name=>'tiff_author');
2013   if (defined $author) {
2014     print "Author: $author\n";
2015   }
2016
2017 =head1 BUGS
2018
2019 When saving GIF images the program does NOT try to shave off extra
2020 colors if it is possible.  If you specify 128 colors and there are
2021 only 2 colors used - it will have a 128 color table anyway.
2022
2023 =head1 SEE ALSO
2024
2025 Imager(3)
2026
2027 =head1 AUTHOR
2028
2029 Tony Cook <tonyc@cpan.org>, Arnar M. Hrafnkelsson
2030
2031 =cut