API documentation (mostly)
[imager.git] / lib / Imager / Files.pod
CommitLineData
c2188f93
TC
1=head1 NAME
2
3Imager::Files - working with image files
4
5=head1 SYNOPSIS
6
9e003c1a 7 use Imager;
c2188f93
TC
8 my $img = ...;
9 $img->write(file=>$filename, type=>$type)
10 or die "Cannot write: ",$img->errstr;
11
24a462db
TC
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
c2188f93
TC
16 $img = Imager->new;
17 $img->read(file=>$filename, type=>$type)
18 or die "Cannot read: ", $img->errstr;
19
24a462db
TC
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
c2188f93
TC
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
77157728
TC
31 Imager->set_file_limits(width=>$max_width, height=>$max_height)
32
f245645a
TC
33 my @read_types = Imager->read_types;
34 my @write_types = Imager->write_types;
35
24a462db
TC
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
2f2a6e54
TC
54 use Imager 0.68;
55 my $img = Imager->new(file => $filename)
56 or die Imager->errstr;
57
c2188f93
TC
58=head1 DESCRIPTION
59
60You can read and write a variety of images formats, assuming you have
61the appropriate libraries, and images can be read or written to/from
62files, file handles, file descriptors, scalars, or through callbacks.
63
64To see which image formats Imager is compiled to support the following
65code snippet is sufficient:
66
67 use Imager;
68 print join " ", keys %Imager::formats;
69
70This will include some other information identifying libraries rather
f245645a
TC
71than file formats. For new code you might find the L</read_types> or
72L</write_types> methods useful.
c2188f93 73
f7450478
TC
74=over
75
76=item read
77
c2188f93
TC
78Reading writing to and from files is simple, use the C<read()>
79method 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
6e85a9ac
TC
85In most cases Imager can auto-detect the file type, so you can just
86supply the filename:
87
88 $img->read(file => $filename)
89 or die "Cannot read $filename: ", $img->errstr;
90
9c106321
TC
91The read() method accepts the C<allow_partial> parameter. If this is
92non-zero then read() can return true on an incomplete image and set
93the C<i_incomplete> tag.
94
2f2a6e54
TC
95From Imager 0.68 you can supply most read() parameters to the new()
96method to read the image file on creation. If the read fails, check
97Imager->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
f7450478
TC
103=item write
104
c2188f93
TC
105and 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
f7450478
TC
110=item read_multi
111
c2188f93
TC
112If you're reading from a format that supports multiple images per
113file, 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
6e85a9ac
TC
118As with the read() method, Imager will normally detect the C<type>
119automatically.
120
f7450478
TC
121=item write_multi
122
c2188f93
TC
123and if you want to write multiple images to a single file use the
124C<write_multi()> method:
125
126 Imager->write_multi({ file=> $filename, type=>$type }, @images)
127 or die "Cannot write $filename: ", Imager->errstr;
128
f245645a
TC
129=item read_types
130
131This is a class method that returns a list of the image file types
132that Imager can read.
133
134 my @types = Imager->read_types;
135
136These types are the possible values for the C<type> parameter, not
137necessarily the extension of the files you're reading.
138
139It is possible for extra file read handlers to be loaded when
140attempting to read a file, which may modify the list of available read
141types.
142
143=item write_types
144
145This is a class method that returns a list of the image file types
146that Imager can write.
147
148 my @types = Imager->write_types;
149
150Note that these are the possible values for the C<type> parameter, not
151necessarily the extension of the files you're writing.
152
153It is possible for extra file write handlers to be loaded when
154attempting to write a file, which may modify the list of available
155write types.
156
f7450478
TC
157=back
158
6e85a9ac
TC
159When writing, if the I<filename> includes an extension that Imager
160recognizes, then you don't need the I<type>, but you may want to
161provide one anyway. See L</Guessing types> for information on
162controlling this recognition.
c2188f93 163
f6af7cb4
TC
164The C<type> parameter is a lowercase representation of the file type,
165and 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
f3dcbf8a 173 sgi SGI .rgb files
f6af7cb4
TC
174 tga TARGA
175 tiff Tagged Image File Format (TIFF)
176
c2188f93
TC
177When you read an image, Imager may set some tags, possibly including
178information about the spatial resolution, textual information, and
9d1c4956 179animation information. See L<Imager::ImageTypes/Tags> for specifics.
c2188f93 180
e36d02ad
TC
181The open() method is a historical alias for the read() method.
182
c2188f93
TC
183=head2 Input and output
184
185When reading or writing you can specify one of a variety of sources or
186targets:
187
188=over
189
6e85a9ac 190=item *
c2188f93 191
6e85a9ac
TC
192file - The C<file> parameter is the name of the image file to be
193written to or read from. If Imager recognizes the extension of the
194file you do not need to supply a C<type>.
c2188f93 195
1f106142
TC
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
4b387370 207=item *
c2188f93 208
6e85a9ac 209fh - C<fh> is a file handle, typically either returned from
c2188f93
TC
210C<<IO::File->new()>>, or a glob from an C<open> call. You should call
211C<binmode> on the handle before passing it to Imager.
212
9d1c4956
TC
213Imager will set the handle to autoflush to make sure any buffered data
214is flushed , since Imager will write to the file descriptor (from
215fileno()) rather than writing at the perl level.
216
1f106142
TC
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
4b387370 224=item *
c2188f93 225
6e85a9ac 226fd - C<fd> is a file descriptor. You can get this by calling the
c2188f93
TC
227C<fileno()> function on a file handle, or by using one of the standard
228file descriptor numbers.
229
9d1c4956
TC
230If you get this from a perl file handle, you may need to flush any
231buffered output, otherwise it may appear in the output stream after
232the image.
233
1f106142
TC
234 $image->write(fd => file(STDOUT), type => 'gif')
235 or die $image->errstr;
236
4b387370 237=item *
c2188f93 238
6e85a9ac
TC
239data - When reading data, C<data> is a scalar containing the image
240file data, when writing, C<data> is a reference to the scalar to save
241the image file data too. For GIF images you will need giflib 4 or
242higher, and you may need to patch giflib to use this option for
243writing.
c2188f93 244
1f106142
TC
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
b81163cc
TC
250 my @images = Imager->read_multi(data => $data)
251 or die Imager->errstr;
1f106142 252
1f4f4966 253=item *
c2188f93 254
1f4f4966
TC
255callback - Imager will make calls back to your supplied coderefs to
256read, write and seek from/to/through the image file.
c2188f93
TC
257
258When reading from a file you can use either C<callback> or C<readcb>
259to supply the read callback, and when writing C<callback> or
260C<writecb> to supply the write callback.
261
262When writing you can also supply the C<maxbuffer> option to set the
263maximum amount of data that will be buffered before your write
264callback is called. Note: the amount of data supplied to your
265callback can be smaller or larger than this size.
266
267The read callback is called with 2 parameters, the minimum amount of
268data required, and the maximum amount that Imager will store in it's C
269level buffer. You may want to return the minimum if you have a slow
270data source, or the maximum if you have a fast source and want to
271prevent many calls to your perl callback. The read data should be
272returned as a scalar.
273
274Your write callback takes exactly one parameter, a scalar containing
275the data to be written. Return true for success.
276
277The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
278defined in the same way as perl's seek function.
279
280You can also supply a C<closecb> which is called with no parameters
281when there is no more data to be written. This could be used to flush
282buffered data.
283
1f106142
TC
284 # contrived
285 my $data;
286 sub mywrite {
287 $data .= unpack("H*", shift);
288 1;
289 }
b81163cc
TC
290 Imager->write_multi({ callback => \&mywrite, type => 'gif'}, @images)
291 or die Imager->errstr;
1f106142
TC
292
293Note that for reading you'll almost always need to provide a
294C<seekcb>.
295
c2188f93
TC
296=back
297
298=head2 Guessing types
299
9e00434a
TC
300When writing to a file, if you don't supply a C<type> parameter Imager
301will attempt to guess it from the filename. This is done by calling
302the code reference stored in C<$Imager::FORMATGUESS>. This is only
303done when write() or write_multi() is called with a C<file> parameter.
c2188f93 304
d5556805
TC
305The default function value of C<$Imager::FORMATGUESS> is
306C<\&Imager::def_guess_type>.
307
308=over
309
310=item def_guess_type
311
312This is the default function Imager uses to derive a file type from a
313file name. This is a function, not a method.
314
315Accepts a single parameter, the filename and returns the type or
316undef.
317
318=back
9e00434a
TC
319
320You can replace function with your own implementation if you have some
321specialized need. The function takes a single parameter, the name of
322the file, and should return either a file type or under.
c2188f93
TC
323
324 # I'm writing jpegs to weird filenames
325 local $Imager::FORMATGUESS = sub { 'jpeg' };
326
9e00434a
TC
327When reading a file Imager examines beginning of the file for
328identifying information. The current implementation attempts to
329detect the following image types beyond those supported by Imager:
330
331=over
332
333xpm, mng, jng, SGI RGB, ilbm, pcx, fits, psd (Photoshop), eps, Utah
334RLE
335
336=back
337
77157728
TC
338=head2 Limiting the sizes of images you read
339
58a9ba58
TC
340=over
341
342=item set_file_limits
343
77157728
TC
344In some cases you will be receiving images from an untested source,
345such as submissions via CGI. To prevent such images from consuming
346large amounts of memory, you can set limits on the dimensions of
347images you read from files:
348
349=over
350
351=item *
352
353width - limit the width in pixels of the image
354
355=item *
356
357height - limit the height in pixels of the image
358
359=item *
360
361bytes - limits the amount of storage used by the image. This depends
362on the width, height, channels and sample size of the image. For
363paletted images this is calculated as if the image was expanded to a
364direct color image.
365
366=back
367
368To set the limits, call the class method set_file_limits:
369
370 Imager->set_file_limits(width=>$max_width, height=>$max_height);
371
372You can pass any or all of the limits above, any limits you do not
373pass are left as they were.
374
375Any limit of zero is treated as unlimited.
376
377By default, all of the limits are zero, or unlimited.
378
379You can reset all of the limited to their defaults by passing in the
380reset parameter as a true value:
381
382 # no limits
383 Imager->set_file_limits(reset=>1);
384
385This can be used with the other limits to reset all but the limit you
386pass:
387
388 # only width is limited
389 Imager->set_file_limits(reset=>1, width=>100);
390
391 # only bytes is limited
392 Imager->set_file_limits(reset=>1, bytes=>10_000_000);
393
58a9ba58
TC
394=item get_file_limits
395
77157728
TC
396You can get the current limits with the get_file_limits() method:
397
398 my ($max_width, $max_height, $max_bytes) =
399 Imager->get_file_limits();
400
58a9ba58 401=back
77157728 402
c2188f93
TC
403=head1 TYPE SPECIFIC INFORMATION
404
405The different image formats can write different image type, and some have
406different options to control how the images are written.
407
97c4effc
TC
408When you call C<write()> or C<write_multi()> with an option that has
409the same name as a tag for the image format you're writing, then the
410value supplied to that option will be used to set the corresponding
411tag in the image. Depending on the image format, these values will be
412used when writing the image.
413
414This replaces the previous options that were used when writing GIF
415images. Currently if you use an obsolete option, it will be converted
416to the equivalent tag and Imager will produced a warning. You can
417suppress these warnings by calling the C<Imager::init()> function with
418the C<warn_obsolete> option set to false:
419
420 Imager::init(warn_obsolete=>0);
421
422At some point in the future these obsolete options will no longer be
423supported.
424
c2188f93
TC
425=head2 PNM (Portable aNy Map)
426
427Imager can write PGM (Portable Gray Map) and PPM (Portable PixMaps)
428files, depending on the number of channels in the image. Currently
429the images are written in binary formats. Only 1 and 3 channel images
430can be written, including 1 and 3 channel paletted images.
431
432 $img->write(file=>'foo.ppm') or die $img->errstr;
433
434Imager can read both the ASCII and binary versions of each of the PBM
435(Portable BitMap), PGM and PPM formats.
436
437 $img->read(file=>'foo.ppm') or die $img->errstr;
438
439PNM does not support the spatial resolution tags.
440
9c106321
TC
441The following tags are set when reading a PNM file:
442
443=over
444
445=item *
446
447X<pnm_maxval>pnm_maxval - the maxvals number from the PGM/PPM header.
448Always set to 2 for a PBM file.
449
450=item *
451
452X<pnm_type>pnm_type - the type number from the PNM header, 1 for ASCII
453PBM files, 2 for ASCII PGM files, 3 for ASCII PPM files, 4 for binary
454PBM files, 5 for binary PGM files, 6 for binary PPM files.
455
456=back
457
458The following tag is checked when writing an image with more than
4598-bits/sample:
460
461=over
462
463=item *
464
465X<pnm_write_wide_data>pnm_write_wide_data - if this is non-zero then
466write() can write PGM/PPM files with 16-bits/sample. Some
467applications, for example GIMP 2.2, and tools can only read
4688-bit/sample binary PNM files, so Imager will only write a 16-bit
469image when this tag is non-zero.
470
471=back
472
c2188f93
TC
473=head2 JPEG
474
475You can supply a C<jpegquality> parameter (0-100) when writing a JPEG
6e4af7d4
TC
476file, which defaults to 75%. If you write an image with an alpha
477channel to a jpeg file then it will be composited against the
478background set by the C<i_background> parameter (or tag).
c2188f93
TC
479
480 $img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
481
482Imager will read a grayscale JPEG as a 1 channel image and a color
483JPEG as a 3 channel image.
484
485 $img->read(file=>'foo.jpg') or die $img->errstr;
486
6d54291b
TC
487The following tags are set in a JPEG image when read, and can be set
488to control output:
489
490=over
491
492=item jpeg_density_unit
493
494The value of the density unit field in the JFIF header. This is
9c106321 495ignored on writing if the C<i_aspect_only> tag is non-zero.
6d54291b
TC
496
497The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
498matter the value of this tag, they will be converted to/from the value
499stored in the JPEG file.
500
501=item jpeg_density_unit_name
502
503This is set when reading a JPEG file to the name of the unit given by
504C<jpeg_density_unit>. Possible results include C<inch>,
505C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
506these files). If the value of jpeg_density_unit is unknown then this
507tag isn't set.
508
509=item jpeg_comment
510
511Text comment.
512
513=back
514
515JPEG supports the spatial resolution tags C<i_xres>, C<i_yres> and
516C<i_aspect_only>.
f7450478
TC
517
518If an APP1 block containing EXIF information is found, then any of the
519following tags can be set:
520
521=over
522
523exif_aperture exif_artist exif_brightness exif_color_space
524exif_contrast exif_copyright exif_custom_rendered exif_date_time
525exif_date_time_digitized exif_date_time_original
526exif_digital_zoom_ratio exif_exposure_bias exif_exposure_index
527exif_exposure_mode exif_exposure_program exif_exposure_time
528exif_f_number exif_flash exif_flash_energy exif_flashpix_version
529exif_focal_length exif_focal_length_in_35mm_film
530exif_focal_plane_resolution_unit exif_focal_plane_x_resolution
531exif_focal_plane_y_resolution exif_gain_control exif_image_description
532exif_image_unique_id exif_iso_speed_rating exif_make exif_max_aperture
533exif_metering_mode exif_model exif_orientation exif_related_sound_file
534exif_resolution_unit exif_saturation exif_scene_capture_type
535exif_sensing_method exif_sharpness exif_shutter_speed exif_software
536exif_spectral_sensitivity exif_sub_sec_time
537exif_sub_sec_time_digitized exif_sub_sec_time_original
538exif_subject_distance exif_subject_distance_range
539exif_subject_location exif_tag_light_source exif_user_comment
540exif_version exif_white_balance exif_x_resolution exif_y_resolution
541
542=back
543
544The following derived tags can also be set:
545
546=over
547
548exif_color_space_name exif_contrast_name exif_custom_rendered_name
549exif_exposure_mode_name exif_exposure_program_name exif_flash_name
550exif_focal_plane_resolution_unit_name exif_gain_control_name
551exif_light_source_name exif_metering_mode_name
552exif_resolution_unit_name exif_saturation_name
553exif_scene_capture_type_name exif_sensing_method_name
554exif_sharpness_name exif_subject_distance_range_name
555exif_white_balance_name
556
557=back
558
559The derived tags are for enumerated fields, when the value for the
560base field is valid then the text that appears in the EXIF
561specification for that value appears in the derived field. So for
562example if C<exf_metering_mode> is C<5> then
563C<exif_metering_mode_name> is set to C<Pattern>.
c2188f93 564
cb00d347
TC
565eg.
566
567 my $image = Imager->new;
568 $image->read(file => 'exiftest.jpg')
569 or die "Cannot load image: ", $image->errstr;
570 print $image->tags(name => "exif_image_description"), "\n";
571 print $image->tags(name => "exif_exposure_mode"), "\n";
572 print $image->tags(name => "exif_exposure_mode_name"), "\n";
573
574 # for the exiftest.jpg in the Imager distribution the output would be:
575 Imager Development Notes
576 0
577 Auto exposure
578
f0fe9c14
TC
579=over
580
581=item parseiptc
582
583Historically, Imager saves IPTC data when reading a JPEG image, the
584parseiptc() method returns a list of key/value pairs resulting from a
585simple decoding of that data.
586
587Any future IPTC data decoding is likely to go into tags.
588
589=back
590
c2188f93
TC
591=head2 GIF (Graphics Interchange Format)
592
97c4effc
TC
593When writing one of more GIF images you can use the same
594L<Quantization Options|Imager::ImageTypes> as you can when converting
595an RGB image into a paletted image.
61c59c54 596
00424555
TC
597When reading a GIF all of the sub-images are combined using the screen
598size and image positions into one big image, producing an RGB image.
599This may change in the future to produce a paletted image where possible.
600
8889dffd 601When you read a single GIF with C<$img-E<gt>read()> you can supply a
00424555
TC
602reference to a scalar in the C<colors> parameter, if the image is read
603the scalar will be filled with a reference to an anonymous array of
604L<Imager::Color> objects, representing the palette of the image. This
605will be the first palette found in the image. If you want the
606palettes for each of the images in the file, use C<read_multi()> and
607use the C<getcolors()> method on each image.
608
609GIF does not support the spatial resolution tags.
610
97c4effc
TC
611Imager will set the following tags in each image when reading, and can
612use most of them when writing to GIF:
00424555
TC
613
614=over
615
b0618399 616=item *
00424555 617
b0618399
TC
618gif_left - the offset of the image from the left of the "screen"
619("Image Left Position")
00424555 620
b0618399 621=item *
00424555 622
b0618399
TC
623gif_top - the offset of the image from the top of the "screen" ("Image
624Top Position")
00424555 625
b0618399 626=item *
00424555 627
b0618399
TC
628gif_interlace - non-zero if the image was interlaced ("Interlace
629Flag")
00424555 630
b0618399 631=item *
00424555 632
b0618399
TC
633gif_screen_width, gif_screen_height - the size of the logical
634screen. When writing this is used as the minimum. If any image being
635written would extend beyond this then the screen size is extended.
636("Logical Screen Width", "Logical Screen Height").
00424555 637
b0618399 638=item *
97c4effc 639
b0618399
TC
640gif_local_map - Non-zero if this image had a local color map. If set
641for an image when writing the image is quantized separately from the
642other images in the file.
00424555 643
b0618399 644=item *
00424555 645
b0618399
TC
646gif_background - The index in the global colormap of the logical
647screen's background color. This is only set if the current image uses
648the global colormap. You can set this on write too, but for it to
649choose the color you want, you will need to supply only paletted
650images and set the C<gif_eliminate_unused> tag to 0.
00424555 651
b0618399 652=item *
00424555 653
b0618399
TC
654gif_trans_index - The index of the color in the colormap used for
655transparency. If the image has a transparency then it is returned as
656a 4 channel image with the alpha set to zero in this palette entry.
657This value is not used when writing. ("Transparent Color Index")
00424555 658
b0618399 659=item *
97c4effc 660
b0618399
TC
661gif_trans_color - A reference to an Imager::Color object, which is the
662colour to use for the palette entry used to represent transparency in
663the palette. You need to set the transp option (see L<Quantization
664options>) for this value to be used.
97c4effc 665
b0618399 666=item *
00424555 667
b0618399
TC
668gif_delay - The delay until the next frame is displayed, in 1/100 of a
669second. ("Delay Time").
00424555 670
b0618399 671=item *
00424555 672
b0618399
TC
673gif_user_input - whether or not a user input is expected before
674continuing (view dependent) ("User Input Flag").
00424555 675
b0618399 676=item *
00424555 677
b0618399 678gif_disposal - how the next frame is displayed ("Disposal Method")
00424555 679
b0618399 680=item *
00424555 681
b0618399
TC
682gif_loop - the number of loops from the Netscape Loop extension. This
683may be zero to loop forever.
00424555 684
b0618399 685=item *
00424555 686
b0618399
TC
687gif_comment - the first block of the first gif comment before each
688image.
00424555 689
b0618399 690=item *
00424555 691
b0618399
TC
692gif_eliminate_unused - If this is true, when you write a paletted
693image any unused colors will be eliminated from its palette. This is
694set by default.
695
696=item *
97c4effc 697
b0618399
TC
698gif_colormap_size - the original size of the color map for the image.
699The color map of the image may have been expanded to include out of
700range color indexes.
97c4effc 701
00424555
TC
702=back
703
704Where applicable, the ("name") is the name of that field from the GIF89
705standard.
c2188f93 706
97c4effc
TC
707The following gif writing options are obsolete, you should set the
708corresponding tag in the image, either by using the tags functions, or
709by supplying the tag and value as options.
710
711=over
712
b0618399 713=item *
97c4effc 714
b0618399
TC
715gif_each_palette - Each image in the gif file has it's own palette if
716this is non-zero. All but the first image has a local colour table
717(the first uses the global colour table.
97c4effc
TC
718
719Use C<gif_local_map> in new code.
720
b0618399 721=item *
97c4effc 722
b0618399 723interlace - The images are written interlaced if this is non-zero.
97c4effc
TC
724
725Use C<gif_interlace> in new code.
726
b0618399 727=item *
97c4effc 728
b0618399
TC
729gif_delays - A reference to an array containing the delays between
730images, in 1/100 seconds.
97c4effc
TC
731
732Use C<gif_delay> in new code.
733
b0618399 734=item *
97c4effc 735
b0618399
TC
736gif_positions - A reference to an array of references to arrays which
737represent screen positions for each image.
97c4effc
TC
738
739New code should use the C<gif_left> and C<gif_top> tags.
740
b0618399 741=item *
97c4effc 742
b0618399
TC
743gif_loop_count - If this is non-zero the Netscape loop extension block
744is generated, which makes the animation of the images repeat.
97c4effc
TC
745
746This is currently unimplemented due to some limitations in giflib.
747
748=back
749
f1adece7
TC
750You can supply a C<page> parameter to the C<read()> method to read
751some page other than the first. The page is 0 based:
752
753 # read the second image in the file
754 $image->read(file=>"example.gif", page=>1)
755 or die "Cannot read second page: ",$image->errstr,"\n";
756
757Before release 0.46, Imager would read multi-image GIF image files
758into a single image, overlaying each of the images onto the virtual
759GIF screen.
760
761As of 0.46 the default is to read the first image from the file, as if
762called with C<< page => 0 >>.
763
764You can return to the previous behaviour by calling read with the
765C<gif_consolidate> parameter set to a true value:
766
767 $img->read(file=>$some_gif_file, gif_consolidate=>1);
768
5c0d0ddf
TC
769As with the to_paletted() method, if you supply a colors parameter as
770a reference to an array, this will be filled with Imager::Color
771objects of the color table generated for the image file.
772
c2188f93
TC
773=head2 TIFF (Tagged Image File Format)
774
b5dd0159
TC
775Imager can write images to either paletted or RGB TIFF images,
776depending on the type of the source image. Currently if you write a
77716-bit/sample or double/sample image it will be written as an
7788-bit/sample image. Only 1 or 3 channel images can be written.
779
780If you are creating images for faxing you can set the I<class>
781parameter set to C<fax>. By default the image is written in fine
782mode, but this can be overridden by setting the I<fax_fine> parameter
783to zero. Since a fax image is bi-level, Imager uses a threshold to
784decide if a given pixel is black or white, based on a single channel.
785For greyscale images channel 0 is used, for color images channel 1
786(green) is used. If you want more control over the conversion you can
787use $img->to_paletted() to product a bi-level image. This way you can
788use dithering:
789
bd8052a6 790 my $bilevel = $img->to_paletted(make_colors => 'mono',
b5dd0159
TC
791 translate => 'errdiff',
792 errdiff => 'stucki');
00424555 793
b5dd0159
TC
794=over
795
796=item class
797
798If set to 'fax' the image will be written as a bi-level fax image.
799
800=item fax_fine
801
802By default when I<class> is set to 'fax' the image is written in fine
803mode, you can select normal mode by setting I<fax_fine> to 0.
804
805=back
806
807Imager should be able to read any TIFF image you supply. Paletted
808TIFF images are read as paletted Imager images, since paletted TIFF
809images have 16-bits/sample (48-bits/color) this means the bottom
8108-bits are lost, but this shouldn't be a big deal. Currently all
811direct color images are read at 8-bits/sample.
812
813TIFF supports the spatial resolution tags. See the
814C<tiff_resolutionunit> tag for some extra options.
00424555 815
bd8052a6
TC
816As of Imager 0.62 Imager reads:
817
818=over
819
820=item *
821
82216-bit grey, RGB, or CMYK image, including a possible alpha channel as
823a 16-bit/sample image.
824
825=item *
826
82732-bit grey, RGB image, including a possible alpha channel as a
828double/sample image.
829
830=item *
831
832bi-level images as paletted images containing only black and white,
833which other formats will also write as bi-level.
834
835=item *
836
837tiled paletted images are now handled correctly
838
839=back
840
5df0fac7
AMH
841The following tags are set in a TIFF image when read, and can be set
842to control output:
843
844=over
845
bd8052a6
TC
846=item tiff_compression
847
848When reading an image this is set to the numeric value of the TIFF
849compression tag.
850
851On writing you can set this to either a numeric compression tag value,
852or one of the following values:
853
854 Ident Number Description
855 none 1 No compression
856 packbits 32773 Macintosh RLE
857 ccittrle 2 CCITT RLE
858 fax3 3 CCITT Group 3 fax encoding (T.4)
859 t4 3 As above
860 fax4 4 CCITT Group 4 fax encoding (T.6)
861 t6 4 As above
862 lzw 5 LZW
863 jpeg 7 JPEG
864 zip 8 Deflate (GZIP) Non-standard
865 deflate 8 As above.
866 oldzip 32946 Deflate with an older code.
867 ccittrlew 32771 Word aligned CCITT RLE
868
869In general a compression setting will be ignored where it doesn't make
870sense, eg. C<jpeg> will be ignored for compression if the image is
871being written as bilevel.
872
873Imager attempts to check that your build of libtiff supports the given
874compression, and will fallback to C<packbits> if it isn't enabled.
875eg. older distributions didn't include LZW compression, and JPEG
876compression is only available if libtiff is configured with libjpeg's
877location.
878
879 $im->write(file => 'foo.tif', tiff_compression => 'lzw')
880 or die $im->errstr;
881
882=item tiff_jpegquality
883
884If I<tiff_compression> if C<jpeg> then this can be a number from 1 to
885100 giving the JPEG compression quality. High values are better
886quality and larger files.
887
5df0fac7
AMH
888=item tiff_resolutionunit
889
890The value of the ResolutionUnit tag. This is ignored on writing if
891the i_aspect_only tag is non-zero.
892
b5dd0159 893The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
6d54291b 894matter the value of this tag, they will be converted to/from the value
b5dd0159
TC
895stored in the TIFF file.
896
3cff89e2
TC
897=item tiff_resolutionunit_name
898
899This is set when reading a TIFF file to the name of the unit given by
900C<tiff_resolutionunit>. Possible results include C<inch>,
901C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
902these files) or C<unknown>.
903
f00e06a0
TC
904=item tiff_bitspersample
905
906Bits per sample from the image. This value is not used when writing
907an image, it is only set on a read image.
908
909=item tiff_photometric
910
911Value of the PhotometricInterpretation tag from the image. This value
912is not used when writing an image, it is only set on a read image.
913
5df0fac7
AMH
914=item tiff_documentname
915
916=item tiff_imagedescription
917
918=item tiff_make
919
920=item tiff_model
921
922=item tiff_pagename
923
924=item tiff_software
925
926=item tiff_datetime
927
928=item tiff_artist
929
930=item tiff_hostcomputer
931
932Various strings describing the image. tiff_datetime must be formatted
933as "YYYY:MM:DD HH:MM:SS". These correspond directly to the mixed case
934names in the TIFF specification. These are set in images read from a
b5dd0159 935TIFF and saved when writing a TIFF image.
5df0fac7 936
377f56e5
TC
937=back
938
8f8bd9aa
TC
939You can supply a C<page> parameter to the C<read()> method to read
940some page other than the first. The page is 0 based:
941
942 # read the second image in the file
943 $image->read(file=>"example.tif", page=>1)
944 or die "Cannot read second page: ",$image->errstr,"\n";
945
a50608d2
TC
946Note: Imager uses the TIFF*RGBA* family of libtiff functions,
947unfortunately these don't support alpha channels on CMYK images. This
948will result in a full coverage alpha channel on CMYK images with an
949alpha channel, until this is implemented in libtiff (or Imager's TIFF
950implementation changes.)
951
952If you read an image with multiple alpha channels, then only the first
953alpha channel will be read.
954
955Currently Imager's TIFF support reads all direct color images as 8-bit
956RGB images, this may change in the future to reading 16-bit/sample
957images.
958
959Currently tags that control the output color type and compression are
960ignored when writing, this may change in the future. If you have
961processes that rely upon Imager always producing packbits compressed
962RGB images, you should strip any tags before writing.
963
b5dd0159 964=head2 BMP (BitMaP)
5df0fac7 965
b5dd0159
TC
966Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
967Windows BMP files. Currently you cannot write compressed BMP files
968with Imager.
5df0fac7 969
b5dd0159
TC
970Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
971Windows BMP files. There is some support for reading 16-bit per pixel
972images, but I haven't found any for testing.
5df0fac7 973
b5dd0159 974BMP has no support for multi-image files.
c2188f93 975
b5dd0159
TC
976BMP files support the spatial resolution tags, but since BMP has no
977support for storing only an aspect ratio, if C<i_aspect_only> is set
978when you write the C<i_xres> and C<i_yres> values are scaled so the
b294e724 979smaller is 72 DPI.
5df0fac7 980
b5dd0159 981The following tags are set when you read an image from a BMP file:
5df0fac7
AMH
982
983=over
984
985=item bmp_compression
986
b5dd0159
TC
987The type of compression, if any. This can be any of the following
988values:
989
990=over
991
992=item BI_RGB (0)
993
994Uncompressed.
995
996=item BI_RLE8 (1)
997
9988-bits/pixel paletted value RLE compression.
999
1000=item BI_RLE4 (2)
1001
10024-bits/pixel paletted value RLE compression.
1003
1004=item BI_BITFIELDS (3)
1005
1006Packed RGB values.
1007
1008=back
5df0fac7 1009
662e3c02
TC
1010=item bmp_compression_name
1011
1012The bmp_compression value as a BI_* string
1013
5df0fac7
AMH
1014=item bmp_important_colors
1015
1016The number of important colors as defined by the writer of the image.
1017
662e3c02
TC
1018=item bmp_used_colors
1019
1020Number of color used from the BMP header
1021
1022=item bmp_filesize
1023
1024The file size from the BMP header
1025
1026=item bmp_bit_count
1027
1028Number of bits stored per pixel. (24, 8, 4 or 1)
1029
5df0fac7
AMH
1030=back
1031
b5dd0159
TC
1032=head2 TGA (TarGA)
1033
f5fd108b
AMH
1034When storing targa images rle compression can be activated with the
1035'compress' parameter, the 'idstring' parameter can be used to set the
1036targa comment field and the 'wierdpack' option can be used to use the
103715 and 16 bit targa formats for rgb and rgba data. The 15 bit format
1038has 5 of each red, green and blue. The 16 bit format in addition
1039allows 1 bit of alpha. The most significant bits are used for each
1040channel.
1041
1042
b5dd0159 1043Tags:
5df0fac7 1044
b5dd0159 1045=over
5df0fac7 1046
b5dd0159 1047=item tga_idstring
5df0fac7 1048
b5dd0159 1049=item tga_bitspp
5df0fac7 1050
b5dd0159 1051=item compressed
5df0fac7 1052
b5dd0159
TC
1053=back
1054
f5fd108b
AMH
1055=head2 RAW
1056
f5fd108b
AMH
1057When reading raw images you need to supply the width and height of the
1058image in the xsize and ysize options:
1059
1060 $img->read(file=>'foo.raw', xsize=>100, ysize=>100)
1061 or die "Cannot read raw image\n";
1062
1063If your input file has more channels than you want, or (as is common),
1064junk in the fourth channel, you can use the datachannels and
1065storechannels options to control the number of channels in your input
1066file and the resulting channels in your image. For example, if your
1067input image uses 32-bits per pixel with red, green, blue and junk
1068values for each pixel you could do:
1069
1070 $img->read(file=>'foo.raw', xsize=>100, ysize=>100, datachannels=>4,
1071 storechannels=>3)
1072 or die "Cannot read raw image\n";
1073
500888da 1074Read parameters:
f5fd108b 1075
500888da
TC
1076=over
1077
1078=item *
1079
1080raw_interleave - controls the ordering of samples within the image.
1081Default: 1. Alternatively and historically spelled C<interleave>.
1082Possible values:
1083
1084=over
1085
1086=item *
1087
10880 - samples are pixel by pixel, so all samples for the first pixel,
1089then all samples for the second pixel and so on. eg. for a four pixel
1090scanline the channels would be laid out as:
1091
1092 012012012012
1093
1094=item *
1095
10961 - samples are line by line, so channel 0 for the entire scanline is
1097followed by channel 1 for the entire scanline and so on. eg. for a
1098four pixel scanline the channels would be laid out as:
1099
1100 000011112222
1101
1102This is the default.
1103
1104=back
1105
1106Unfortunately, historically, the default C<raw_interleave> for read
1107has been 1, while writing only supports the C<raw_interleave> = 0
1108format.
1109
1110For future compatibility, you should always supply the
1111C<raw_interleave> (or C<interleave>) parameter. As of 0.68, Imager
1112will warn if you attempt to read a raw image without a
1113C<raw_interleave> parameter.
1114
1115=item *
1116
1117raw_storechannels - the number of channels to store in the image.
1118Range: 1 to 4. Default: 3. Alternatively and historically spelled
1119C<storechannels>.
1120
1121=item *
1122
1123raw_datachannels - the number of channels to read from the file.
1124Range: 1 or more. Default: 3. Alternatively and historically spelled
1125C<datachannels>.
1126
1127=back
1128
1129 $img->read(file=>'foo.raw', xsize=100, ysize=>100, raw_interleave=>1)
f5fd108b
AMH
1130 or die "Cannot read raw image\n";
1131
f52db34b
TC
1132=head2 PNG
1133
1134There are no PNG specific tags.
1135
2b405c9e
TC
1136=head2 ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)
1137
1138Icon and Cursor files are very similar, the only differences being a
1139number in the header and the storage of the cursor hotspot. I've
1140treated them separately so that you're not messing with tags to
1141distinguish between them.
1142
1143The following tags are set when reading an icon image and are used
1144when writing it:
1145
1146=over
1147
1148=item ico_mask
1149
1150This is the AND mask of the icon. When used as an icon in Windows 1
1151bits in the mask correspond to pixels that are modified by the source
1152image rather than simply replaced by the source image.
1153
1154Rather than requiring a binary bitmap this is accepted in a specific format:
1155
1156=over
1157
1158=item *
1159
1160first line consisting of the 0 placeholder, the 1 placeholder and a
1161newline.
1162
1163=item *
1164
1165following lines which contain 0 and 1 placeholders for each scanline
1166of the image, starting from the top of the image.
1167
1168=back
1169
1170When reading an image, '.' is used as the 0 placeholder and '*' as the
11711 placeholder. An example:
1172
1173 .*
1174 ..........................******
1175 ..........................******
1176 ..........................******
1177 ..........................******
1178 ...........................*****
1179 ............................****
1180 ............................****
1181 .............................***
1182 .............................***
1183 .............................***
1184 .............................***
1185 ..............................**
1186 ..............................**
1187 ...............................*
1188 ...............................*
1189 ................................
1190 ................................
1191 ................................
1192 ................................
1193 ................................
1194 ................................
1195 *...............................
1196 **..............................
1197 **..............................
1198 ***.............................
1199 ***.............................
1200 ****............................
1201 ****............................
1202 *****...........................
1203 *****...........................
1204 *****...........................
1205 *****...........................
1206
1207=back
1208
1209The following tags are set when reading an icon:
1210
1211=over
1212
1213=item ico_bits
1214
1215The number of bits per pixel used to store the image.
1216
1217=back
1218
1219For cursor files the following tags are set and read when reading and
1220writing:
1221
1222=over
1223
1224=item cur_mask
1225
1226This is the same as the ico_mask above.
1227
1228=item cur_hotspotx
1229
1230=item cur_hotspoty
1231
1232The "hot" spot of the cursor image. This is the spot on the cursor
1233that you click with. If you set these to out of range values they are
1234clipped to the size of the image when written to the file.
1235
1236=back
1237
413dc198
TC
1238The following parameters can be supplied to read() or read_multi() to
1239control reading of ICO/CUR files:
1240
1241=over
1242
1243=item *
1244
1245ico_masked - if true, the default, then the icon/cursors mask is
1246applied as an alpha channel to the image. This may result in a
1247paletted image being returned as a direct color image. Default: 1
1248
1249 # retrieve the image as stored, without using the mask as an alpha
1250 # channel
1251 $img->read(file => 'foo.ico', ico_masked => 0)
1252 or die $img->errstr;
1253
1254This was introduced in Imager 0.60. Previously reading ICO images
6cfee9d1 1255acted as if C<ico_masked =E<gt> 0>.
413dc198
TC
1256
1257=back
1258
2b405c9e
TC
1259C<cur_bits> is set when reading a cursor.
1260
1261Examples:
1262
1263 my $img = Imager->new(xsize => 32, ysize => 32, channels => 4);
1264 $im->box(color => 'FF0000');
1265 $im->write(file => 'box.ico');
1266
1267 $im->settag(name => 'cur_hotspotx', value => 16);
1268 $im->settag(name => 'cur_hotspoty', value => 16);
1269 $im->write(file => 'box.cur');
1270
d5477d3d
TC
1271=head2 SGI (RGB, BW)
1272
1273SGI images, often called by the extensions, RGB or BW, can be stored
1274either uncompressed or compressed using an RLE compression.
1275
1276By default, when saving to an extension of C<rgb>, C<bw>, C<sgi>,
1277C<rgba> the file will be saved in SGI format. The file extension is
1278otherwise ignored, so saving a 3-channel image to a C<.bw> file will
1279result in a 3-channel image on disk.
1280
1281The following tags are set when reading a SGI image:
1282
1283=over
1284
1285=item *
1286
1287i_comment - the IMAGENAME field from the image. Also written to the
1288file when writing.
1289
1290=item *
1291
1292sgi_pixmin, sgi_pixmax - the PIXMIN and PIXMAX fields from the image.
1293On reading image data is expanded from this range to the full range of
1294samples in the image.
1295
1296=item *
1297
1298sgi_bpc - the number of bytes per sample for the image. Ignored when
1299writing.
1300
1301=item *
1302
1303sgi_rle - whether or not the image is compressed. If this is non-zero
1304when writing the image will be compressed.
1305
1306=back
1307
53a6bbd4
TC
1308=head1 ADDING NEW FORMATS
1309
1310To support a new format for reading, call the register_reader() class
1311method:
1312
1313=over
1314
1315=item register_reader
1316
1317Registers single or multiple image read functions.
1318
1319Parameters:
1320
1321=over
1322
1323=item *
1324
1325type - the identifier of the file format, if Imager's
1326i_test_format_probe() can identify the format then this value should
1327match i_test_format_probe()'s result.
1328
1329This parameter is required.
1330
1331=item *
1332
1333single - a code ref to read a single image from a file. This is
1334supplied:
1335
1336=over
1337
1338=item *
1339
1340the object that read() was called on,
1341
1342=item *
1343
1344an Imager::IO object that should be used to read the file, and
1345
1346=item *
1347
1348all the parameters supplied to the read() method.
1349
1350=back
1351
1352The single parameter is required.
1353
1354=item *
1355
1356multiple - a code ref which is called to read multiple images from a
1357file. This is supplied:
1358
1359=over
1360
1361=item *
1362
1363an Imager::IO object that should be used to read the file, and
1364
1365=item *
1366
1367all the parameters supplied to the read_multi() method.
1368
1369=back
1370
1371=back
1372
1373Example:
1374
1375 # from Imager::File::ICO
1376 Imager->register_reader
1377 (
1378 type=>'ico',
1379 single =>
1380 sub {
1381 my ($im, $io, %hsh) = @_;
1382 $im->{IMG} = i_readico_single($io, $hsh{page} || 0);
1383
1384 unless ($im->{IMG}) {
1385 $im->_set_error(Imager->_error_as_msg);
1386 return;
1387 }
1388 return $im;
1389 },
1390 multiple =>
1391 sub {
1392 my ($io, %hsh) = @_;
1393
1394 my @imgs = i_readico_multi($io);
1395 unless (@imgs) {
1396 Imager->_set_error(Imager->_error_as_msg);
1397 return;
1398 }
1399 return map {
1400 bless { IMG => $_, DEBUG => $Imager::DEBUG, ERRSTR => undef }, 'Imager'
1401 } @imgs;
1402 },
1403 );
1404
2b405c9e
TC
1405=item register_writer
1406
1407Registers single or multiple image write functions.
1408
1409Parameters:
1410
1411=over
1412
1413=item *
1414
1415type - the identifier of the file format. This is typically the
1416extension in lowercase.
1417
1418This parameter is required.
1419
1420=item *
1421
1422single - a code ref to write a single image to a file. This is
1423supplied:
1424
1425=over
1426
1427=item *
1428
1429the object that write() was called on,
1430
1431=item *
1432
1433an Imager::IO object that should be used to write the file, and
1434
1435=item *
1436
1437all the parameters supplied to the write() method.
1438
1439=back
1440
1441The single parameter is required.
1442
1443=item *
1444
1445multiple - a code ref which is called to write multiple images to a
1446file. This is supplied:
1447
1448=over
1449
1450=item *
1451
1452the class name write_multi() was called on, this is typically
1453C<Imager>.
1454
1455=item *
1456
1457an Imager::IO object that should be used to write the file, and
1458
1459=item *
1460
1461all the parameters supplied to the read_multi() method.
1462
1463=back
1464
1465=back
1466
53a6bbd4
TC
1467=back
1468
1469If you name the reader module C<Imager::File::>I<your-format-name>
1470where I<your-format-name> is a fully upper case version of the type
2b405c9e
TC
1471value you would pass to read(), read_multi(), write() or write_multi()
1472then Imager will attempt to load that module if it has no other way to
1473read or write that format.
53a6bbd4
TC
1474
1475For example, if you create a module Imager::File::GIF and the user has
1476built Imager without it's normal GIF support then an attempt to read a
1477GIF image will attempt to load Imager::File::GIF.
1478
2b405c9e
TC
1479If your module can only handle reading then you can name your module
1480C<Imager::File::>I<your-format-name>C<Reader> and Imager will attempt
1481to autoload it.
1482
1483If your module can only handle writing then you can name your module
1484C<Imager::File::>I<your-format-name>C<Writer> and Imager will attempt
1485to autoload it.
1486
9d1c4956 1487=head1 EXAMPLES
f5fd108b 1488
9d1c4956 1489=head2 Producing an image from a CGI script
f5fd108b 1490
9d1c4956
TC
1491Once you have an image the basic mechanism is:
1492
1493=over
1494
1495=item 1.
1496
1497set STDOUT to autoflush
1498
1499=item 2.
1500
1501output a content-type header, and optionally a content-length header
1502
1503=item 3.
1504
1505put STDOUT into binmode
1506
1507=item 4.
1508
1509call write() with the C<fd> or C<fh> parameter. You will need to
926880d8
TC
1510provide the C<type> parameter since Imager can't use the extension to
1511guess the file format you want.
9d1c4956
TC
1512
1513=back
1514
1515 # write an image from a CGI script
1516 # using CGI.pm
1517 use CGI qw(:standard);
1518 $| = 1;
1519 binmode STDOUT;
1520 print header(-type=>'image/gif');
1521 $img->write(type=>'gif', fd=>fileno(STDOUT))
1522 or die $img->errstr;
b5dd0159 1523
9d1c4956
TC
1524If you want to send a content length you can send the output to a
1525scalar to get the length:
b5dd0159 1526
9d1c4956
TC
1527 my $data;
1528 $img->write(type=>'gif', data=>\$data)
1529 or die $img->errstr;
1530 binmode STDOUT;
1531 print header(-type=>'image/gif', -content_length=>length($data));
1532 print $data;
b5dd0159 1533
9d1c4956 1534=head2 Writing an animated GIF
c2188f93 1535
9d1c4956
TC
1536The basic idea is simple, just use write_multi():
1537
1538 my @imgs = ...;
1539 Imager->write_multi({ file=>$filename, type=>'gif' }, @imgs);
1540
1541If your images are RGB images the default quantization mechanism will
1542produce a very good result, but can take a long time to execute. You
1543could either use the standard webmap:
1544
1545 Imager->write_multi({ file=>$filename,
1546 type=>'gif',
1547 make_colors=>'webmap' },
1548 @imgs);
1549
1550or use a median cut algorithm to built a fairly optimal color map:
1551
1552 Imager->write_multi({ file=>$filename,
1553 type=>'gif',
1554 make_colors=>'mediancut' },
1555 @imgs);
1556
1557By default all of the images will use the same global colormap, which
1558will produce a smaller image. If your images have significant color
1559differences, you may want to generate a new palette for each image:
1560
1561 Imager->write_multi({ file=>$filename,
1562 type=>'gif',
1563 make_colors=>'mediancut',
1564 gif_local_map => 1 },
1565 @imgs);
1566
1567which will set the C<gif_local_map> tag in each image to 1.
1568Alternatively, if you know only some images have different colors, you
1569can set the tag just for those images:
1570
1571 $imgs[2]->settag(name=>'gif_local_map', value=>1);
1572 $imgs[4]->settag(name=>'gif_local_map', value=>1);
1573
1574and call write_multi() without a C<gif_local_map> parameter, or supply
1575an arrayref of values for the tag:
1576
1577 Imager->write_multi({ file=>$filename,
1578 type=>'gif',
1579 make_colors=>'mediancut',
1580 gif_local_map => [ 0, 0, 1, 0, 1 ] },
1581 @imgs);
1582
1583Other useful parameters include C<gif_delay> to control the delay
1584between frames and C<transp> to control transparency.
1585
1586=head2 Reading tags after reading an image
1587
1588This is pretty simple:
1589
1590 # print the author of a TIFF, if any
1591 my $img = Imager->new;
1592 $img->read(file=>$filename, type='tiff') or die $img->errstr;
1593 my $author = $img->tags(name=>'tiff_author');
1594 if (defined $author) {
1595 print "Author: $author\n";
1596 }
bac4fcee
AMH
1597
1598=head1 BUGS
1599
1600When saving Gif images the program does NOT try to shave of extra
1601colors if it is possible. If you specify 128 colors and there are
1602only 2 colors used - it will have a 128 colortable anyway.
1603
97c4effc
TC
1604=head1 SEE ALSO
1605
1606Imager(3)
bac4fcee 1607
c2188f93 1608=cut