1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
|
=head1 NAME
Imager::Files - working with image files
=head1 SYNOPSIS
my $img = ...;
$img->write(file=>$filename, type=>$type)
or die "Cannot write: ",$img->errstr;
$img = Imager->new;
$img->read(file=>$filename, type=>$type)
or die "Cannot read: ", $img->errstr;
Imager->write_multi({ file=> $filename, ... }, @images)
or die "Cannot write: ", Imager->errstr;
my @imgs = Imager->read_multi(file=>$filename)
or die "Cannot read: ", Imager->errstr;
Imager->set_file_limits(width=>$max_width, height=>$max_height)
=head1 DESCRIPTION
You can read and write a variety of images formats, assuming you have
the appropriate libraries, and images can be read or written to/from
files, file handles, file descriptors, scalars, or through callbacks.
To see which image formats Imager is compiled to support the following
code snippet is sufficient:
use Imager;
print join " ", keys %Imager::formats;
This will include some other information identifying libraries rather
than file formats.
=over
=item read
Reading writing to and from files is simple, use the C<read()>
method to read an image:
my $img = Imager->new;
$img->read(file=>$filename, type=>$type)
or die "Cannot read $filename: ", $img->errstr;
=item write
and the C<write()> method to write an image:
$img->write(file=>$filename, type=>$type)
or die "Cannot write $filename: ", $img->errstr;
=item read_multi
If you're reading from a format that supports multiple images per
file, use the C<read_multi()> method:
my @imgs = Imager->read_multi(file=>$filename, type=>$type)
or die "Cannot read $filename: ", Imager->errstr;
=item write_multi
and if you want to write multiple images to a single file use the
C<write_multi()> method:
Imager->write_multi({ file=> $filename, type=>$type }, @images)
or die "Cannot write $filename: ", Imager->errstr;
=back
If the I<filename> includes an extension that Imager recognizes, then
you don't need the I<type>, but you may want to provide one anyway.
See L</Guessing types> for information on controlling this
recognition.
The C<type> parameter is a lowercase representation of the file type,
and can be any of the following:
bmp Windows BitMaP (BMP)
gif Graphics Interchange Format (GIF)
jpeg JPEG/JFIF
png Portable Network Graphics (PNG)
pnm Portable aNyMap (PNM)
raw Raw
rgb SGI .rgb files
tga TARGA
tiff Tagged Image File Format (TIFF)
When you read an image, Imager may set some tags, possibly including
information about the spatial resolution, textual information, and
animation information. See L<Imager::ImageTypes/Tags> for specifics.
The open() method is a historical alias for the read() method.
=head2 Input and output
When reading or writing you can specify one of a variety of sources or
targets:
=over
=item file
The C<file> parameter is the name of the image file to be written to
or read from. If Imager recognizes the extension of the file you do
not need to supply a C<type>.
=item fh
C<fh> is a file handle, typically either returned from
C<<IO::File->new()>>, or a glob from an C<open> call. You should call
C<binmode> on the handle before passing it to Imager.
Imager will set the handle to autoflush to make sure any buffered data
is flushed , since Imager will write to the file descriptor (from
fileno()) rather than writing at the perl level.
=item fd
C<fd> is a file descriptor. You can get this by calling the
C<fileno()> function on a file handle, or by using one of the standard
file descriptor numbers.
If you get this from a perl file handle, you may need to flush any
buffered output, otherwise it may appear in the output stream after
the image.
=item data
When reading data, C<data> is a scalar containing the image file data,
when writing, C<data> is a reference to the scalar to save the image
file data too. For GIF images you will need giflib 4 or higher, and
you may need to patch giflib to use this option for writing.
=item callback
Imager will make calls back to your supplied coderefs to read, write
and seek from/to/through the image file.
When reading from a file you can use either C<callback> or C<readcb>
to supply the read callback, and when writing C<callback> or
C<writecb> to supply the write callback.
When writing you can also supply the C<maxbuffer> option to set the
maximum amount of data that will be buffered before your write
callback is called. Note: the amount of data supplied to your
callback can be smaller or larger than this size.
The read callback is called with 2 parameters, the minimum amount of
data required, and the maximum amount that Imager will store in it's C
level buffer. You may want to return the minimum if you have a slow
data source, or the maximum if you have a fast source and want to
prevent many calls to your perl callback. The read data should be
returned as a scalar.
Your write callback takes exactly one parameter, a scalar containing
the data to be written. Return true for success.
The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
defined in the same way as perl's seek function.
You can also supply a C<closecb> which is called with no parameters
when there is no more data to be written. This could be used to flush
buffered data.
=back
=head2 Guessing types
Imager uses the code reference in $Imager::FORMATGUESS to guess the
file type when you don't supply a C<type>. The code reference is
called with a single parameter, the filename of the file. The code
reference is only called if a C<file> parameter is supplied to the
file access method.
Return either a valid Imager file type, or undef.
# I'm writing jpegs to weird filenames
local $Imager::FORMATGUESS = sub { 'jpeg' };
=head2 Limiting the sizes of images you read
=over
=item set_file_limits
In some cases you will be receiving images from an untested source,
such as submissions via CGI. To prevent such images from consuming
large amounts of memory, you can set limits on the dimensions of
images you read from files:
=over
=item *
width - limit the width in pixels of the image
=item *
height - limit the height in pixels of the image
=item *
bytes - limits the amount of storage used by the image. This depends
on the width, height, channels and sample size of the image. For
paletted images this is calculated as if the image was expanded to a
direct color image.
=back
To set the limits, call the class method set_file_limits:
Imager->set_file_limits(width=>$max_width, height=>$max_height);
You can pass any or all of the limits above, any limits you do not
pass are left as they were.
Any limit of zero is treated as unlimited.
By default, all of the limits are zero, or unlimited.
You can reset all of the limited to their defaults by passing in the
reset parameter as a true value:
# no limits
Imager->set_file_limits(reset=>1);
This can be used with the other limits to reset all but the limit you
pass:
# only width is limited
Imager->set_file_limits(reset=>1, width=>100);
# only bytes is limited
Imager->set_file_limits(reset=>1, bytes=>10_000_000);
=item get_file_limits
You can get the current limits with the get_file_limits() method:
my ($max_width, $max_height, $max_bytes) =
Imager->get_file_limits();
=back
=head1 TYPE SPECIFIC INFORMATION
The different image formats can write different image type, and some have
different options to control how the images are written.
When you call C<write()> or C<write_multi()> with an option that has
the same name as a tag for the image format you're writing, then the
value supplied to that option will be used to set the corresponding
tag in the image. Depending on the image format, these values will be
used when writing the image.
This replaces the previous options that were used when writing GIF
images. Currently if you use an obsolete option, it will be converted
to the equivalent tag and Imager will produced a warning. You can
suppress these warnings by calling the C<Imager::init()> function with
the C<warn_obsolete> option set to false:
Imager::init(warn_obsolete=>0);
At some point in the future these obsolete options will no longer be
supported.
=head2 PNM (Portable aNy Map)
Imager can write PGM (Portable Gray Map) and PPM (Portable PixMaps)
files, depending on the number of channels in the image. Currently
the images are written in binary formats. Only 1 and 3 channel images
can be written, including 1 and 3 channel paletted images.
$img->write(file=>'foo.ppm') or die $img->errstr;
Imager can read both the ASCII and binary versions of each of the PBM
(Portable BitMap), PGM and PPM formats.
$img->read(file=>'foo.ppm') or die $img->errstr;
PNM does not support the spatial resolution tags.
=head2 JPEG
You can supply a C<jpegquality> parameter (0-100) when writing a JPEG
file, which defaults to 75%. Only 1 and 3 channel images
can be written, including 1 and 3 channel paletted images.
$img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
Imager will read a grayscale JPEG as a 1 channel image and a color
JPEG as a 3 channel image.
$img->read(file=>'foo.jpg') or die $img->errstr;
The following tags are set in a JPEG image when read, and can be set
to control output:
=over
=item jpeg_density_unit
The value of the density unit field in the JFIF header. This is
ignored on writing if the i_aspect_only tag is non-zero.
The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
matter the value of this tag, they will be converted to/from the value
stored in the JPEG file.
=item jpeg_density_unit_name
This is set when reading a JPEG file to the name of the unit given by
C<jpeg_density_unit>. Possible results include C<inch>,
C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
these files). If the value of jpeg_density_unit is unknown then this
tag isn't set.
=item jpeg_comment
Text comment.
=back
JPEG supports the spatial resolution tags C<i_xres>, C<i_yres> and
C<i_aspect_only>.
If an APP1 block containing EXIF information is found, then any of the
following tags can be set:
=over
exif_aperture exif_artist exif_brightness exif_color_space
exif_contrast exif_copyright exif_custom_rendered exif_date_time
exif_date_time_digitized exif_date_time_original
exif_digital_zoom_ratio exif_exposure_bias exif_exposure_index
exif_exposure_mode exif_exposure_program exif_exposure_time
exif_f_number exif_flash exif_flash_energy exif_flashpix_version
exif_focal_length exif_focal_length_in_35mm_film
exif_focal_plane_resolution_unit exif_focal_plane_x_resolution
exif_focal_plane_y_resolution exif_gain_control exif_image_description
exif_image_unique_id exif_iso_speed_rating exif_make exif_max_aperture
exif_metering_mode exif_model exif_orientation exif_related_sound_file
exif_resolution_unit exif_saturation exif_scene_capture_type
exif_sensing_method exif_sharpness exif_shutter_speed exif_software
exif_spectral_sensitivity exif_sub_sec_time
exif_sub_sec_time_digitized exif_sub_sec_time_original
exif_subject_distance exif_subject_distance_range
exif_subject_location exif_tag_light_source exif_user_comment
exif_version exif_white_balance exif_x_resolution exif_y_resolution
=back
The following derived tags can also be set:
=over
exif_color_space_name exif_contrast_name exif_custom_rendered_name
exif_exposure_mode_name exif_exposure_program_name exif_flash_name
exif_focal_plane_resolution_unit_name exif_gain_control_name
exif_light_source_name exif_metering_mode_name
exif_resolution_unit_name exif_saturation_name
exif_scene_capture_type_name exif_sensing_method_name
exif_sharpness_name exif_subject_distance_range_name
exif_white_balance_name
=back
The derived tags are for enumerated fields, when the value for the
base field is valid then the text that appears in the EXIF
specification for that value appears in the derived field. So for
example if C<exf_metering_mode> is C<5> then
C<exif_metering_mode_name> is set to C<Pattern>.
=head2 GIF (Graphics Interchange Format)
When writing one of more GIF images you can use the same
L<Quantization Options|Imager::ImageTypes> as you can when converting
an RGB image into a paletted image.
When reading a GIF all of the sub-images are combined using the screen
size and image positions into one big image, producing an RGB image.
This may change in the future to produce a paletted image where possible.
When you read a single GIF with C<$img-E<gt>read()> you can supply a
reference to a scalar in the C<colors> parameter, if the image is read
the scalar will be filled with a reference to an anonymous array of
L<Imager::Color> objects, representing the palette of the image. This
will be the first palette found in the image. If you want the
palettes for each of the images in the file, use C<read_multi()> and
use the C<getcolors()> method on each image.
GIF does not support the spatial resolution tags.
Imager will set the following tags in each image when reading, and can
use most of them when writing to GIF:
=over
=item gif_left
the offset of the image from the left of the "screen" ("Image Left
Position")
=item gif_top
the offset of the image from the top of the "screen" ("Image Top Position")
=item gif_interlace
non-zero if the image was interlaced ("Interlace Flag")
=item gif_screen_width
=item gif_screen_height
the size of the logical screen. When writing this is used as the
minimum. If any image being written would extend beyond this the
screen size is extended. ("Logical Screen Width", "Logical Screen
Height").
When writing this is used as a minimum, if the combination of the
image size and the image's C<gif_left> and C<gif_top> is beyond this
size then the screen size will be expanded.
=item gif_local_map
Non-zero if this image had a local color map. If set for an image
when writing the image is quantized separately from the other images
in the file.
=item gif_background
The index in the global colormap of the logical screen's background
color. This is only set if the current image uses the global
colormap. You can set this on write too, but for it to choose the
color you want, you will need to supply only paletted images and set
the C<gif_eliminate_unused> tag to 0.
=item gif_trans_index
The index of the color in the colormap used for transparency. If the
image has a transparency then it is returned as a 4 channel image with
the alpha set to zero in this palette entry. This value is not used
when writing. ("Transparent Color Index")
=item gif_trans_color
A reference to an Imager::Color object, which is the colour to use for
the palette entry used to represent transparency in the palette. You
need to set the transp option (see L<Quantization options>) for this
value to be used.
=item gif_delay
The delay until the next frame is displayed, in 1/100 of a second.
("Delay Time").
=item gif_user_input
whether or not a user input is expected before continuing (view dependent)
("User Input Flag").
=item gif_disposal
how the next frame is displayed ("Disposal Method")
=item gif_loop
the number of loops from the Netscape Loop extension. This may be zero.
=item gif_comment
the first block of the first gif comment before each image.
=item gif_eliminate_unused
If this is true, when you write a paletted image any unused colors
will be eliminated from its palette. This is set by default.
=back
Where applicable, the ("name") is the name of that field from the GIF89
standard.
The following gif writing options are obsolete, you should set the
corresponding tag in the image, either by using the tags functions, or
by supplying the tag and value as options.
=over
=item gif_each_palette
Each image in the gif file has it's own palette if this is non-zero.
All but the first image has a local colour table (the first uses the
global colour table.
Use C<gif_local_map> in new code.
=item interlace
The images are written interlaced if this is non-zero.
Use C<gif_interlace> in new code.
=item gif_delays
A reference to an array containing the delays between images, in 1/100
seconds.
Use C<gif_delay> in new code.
=item gif_positions
A reference to an array of references to arrays which represent screen
positions for each image.
New code should use the C<gif_left> and C<gif_top> tags.
=item gif_loop_count
If this is non-zero the Netscape loop extension block is generated,
which makes the animation of the images repeat.
This is currently unimplemented due to some limitations in giflib.
=back
You can supply a C<page> parameter to the C<read()> method to read
some page other than the first. The page is 0 based:
# read the second image in the file
$image->read(file=>"example.gif", page=>1)
or die "Cannot read second page: ",$image->errstr,"\n";
Before release 0.46, Imager would read multi-image GIF image files
into a single image, overlaying each of the images onto the virtual
GIF screen.
As of 0.46 the default is to read the first image from the file, as if
called with C<< page => 0 >>.
You can return to the previous behaviour by calling read with the
C<gif_consolidate> parameter set to a true value:
$img->read(file=>$some_gif_file, gif_consolidate=>1);
=head2 TIFF (Tagged Image File Format)
Imager can write images to either paletted or RGB TIFF images,
depending on the type of the source image. Currently if you write a
16-bit/sample or double/sample image it will be written as an
8-bit/sample image. Only 1 or 3 channel images can be written.
If you are creating images for faxing you can set the I<class>
parameter set to C<fax>. By default the image is written in fine
mode, but this can be overridden by setting the I<fax_fine> parameter
to zero. Since a fax image is bi-level, Imager uses a threshold to
decide if a given pixel is black or white, based on a single channel.
For greyscale images channel 0 is used, for color images channel 1
(green) is used. If you want more control over the conversion you can
use $img->to_paletted() to product a bi-level image. This way you can
use dithering:
my $bilevel = $img->to_paletted(colors=>[ NC(0,0,0), NC(255,255,255) ],
make_colors => 'none',
translate => 'errdiff',
errdiff => 'stucki');
=over
=item class
If set to 'fax' the image will be written as a bi-level fax image.
=item fax_fine
By default when I<class> is set to 'fax' the image is written in fine
mode, you can select normal mode by setting I<fax_fine> to 0.
=back
Imager should be able to read any TIFF image you supply. Paletted
TIFF images are read as paletted Imager images, since paletted TIFF
images have 16-bits/sample (48-bits/color) this means the bottom
8-bits are lost, but this shouldn't be a big deal. Currently all
direct color images are read at 8-bits/sample.
TIFF supports the spatial resolution tags. See the
C<tiff_resolutionunit> tag for some extra options.
The following tags are set in a TIFF image when read, and can be set
to control output:
=over
=item tiff_resolutionunit
The value of the ResolutionUnit tag. This is ignored on writing if
the i_aspect_only tag is non-zero.
The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
matter the value of this tag, they will be converted to/from the value
stored in the TIFF file.
=item tiff_resolutionunit_name
This is set when reading a TIFF file to the name of the unit given by
C<tiff_resolutionunit>. Possible results include C<inch>,
C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
these files) or C<unknown>.
=item tiff_bitspersample
Bits per sample from the image. This value is not used when writing
an image, it is only set on a read image.
=item tiff_photometric
Value of the PhotometricInterpretation tag from the image. This value
is not used when writing an image, it is only set on a read image.
=item tiff_documentname
=item tiff_imagedescription
=item tiff_make
=item tiff_model
=item tiff_pagename
=item tiff_software
=item tiff_datetime
=item tiff_artist
=item tiff_hostcomputer
Various strings describing the image. tiff_datetime must be formatted
as "YYYY:MM:DD HH:MM:SS". These correspond directly to the mixed case
names in the TIFF specification. These are set in images read from a
TIFF and saved when writing a TIFF image.
You can supply a C<page> parameter to the C<read()> method to read
some page other than the first. The page is 0 based:
# read the second image in the file
$image->read(file=>"example.tif", page=>1)
or die "Cannot read second page: ",$image->errstr,"\n";
=back
=head2 BMP (BitMaP)
Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
Windows BMP files. Currently you cannot write compressed BMP files
with Imager.
Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
Windows BMP files. There is some support for reading 16-bit per pixel
images, but I haven't found any for testing.
BMP has no support for multi-image files.
BMP files support the spatial resolution tags, but since BMP has no
support for storing only an aspect ratio, if C<i_aspect_only> is set
when you write the C<i_xres> and C<i_yres> values are scaled so the
smaller is 72 DPI.
The following tags are set when you read an image from a BMP file:
=over
=item bmp_compression
The type of compression, if any. This can be any of the following
values:
=over
=item BI_RGB (0)
Uncompressed.
=item BI_RLE8 (1)
8-bits/pixel paletted value RLE compression.
=item BI_RLE4 (2)
4-bits/pixel paletted value RLE compression.
=item BI_BITFIELDS (3)
Packed RGB values.
=back
=item bmp_compression_name
The bmp_compression value as a BI_* string
=item bmp_important_colors
The number of important colors as defined by the writer of the image.
=item bmp_used_colors
Number of color used from the BMP header
=item bmp_filesize
The file size from the BMP header
=item bmp_bit_count
Number of bits stored per pixel. (24, 8, 4 or 1)
=back
=head2 TGA (TarGA)
When storing targa images rle compression can be activated with the
'compress' parameter, the 'idstring' parameter can be used to set the
targa comment field and the 'wierdpack' option can be used to use the
15 and 16 bit targa formats for rgb and rgba data. The 15 bit format
has 5 of each red, green and blue. The 16 bit format in addition
allows 1 bit of alpha. The most significant bits are used for each
channel.
Tags:
=over
=item tga_idstring
=item tga_bitspp
=item compressed
=back
=head2 RAW
When reading raw images you need to supply the width and height of the
image in the xsize and ysize options:
$img->read(file=>'foo.raw', xsize=>100, ysize=>100)
or die "Cannot read raw image\n";
If your input file has more channels than you want, or (as is common),
junk in the fourth channel, you can use the datachannels and
storechannels options to control the number of channels in your input
file and the resulting channels in your image. For example, if your
input image uses 32-bits per pixel with red, green, blue and junk
values for each pixel you could do:
$img->read(file=>'foo.raw', xsize=>100, ysize=>100, datachannels=>4,
storechannels=>3)
or die "Cannot read raw image\n";
Normally the raw image is expected to have the value for channel 1
immediately following channel 0 and channel 2 immediately following
channel 1 for each pixel. If your input image has all the channel 0
values for the first line of the image, followed by all the channel 1
values for the first line and so on, you can use the interleave option:
$img->read(file=>'foo.raw', xsize=100, ysize=>100, interleave=>1)
or die "Cannot read raw image\n";
=head2 PNG
There are no PNG specific tags.
=head1 EXAMPLES
=head2 Producing an image from a CGI script
Once you have an image the basic mechanism is:
=over
=item 1.
set STDOUT to autoflush
=item 2.
output a content-type header, and optionally a content-length header
=item 3.
put STDOUT into binmode
=item 4.
call write() with the C<fd> or C<fh> parameter. You will need to
provide the C<type> parameter since Imager can't use the extension to
guess the file format you want.
=back
# write an image from a CGI script
# using CGI.pm
use CGI qw(:standard);
$| = 1;
binmode STDOUT;
print header(-type=>'image/gif');
$img->write(type=>'gif', fd=>fileno(STDOUT))
or die $img->errstr;
If you want to send a content length you can send the output to a
scalar to get the length:
my $data;
$img->write(type=>'gif', data=>\$data)
or die $img->errstr;
binmode STDOUT;
print header(-type=>'image/gif', -content_length=>length($data));
print $data;
=head2 Writing an animated GIF
The basic idea is simple, just use write_multi():
my @imgs = ...;
Imager->write_multi({ file=>$filename, type=>'gif' }, @imgs);
If your images are RGB images the default quantization mechanism will
produce a very good result, but can take a long time to execute. You
could either use the standard webmap:
Imager->write_multi({ file=>$filename,
type=>'gif',
make_colors=>'webmap' },
@imgs);
or use a median cut algorithm to built a fairly optimal color map:
Imager->write_multi({ file=>$filename,
type=>'gif',
make_colors=>'mediancut' },
@imgs);
By default all of the images will use the same global colormap, which
will produce a smaller image. If your images have significant color
differences, you may want to generate a new palette for each image:
Imager->write_multi({ file=>$filename,
type=>'gif',
make_colors=>'mediancut',
gif_local_map => 1 },
@imgs);
which will set the C<gif_local_map> tag in each image to 1.
Alternatively, if you know only some images have different colors, you
can set the tag just for those images:
$imgs[2]->settag(name=>'gif_local_map', value=>1);
$imgs[4]->settag(name=>'gif_local_map', value=>1);
and call write_multi() without a C<gif_local_map> parameter, or supply
an arrayref of values for the tag:
Imager->write_multi({ file=>$filename,
type=>'gif',
make_colors=>'mediancut',
gif_local_map => [ 0, 0, 1, 0, 1 ] },
@imgs);
Other useful parameters include C<gif_delay> to control the delay
between frames and C<transp> to control transparency.
=head2 Reading tags after reading an image
This is pretty simple:
# print the author of a TIFF, if any
my $img = Imager->new;
$img->read(file=>$filename, type='tiff') or die $img->errstr;
my $author = $img->tags(name=>'tiff_author');
if (defined $author) {
print "Author: $author\n";
}
=head1 BUGS
When saving Gif images the program does NOT try to shave of extra
colors if it is possible. If you specify 128 colors and there are
only 2 colors used - it will have a 128 colortable anyway.
=head1 SEE ALSO
Imager(3)
=cut
|