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