3 Imager::Files - working with image files
8 $img->write(file=>$filename, type=>$type)
9 or die "Cannot write: ",$img->errstr;
12 $img->read(file=>$filename, type=>$type)
13 or die "Cannot read: ", $img->errstr;
15 Imager->write_multi({ file=> $filename, ... }, @images)
16 or die "Cannot write: ", Imager->errstr;
18 my @imgs = Imager->read_multi(file=>$filename)
19 or die "Cannot read: ", Imager->errstr;
21 Imager->set_file_limits(width=>$max_width, height=>$max_height)
25 You can read and write a variety of images formats, assuming you have
26 the appropriate libraries, and images can be read or written to/from
27 files, file handles, file descriptors, scalars, or through callbacks.
29 To see which image formats Imager is compiled to support the following
30 code snippet is sufficient:
33 print join " ", keys %Imager::formats;
35 This will include some other information identifying libraries rather
42 Reading writing to and from files is simple, use the C<read()>
43 method to read an image:
45 my $img = Imager->new;
46 $img->read(file=>$filename, type=>$type)
47 or die "Cannot read $filename: ", $img->errstr;
49 In most cases Imager can auto-detect the file type, so you can just
52 $img->read(file => $filename)
53 or die "Cannot read $filename: ", $img->errstr;
55 The read() method accepts the C<allow_partial> parameter. If this is
56 non-zero then read() can return true on an incomplete image and set
57 the C<i_incomplete> tag.
61 and the C<write()> method to write an image:
63 $img->write(file=>$filename, type=>$type)
64 or die "Cannot write $filename: ", $img->errstr;
68 If you're reading from a format that supports multiple images per
69 file, use the C<read_multi()> method:
71 my @imgs = Imager->read_multi(file=>$filename, type=>$type)
72 or die "Cannot read $filename: ", Imager->errstr;
74 As with the read() method, Imager will normally detect the C<type>
79 and if you want to write multiple images to a single file use the
80 C<write_multi()> method:
82 Imager->write_multi({ file=> $filename, type=>$type }, @images)
83 or die "Cannot write $filename: ", Imager->errstr;
87 When writing, if the I<filename> includes an extension that Imager
88 recognizes, then you don't need the I<type>, but you may want to
89 provide one anyway. See L</Guessing types> for information on
90 controlling this recognition.
92 The C<type> parameter is a lowercase representation of the file type,
93 and can be any of the following:
95 bmp Windows BitMaP (BMP)
96 gif Graphics Interchange Format (GIF)
98 png Portable Network Graphics (PNG)
99 pnm Portable aNyMap (PNM)
103 tiff Tagged Image File Format (TIFF)
105 When you read an image, Imager may set some tags, possibly including
106 information about the spatial resolution, textual information, and
107 animation information. See L<Imager::ImageTypes/Tags> for specifics.
109 The open() method is a historical alias for the read() method.
111 =head2 Input and output
113 When reading or writing you can specify one of a variety of sources or
120 file - The C<file> parameter is the name of the image file to be
121 written to or read from. If Imager recognizes the extension of the
122 file you do not need to supply a C<type>.
124 # write in tiff format
125 $image->write(file => "example.tif")
126 or die $image->errstr;
128 $image->write(file => 'foo.tmp', type => 'tiff')
129 or die $image->errstr;
131 my $image = Imager->new;
132 $image->read(file => 'example.tif')
133 or die $image->errstr;
137 fh - C<fh> is a file handle, typically either returned from
138 C<<IO::File->new()>>, or a glob from an C<open> call. You should call
139 C<binmode> on the handle before passing it to Imager.
141 Imager will set the handle to autoflush to make sure any buffered data
142 is flushed , since Imager will write to the file descriptor (from
143 fileno()) rather than writing at the perl level.
145 $image->write(fh => \*STDOUT, type => 'gif')
146 or die $image->errstr;
148 # for example, a file uploaded via CGI.pm
149 $image->read(fd => $cgi->param('file'))
150 or die $image->errstr;
154 fd - C<fd> is a file descriptor. You can get this by calling the
155 C<fileno()> function on a file handle, or by using one of the standard
156 file descriptor numbers.
158 If you get this from a perl file handle, you may need to flush any
159 buffered output, otherwise it may appear in the output stream after
162 $image->write(fd => file(STDOUT), type => 'gif')
163 or die $image->errstr;
167 data - When reading data, C<data> is a scalar containing the image
168 file data, when writing, C<data> is a reference to the scalar to save
169 the image file data too. For GIF images you will need giflib 4 or
170 higher, and you may need to patch giflib to use this option for
174 $image->write(data => \$data, type => 'tiff')
175 or die $image->errstr;
177 my $data = $row->{someblob}; # eg. from a database
178 my @images = Imager->read_multi(data => $data)
179 or die Imager->errstr;
183 callback - Imager will make calls back to your supplied coderefs to
184 read, write and seek from/to/through the image file.
186 When reading from a file you can use either C<callback> or C<readcb>
187 to supply the read callback, and when writing C<callback> or
188 C<writecb> to supply the write callback.
190 When writing you can also supply the C<maxbuffer> option to set the
191 maximum amount of data that will be buffered before your write
192 callback is called. Note: the amount of data supplied to your
193 callback can be smaller or larger than this size.
195 The read callback is called with 2 parameters, the minimum amount of
196 data required, and the maximum amount that Imager will store in it's C
197 level buffer. You may want to return the minimum if you have a slow
198 data source, or the maximum if you have a fast source and want to
199 prevent many calls to your perl callback. The read data should be
200 returned as a scalar.
202 Your write callback takes exactly one parameter, a scalar containing
203 the data to be written. Return true for success.
205 The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
206 defined in the same way as perl's seek function.
208 You can also supply a C<closecb> which is called with no parameters
209 when there is no more data to be written. This could be used to flush
215 $data .= unpack("H*", shift);
218 Imager->write_multi({ callback => \&mywrite, type => 'gif'}, @images)
219 or die Imager->errstr;
221 Note that for reading you'll almost always need to provide a
226 =head2 Guessing types
228 When writing to a file, if you don't supply a C<type> parameter Imager
229 will attempt to guess it from the filename. This is done by calling
230 the code reference stored in C<$Imager::FORMATGUESS>. This is only
231 done when write() or write_multi() is called with a C<file> parameter.
233 The default function value of C<$Imager::FORMATGUESS> is
234 C<\&Imager::def_guess_type>.
240 This is the default function Imager uses to derive a file type from a
241 file name. This is a function, not a method.
243 Accepts a single parameter, the filename and returns the type or
248 You can replace function with your own implementation if you have some
249 specialized need. The function takes a single parameter, the name of
250 the file, and should return either a file type or under.
252 # I'm writing jpegs to weird filenames
253 local $Imager::FORMATGUESS = sub { 'jpeg' };
255 When reading a file Imager examines beginning of the file for
256 identifying information. The current implementation attempts to
257 detect the following image types beyond those supported by Imager:
261 xpm, mng, jng, SGI RGB, ilbm, pcx, fits, psd (Photoshop), eps, Utah
266 =head2 Limiting the sizes of images you read
270 =item set_file_limits
272 In some cases you will be receiving images from an untested source,
273 such as submissions via CGI. To prevent such images from consuming
274 large amounts of memory, you can set limits on the dimensions of
275 images you read from files:
281 width - limit the width in pixels of the image
285 height - limit the height in pixels of the image
289 bytes - limits the amount of storage used by the image. This depends
290 on the width, height, channels and sample size of the image. For
291 paletted images this is calculated as if the image was expanded to a
296 To set the limits, call the class method set_file_limits:
298 Imager->set_file_limits(width=>$max_width, height=>$max_height);
300 You can pass any or all of the limits above, any limits you do not
301 pass are left as they were.
303 Any limit of zero is treated as unlimited.
305 By default, all of the limits are zero, or unlimited.
307 You can reset all of the limited to their defaults by passing in the
308 reset parameter as a true value:
311 Imager->set_file_limits(reset=>1);
313 This can be used with the other limits to reset all but the limit you
316 # only width is limited
317 Imager->set_file_limits(reset=>1, width=>100);
319 # only bytes is limited
320 Imager->set_file_limits(reset=>1, bytes=>10_000_000);
322 =item get_file_limits
324 You can get the current limits with the get_file_limits() method:
326 my ($max_width, $max_height, $max_bytes) =
327 Imager->get_file_limits();
331 =head1 TYPE SPECIFIC INFORMATION
333 The different image formats can write different image type, and some have
334 different options to control how the images are written.
336 When you call C<write()> or C<write_multi()> with an option that has
337 the same name as a tag for the image format you're writing, then the
338 value supplied to that option will be used to set the corresponding
339 tag in the image. Depending on the image format, these values will be
340 used when writing the image.
342 This replaces the previous options that were used when writing GIF
343 images. Currently if you use an obsolete option, it will be converted
344 to the equivalent tag and Imager will produced a warning. You can
345 suppress these warnings by calling the C<Imager::init()> function with
346 the C<warn_obsolete> option set to false:
348 Imager::init(warn_obsolete=>0);
350 At some point in the future these obsolete options will no longer be
353 =head2 PNM (Portable aNy Map)
355 Imager can write PGM (Portable Gray Map) and PPM (Portable PixMaps)
356 files, depending on the number of channels in the image. Currently
357 the images are written in binary formats. Only 1 and 3 channel images
358 can be written, including 1 and 3 channel paletted images.
360 $img->write(file=>'foo.ppm') or die $img->errstr;
362 Imager can read both the ASCII and binary versions of each of the PBM
363 (Portable BitMap), PGM and PPM formats.
365 $img->read(file=>'foo.ppm') or die $img->errstr;
367 PNM does not support the spatial resolution tags.
369 The following tags are set when reading a PNM file:
375 X<pnm_maxval>pnm_maxval - the maxvals number from the PGM/PPM header.
376 Always set to 2 for a PBM file.
380 X<pnm_type>pnm_type - the type number from the PNM header, 1 for ASCII
381 PBM files, 2 for ASCII PGM files, 3 for ASCII PPM files, 4 for binary
382 PBM files, 5 for binary PGM files, 6 for binary PPM files.
386 The following tag is checked when writing an image with more than
393 X<pnm_write_wide_data>pnm_write_wide_data - if this is non-zero then
394 write() can write PGM/PPM files with 16-bits/sample. Some
395 applications, for example GIMP 2.2, and tools can only read
396 8-bit/sample binary PNM files, so Imager will only write a 16-bit
397 image when this tag is non-zero.
403 You can supply a C<jpegquality> parameter (0-100) when writing a JPEG
404 file, which defaults to 75%. Only 1 and 3 channel images
405 can be written, including 1 and 3 channel paletted images.
407 $img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
409 Imager will read a grayscale JPEG as a 1 channel image and a color
410 JPEG as a 3 channel image.
412 $img->read(file=>'foo.jpg') or die $img->errstr;
414 The following tags are set in a JPEG image when read, and can be set
419 =item jpeg_density_unit
421 The value of the density unit field in the JFIF header. This is
422 ignored on writing if the C<i_aspect_only> tag is non-zero.
424 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
425 matter the value of this tag, they will be converted to/from the value
426 stored in the JPEG file.
428 =item jpeg_density_unit_name
430 This is set when reading a JPEG file to the name of the unit given by
431 C<jpeg_density_unit>. Possible results include C<inch>,
432 C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
433 these files). If the value of jpeg_density_unit is unknown then this
442 JPEG supports the spatial resolution tags C<i_xres>, C<i_yres> and
445 If an APP1 block containing EXIF information is found, then any of the
446 following tags can be set:
450 exif_aperture exif_artist exif_brightness exif_color_space
451 exif_contrast exif_copyright exif_custom_rendered exif_date_time
452 exif_date_time_digitized exif_date_time_original
453 exif_digital_zoom_ratio exif_exposure_bias exif_exposure_index
454 exif_exposure_mode exif_exposure_program exif_exposure_time
455 exif_f_number exif_flash exif_flash_energy exif_flashpix_version
456 exif_focal_length exif_focal_length_in_35mm_film
457 exif_focal_plane_resolution_unit exif_focal_plane_x_resolution
458 exif_focal_plane_y_resolution exif_gain_control exif_image_description
459 exif_image_unique_id exif_iso_speed_rating exif_make exif_max_aperture
460 exif_metering_mode exif_model exif_orientation exif_related_sound_file
461 exif_resolution_unit exif_saturation exif_scene_capture_type
462 exif_sensing_method exif_sharpness exif_shutter_speed exif_software
463 exif_spectral_sensitivity exif_sub_sec_time
464 exif_sub_sec_time_digitized exif_sub_sec_time_original
465 exif_subject_distance exif_subject_distance_range
466 exif_subject_location exif_tag_light_source exif_user_comment
467 exif_version exif_white_balance exif_x_resolution exif_y_resolution
471 The following derived tags can also be set:
475 exif_color_space_name exif_contrast_name exif_custom_rendered_name
476 exif_exposure_mode_name exif_exposure_program_name exif_flash_name
477 exif_focal_plane_resolution_unit_name exif_gain_control_name
478 exif_light_source_name exif_metering_mode_name
479 exif_resolution_unit_name exif_saturation_name
480 exif_scene_capture_type_name exif_sensing_method_name
481 exif_sharpness_name exif_subject_distance_range_name
482 exif_white_balance_name
486 The derived tags are for enumerated fields, when the value for the
487 base field is valid then the text that appears in the EXIF
488 specification for that value appears in the derived field. So for
489 example if C<exf_metering_mode> is C<5> then
490 C<exif_metering_mode_name> is set to C<Pattern>.
494 my $image = Imager->new;
495 $image->read(file => 'exiftest.jpg')
496 or die "Cannot load image: ", $image->errstr;
497 print $image->tags(name => "exif_image_description"), "\n";
498 print $image->tags(name => "exif_exposure_mode"), "\n";
499 print $image->tags(name => "exif_exposure_mode_name"), "\n";
501 # for the exiftest.jpg in the Imager distribution the output would be:
502 Imager Development Notes
510 Historically, Imager saves IPTC data when reading a JPEG image, the
511 parseiptc() method returns a list of key/value pairs resulting from a
512 simple decoding of that data.
514 Any future IPTC data decoding is likely to go into tags.
518 =head2 GIF (Graphics Interchange Format)
520 When writing one of more GIF images you can use the same
521 L<Quantization Options|Imager::ImageTypes> as you can when converting
522 an RGB image into a paletted image.
524 When reading a GIF all of the sub-images are combined using the screen
525 size and image positions into one big image, producing an RGB image.
526 This may change in the future to produce a paletted image where possible.
528 When you read a single GIF with C<$img-E<gt>read()> you can supply a
529 reference to a scalar in the C<colors> parameter, if the image is read
530 the scalar will be filled with a reference to an anonymous array of
531 L<Imager::Color> objects, representing the palette of the image. This
532 will be the first palette found in the image. If you want the
533 palettes for each of the images in the file, use C<read_multi()> and
534 use the C<getcolors()> method on each image.
536 GIF does not support the spatial resolution tags.
538 Imager will set the following tags in each image when reading, and can
539 use most of them when writing to GIF:
545 the offset of the image from the left of the "screen" ("Image Left
550 the offset of the image from the top of the "screen" ("Image Top Position")
554 non-zero if the image was interlaced ("Interlace Flag")
556 =item gif_screen_width
558 =item gif_screen_height
560 the size of the logical screen. When writing this is used as the
561 minimum. If any image being written would extend beyond this the
562 screen size is extended. ("Logical Screen Width", "Logical Screen
565 When writing this is used as a minimum, if the combination of the
566 image size and the image's C<gif_left> and C<gif_top> is beyond this
567 size then the screen size will be expanded.
571 Non-zero if this image had a local color map. If set for an image
572 when writing the image is quantized separately from the other images
577 The index in the global colormap of the logical screen's background
578 color. This is only set if the current image uses the global
579 colormap. You can set this on write too, but for it to choose the
580 color you want, you will need to supply only paletted images and set
581 the C<gif_eliminate_unused> tag to 0.
583 =item gif_trans_index
585 The index of the color in the colormap used for transparency. If the
586 image has a transparency then it is returned as a 4 channel image with
587 the alpha set to zero in this palette entry. This value is not used
588 when writing. ("Transparent Color Index")
590 =item gif_trans_color
592 A reference to an Imager::Color object, which is the colour to use for
593 the palette entry used to represent transparency in the palette. You
594 need to set the transp option (see L<Quantization options>) for this
599 The delay until the next frame is displayed, in 1/100 of a second.
604 whether or not a user input is expected before continuing (view dependent)
609 how the next frame is displayed ("Disposal Method")
613 the number of loops from the Netscape Loop extension. This may be zero.
617 the first block of the first gif comment before each image.
619 =item gif_eliminate_unused
621 If this is true, when you write a paletted image any unused colors
622 will be eliminated from its palette. This is set by default.
626 Where applicable, the ("name") is the name of that field from the GIF89
629 The following gif writing options are obsolete, you should set the
630 corresponding tag in the image, either by using the tags functions, or
631 by supplying the tag and value as options.
635 =item gif_each_palette
637 Each image in the gif file has it's own palette if this is non-zero.
638 All but the first image has a local colour table (the first uses the
641 Use C<gif_local_map> in new code.
645 The images are written interlaced if this is non-zero.
647 Use C<gif_interlace> in new code.
651 A reference to an array containing the delays between images, in 1/100
654 Use C<gif_delay> in new code.
658 A reference to an array of references to arrays which represent screen
659 positions for each image.
661 New code should use the C<gif_left> and C<gif_top> tags.
665 If this is non-zero the Netscape loop extension block is generated,
666 which makes the animation of the images repeat.
668 This is currently unimplemented due to some limitations in giflib.
672 You can supply a C<page> parameter to the C<read()> method to read
673 some page other than the first. The page is 0 based:
675 # read the second image in the file
676 $image->read(file=>"example.gif", page=>1)
677 or die "Cannot read second page: ",$image->errstr,"\n";
679 Before release 0.46, Imager would read multi-image GIF image files
680 into a single image, overlaying each of the images onto the virtual
683 As of 0.46 the default is to read the first image from the file, as if
684 called with C<< page => 0 >>.
686 You can return to the previous behaviour by calling read with the
687 C<gif_consolidate> parameter set to a true value:
689 $img->read(file=>$some_gif_file, gif_consolidate=>1);
691 =head2 TIFF (Tagged Image File Format)
693 Imager can write images to either paletted or RGB TIFF images,
694 depending on the type of the source image. Currently if you write a
695 16-bit/sample or double/sample image it will be written as an
696 8-bit/sample image. Only 1 or 3 channel images can be written.
698 If you are creating images for faxing you can set the I<class>
699 parameter set to C<fax>. By default the image is written in fine
700 mode, but this can be overridden by setting the I<fax_fine> parameter
701 to zero. Since a fax image is bi-level, Imager uses a threshold to
702 decide if a given pixel is black or white, based on a single channel.
703 For greyscale images channel 0 is used, for color images channel 1
704 (green) is used. If you want more control over the conversion you can
705 use $img->to_paletted() to product a bi-level image. This way you can
708 my $bilevel = $img->to_paletted(colors=>[ NC(0,0,0), NC(255,255,255) ],
709 make_colors => 'none',
710 translate => 'errdiff',
711 errdiff => 'stucki');
717 If set to 'fax' the image will be written as a bi-level fax image.
721 By default when I<class> is set to 'fax' the image is written in fine
722 mode, you can select normal mode by setting I<fax_fine> to 0.
726 Imager should be able to read any TIFF image you supply. Paletted
727 TIFF images are read as paletted Imager images, since paletted TIFF
728 images have 16-bits/sample (48-bits/color) this means the bottom
729 8-bits are lost, but this shouldn't be a big deal. Currently all
730 direct color images are read at 8-bits/sample.
732 TIFF supports the spatial resolution tags. See the
733 C<tiff_resolutionunit> tag for some extra options.
735 The following tags are set in a TIFF image when read, and can be set
740 =item tiff_resolutionunit
742 The value of the ResolutionUnit tag. This is ignored on writing if
743 the i_aspect_only tag is non-zero.
745 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
746 matter the value of this tag, they will be converted to/from the value
747 stored in the TIFF file.
749 =item tiff_resolutionunit_name
751 This is set when reading a TIFF file to the name of the unit given by
752 C<tiff_resolutionunit>. Possible results include C<inch>,
753 C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
754 these files) or C<unknown>.
756 =item tiff_bitspersample
758 Bits per sample from the image. This value is not used when writing
759 an image, it is only set on a read image.
761 =item tiff_photometric
763 Value of the PhotometricInterpretation tag from the image. This value
764 is not used when writing an image, it is only set on a read image.
766 =item tiff_documentname
768 =item tiff_imagedescription
782 =item tiff_hostcomputer
784 Various strings describing the image. tiff_datetime must be formatted
785 as "YYYY:MM:DD HH:MM:SS". These correspond directly to the mixed case
786 names in the TIFF specification. These are set in images read from a
787 TIFF and saved when writing a TIFF image.
791 You can supply a C<page> parameter to the C<read()> method to read
792 some page other than the first. The page is 0 based:
794 # read the second image in the file
795 $image->read(file=>"example.tif", page=>1)
796 or die "Cannot read second page: ",$image->errstr,"\n";
798 Note: Imager uses the TIFF*RGBA* family of libtiff functions,
799 unfortunately these don't support alpha channels on CMYK images. This
800 will result in a full coverage alpha channel on CMYK images with an
801 alpha channel, until this is implemented in libtiff (or Imager's TIFF
802 implementation changes.)
804 If you read an image with multiple alpha channels, then only the first
805 alpha channel will be read.
807 Currently Imager's TIFF support reads all direct color images as 8-bit
808 RGB images, this may change in the future to reading 16-bit/sample
811 Currently tags that control the output color type and compression are
812 ignored when writing, this may change in the future. If you have
813 processes that rely upon Imager always producing packbits compressed
814 RGB images, you should strip any tags before writing.
818 Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
819 Windows BMP files. Currently you cannot write compressed BMP files
822 Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
823 Windows BMP files. There is some support for reading 16-bit per pixel
824 images, but I haven't found any for testing.
826 BMP has no support for multi-image files.
828 BMP files support the spatial resolution tags, but since BMP has no
829 support for storing only an aspect ratio, if C<i_aspect_only> is set
830 when you write the C<i_xres> and C<i_yres> values are scaled so the
833 The following tags are set when you read an image from a BMP file:
837 =item bmp_compression
839 The type of compression, if any. This can be any of the following
850 8-bits/pixel paletted value RLE compression.
854 4-bits/pixel paletted value RLE compression.
856 =item BI_BITFIELDS (3)
862 =item bmp_compression_name
864 The bmp_compression value as a BI_* string
866 =item bmp_important_colors
868 The number of important colors as defined by the writer of the image.
870 =item bmp_used_colors
872 Number of color used from the BMP header
876 The file size from the BMP header
880 Number of bits stored per pixel. (24, 8, 4 or 1)
886 When storing targa images rle compression can be activated with the
887 'compress' parameter, the 'idstring' parameter can be used to set the
888 targa comment field and the 'wierdpack' option can be used to use the
889 15 and 16 bit targa formats for rgb and rgba data. The 15 bit format
890 has 5 of each red, green and blue. The 16 bit format in addition
891 allows 1 bit of alpha. The most significant bits are used for each
909 When reading raw images you need to supply the width and height of the
910 image in the xsize and ysize options:
912 $img->read(file=>'foo.raw', xsize=>100, ysize=>100)
913 or die "Cannot read raw image\n";
915 If your input file has more channels than you want, or (as is common),
916 junk in the fourth channel, you can use the datachannels and
917 storechannels options to control the number of channels in your input
918 file and the resulting channels in your image. For example, if your
919 input image uses 32-bits per pixel with red, green, blue and junk
920 values for each pixel you could do:
922 $img->read(file=>'foo.raw', xsize=>100, ysize=>100, datachannels=>4,
924 or die "Cannot read raw image\n";
926 Normally the raw image is expected to have the value for channel 1
927 immediately following channel 0 and channel 2 immediately following
928 channel 1 for each pixel. If your input image has all the channel 0
929 values for the first line of the image, followed by all the channel 1
930 values for the first line and so on, you can use the interleave option:
932 $img->read(file=>'foo.raw', xsize=100, ysize=>100, interleave=>1)
933 or die "Cannot read raw image\n";
937 There are no PNG specific tags.
939 =head2 ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)
941 Icon and Cursor files are very similar, the only differences being a
942 number in the header and the storage of the cursor hotspot. I've
943 treated them separately so that you're not messing with tags to
944 distinguish between them.
946 The following tags are set when reading an icon image and are used
953 This is the AND mask of the icon. When used as an icon in Windows 1
954 bits in the mask correspond to pixels that are modified by the source
955 image rather than simply replaced by the source image.
957 Rather than requiring a binary bitmap this is accepted in a specific format:
963 first line consisting of the 0 placeholder, the 1 placeholder and a
968 following lines which contain 0 and 1 placeholders for each scanline
969 of the image, starting from the top of the image.
973 When reading an image, '.' is used as the 0 placeholder and '*' as the
974 1 placeholder. An example:
977 ..........................******
978 ..........................******
979 ..........................******
980 ..........................******
981 ...........................*****
982 ............................****
983 ............................****
984 .............................***
985 .............................***
986 .............................***
987 .............................***
988 ..............................**
989 ..............................**
990 ...............................*
991 ...............................*
992 ................................
993 ................................
994 ................................
995 ................................
996 ................................
997 ................................
998 *...............................
999 **..............................
1000 **..............................
1001 ***.............................
1002 ***.............................
1003 ****............................
1004 ****............................
1005 *****...........................
1006 *****...........................
1007 *****...........................
1008 *****...........................
1012 The following tags are set when reading an icon:
1018 The number of bits per pixel used to store the image.
1022 For cursor files the following tags are set and read when reading and
1029 This is the same as the ico_mask above.
1035 The "hot" spot of the cursor image. This is the spot on the cursor
1036 that you click with. If you set these to out of range values they are
1037 clipped to the size of the image when written to the file.
1041 C<cur_bits> is set when reading a cursor.
1045 my $img = Imager->new(xsize => 32, ysize => 32, channels => 4);
1046 $im->box(color => 'FF0000');
1047 $im->write(file => 'box.ico');
1049 $im->settag(name => 'cur_hotspotx', value => 16);
1050 $im->settag(name => 'cur_hotspoty', value => 16);
1051 $im->write(file => 'box.cur');
1053 =head1 ADDING NEW FORMATS
1055 To support a new format for reading, call the register_reader() class
1060 =item register_reader
1062 Registers single or multiple image read functions.
1070 type - the identifier of the file format, if Imager's
1071 i_test_format_probe() can identify the format then this value should
1072 match i_test_format_probe()'s result.
1074 This parameter is required.
1078 single - a code ref to read a single image from a file. This is
1085 the object that read() was called on,
1089 an Imager::IO object that should be used to read the file, and
1093 all the parameters supplied to the read() method.
1097 The single parameter is required.
1101 multiple - a code ref which is called to read multiple images from a
1102 file. This is supplied:
1108 an Imager::IO object that should be used to read the file, and
1112 all the parameters supplied to the read_multi() method.
1120 # from Imager::File::ICO
1121 Imager->register_reader
1126 my ($im, $io, %hsh) = @_;
1127 $im->{IMG} = i_readico_single($io, $hsh{page} || 0);
1129 unless ($im->{IMG}) {
1130 $im->_set_error(Imager->_error_as_msg);
1137 my ($io, %hsh) = @_;
1139 my @imgs = i_readico_multi($io);
1141 Imager->_set_error(Imager->_error_as_msg);
1145 bless { IMG => $_, DEBUG => $Imager::DEBUG, ERRSTR => undef }, 'Imager'
1150 =item register_writer
1152 Registers single or multiple image write functions.
1160 type - the identifier of the file format. This is typically the
1161 extension in lowercase.
1163 This parameter is required.
1167 single - a code ref to write a single image to a file. This is
1174 the object that write() was called on,
1178 an Imager::IO object that should be used to write the file, and
1182 all the parameters supplied to the write() method.
1186 The single parameter is required.
1190 multiple - a code ref which is called to write multiple images to a
1191 file. This is supplied:
1197 the class name write_multi() was called on, this is typically
1202 an Imager::IO object that should be used to write the file, and
1206 all the parameters supplied to the read_multi() method.
1214 If you name the reader module C<Imager::File::>I<your-format-name>
1215 where I<your-format-name> is a fully upper case version of the type
1216 value you would pass to read(), read_multi(), write() or write_multi()
1217 then Imager will attempt to load that module if it has no other way to
1218 read or write that format.
1220 For example, if you create a module Imager::File::GIF and the user has
1221 built Imager without it's normal GIF support then an attempt to read a
1222 GIF image will attempt to load Imager::File::GIF.
1224 If your module can only handle reading then you can name your module
1225 C<Imager::File::>I<your-format-name>C<Reader> and Imager will attempt
1228 If your module can only handle writing then you can name your module
1229 C<Imager::File::>I<your-format-name>C<Writer> and Imager will attempt
1234 =head2 Producing an image from a CGI script
1236 Once you have an image the basic mechanism is:
1242 set STDOUT to autoflush
1246 output a content-type header, and optionally a content-length header
1250 put STDOUT into binmode
1254 call write() with the C<fd> or C<fh> parameter. You will need to
1255 provide the C<type> parameter since Imager can't use the extension to
1256 guess the file format you want.
1260 # write an image from a CGI script
1262 use CGI qw(:standard);
1265 print header(-type=>'image/gif');
1266 $img->write(type=>'gif', fd=>fileno(STDOUT))
1267 or die $img->errstr;
1269 If you want to send a content length you can send the output to a
1270 scalar to get the length:
1273 $img->write(type=>'gif', data=>\$data)
1274 or die $img->errstr;
1276 print header(-type=>'image/gif', -content_length=>length($data));
1279 =head2 Writing an animated GIF
1281 The basic idea is simple, just use write_multi():
1284 Imager->write_multi({ file=>$filename, type=>'gif' }, @imgs);
1286 If your images are RGB images the default quantization mechanism will
1287 produce a very good result, but can take a long time to execute. You
1288 could either use the standard webmap:
1290 Imager->write_multi({ file=>$filename,
1292 make_colors=>'webmap' },
1295 or use a median cut algorithm to built a fairly optimal color map:
1297 Imager->write_multi({ file=>$filename,
1299 make_colors=>'mediancut' },
1302 By default all of the images will use the same global colormap, which
1303 will produce a smaller image. If your images have significant color
1304 differences, you may want to generate a new palette for each image:
1306 Imager->write_multi({ file=>$filename,
1308 make_colors=>'mediancut',
1309 gif_local_map => 1 },
1312 which will set the C<gif_local_map> tag in each image to 1.
1313 Alternatively, if you know only some images have different colors, you
1314 can set the tag just for those images:
1316 $imgs[2]->settag(name=>'gif_local_map', value=>1);
1317 $imgs[4]->settag(name=>'gif_local_map', value=>1);
1319 and call write_multi() without a C<gif_local_map> parameter, or supply
1320 an arrayref of values for the tag:
1322 Imager->write_multi({ file=>$filename,
1324 make_colors=>'mediancut',
1325 gif_local_map => [ 0, 0, 1, 0, 1 ] },
1328 Other useful parameters include C<gif_delay> to control the delay
1329 between frames and C<transp> to control transparency.
1331 =head2 Reading tags after reading an image
1333 This is pretty simple:
1335 # print the author of a TIFF, if any
1336 my $img = Imager->new;
1337 $img->read(file=>$filename, type='tiff') or die $img->errstr;
1338 my $author = $img->tags(name=>'tiff_author');
1339 if (defined $author) {
1340 print "Author: $author\n";
1345 When saving Gif images the program does NOT try to shave of extra
1346 colors if it is possible. If you specify 128 colors and there are
1347 only 2 colors used - it will have a 128 colortable anyway.