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