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