| 1 |
=head1 NAME |
| 2 |
|
| 3 |
Imager::Files - working with image files |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
use Imager; |
| 8 |
my $img = ...; |
| 9 |
$img->write(file=>$filename, type=>$type) |
| 10 |
or die "Cannot write: ",$img->errstr; |
| 11 |
|
| 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 |
|
| 16 |
$img = Imager->new; |
| 17 |
$img->read(file=>$filename, type=>$type) |
| 18 |
or die "Cannot read: ", $img->errstr; |
| 19 |
|
| 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 |
|
| 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 |
|
| 31 |
Imager->set_file_limits(width=>$max_width, height=>$max_height) |
| 32 |
|
| 33 |
my @read_types = Imager->read_types; |
| 34 |
my @write_types = Imager->write_types; |
| 35 |
|
| 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 |
open my $fh, "+>:raw", ... ; |
| 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 |
|
| 54 |
use Imager 0.68; |
| 55 |
my $img = Imager->new(file => $filename) |
| 56 |
or die Imager->errstr; |
| 57 |
|
| 58 |
Imager->add_file_magic(name => $name, bits => $bits, mask => $mask); |
| 59 |
|
| 60 |
=head1 DESCRIPTION |
| 61 |
|
| 62 |
You can read and write a variety of images formats, assuming you have |
| 63 |
the L<appropriate libraries|Imager::Install/EXTERNAL LIBRARIES>, and |
| 64 |
images can be read or written to/from files, file handles, file |
| 65 |
descriptors, scalars, or through callbacks. |
| 66 |
|
| 67 |
To see which image formats Imager is compiled to support the following |
| 68 |
code snippet is sufficient: |
| 69 |
|
| 70 |
use Imager; |
| 71 |
print join " ", keys %Imager::formats; |
| 72 |
|
| 73 |
This will include some other information identifying libraries rather |
| 74 |
than file formats. For new code you might find the L</read_types()> |
| 75 |
or L</write_types()> methods useful. |
| 76 |
|
| 77 |
=over |
| 78 |
|
| 79 |
=item read() |
| 80 |
|
| 81 |
Reading writing to and from files is simple, use the C<read()> |
| 82 |
method to read an image: |
| 83 |
|
| 84 |
my $img = Imager->new; |
| 85 |
$img->read(file=>$filename, type=>$type) |
| 86 |
or die "Cannot read $filename: ", $img->errstr; |
| 87 |
|
| 88 |
In most cases Imager can auto-detect the file type, so you can just |
| 89 |
supply the file name: |
| 90 |
|
| 91 |
$img->read(file => $filename) |
| 92 |
or die "Cannot read $filename: ", $img->errstr; |
| 93 |
|
| 94 |
The read() method accepts the C<allow_incomplete> parameter. If this |
| 95 |
is non-zero then read() can return true on an incomplete image and set |
| 96 |
the C<i_incomplete> tag. |
| 97 |
|
| 98 |
From Imager 0.68 you can supply most read() parameters to the new() |
| 99 |
method to read the image file on creation. If the read fails, check |
| 100 |
Imager->errstr() for the cause: |
| 101 |
|
| 102 |
use Imager 0.68; |
| 103 |
my $img = Imager->new(file => $filename) |
| 104 |
or die "Cannot read $filename: ", Imager->errstr; |
| 105 |
|
| 106 |
=item write() |
| 107 |
|
| 108 |
and the C<write()> method to write an image: |
| 109 |
|
| 110 |
$img->write(file=>$filename, type=>$type) |
| 111 |
or die "Cannot write $filename: ", $img->errstr; |
| 112 |
|
| 113 |
=item read_multi() |
| 114 |
|
| 115 |
If you're reading from a format that supports multiple images per |
| 116 |
file, use the C<read_multi()> method: |
| 117 |
|
| 118 |
my @imgs = Imager->read_multi(file=>$filename, type=>$type) |
| 119 |
or die "Cannot read $filename: ", Imager->errstr; |
| 120 |
|
| 121 |
As with the read() method, Imager will normally detect the C<type> |
| 122 |
automatically. |
| 123 |
|
| 124 |
=item write_multi() |
| 125 |
|
| 126 |
and if you want to write multiple images to a single file use the |
| 127 |
C<write_multi()> method: |
| 128 |
|
| 129 |
Imager->write_multi({ file=> $filename, type=>$type }, @images) |
| 130 |
or die "Cannot write $filename: ", Imager->errstr; |
| 131 |
|
| 132 |
=item read_types() |
| 133 |
|
| 134 |
This is a class method that returns a list of the image file types |
| 135 |
that Imager can read. |
| 136 |
|
| 137 |
my @types = Imager->read_types; |
| 138 |
|
| 139 |
These types are the possible values for the C<type> parameter, not |
| 140 |
necessarily the extension of the files you're reading. |
| 141 |
|
| 142 |
It is possible for extra file read handlers to be loaded when |
| 143 |
attempting to read a file, which may modify the list of available read |
| 144 |
types. |
| 145 |
|
| 146 |
=item write_types() |
| 147 |
|
| 148 |
This is a class method that returns a list of the image file types |
| 149 |
that Imager can write. |
| 150 |
|
| 151 |
my @types = Imager->write_types; |
| 152 |
|
| 153 |
Note that these are the possible values for the C<type> parameter, not |
| 154 |
necessarily the extension of the files you're writing. |
| 155 |
|
| 156 |
It is possible for extra file write handlers to be loaded when |
| 157 |
attempting to write a file, which may modify the list of available |
| 158 |
write types. |
| 159 |
|
| 160 |
=back |
| 161 |
|
| 162 |
When writing, if the C<filename> includes an extension that Imager |
| 163 |
recognizes, then you don't need the C<type>, but you may want to |
| 164 |
provide one anyway. See L</Guessing types> for information on |
| 165 |
controlling this recognition. |
| 166 |
|
| 167 |
The C<type> parameter is a lowercase representation of the file type, |
| 168 |
and can be any of the following: |
| 169 |
|
| 170 |
bmp Windows BitMaP (BMP) |
| 171 |
gif Graphics Interchange Format (GIF) |
| 172 |
jpeg JPEG/JFIF |
| 173 |
png Portable Network Graphics (PNG) |
| 174 |
pnm Portable aNyMap (PNM) |
| 175 |
raw Raw |
| 176 |
sgi SGI .rgb files |
| 177 |
tga TARGA |
| 178 |
tiff Tagged Image File Format (TIFF) |
| 179 |
|
| 180 |
Support for other formats can be found on CPAN, including: |
| 181 |
X<C<heif>> X<C<QOI>> X<C<webp>> |
| 182 |
|
| 183 |
apng Imager::File::APNG |
| 184 |
avif Imager::File::AVIF |
| 185 |
heif/heic Imager::File::HEIF |
| 186 |
qoi Imager::File::QOI |
| 187 |
webp Imager::File::WEBP |
| 188 |
|
| 189 |
When you read an image, Imager may set some tags, possibly including |
| 190 |
information about the spatial resolution, textual information, and |
| 191 |
animation information. See L<Imager::ImageTypes/Tags> for specifics. |
| 192 |
|
| 193 |
The open() method is a historical alias for the read() method. |
| 194 |
|
| 195 |
=head2 Input and output |
| 196 |
|
| 197 |
When reading or writing you can specify one of a variety of sources or |
| 198 |
targets: |
| 199 |
|
| 200 |
=over |
| 201 |
|
| 202 |
=item * |
| 203 |
|
| 204 |
C<file> - The C<file> parameter is the name of the image file to be |
| 205 |
written to or read from. If Imager recognizes the extension of the |
| 206 |
file you do not need to supply a C<type>. |
| 207 |
|
| 208 |
# write in tiff format |
| 209 |
$image->write(file => "example.tif") |
| 210 |
or die $image->errstr; |
| 211 |
|
| 212 |
$image->write(file => 'foo.tmp', type => 'tiff') |
| 213 |
or die $image->errstr; |
| 214 |
|
| 215 |
my $image = Imager->new; |
| 216 |
$image->read(file => 'example.tif') |
| 217 |
or die $image->errstr; |
| 218 |
|
| 219 |
=item * |
| 220 |
|
| 221 |
C<fh> - C<fh> is a file handle, typically returned from an C<open> |
| 222 |
call. You should call C<binmode> on the handle before passing it to |
| 223 |
Imager. |
| 224 |
|
| 225 |
Imager will set the handle to autoflush to make sure any buffered data |
| 226 |
is flushed , since Imager will write to the file descriptor (from |
| 227 |
fileno()) rather than writing at the perl level. |
| 228 |
|
| 229 |
$image->write(fh => \*STDOUT, type => 'gif') |
| 230 |
or die $image->errstr; |
| 231 |
|
| 232 |
# for example, a file uploaded via CGI.pm |
| 233 |
$image->read(fd => $cgi->param('file')) |
| 234 |
or die $image->errstr; |
| 235 |
|
| 236 |
=item * |
| 237 |
|
| 238 |
C<fd> - C<fd> is a file descriptor. You can get this by calling the |
| 239 |
C<fileno()> function on a file handle, or by using one of the standard |
| 240 |
file descriptor numbers. |
| 241 |
|
| 242 |
If you get this from a perl file handle, you may need to flush any |
| 243 |
buffered output, otherwise it may appear in the output stream after |
| 244 |
the image. |
| 245 |
|
| 246 |
$image->write(fd => file(STDOUT), type => 'gif') |
| 247 |
or die $image->errstr; |
| 248 |
|
| 249 |
=item * |
| 250 |
|
| 251 |
C<data> - When reading data, C<data> is a scalar containing the image |
| 252 |
file data, or a reference to such a scalar. When writing, C<data> is |
| 253 |
a reference to the scalar to save the image file data to. |
| 254 |
|
| 255 |
my $data; |
| 256 |
$image->write(data => \$data, type => 'tiff') |
| 257 |
or die $image->errstr; |
| 258 |
|
| 259 |
my $data = $row->{someblob}; # eg. from a database |
| 260 |
my @images = Imager->read_multi(data => $data) |
| 261 |
or die Imager->errstr; |
| 262 |
|
| 263 |
# from Imager 0.99 |
| 264 |
my @images = Imager->read_multi(data => \$data) |
| 265 |
or die Imager->errstr; |
| 266 |
|
| 267 |
=item * |
| 268 |
|
| 269 |
C<callback>, C<readcb>, C<writecb>, C<seekcb>, C<closecb> - Imager |
| 270 |
will make calls back to your supplied coderefs to read, write and seek |
| 271 |
from/to/through the image file. See L</"I/O Callbacks"> below for details. |
| 272 |
|
| 273 |
=item * |
| 274 |
|
| 275 |
C<io> - an L<Imager::IO> object. |
| 276 |
|
| 277 |
=back |
| 278 |
|
| 279 |
X<buffering> X<unbuffered> By default Imager will use buffered I/O when |
| 280 |
reading or writing an image. You can disabled buffering for output by |
| 281 |
supplying a C<< buffered => 0 >> parameter to C<write()> or |
| 282 |
C<write_multi()>. |
| 283 |
|
| 284 |
=head2 I/O Callbacks |
| 285 |
|
| 286 |
When reading from a file you can use either C<callback> or C<readcb> |
| 287 |
to supply the read callback, and when writing C<callback> or |
| 288 |
C<writecb> to supply the write callback. |
| 289 |
|
| 290 |
Whether reading or writing a C<TIFF> image, C<seekcb> and C<readcb> |
| 291 |
are required. |
| 292 |
|
| 293 |
If a file handler attempts to use C<readcb>, C<writecb> or C<seekcb> |
| 294 |
and you haven't supplied one, the call will fail, failing the image |
| 295 |
read or write, returning an error message indicating that the callback |
| 296 |
is missing: |
| 297 |
|
| 298 |
# attempting to read a TIFF image without a seekcb |
| 299 |
open my $fh, "<", $filename or die; |
| 300 |
my $rcb = sub { |
| 301 |
my $val; |
| 302 |
read($fh, $val, $_[0]) or return ""; |
| 303 |
return $val; |
| 304 |
}; |
| 305 |
my $im = Imager->new(callback => $rcb) |
| 306 |
or die Imager->errstr |
| 307 |
# dies with (wrapped here): |
| 308 |
# Error opening file: (Iolayer): Failed to read directory at offset 0: |
| 309 |
# (Iolayer): Seek error accessing TIFF directory: seek callback called |
| 310 |
# but no seekcb supplied |
| 311 |
|
| 312 |
You can also provide a C<closecb> parameter called when writing the |
| 313 |
file is complete. If no C<closecb> is supplied the default will |
| 314 |
succeed silently. |
| 315 |
|
| 316 |
# contrived |
| 317 |
my $data; |
| 318 |
sub mywrite { |
| 319 |
$data .= unpack("H*", shift); |
| 320 |
1; |
| 321 |
} |
| 322 |
Imager->write_multi({ callback => \&mywrite, type => 'gif'}, @images) |
| 323 |
or die Imager->errstr; |
| 324 |
|
| 325 |
=head3 C<readcb> |
| 326 |
|
| 327 |
The read callback is called with 2 parameters: |
| 328 |
|
| 329 |
=over |
| 330 |
|
| 331 |
=item * |
| 332 |
|
| 333 |
C<size> - the minimum amount of data required. |
| 334 |
|
| 335 |
=item * |
| 336 |
|
| 337 |
C<maxsize> - previously this was the maximum amount of data returnable |
| 338 |
- currently it's always the same as C<size> |
| 339 |
|
| 340 |
=back |
| 341 |
|
| 342 |
Your read callback should return the data as a scalar: |
| 343 |
|
| 344 |
=over |
| 345 |
|
| 346 |
=item * |
| 347 |
|
| 348 |
on success, a string containing the bytes read. |
| 349 |
|
| 350 |
=item * |
| 351 |
|
| 352 |
on end of file, an empty string |
| 353 |
|
| 354 |
=item * |
| 355 |
|
| 356 |
on error, C<undef>. |
| 357 |
|
| 358 |
=back |
| 359 |
|
| 360 |
If your return value contains more data than C<size> Imager will |
| 361 |
panic. |
| 362 |
|
| 363 |
Your return value must not contain any characters over C<\xFF> or |
| 364 |
Imager will panic. |
| 365 |
|
| 366 |
=head3 C<writecb> |
| 367 |
|
| 368 |
Your write callback takes exactly one parameter, a scalar containing |
| 369 |
the data to be written. |
| 370 |
|
| 371 |
Return true for success. |
| 372 |
|
| 373 |
=head3 C<seekcb> |
| 374 |
|
| 375 |
The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>, |
| 376 |
defined in the same way as perl's seek function. |
| 377 |
|
| 378 |
Previously you always needed a C<seekcb> callback if you called |
| 379 |
Imager's L</read()> or L</read_multi()> without a C<type> parameter, |
| 380 |
but this is no longer necessary unless the file handler requires |
| 381 |
seeking, such as for TIFF files. |
| 382 |
|
| 383 |
Returns the new position in the file, or -1 on failure. |
| 384 |
|
| 385 |
=head3 C<closecb> |
| 386 |
|
| 387 |
You can also supply a C<closecb> which is called with no parameters |
| 388 |
when there is no more data to be written. This could be used to flush |
| 389 |
buffered data. |
| 390 |
|
| 391 |
Return true on success. |
| 392 |
|
| 393 |
=head2 Guessing types |
| 394 |
|
| 395 |
X<C<FORMATGUESS>> |
| 396 |
When writing to a file, if you don't supply a C<type> parameter, Imager |
| 397 |
will attempt to guess it from the file name. This is done by calling |
| 398 |
the code reference stored in C<$Imager::FORMATGUESS>. This is only |
| 399 |
done when write() or write_multi() is called with a C<file> parameter, |
| 400 |
or if read() or read_multi() can't determine the type from the file's |
| 401 |
header. |
| 402 |
|
| 403 |
The default function value of C<$Imager::FORMATGUESS> is |
| 404 |
C<\&Imager::def_guess_type>. |
| 405 |
|
| 406 |
=over |
| 407 |
|
| 408 |
=item def_guess_type() |
| 409 |
X<methods, def_guess_type()> |
| 410 |
|
| 411 |
This is the default function Imager uses to derive a file type from a |
| 412 |
file name. This is a function, not a method. |
| 413 |
|
| 414 |
Accepts a single parameter, the file name, and returns the type or |
| 415 |
C<undef>. |
| 416 |
|
| 417 |
=back |
| 418 |
|
| 419 |
You can replace the function with your own implementation if you have some |
| 420 |
specialized need. The function takes a single parameter, the name of |
| 421 |
the file, and should return either a file type or C<undef>. |
| 422 |
|
| 423 |
# I'm writing jpegs to weird filenames |
| 424 |
local $Imager::FORMATGUESS = sub { 'jpeg' }; |
| 425 |
|
| 426 |
When reading a file Imager examines the beginning of the file for |
| 427 |
identifying information. The current implementation attempts to |
| 428 |
detect the following image types beyond those supported by Imager: |
| 429 |
|
| 430 |
=for stopwords Photoshop |
| 431 |
|
| 432 |
=over |
| 433 |
|
| 434 |
C<xpm>, C<mng>, C<jng>, C<ilbm>, C<pcx>, C<fits>, C<psd> (Photoshop), C<eps>, Utah |
| 435 |
C<RLE>. |
| 436 |
|
| 437 |
=back |
| 438 |
|
| 439 |
You can now add to the magic database Imager uses for detecting file |
| 440 |
types: |
| 441 |
|
| 442 |
=over |
| 443 |
|
| 444 |
=item add_file_magic() |
| 445 |
|
| 446 |
Imager->add_file_magic(name => $name, bits => $bits, mask => $mask) |
| 447 |
|
| 448 |
Adds to list of magic, the parameters are all required. The |
| 449 |
parameters are: |
| 450 |
|
| 451 |
=over |
| 452 |
|
| 453 |
=item * |
| 454 |
|
| 455 |
C<name> - the file type name to return on match. |
| 456 |
|
| 457 |
=item * |
| 458 |
|
| 459 |
C<bits> - a binary string to match. |
| 460 |
|
| 461 |
=item * |
| 462 |
|
| 463 |
C<mask> - a mask controlling which parts of I<bits> are significant. |
| 464 |
|
| 465 |
=back |
| 466 |
|
| 467 |
While I<mask> is mostly a bit mask, some byte values are translated: |
| 468 |
The space character is treated as all zeros (C<"\x00">), and the C<x> character as |
| 469 |
all ones (C<"\xFF">). |
| 470 |
|
| 471 |
New magic entries take priority over old entries. |
| 472 |
|
| 473 |
You can add more than one magic entry for a given I<name>. |
| 474 |
|
| 475 |
Imager->add_file_magic(name => "heif", |
| 476 |
bits => " ftypheif" |
| 477 |
mask => " xxxxxxxx"); |
| 478 |
|
| 479 |
=back |
| 480 |
|
| 481 |
=head2 Limiting the sizes of images you read |
| 482 |
|
| 483 |
=over |
| 484 |
|
| 485 |
=item set_file_limits() |
| 486 |
|
| 487 |
In some cases you will be receiving images from an untested source, |
| 488 |
such as submissions via CGI. To prevent such images from consuming |
| 489 |
large amounts of memory, you can set limits on the dimensions of |
| 490 |
images you read from files: |
| 491 |
|
| 492 |
=over |
| 493 |
|
| 494 |
=item * |
| 495 |
|
| 496 |
width - limit the width in pixels of the image |
| 497 |
|
| 498 |
=item * |
| 499 |
|
| 500 |
height - limit the height in pixels of the image |
| 501 |
|
| 502 |
=item * |
| 503 |
|
| 504 |
bytes - limits the amount of storage used by the image. This depends |
| 505 |
on the width, height, channels and sample size of the image. For |
| 506 |
paletted images this is calculated as if the image was expanded to a |
| 507 |
direct color image. |
| 508 |
|
| 509 |
=back |
| 510 |
|
| 511 |
To set the limits, call the class method set_file_limits: |
| 512 |
|
| 513 |
Imager->set_file_limits(width=>$max_width, height=>$max_height); |
| 514 |
|
| 515 |
You can pass any or all of the limits above, any limits you do not |
| 516 |
pass are left as they were. |
| 517 |
|
| 518 |
Any limit of zero for width or height is treated as unlimited. |
| 519 |
|
| 520 |
A limit of zero for bytes is treated as one gigabyte, but higher bytes |
| 521 |
limits can be set explicitly. |
| 522 |
|
| 523 |
By default, the width and height limits are zero, or unlimited. The |
| 524 |
default memory size limit is one gigabyte. |
| 525 |
|
| 526 |
You can reset all limits to their defaults with the reset parameter: |
| 527 |
|
| 528 |
# no limits |
| 529 |
Imager->set_file_limits(reset=>1); |
| 530 |
|
| 531 |
This can be used with the other limits to reset all but the limit you |
| 532 |
pass: |
| 533 |
|
| 534 |
# only width is limited |
| 535 |
Imager->set_file_limits(reset=>1, width=>100); |
| 536 |
|
| 537 |
# only bytes is limited |
| 538 |
Imager->set_file_limits(reset=>1, bytes=>10_000_000); |
| 539 |
|
| 540 |
=item get_file_limits() |
| 541 |
|
| 542 |
You can get the current limits with the get_file_limits() method: |
| 543 |
|
| 544 |
my ($max_width, $max_height, $max_bytes) = |
| 545 |
Imager->get_file_limits(); |
| 546 |
|
| 547 |
=item check_file_limits() |
| 548 |
X<class methods, check_file_limits()> X<check_file_limits()> |
| 549 |
|
| 550 |
Intended for use by file handlers to check that the size of a file is |
| 551 |
within the limits set by C<set_file_limits()>. |
| 552 |
|
| 553 |
Parameters: |
| 554 |
|
| 555 |
=over |
| 556 |
|
| 557 |
=item * |
| 558 |
|
| 559 |
C<width>, C<height> - the width and height of the image in pixels. |
| 560 |
Must be a positive integer. Required. |
| 561 |
|
| 562 |
=item * |
| 563 |
|
| 564 |
C<channels> - the number of channels in the image, including the alpha |
| 565 |
channel if any. Must be a positive integer between 1 and 4 |
| 566 |
inclusive. Default: 3. |
| 567 |
|
| 568 |
=item * |
| 569 |
|
| 570 |
C<sample_size> - the number of bytes stored per sample. Must be a |
| 571 |
positive integer or C<"float">. Note that this should be the sample |
| 572 |
size of the Imager image you will be creating, not the sample size in |
| 573 |
the source, eg. if the source has 32-bit samples this should be |
| 574 |
C<"float"> since Imager doesn't have 32-bit/sample images. |
| 575 |
|
| 576 |
=back |
| 577 |
|
| 578 |
=back |
| 579 |
|
| 580 |
=head1 TYPE SPECIFIC INFORMATION |
| 581 |
|
| 582 |
The different image formats can write different image type, and some have |
| 583 |
different options to control how the images are written. |
| 584 |
|
| 585 |
When you call C<write()> or C<write_multi()> with an option that has |
| 586 |
the same name as a tag for the image format you're writing, then the |
| 587 |
value supplied to that option will be used to set the corresponding |
| 588 |
tag in the image. Depending on the image format, these values will be |
| 589 |
used when writing the image. |
| 590 |
|
| 591 |
This replaces the previous options that were used when writing GIF |
| 592 |
images. Currently if you use an obsolete option, it will be converted |
| 593 |
to the equivalent tag and Imager will produced a warning. You can |
| 594 |
suppress these warnings by calling the C<Imager::init()> function with |
| 595 |
the C<warn_obsolete> option set to false: |
| 596 |
|
| 597 |
Imager::init(warn_obsolete=>0); |
| 598 |
|
| 599 |
At some point in the future these obsolete options will no longer be |
| 600 |
supported. |
| 601 |
|
| 602 |
=for stopwords aNy PixMaps BitMap |
| 603 |
|
| 604 |
=head2 PNM (Portable aNy Map) |
| 605 |
|
| 606 |
Imager can write C<PGM> (Portable Gray Map) and C<PPM> (Portable |
| 607 |
PixMaps) files, depending on the number of channels in the image. |
| 608 |
Currently the images are written in binary formats. Only 1 and 3 |
| 609 |
channel images can be written, including 1 and 3 channel paletted |
| 610 |
images. |
| 611 |
|
| 612 |
$img->write(file=>'foo.ppm') or die $img->errstr; |
| 613 |
|
| 614 |
Imager can read both the ASCII and binary versions of each of the |
| 615 |
C<PBM> (Portable BitMap), C<PGM> and C<PPM> formats. |
| 616 |
|
| 617 |
$img->read(file=>'foo.ppm') or die $img->errstr; |
| 618 |
|
| 619 |
PNM does not support the spatial resolution tags. |
| 620 |
|
| 621 |
The following tags are set when reading a PNM file: |
| 622 |
|
| 623 |
=over |
| 624 |
|
| 625 |
=item * |
| 626 |
|
| 627 |
X<pnm_maxval> C<pnm_maxval> - the C<maxvals> number from the PGM/PPM header. |
| 628 |
Always set to 2 for a C<PBM> file. |
| 629 |
|
| 630 |
=item * |
| 631 |
|
| 632 |
X<pnm_type> C<pnm_type> - the type number from the C<PNM> header, 1 for ASCII |
| 633 |
C<PBM> files, 2 for ASCII C<PGM> files, 3 for ASCII c<PPM> files, 4 for binary |
| 634 |
C<PBM> files, 5 for binary C<PGM> files, 6 for binary C<PPM> files. |
| 635 |
|
| 636 |
=back |
| 637 |
|
| 638 |
The following tag is checked when writing an image with more than |
| 639 |
8-bits/sample: |
| 640 |
|
| 641 |
=over |
| 642 |
|
| 643 |
=item * |
| 644 |
|
| 645 |
X<pnm_write_wide_data> pnm_write_wide_data - if this is non-zero then |
| 646 |
write() can write C<PGM>/C<PPM> files with 16-bits/sample. Some |
| 647 |
applications, for example GIMP 2.2, and tools can only read |
| 648 |
8-bit/sample binary PNM files, so Imager will only write a 16-bit |
| 649 |
image when this tag is non-zero. |
| 650 |
|
| 651 |
=back |
| 652 |
|
| 653 |
=head2 JPEG |
| 654 |
|
| 655 |
You can supply a C<jpegquality> parameter ranging from 0 (worst |
| 656 |
quality) to 100 (best quality) when writing a JPEG file, which |
| 657 |
defaults to 75. |
| 658 |
|
| 659 |
$img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr; |
| 660 |
|
| 661 |
If you write an image with an alpha channel to a JPEG file then it |
| 662 |
will be composed against the background set by the C<i_background> |
| 663 |
parameter (or tag), or black if not supplied. |
| 664 |
|
| 665 |
Imager will read a gray scale JPEG as a 1 channel image and a color |
| 666 |
JPEG as a 3 channel image. |
| 667 |
|
| 668 |
$img->read(file=>'foo.jpg') or die $img->errstr; |
| 669 |
|
| 670 |
The following tags are set in a JPEG image when read, and can be set |
| 671 |
to control output: |
| 672 |
|
| 673 |
=over |
| 674 |
|
| 675 |
=item * |
| 676 |
|
| 677 |
C<jpeg_density_unit> - The value of the density unit field in the |
| 678 |
C<JFIF> header. This is ignored on writing if the C<i_aspect_only> |
| 679 |
tag is non-zero. |
| 680 |
|
| 681 |
The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no |
| 682 |
matter the value of this tag, they will be converted to/from the value |
| 683 |
stored in the JPEG file. |
| 684 |
|
| 685 |
=item * |
| 686 |
|
| 687 |
C<jpeg_density_unit_name> - This is set when reading a JPEG file to |
| 688 |
the name of the unit given by C<jpeg_density_unit>. Possible results |
| 689 |
include C<inch>, C<centimeter>, C<none> (the C<i_aspect_only> tag is |
| 690 |
also set reading these files). If the value of C<jpeg_density_unit> |
| 691 |
is unknown then this tag isn't set. |
| 692 |
|
| 693 |
=item * |
| 694 |
|
| 695 |
C<jpeg_comment> - Text comment. |
| 696 |
|
| 697 |
=item * |
| 698 |
|
| 699 |
C<jpeg_progressive> - Whether the JPEG file is a progressive |
| 700 |
file. (Imager 0.84) |
| 701 |
|
| 702 |
=back |
| 703 |
|
| 704 |
JPEG supports the spatial resolution tags C<i_xres>, C<i_yres> and |
| 705 |
C<i_aspect_only>. |
| 706 |
|
| 707 |
You can also set the following tags when writing to an image, they are |
| 708 |
not set in the image when reading: |
| 709 |
|
| 710 |
=over |
| 711 |
|
| 712 |
=item * |
| 713 |
|
| 714 |
C<jpeg_optimize> - set to a non-zero integer to compute optimal |
| 715 |
Huffman coding tables for the image. This will increase memory usage |
| 716 |
and processing time (about 12% in my simple tests) but can |
| 717 |
significantly reduce file size without a loss of quality. |
| 718 |
|
| 719 |
=item * |
| 720 |
|
| 721 |
C<jpeg_compress_profile> - set to either C<fastest>, the default, or |
| 722 |
only with C<MozJPEG>, to C<max>. Setting this to any other value will |
| 723 |
cause an error, failing the write() call. Setting this to C<max> |
| 724 |
without C<MozJPEG> will cause an error. |
| 725 |
|
| 726 |
use Imager::File::JPEG; |
| 727 |
my $prof = Imager::File::JPEG->is_mozjpeg ? "max" : "fastest"; |
| 728 |
$im->write(file => "foo.jpeg", jpeg_compress_profile => $prof) |
| 729 |
or die $im->errstr; |
| 730 |
|
| 731 |
Note that unlike C<MozJPEG>, Imager always defaults to C<fastest>. |
| 732 |
|
| 733 |
=item * |
| 734 |
|
| 735 |
C<jpeg_tune> - corresponds to the C<MozJPEG> C<cjpeg> C<-tune> |
| 736 |
parameters, this can be any of: |
| 737 |
|
| 738 |
=over |
| 739 |
|
| 740 |
=item * C<psnr> |
| 741 |
|
| 742 |
=item * C<ssim> |
| 743 |
|
| 744 |
=item * C<ms-ssim> |
| 745 |
|
| 746 |
=item * C<hvs-psnr> |
| 747 |
|
| 748 |
=back |
| 749 |
|
| 750 |
These set the same values as the C<jpeg_base_quant_tbl_idx>, |
| 751 |
C<jpeg_lambda_log_scale1>, C<jpeg_lambda_log_scale2> and |
| 752 |
C<jpeg_use_lambda_weight_tbl> tags, which can override the values set |
| 753 |
by C<jpeg_tune>. |
| 754 |
|
| 755 |
Unlike C<cjpeg> C<-tune>, C<jpeg_tune> doesn't force the quality to |
| 756 |
75. |
| 757 |
|
| 758 |
Requires that Imager::File::JPEG was built with C<MozJPEG>. |
| 759 |
|
| 760 |
=item * |
| 761 |
|
| 762 |
C<jpeg_arithmetic> - if set to non-zero, use arithmetic coding when |
| 763 |
writing the image. This requires that the C<libjpeg> variant that |
| 764 |
L<Imager::File::JPEG> was built with had encode support for arithmetic |
| 765 |
coding enabled when it was built. |
| 766 |
|
| 767 |
=item * |
| 768 |
|
| 769 |
=for stopwords JFIF |
| 770 |
|
| 771 |
C<jpeg_jfif> - if set to zero, disable writing the JFIF header even if |
| 772 |
one is technically required, which it is for the color spaces Imager |
| 773 |
works with. This saves 18 bytes in the output file and most |
| 774 |
applications including web browsers will successfully read the file, |
| 775 |
but your experience may differ. |
| 776 |
|
| 777 |
This will prevent the C<i_xres>, C<i_yres> and C<jpeg_density_units> |
| 778 |
tags from having an effect. |
| 779 |
|
| 780 |
=item * |
| 781 |
|
| 782 |
C<jpeg_smooth> - if set to a value from 1 to 100 apply smoothing to |
| 783 |
the image to eliminate dithering noise (without modifying the Imager |
| 784 |
image). Default: 0 (no smoothing). |
| 785 |
|
| 786 |
=item * |
| 787 |
|
| 788 |
=for stopwords MCUs |
| 789 |
|
| 790 |
C<jpeg_restart> - if set to a plain integer C<N>, generate a JPEG |
| 791 |
restart marker every C<N> rows, if set to an integer followed by C<B>, |
| 792 |
generate a JPEG restart marker every C<N> MCUs, the 8x8 pixel blocks |
| 793 |
that make up a JPEG image. Note that if this is specified in rows it |
| 794 |
will be translated to MCUs by C<libjpeg>. |
| 795 |
|
| 796 |
This allows a decoder to recover from corruption, but it makes the |
| 797 |
file slightly larger. |
| 798 |
|
| 799 |
Default: 0, no restart markers are produced. |
| 800 |
|
| 801 |
=item * |
| 802 |
|
| 803 |
=for stopwords subsampling YCbCr |
| 804 |
|
| 805 |
C<jpeg_sample> - control subsampling of the YCbCr components, |
| 806 |
equivalent to the C<-sample> parameter for C<cjpeg>. |
| 807 |
|
| 808 |
Default: "2x2,1x1,1x1", default 4:2:0 JPEG subsampling. |
| 809 |
|
| 810 |
=back |
| 811 |
|
| 812 |
The following options can be set on writing with C<MozJPEG> and |
| 813 |
correspond directly to the API options described in the C<MozJPEG> |
| 814 |
F<README-mozilla.txt>. Attempting to set them when Imager::File::JPEG |
| 815 |
has been built with another C<libjpeg> will result in an error. |
| 816 |
|
| 817 |
=over |
| 818 |
|
| 819 |
=item * |
| 820 |
|
| 821 |
C<jpeg_optimize_scans>, C<jpeg_trellis_quant>, |
| 822 |
C<jpeg_trellis_quant_dc>, C<jpeg_tresllis_eob_opt>, |
| 823 |
C<jpeg_use_lambda_weight_tbl>, C<jpeg_use_scans_in_trellis>, |
| 824 |
C<jpeg_overshoot_deringing> - boolean options, set to 0 or 1. |
| 825 |
|
| 826 |
C<jpeg_lambda_log_scale1>, C<jpeg_lambda_log_scale2>, |
| 827 |
C<jpeg_trellis_delta_dc_weight> - integer options. |
| 828 |
|
| 829 |
|
| 830 |
C<jpeg_trellis_freq_split>, C<jpeg_trellis_num_loops>, |
| 831 |
C<jpeg_base_quant_tbl_idx>, C<jpeg_dc_scan_opt_mode> - floating point |
| 832 |
options. |
| 833 |
|
| 834 |
=back |
| 835 |
|
| 836 |
When reading a JPEG image, the following tag will be set, and ignored |
| 837 |
on writing: |
| 838 |
|
| 839 |
=over |
| 840 |
|
| 841 |
=item * |
| 842 |
|
| 843 |
C<jpeg_read_arithmetic> - the image had been written with arithmetic |
| 844 |
coding. This uses a separate name from writing to avoid arithmetic |
| 845 |
coding from being accidentally propagated to a file that might need |
| 846 |
to be read by an implementation without arithmetic coding support. |
| 847 |
|
| 848 |
=back |
| 849 |
|
| 850 |
=for stopwords EXIF |
| 851 |
|
| 852 |
If an C<APP1> block containing EXIF information is found, then any of the |
| 853 |
following tags can be set when reading a JPEG image: |
| 854 |
|
| 855 |
=over |
| 856 |
|
| 857 |
exif_aperture exif_artist exif_brightness exif_color_space |
| 858 |
exif_contrast exif_copyright exif_custom_rendered exif_date_time |
| 859 |
exif_date_time_digitized exif_date_time_original |
| 860 |
exif_digital_zoom_ratio exif_exposure_bias exif_exposure_index |
| 861 |
exif_exposure_mode exif_exposure_program exif_exposure_time |
| 862 |
exif_f_number exif_flash exif_flash_energy exif_flashpix_version |
| 863 |
exif_focal_length exif_focal_length_in_35mm_film |
| 864 |
exif_focal_plane_resolution_unit exif_focal_plane_x_resolution |
| 865 |
exif_focal_plane_y_resolution exif_gain_control exif_image_description |
| 866 |
exif_image_unique_id exif_iso_speed_rating exif_make exif_max_aperture |
| 867 |
exif_metering_mode exif_model exif_orientation exif_related_sound_file |
| 868 |
exif_resolution_unit exif_saturation exif_scene_capture_type |
| 869 |
exif_sensing_method exif_sharpness exif_shutter_speed exif_software |
| 870 |
exif_spectral_sensitivity exif_sub_sec_time |
| 871 |
exif_sub_sec_time_digitized exif_sub_sec_time_original |
| 872 |
exif_subject_distance exif_subject_distance_range |
| 873 |
exif_subject_location exif_tag_light_source exif_user_comment |
| 874 |
exif_version exif_white_balance exif_x_resolution exif_y_resolution |
| 875 |
|
| 876 |
=back |
| 877 |
|
| 878 |
The following derived tags can also be set when reading a JPEG image: |
| 879 |
|
| 880 |
=over |
| 881 |
|
| 882 |
exif_color_space_name exif_contrast_name exif_custom_rendered_name |
| 883 |
exif_exposure_mode_name exif_exposure_program_name exif_flash_name |
| 884 |
exif_focal_plane_resolution_unit_name exif_gain_control_name |
| 885 |
exif_light_source_name exif_metering_mode_name |
| 886 |
exif_resolution_unit_name exif_saturation_name |
| 887 |
exif_scene_capture_type_name exif_sensing_method_name |
| 888 |
exif_sharpness_name exif_subject_distance_range_name |
| 889 |
exif_white_balance_name |
| 890 |
|
| 891 |
=back |
| 892 |
|
| 893 |
The derived tags are for enumerated fields, when the value for the |
| 894 |
base field is valid then the text that appears in the EXIF |
| 895 |
specification for that value appears in the derived field. So for |
| 896 |
example if C<exf_metering_mode> is C<5> then |
| 897 |
C<exif_metering_mode_name> is set to C<Pattern>. |
| 898 |
|
| 899 |
eg. |
| 900 |
|
| 901 |
my $image = Imager->new; |
| 902 |
$image->read(file => 'exiftest.jpg') |
| 903 |
or die "Cannot load image: ", $image->errstr; |
| 904 |
print $image->tags(name => "exif_image_description"), "\n"; |
| 905 |
print $image->tags(name => "exif_exposure_mode"), "\n"; |
| 906 |
print $image->tags(name => "exif_exposure_mode_name"), "\n"; |
| 907 |
|
| 908 |
# for the exiftest.jpg in the Imager distribution the output would be: |
| 909 |
Imager Development Notes |
| 910 |
0 |
| 911 |
Auto exposure |
| 912 |
|
| 913 |
Imager will not write EXIF tags to any type of image, if you need more |
| 914 |
advanced EXIF handling, consider L<Image::ExifTool>. |
| 915 |
|
| 916 |
=for stopwords IPTC |
| 917 |
|
| 918 |
=over |
| 919 |
|
| 920 |
=item parseiptc() |
| 921 |
|
| 922 |
Historically, Imager saves IPTC data when reading a JPEG image, the |
| 923 |
parseiptc() method returns a list of key/value pairs resulting from a |
| 924 |
simple decoding of that data. |
| 925 |
|
| 926 |
Any future IPTC data decoding is likely to go into tags. |
| 927 |
|
| 928 |
=back |
| 929 |
|
| 930 |
=head2 GIF |
| 931 |
|
| 932 |
When writing one of more GIF images you can use the same |
| 933 |
L<Quantization Options|Imager::ImageTypes> as you can when converting |
| 934 |
an RGB image into a paletted image. |
| 935 |
|
| 936 |
When reading a GIF all of the sub-images are combined using the screen |
| 937 |
size and image positions into one big image, producing an RGB image. |
| 938 |
This may change in the future to produce a paletted image where possible. |
| 939 |
|
| 940 |
When you read a single GIF with C<$img-E<gt>read()> you can supply a |
| 941 |
reference to a scalar in the C<colors> parameter, if the image is read |
| 942 |
the scalar will be filled with a reference to an anonymous array of |
| 943 |
L<Imager::Color> objects, representing the palette of the image. This |
| 944 |
will be the first palette found in the image. If you want the |
| 945 |
palettes for each of the images in the file, use C<read_multi()> and |
| 946 |
use the C<getcolors()> method on each image. |
| 947 |
|
| 948 |
GIF does not support the spatial resolution tags. |
| 949 |
|
| 950 |
Imager will set the following tags in each image when reading, and can |
| 951 |
use most of them when writing to GIF: |
| 952 |
|
| 953 |
=over |
| 954 |
|
| 955 |
=item * |
| 956 |
|
| 957 |
gif_left - the offset of the image from the left of the "screen" |
| 958 |
("Image Left Position") |
| 959 |
|
| 960 |
=item * |
| 961 |
|
| 962 |
gif_top - the offset of the image from the top of the "screen" ("Image |
| 963 |
Top Position") |
| 964 |
|
| 965 |
=item * |
| 966 |
|
| 967 |
gif_interlace - non-zero if the image was interlaced ("Interlace |
| 968 |
Flag") |
| 969 |
|
| 970 |
=item * |
| 971 |
|
| 972 |
gif_screen_width, gif_screen_height - the size of the logical |
| 973 |
screen. When writing this is used as the minimum. If any image being |
| 974 |
written would extend beyond this then the screen size is extended. |
| 975 |
("Logical Screen Width", "Logical Screen Height"). |
| 976 |
|
| 977 |
=item * |
| 978 |
|
| 979 |
gif_local_map - Non-zero if this image had a local color map. If set |
| 980 |
for an image when writing the image is quantized separately from the |
| 981 |
other images in the file. |
| 982 |
|
| 983 |
=item * |
| 984 |
|
| 985 |
gif_background - The index in the global color map of the logical |
| 986 |
screen's background color. This is only set if the current image uses |
| 987 |
the global color map. You can set this on write too, but for it to |
| 988 |
choose the color you want, you will need to supply only paletted |
| 989 |
images and set the C<gif_eliminate_unused> tag to 0. |
| 990 |
|
| 991 |
=item * |
| 992 |
|
| 993 |
gif_trans_index - The index of the color in the color map used for |
| 994 |
transparency. If the image has a transparency then it is returned as |
| 995 |
a 4 channel image with the alpha set to zero in this palette entry. |
| 996 |
This value is not used when writing. ("Transparent Color Index") |
| 997 |
|
| 998 |
=item * |
| 999 |
|
| 1000 |
gif_trans_color - A reference to an Imager::Color object, which is the |
| 1001 |
color to use for the palette entry used to represent transparency in |
| 1002 |
the palette. You need to set the C<transp> option (see |
| 1003 |
L<Imager::ImageTypes/"Quantization options">) for this value to be |
| 1004 |
used. |
| 1005 |
|
| 1006 |
=item * |
| 1007 |
|
| 1008 |
gif_delay - The delay until the next frame is displayed, in 1/100 of a |
| 1009 |
second. ("Delay Time"). |
| 1010 |
|
| 1011 |
=item * |
| 1012 |
|
| 1013 |
gif_user_input - whether or not a user input is expected before |
| 1014 |
continuing (view dependent) ("User Input Flag"). |
| 1015 |
|
| 1016 |
=item * |
| 1017 |
|
| 1018 |
gif_disposal - how the next frame is displayed ("Disposal Method") |
| 1019 |
|
| 1020 |
=item * |
| 1021 |
|
| 1022 |
gif_loop - the number of loops from the Netscape Loop extension. This |
| 1023 |
may be zero to loop forever. |
| 1024 |
|
| 1025 |
=item * |
| 1026 |
|
| 1027 |
gif_comment - the first block of the first GIF comment before each |
| 1028 |
image. |
| 1029 |
|
| 1030 |
=item * |
| 1031 |
|
| 1032 |
gif_eliminate_unused - If this is true, when you write a paletted |
| 1033 |
image any unused colors will be eliminated from its palette. This is |
| 1034 |
set by default. |
| 1035 |
|
| 1036 |
=item * |
| 1037 |
|
| 1038 |
gif_colormap_size - the original size of the color map for the image. |
| 1039 |
The color map of the image may have been expanded to include out of |
| 1040 |
range color indexes. |
| 1041 |
|
| 1042 |
=back |
| 1043 |
|
| 1044 |
Where applicable, the ("name") is the name of that field from the C<GIF89> |
| 1045 |
standard. |
| 1046 |
|
| 1047 |
The following GIF writing options are obsolete, you should set the |
| 1048 |
corresponding tag in the image, either by using the tags functions, or |
| 1049 |
by supplying the tag and value as options. |
| 1050 |
|
| 1051 |
=over |
| 1052 |
|
| 1053 |
=item * |
| 1054 |
|
| 1055 |
gif_each_palette - Each image in the GIF file has it's own palette if |
| 1056 |
this is non-zero. All but the first image has a local color table |
| 1057 |
(the first uses the global color table. |
| 1058 |
|
| 1059 |
Use C<gif_local_map> in new code. |
| 1060 |
|
| 1061 |
=item * |
| 1062 |
|
| 1063 |
interlace - The images are written interlaced if this is non-zero. |
| 1064 |
|
| 1065 |
Use C<gif_interlace> in new code. |
| 1066 |
|
| 1067 |
=item * |
| 1068 |
|
| 1069 |
gif_delays - A reference to an array containing the delays between |
| 1070 |
images, in 1/100 seconds. |
| 1071 |
|
| 1072 |
Use C<gif_delay> in new code. |
| 1073 |
|
| 1074 |
=item * |
| 1075 |
|
| 1076 |
gif_positions - A reference to an array of references to arrays which |
| 1077 |
represent screen positions for each image. |
| 1078 |
|
| 1079 |
New code should use the C<gif_left> and C<gif_top> tags. |
| 1080 |
|
| 1081 |
=item * |
| 1082 |
|
| 1083 |
gif_loop_count - If this is non-zero the Netscape loop extension block |
| 1084 |
is generated, which makes the animation of the images repeat. |
| 1085 |
|
| 1086 |
This is currently unimplemented due to some limitations in C<giflib>. |
| 1087 |
|
| 1088 |
=back |
| 1089 |
|
| 1090 |
You can supply a C<page> parameter to the C<read()> method to read |
| 1091 |
some page other than the first. The page is 0 based: |
| 1092 |
|
| 1093 |
# read the second image in the file |
| 1094 |
$image->read(file=>"example.gif", page=>1) |
| 1095 |
or die "Cannot read second page: ",$image->errstr,"\n"; |
| 1096 |
|
| 1097 |
Before release 0.46, Imager would read multiple image GIF image files |
| 1098 |
into a single image, overlaying each of the images onto the virtual |
| 1099 |
GIF screen. |
| 1100 |
|
| 1101 |
As of 0.46 the default is to read the first image from the file, as if |
| 1102 |
called with C<< page => 0 >>. |
| 1103 |
|
| 1104 |
You can return to the previous behavior by calling read with the |
| 1105 |
C<gif_consolidate> parameter set to a true value: |
| 1106 |
|
| 1107 |
$img->read(file=>$some_gif_file, gif_consolidate=>1); |
| 1108 |
|
| 1109 |
As with the to_paletted() method, if you supply a colors parameter as |
| 1110 |
a reference to an array, this will be filled with Imager::Color |
| 1111 |
objects of the color table generated for the image file. |
| 1112 |
|
| 1113 |
=head2 TIFF (Tagged Image File Format) |
| 1114 |
|
| 1115 |
Imager can write images to either paletted or RGB TIFF images, |
| 1116 |
depending on the type of the source image. |
| 1117 |
|
| 1118 |
When writing direct color images to TIFF the sample size of the |
| 1119 |
output file depends on the input: |
| 1120 |
|
| 1121 |
=over |
| 1122 |
|
| 1123 |
=item * |
| 1124 |
|
| 1125 |
double/sample - written as 32-bit/sample TIFF |
| 1126 |
|
| 1127 |
=item * |
| 1128 |
|
| 1129 |
16-bit/sample - written as 16-bit/sample TIFF |
| 1130 |
|
| 1131 |
=item * |
| 1132 |
|
| 1133 |
8-bit/sample - written as 8-bit/sample TIFF |
| 1134 |
|
| 1135 |
=back |
| 1136 |
|
| 1137 |
For paletted images: |
| 1138 |
|
| 1139 |
=over |
| 1140 |
|
| 1141 |
=item * |
| 1142 |
|
| 1143 |
C<< $img->is_bilevel >> is true - the image is written as bi-level |
| 1144 |
|
| 1145 |
=item * |
| 1146 |
|
| 1147 |
otherwise - image is written as paletted. |
| 1148 |
|
| 1149 |
=back |
| 1150 |
|
| 1151 |
If you are creating images for faxing you can set the I<class> |
| 1152 |
parameter set to C<fax>. By default the image is written in fine |
| 1153 |
mode, but this can be overridden by setting the I<fax_fine> parameter |
| 1154 |
to zero. Since a fax image is bi-level, Imager uses a threshold to |
| 1155 |
decide if a given pixel is black or white, based on a single channel. |
| 1156 |
For gray scale images channel 0 is used, for color images channel 1 |
| 1157 |
(green) is used. If you want more control over the conversion you can |
| 1158 |
use $img->to_paletted() to product a bi-level image. This way you can |
| 1159 |
use dithering: |
| 1160 |
|
| 1161 |
my $bilevel = $img->to_paletted(make_colors => 'mono', |
| 1162 |
translate => 'errdiff', |
| 1163 |
errdiff => 'stucki'); |
| 1164 |
|
| 1165 |
=over |
| 1166 |
|
| 1167 |
=item * |
| 1168 |
|
| 1169 |
C<class> - If set to 'fax' the image will be written as a bi-level fax |
| 1170 |
image. |
| 1171 |
|
| 1172 |
=item * |
| 1173 |
|
| 1174 |
C<fax_fine> - By default when C<class> is set to 'fax' the image is |
| 1175 |
written in fine mode, you can select normal mode by setting |
| 1176 |
C<fax_fine> to 0. |
| 1177 |
|
| 1178 |
=back |
| 1179 |
|
| 1180 |
Imager should be able to read any TIFF image you supply. Paletted |
| 1181 |
TIFF images are read as paletted Imager images, since paletted TIFF |
| 1182 |
images have 16-bits/sample (48-bits/color) this means the bottom |
| 1183 |
8-bits are lost, but this shouldn't be a big deal. |
| 1184 |
|
| 1185 |
TIFF supports the spatial resolution tags. See the |
| 1186 |
C<tiff_resolutionunit> tag for some extra options. |
| 1187 |
|
| 1188 |
As of Imager 0.62 Imager reads: |
| 1189 |
|
| 1190 |
=over |
| 1191 |
|
| 1192 |
=item * |
| 1193 |
|
| 1194 |
8-bit/sample gray, RGB or CMYK images, including a possible alpha |
| 1195 |
channel as an 8-bit/sample image. |
| 1196 |
|
| 1197 |
=item * |
| 1198 |
|
| 1199 |
16-bit gray, RGB, or CMYK image, including a possible alpha channel as |
| 1200 |
a 16-bit/sample image. |
| 1201 |
|
| 1202 |
=item * |
| 1203 |
|
| 1204 |
32-bit gray, RGB image, including a possible alpha channel as a |
| 1205 |
double/sample image. |
| 1206 |
|
| 1207 |
=item * |
| 1208 |
|
| 1209 |
bi-level images as paletted images containing only black and white, |
| 1210 |
which other formats will also write as bi-level. |
| 1211 |
|
| 1212 |
=item * |
| 1213 |
|
| 1214 |
tiled paletted images are now handled correctly |
| 1215 |
|
| 1216 |
=item * |
| 1217 |
|
| 1218 |
other images are read using C<tifflib>'s RGBA interface as |
| 1219 |
8-bit/sample images. |
| 1220 |
|
| 1221 |
=back |
| 1222 |
|
| 1223 |
The following tags are set in a TIFF image when read, and can be set |
| 1224 |
to control output: |
| 1225 |
|
| 1226 |
=over |
| 1227 |
|
| 1228 |
=item * |
| 1229 |
|
| 1230 |
C<tiff_compression> - When reading an image this is set to the numeric |
| 1231 |
value of the TIFF compression tag. |
| 1232 |
|
| 1233 |
On writing you can set this to either a numeric compression tag value, |
| 1234 |
or one of the following values: |
| 1235 |
|
| 1236 |
Ident Number Description |
| 1237 |
none 1 No compression |
| 1238 |
packbits 32773 Macintosh RLE |
| 1239 |
ccittrle 2 CCITT RLE |
| 1240 |
fax3 3 CCITT Group 3 fax encoding (T.4) |
| 1241 |
t4 3 As above |
| 1242 |
fax4 4 CCITT Group 4 fax encoding (T.6) |
| 1243 |
t6 4 As above |
| 1244 |
lzw 5 LZW |
| 1245 |
jpeg 7 JPEG |
| 1246 |
zip 8 Deflate (GZIP) Non-standard |
| 1247 |
deflate 8 As above. |
| 1248 |
oldzip 32946 Deflate with an older code. |
| 1249 |
ccittrlew 32771 Word aligned CCITT RLE |
| 1250 |
|
| 1251 |
In general a compression setting will be ignored where it doesn't make |
| 1252 |
sense, eg. C<jpeg> will be ignored for compression if the image is |
| 1253 |
being written as bilevel. |
| 1254 |
|
| 1255 |
=for stopwords LZW |
| 1256 |
|
| 1257 |
Imager attempts to check that your build of C<libtiff> supports the |
| 1258 |
given compression, and will fallback to C<packbits> if it isn't |
| 1259 |
enabled. eg. older distributions didn't include LZW compression, and |
| 1260 |
JPEG compression is only available if C<libtiff> is configured with |
| 1261 |
C<libjpeg>'s location. |
| 1262 |
|
| 1263 |
$im->write(file => 'foo.tif', tiff_compression => 'lzw') |
| 1264 |
or die $im->errstr; |
| 1265 |
|
| 1266 |
=item * |
| 1267 |
|
| 1268 |
C<tags, tiff_jpegquality>C<tiff_jpegquality> - If C<tiff_compression> |
| 1269 |
is C<jpeg> then this can be a number from 1 to 100 giving the JPEG |
| 1270 |
compression quality. High values are better quality and larger files. |
| 1271 |
|
| 1272 |
=item * |
| 1273 |
|
| 1274 |
X<tags, tiff_resolutionunit> C<tiff_resolutionunit> - The value of the |
| 1275 |
C<ResolutionUnit> tag. This is ignored on writing if the |
| 1276 |
i_aspect_only tag is non-zero. |
| 1277 |
|
| 1278 |
The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no |
| 1279 |
matter the value of this tag, they will be converted to/from the value |
| 1280 |
stored in the TIFF file. |
| 1281 |
|
| 1282 |
=item * |
| 1283 |
|
| 1284 |
X<tags, tiff_resolutionunit_name> C<tiff_resolutionunit_name> - This is |
| 1285 |
set when reading a TIFF file to the name of the unit given by |
| 1286 |
C<tiff_resolutionunit>. Possible results include C<inch>, |
| 1287 |
C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading |
| 1288 |
these files) or C<unknown>. |
| 1289 |
|
| 1290 |
=item * |
| 1291 |
|
| 1292 |
X<tags, tiff_bitspersample> C<tiff_bitspersample> - Bits per sample |
| 1293 |
from the image. This value is not used when writing an image, it is |
| 1294 |
only set on a read image. |
| 1295 |
|
| 1296 |
=item * |
| 1297 |
|
| 1298 |
X<tags, tiff_photometric> C<tiff_photometric> - Value of the |
| 1299 |
C<PhotometricInterpretation> tag from the image. This value is not |
| 1300 |
used when writing an image, it is only set on a read image. |
| 1301 |
|
| 1302 |
=item * |
| 1303 |
|
| 1304 |
C<tiff_documentname>, C<tiff_imagedescription>, C<tiff_make>, |
| 1305 |
C<tiff_model>, C<tiff_pagename>, C<tiff_software>, C<tiff_datetime>, |
| 1306 |
C<tiff_artist>, C<tiff_hostcomputer> - Various strings describing the |
| 1307 |
image. C<tiff_datetime> must be formatted as "YYYY:MM:DD HH:MM:SS". |
| 1308 |
These correspond directly to the mixed case names in the TIFF |
| 1309 |
specification. These are set in images read from a TIFF and saved |
| 1310 |
when writing a TIFF image. |
| 1311 |
|
| 1312 |
=back |
| 1313 |
|
| 1314 |
You can supply a C<page> parameter to the C<read()> method to read |
| 1315 |
some page other than the first. The page is 0 based: |
| 1316 |
|
| 1317 |
# read the second image in the file |
| 1318 |
$image->read(file=>"example.tif", page=>1) |
| 1319 |
or die "Cannot read second page: ",$image->errstr,"\n"; |
| 1320 |
|
| 1321 |
If you read an image with multiple alpha channels, then only the first |
| 1322 |
alpha channel will be read. |
| 1323 |
|
| 1324 |
When reading a C<TIFF> image with callbacks, the C<seekcb> callback |
| 1325 |
parameter is also required. |
| 1326 |
|
| 1327 |
When writing a C<TIFF> image with callbacks, the C<seekcb> and |
| 1328 |
C<readcb> parameters are also required. |
| 1329 |
|
| 1330 |
C<TIFF> is a random access file format, it cannot be read from or |
| 1331 |
written to unseekable streams such as pipes or sockets. |
| 1332 |
|
| 1333 |
=head2 BMP (Windows Bitmap) |
| 1334 |
|
| 1335 |
Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted |
| 1336 |
Windows BMP files. Currently you cannot write compressed BMP files |
| 1337 |
with Imager. |
| 1338 |
|
| 1339 |
Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted |
| 1340 |
Windows BMP files. There is some support for reading 16-bit per pixel |
| 1341 |
images, but I haven't found any for testing. |
| 1342 |
|
| 1343 |
BMP has no support for multiple image files. |
| 1344 |
|
| 1345 |
BMP files support the spatial resolution tags, but since BMP has no |
| 1346 |
support for storing only an aspect ratio, if C<i_aspect_only> is set |
| 1347 |
when you write the C<i_xres> and C<i_yres> values are scaled so the |
| 1348 |
smaller is 72 DPI. |
| 1349 |
|
| 1350 |
The following tags are set when you read an image from a BMP file: |
| 1351 |
|
| 1352 |
=over |
| 1353 |
|
| 1354 |
=item bmp_compression |
| 1355 |
|
| 1356 |
The type of compression, if any. This can be any of the following |
| 1357 |
values: |
| 1358 |
|
| 1359 |
=for stopwords RLE |
| 1360 |
|
| 1361 |
=over |
| 1362 |
|
| 1363 |
=item BI_RGB (0) |
| 1364 |
|
| 1365 |
Uncompressed. |
| 1366 |
|
| 1367 |
=item BI_RLE8 (1) |
| 1368 |
|
| 1369 |
8-bits/pixel paletted value RLE compression. |
| 1370 |
|
| 1371 |
=item BI_RLE4 (2) |
| 1372 |
|
| 1373 |
4-bits/pixel paletted value RLE compression. |
| 1374 |
|
| 1375 |
=item BI_BITFIELDS (3) |
| 1376 |
|
| 1377 |
Packed RGB values. |
| 1378 |
|
| 1379 |
=back |
| 1380 |
|
| 1381 |
=item bmp_compression_name |
| 1382 |
|
| 1383 |
The bmp_compression value as a BI_* string |
| 1384 |
|
| 1385 |
=item bmp_important_colors |
| 1386 |
|
| 1387 |
The number of important colors as defined by the writer of the image. |
| 1388 |
|
| 1389 |
=item bmp_used_colors |
| 1390 |
|
| 1391 |
Number of color used from the BMP header |
| 1392 |
|
| 1393 |
=item bmp_filesize |
| 1394 |
|
| 1395 |
The file size from the BMP header |
| 1396 |
|
| 1397 |
=item bmp_bit_count |
| 1398 |
|
| 1399 |
Number of bits stored per pixel. (24, 8, 4 or 1) |
| 1400 |
|
| 1401 |
=back |
| 1402 |
|
| 1403 |
=for stopwords Targa |
| 1404 |
|
| 1405 |
=head2 TGA (Targa) |
| 1406 |
|
| 1407 |
When storing Targa images RLE compression can be activated with the |
| 1408 |
C<compress> parameter, the C<idstring> parameter can be used to set the |
| 1409 |
Targa comment field and the C<wierdpack> option can be used to use the |
| 1410 |
15 and 16 bit Targa formats for RGB and RGBA data. The 15 bit format |
| 1411 |
has 5 of each red, green and blue. The 16 bit format in addition |
| 1412 |
allows 1 bit of alpha. The most significant bits are used for each |
| 1413 |
channel. |
| 1414 |
|
| 1415 |
Tags: |
| 1416 |
|
| 1417 |
=over |
| 1418 |
|
| 1419 |
=item tga_idstring |
| 1420 |
|
| 1421 |
=item tga_bitspp |
| 1422 |
|
| 1423 |
=item compressed |
| 1424 |
|
| 1425 |
=back |
| 1426 |
|
| 1427 |
=head2 RAW |
| 1428 |
|
| 1429 |
When reading raw images you need to supply the width and height of the |
| 1430 |
image in the C<xsize> and C<ysize> options: |
| 1431 |
|
| 1432 |
$img->read(file=>'foo.raw', xsize=>100, ysize=>100) |
| 1433 |
or die "Cannot read raw image\n"; |
| 1434 |
|
| 1435 |
If your input file has more channels than you want, or (as is common), |
| 1436 |
junk in the fourth channel, you can use the C<raw_datachannels> and |
| 1437 |
C<raw_storechannels> options to control the number of channels in your input |
| 1438 |
file and the resulting channels in your image. For example, if your |
| 1439 |
input image uses 32-bits per pixel with red, green, blue and junk |
| 1440 |
values for each pixel you could do: |
| 1441 |
|
| 1442 |
$img->read(file=>'foo.raw', xsize => 100, ysize => 100, |
| 1443 |
raw_datachannels => 4, raw_storechannels => 3, |
| 1444 |
raw_interleave => 0) |
| 1445 |
or die "Cannot read raw image\n"; |
| 1446 |
|
| 1447 |
In general, if you supply C<raw_storechannels> you should also supply |
| 1448 |
C<raw_datachannels> |
| 1449 |
|
| 1450 |
Read parameters: |
| 1451 |
|
| 1452 |
=over |
| 1453 |
|
| 1454 |
=item * |
| 1455 |
|
| 1456 |
C<raw_interleave> - controls the ordering of samples within the image. |
| 1457 |
Default: 1. Alternatively and historically spelled C<interleave>. |
| 1458 |
Possible values: |
| 1459 |
|
| 1460 |
=over |
| 1461 |
|
| 1462 |
=item * |
| 1463 |
|
| 1464 |
0 - samples are pixel by pixel, so all samples for the first pixel, |
| 1465 |
then all samples for the second pixel and so on. eg. for a four pixel |
| 1466 |
scan line the channels would be laid out as: |
| 1467 |
|
| 1468 |
012012012012 |
| 1469 |
|
| 1470 |
=item * |
| 1471 |
|
| 1472 |
1 - samples are line by line, so channel 0 for the entire scan line is |
| 1473 |
followed by channel 1 for the entire scan line and so on. eg. for a |
| 1474 |
four pixel scan line the channels would be laid out as: |
| 1475 |
|
| 1476 |
000011112222 |
| 1477 |
|
| 1478 |
This is the default. |
| 1479 |
|
| 1480 |
=back |
| 1481 |
|
| 1482 |
Unfortunately, historically, the default C<raw_interleave> for read |
| 1483 |
has been 1, while writing only supports the C<raw_interleave> = 0 |
| 1484 |
format. |
| 1485 |
|
| 1486 |
For future compatibility, you should always supply the |
| 1487 |
C<raw_interleave> (or C<interleave>) parameter. As of 0.68, Imager |
| 1488 |
will warn if you attempt to read a raw image without a |
| 1489 |
C<raw_interleave> parameter. |
| 1490 |
|
| 1491 |
=item * |
| 1492 |
|
| 1493 |
C<raw_storechannels> - the number of channels to store in the image. |
| 1494 |
Range: 1 to 4. Default: 3. Alternatively and historically spelled |
| 1495 |
C<storechannels>. |
| 1496 |
|
| 1497 |
=item * |
| 1498 |
|
| 1499 |
C<raw_datachannels> - the number of channels to read from the file. |
| 1500 |
Range: 1 or more. Default: 3. Alternatively and historically spelled |
| 1501 |
C<datachannels>. |
| 1502 |
|
| 1503 |
=back |
| 1504 |
|
| 1505 |
$img->read(file=>'foo.raw', xsize=100, ysize=>100, raw_interleave=>1) |
| 1506 |
or die "Cannot read raw image\n"; |
| 1507 |
|
| 1508 |
=head2 PNG |
| 1509 |
|
| 1510 |
=head3 PNG Image modes |
| 1511 |
|
| 1512 |
PNG files can be read and written in the following modes: |
| 1513 |
|
| 1514 |
=over |
| 1515 |
|
| 1516 |
=item * |
| 1517 |
|
| 1518 |
bi-level - written as a 1-bit per sample gray scale image |
| 1519 |
|
| 1520 |
=item * |
| 1521 |
|
| 1522 |
paletted - Imager gray scale paletted images are written as RGB |
| 1523 |
paletted images. PNG palettes can include alpha values for each entry |
| 1524 |
and this is honored as an Imager four channel paletted image. |
| 1525 |
|
| 1526 |
=item * |
| 1527 |
|
| 1528 |
8 and 16-bit per sample gray scale, optionally with an alpha channel. |
| 1529 |
|
| 1530 |
=item * |
| 1531 |
|
| 1532 |
8 and 16-bit per sample RGB, optionally with an alpha channel. |
| 1533 |
|
| 1534 |
=back |
| 1535 |
|
| 1536 |
Unlike GIF, there is no automatic conversion to a paletted image, |
| 1537 |
since PNG supports direct color. |
| 1538 |
|
| 1539 |
=head3 PNG Text tags |
| 1540 |
|
| 1541 |
Text tags are retrieved from and written to PNG C<tEXT> or C<zTXT> |
| 1542 |
chunks. The following standard tags from the PNG specification are |
| 1543 |
directly supported: |
| 1544 |
|
| 1545 |
=over |
| 1546 |
|
| 1547 |
=item * |
| 1548 |
|
| 1549 |
C<i_comment> X<tags,i_comment> - keyword of "Comment". |
| 1550 |
|
| 1551 |
=item * |
| 1552 |
|
| 1553 |
C<png_author> X<tags,PNG,png_author> - keyword "Author". |
| 1554 |
|
| 1555 |
=item * |
| 1556 |
|
| 1557 |
C<png_copyright> X<tags,PNG,png_copyright> - keyword "Copyright". |
| 1558 |
|
| 1559 |
=item * |
| 1560 |
|
| 1561 |
C<png_creation_time> X<tags,PNG,png_creation_time> - keyword "Creation Time". |
| 1562 |
|
| 1563 |
=item * |
| 1564 |
|
| 1565 |
C<png_description> X<tags,PNG,png_description> - keyword "Description". |
| 1566 |
|
| 1567 |
=item * |
| 1568 |
|
| 1569 |
C<png_disclaimer> X<tags,PNG,png_disclaimer> - keyword "Disclaimer". |
| 1570 |
|
| 1571 |
=item * |
| 1572 |
|
| 1573 |
C<png_software> X<tags,PNG,png_software> - keyword "Software". |
| 1574 |
|
| 1575 |
=item * |
| 1576 |
|
| 1577 |
C<png_title> X<tags,PNG,png_title> - keyword "Title". |
| 1578 |
|
| 1579 |
=item * |
| 1580 |
|
| 1581 |
C<png_warning> X<tags,PNG,png_warning> - keyword "Warning". |
| 1582 |
|
| 1583 |
=back |
| 1584 |
|
| 1585 |
Each of these tags has a corresponding C<< I<base-tag-name>_compressed |
| 1586 |
>> tag, eg. C<png_comment_compressed>. When reading, if the PNG chunk |
| 1587 |
is compressed this tag will be set to 1, but is otherwise unset. When |
| 1588 |
writing, Imager will honor the compression tag if set and non-zero, |
| 1589 |
otherwise the chunk text will be compressed if the value is longer |
| 1590 |
than 1000 characters, as recommended by the C<libpng> documentation. |
| 1591 |
|
| 1592 |
PNG C<tEXT> or C<zTXT> chunks outside of those above are read into or |
| 1593 |
written from Imager tags named like: |
| 1594 |
|
| 1595 |
=over |
| 1596 |
|
| 1597 |
=item * |
| 1598 |
|
| 1599 |
C<< png_textI<N>_key >> - the key for the text chunk. This can be 1 |
| 1600 |
to 79 characters, may not contain any leading, trailing or consecutive |
| 1601 |
spaces, and may contain only Latin-1 characters from 32-126, 161-255. |
| 1602 |
|
| 1603 |
=item * |
| 1604 |
|
| 1605 |
C<< png_textI<N>_text >> - the text for the text chunk. This may not |
| 1606 |
contain any C<NUL> characters. |
| 1607 |
|
| 1608 |
=item * |
| 1609 |
|
| 1610 |
C<< png_textI<N>_compressed >> - whether or not the text chunk is |
| 1611 |
compressed. This behaves similarly to the C<< |
| 1612 |
I<base-tag-name>_compressed >> tags described above. |
| 1613 |
|
| 1614 |
=back |
| 1615 |
|
| 1616 |
Where I<N> starts from 0. When writing both the C<..._key> and |
| 1617 |
C<..._text> tags must be present or the write will fail. If the key |
| 1618 |
or text do not satisfy the requirements above the write will fail. |
| 1619 |
|
| 1620 |
=head3 Other PNG metadata tags |
| 1621 |
|
| 1622 |
=over |
| 1623 |
|
| 1624 |
=item * |
| 1625 |
|
| 1626 |
X<tags, png_interlace> C<png_interlace>, C<png_interlace_name> - only |
| 1627 |
set when reading, C<png_interlace> is set to the type of interlacing |
| 1628 |
used by the file, 0 for one, 1 for Adam7. C<png_interlace_name> is |
| 1629 |
set to a keyword describing the interlacing, either C<none> or |
| 1630 |
C<adam7>. |
| 1631 |
|
| 1632 |
=item * |
| 1633 |
|
| 1634 |
X<tags, png_srgb_intent> C<png_srgb_intent> - the sRGB rendering intent |
| 1635 |
for the image. an integer from 0 to 3, per the PNG specification. If |
| 1636 |
this chunk is found in the PNG file the C<gAMA> and C<cHRM> are |
| 1637 |
ignored and the C<png_gamma> and C<png_chroma_...> tags are not set. |
| 1638 |
Similarly when writing if C<png_srgb_intent> is set the C<gAMA> and |
| 1639 |
C<cHRM> chunks are not written. |
| 1640 |
|
| 1641 |
=item * |
| 1642 |
|
| 1643 |
X<tags, png_gamma> C<png_gamma> - the gamma of the image. This value is |
| 1644 |
not currently used by Imager when processing the image, but this may |
| 1645 |
change in the future. |
| 1646 |
|
| 1647 |
=item * |
| 1648 |
|
| 1649 |
X<tags, png_chroma_...> C<png_chroma_white_x>, C<png_chroma_white_y>, |
| 1650 |
C<png_chroma_red_x>, C<png_chroma_red_y>, C<png_chroma_green_x>, |
| 1651 |
C<png_chroma_green_y>, C<png_chroma_blue_x>, C<png_chroma_blue_y> - |
| 1652 |
the primary chromaticities of the image, defining the color model. |
| 1653 |
This is currently not used by Imager when processing the image, but |
| 1654 |
this may change in the future. |
| 1655 |
|
| 1656 |
=item * |
| 1657 |
|
| 1658 |
C<i_xres>, C<i_yres>, C<i_aspect_only> - processed per |
| 1659 |
I<Imager::ImageTypes/CommonTags>. |
| 1660 |
|
| 1661 |
=item * |
| 1662 |
|
| 1663 |
X<tags, png_bits> C<png_bits> - the number of bits per sample in the |
| 1664 |
representation. Ignored when writing. |
| 1665 |
|
| 1666 |
=item * |
| 1667 |
|
| 1668 |
X<tags, png_time> C<png_time> - the creation time of the file formatted |
| 1669 |
as C<< I<year>-I<month>-I<day>TI<hour>:I<minute>:I<second> >>. This |
| 1670 |
is stored as time data structure in the file, not a string. If you |
| 1671 |
set C<png_time> and it cannot be parsed as above, writing the PNG file |
| 1672 |
will fail. |
| 1673 |
|
| 1674 |
=item * |
| 1675 |
|
| 1676 |
C<i_background> - set from the C<sBKG> when reading an image file. |
| 1677 |
|
| 1678 |
=back |
| 1679 |
|
| 1680 |
X<compression> X<png_compression_level>You can control the level of |
| 1681 |
F<zlib> compression used when writing with the |
| 1682 |
C<png_compression_level> parameter. This can be an integer between 0 |
| 1683 |
(uncompressed) and 9 (best compression). |
| 1684 |
|
| 1685 |
=for stopwords |
| 1686 |
CRC |
| 1687 |
|
| 1688 |
X<png_ignore_benign_errors> If you're using F<libpng> 1.6 or later, or |
| 1689 |
an earlier release configured with C<PNG_BENIGN_ERRORS_SUPPORTED>, you |
| 1690 |
can choose to ignore file format errors the authors of F<libpng> |
| 1691 |
consider I<benign>, this includes at least CRC errors and palette |
| 1692 |
index overflows. Do this by supplying a true value for the |
| 1693 |
C<png_ignore_benign_errors> parameter to the read() method: |
| 1694 |
|
| 1695 |
$im->read(file => "foo.png", png_ignore_benign_errors => 1) |
| 1696 |
or die $im->errstr; |
| 1697 |
|
| 1698 |
=head2 ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor) |
| 1699 |
|
| 1700 |
Icon and Cursor files are very similar, the only differences being a |
| 1701 |
number in the header and the storage of the cursor hot spot. I've |
| 1702 |
treated them separately so that you're not messing with tags to |
| 1703 |
distinguish between them. |
| 1704 |
|
| 1705 |
The following tags are set when reading an icon image and are used |
| 1706 |
when writing it: |
| 1707 |
|
| 1708 |
=over |
| 1709 |
|
| 1710 |
=item ico_mask |
| 1711 |
|
| 1712 |
This is the AND mask of the icon. When used as an icon in Windows 1 |
| 1713 |
bits in the mask correspond to pixels that are modified by the source |
| 1714 |
image rather than simply replaced by the source image. |
| 1715 |
|
| 1716 |
Rather than requiring a binary bitmap this is accepted in a specific format: |
| 1717 |
|
| 1718 |
=over |
| 1719 |
|
| 1720 |
=item * |
| 1721 |
|
| 1722 |
first line consisting of the 0 placeholder, the 1 placeholder and a |
| 1723 |
newline. |
| 1724 |
|
| 1725 |
=item * |
| 1726 |
|
| 1727 |
following lines which contain 0 and 1 placeholders for each scan line |
| 1728 |
of the image, starting from the top of the image. |
| 1729 |
|
| 1730 |
=back |
| 1731 |
|
| 1732 |
When reading an image, '.' is used as the 0 placeholder and '*' as the |
| 1733 |
1 placeholder. An example: |
| 1734 |
|
| 1735 |
.* |
| 1736 |
..........................****** |
| 1737 |
..........................****** |
| 1738 |
..........................****** |
| 1739 |
..........................****** |
| 1740 |
...........................***** |
| 1741 |
............................**** |
| 1742 |
............................**** |
| 1743 |
.............................*** |
| 1744 |
.............................*** |
| 1745 |
.............................*** |
| 1746 |
.............................*** |
| 1747 |
..............................** |
| 1748 |
..............................** |
| 1749 |
...............................* |
| 1750 |
...............................* |
| 1751 |
................................ |
| 1752 |
................................ |
| 1753 |
................................ |
| 1754 |
................................ |
| 1755 |
................................ |
| 1756 |
................................ |
| 1757 |
*............................... |
| 1758 |
**.............................. |
| 1759 |
**.............................. |
| 1760 |
***............................. |
| 1761 |
***............................. |
| 1762 |
****............................ |
| 1763 |
****............................ |
| 1764 |
*****........................... |
| 1765 |
*****........................... |
| 1766 |
*****........................... |
| 1767 |
*****........................... |
| 1768 |
|
| 1769 |
=back |
| 1770 |
|
| 1771 |
The following tags are set when reading an icon: |
| 1772 |
|
| 1773 |
=over |
| 1774 |
|
| 1775 |
=item ico_bits |
| 1776 |
|
| 1777 |
The number of bits per pixel used to store the image. |
| 1778 |
|
| 1779 |
=back |
| 1780 |
|
| 1781 |
For cursor files the following tags are set and read when reading and |
| 1782 |
writing: |
| 1783 |
|
| 1784 |
=over |
| 1785 |
|
| 1786 |
=item cur_mask |
| 1787 |
|
| 1788 |
This is the same as the ico_mask above. |
| 1789 |
|
| 1790 |
=item cur_hotspotx |
| 1791 |
|
| 1792 |
=item cur_hotspoty |
| 1793 |
|
| 1794 |
The "hot" spot of the cursor image. This is the spot on the cursor |
| 1795 |
that you click with. If you set these to out of range values they are |
| 1796 |
clipped to the size of the image when written to the file. |
| 1797 |
|
| 1798 |
=back |
| 1799 |
|
| 1800 |
The following parameters can be supplied to read() or read_multi() to |
| 1801 |
control reading of ICO/CUR files: |
| 1802 |
|
| 1803 |
=over |
| 1804 |
|
| 1805 |
=item * |
| 1806 |
|
| 1807 |
C<ico_masked> - if true, the default, then the icon/cursors mask is |
| 1808 |
applied as an alpha channel to the image, unless that image already |
| 1809 |
has an alpha channel. This may result in a paletted image being |
| 1810 |
returned as a direct color image. Default: 1 |
| 1811 |
|
| 1812 |
# retrieve the image as stored, without using the mask as an alpha |
| 1813 |
# channel |
| 1814 |
$img->read(file => 'foo.ico', ico_masked => 0) |
| 1815 |
or die $img->errstr; |
| 1816 |
|
| 1817 |
This was introduced in Imager 0.60. Previously reading ICO images |
| 1818 |
acted as if C<ico_masked =E<gt> 0>. |
| 1819 |
|
| 1820 |
=item * |
| 1821 |
|
| 1822 |
C<ico_alpha_masked> - if true, then the icon/cursor mask is applied as |
| 1823 |
an alpha channel to images that already have an alpha mask. Note that |
| 1824 |
this will only make pixels transparent, not opaque. Default: 0. |
| 1825 |
|
| 1826 |
Note: If you get different results between C<ico_alpha_masked> being |
| 1827 |
set to 0 and 1, your mask may break when used with the Win32 API. |
| 1828 |
|
| 1829 |
=back |
| 1830 |
|
| 1831 |
C<cur_bits> is set when reading a cursor. |
| 1832 |
|
| 1833 |
Examples: |
| 1834 |
|
| 1835 |
my $img = Imager->new(xsize => 32, ysize => 32, channels => 4); |
| 1836 |
$im->box(color => 'FF0000'); |
| 1837 |
$im->write(file => 'box.ico'); |
| 1838 |
|
| 1839 |
$im->settag(name => 'cur_hotspotx', value => 16); |
| 1840 |
$im->settag(name => 'cur_hotspoty', value => 16); |
| 1841 |
$im->write(file => 'box.cur'); |
| 1842 |
|
| 1843 |
=for stopwords BW |
| 1844 |
|
| 1845 |
=head2 SGI (RGB, BW) |
| 1846 |
|
| 1847 |
SGI images, often called by the extensions, RGB or BW, can be stored |
| 1848 |
either uncompressed or compressed using an RLE compression. |
| 1849 |
|
| 1850 |
By default, when saving to an extension of C<rgb>, C<bw>, C<sgi>, |
| 1851 |
C<rgba> the file will be saved in SGI format. The file extension is |
| 1852 |
otherwise ignored, so saving a 3-channel image to a C<.bw> file will |
| 1853 |
result in a 3-channel image on disk. |
| 1854 |
|
| 1855 |
The following tags are set when reading a SGI image: |
| 1856 |
|
| 1857 |
=over |
| 1858 |
|
| 1859 |
=item * |
| 1860 |
|
| 1861 |
i_comment - the C<IMAGENAME> field from the image. Also written to |
| 1862 |
the file when writing. |
| 1863 |
|
| 1864 |
=item * |
| 1865 |
|
| 1866 |
sgi_pixmin, sgi_pixmax - the C<PIXMIN> and C<PIXMAX> fields from the |
| 1867 |
image. On reading image data is expanded from this range to the full |
| 1868 |
range of samples in the image. |
| 1869 |
|
| 1870 |
=item * |
| 1871 |
|
| 1872 |
sgi_bpc - the number of bytes per sample for the image. Ignored when |
| 1873 |
writing. |
| 1874 |
|
| 1875 |
=item * |
| 1876 |
|
| 1877 |
sgi_rle - whether or not the image is compressed. If this is non-zero |
| 1878 |
when writing the image will be compressed. |
| 1879 |
|
| 1880 |
=back |
| 1881 |
|
| 1882 |
=head1 ADDING NEW FORMATS |
| 1883 |
|
| 1884 |
To support a new format for reading, call the register_reader() class |
| 1885 |
method: |
| 1886 |
|
| 1887 |
=over |
| 1888 |
|
| 1889 |
=item register_reader() |
| 1890 |
|
| 1891 |
Registers single or multiple image read functions. |
| 1892 |
|
| 1893 |
Parameters: |
| 1894 |
|
| 1895 |
=over |
| 1896 |
|
| 1897 |
=item * |
| 1898 |
|
| 1899 |
type - the identifier of the file format, if Imager's |
| 1900 |
i_test_format_probe() can identify the format then this value should |
| 1901 |
match i_test_format_probe()'s result. |
| 1902 |
|
| 1903 |
This parameter is required. |
| 1904 |
|
| 1905 |
=item * |
| 1906 |
|
| 1907 |
single - a code ref to read a single image from a file. This is |
| 1908 |
supplied: |
| 1909 |
|
| 1910 |
=over |
| 1911 |
|
| 1912 |
=item * |
| 1913 |
|
| 1914 |
the object that read() was called on, |
| 1915 |
|
| 1916 |
=item * |
| 1917 |
|
| 1918 |
an Imager::IO object that should be used to read the file, and |
| 1919 |
|
| 1920 |
=item * |
| 1921 |
|
| 1922 |
all the parameters supplied to the read() method. |
| 1923 |
|
| 1924 |
=back |
| 1925 |
|
| 1926 |
The single parameter is required. |
| 1927 |
|
| 1928 |
=item * |
| 1929 |
|
| 1930 |
multiple - a code ref which is called to read multiple images from a |
| 1931 |
file. This is supplied: |
| 1932 |
|
| 1933 |
=over |
| 1934 |
|
| 1935 |
=item * |
| 1936 |
|
| 1937 |
an Imager::IO object that should be used to read the file, and |
| 1938 |
|
| 1939 |
=item * |
| 1940 |
|
| 1941 |
all the parameters supplied to the read_multi() method. |
| 1942 |
|
| 1943 |
=back |
| 1944 |
|
| 1945 |
=back |
| 1946 |
|
| 1947 |
Example: |
| 1948 |
|
| 1949 |
# from Imager::File::ICO |
| 1950 |
Imager->register_reader |
| 1951 |
( |
| 1952 |
type=>'ico', |
| 1953 |
single => |
| 1954 |
sub { |
| 1955 |
my ($im, $io, %hsh) = @_; |
| 1956 |
$im->{IMG} = i_readico_single($io, $hsh{page} || 0); |
| 1957 |
|
| 1958 |
unless ($im->{IMG}) { |
| 1959 |
$im->_set_error(Imager->_error_as_msg); |
| 1960 |
return; |
| 1961 |
} |
| 1962 |
return $im; |
| 1963 |
}, |
| 1964 |
multiple => |
| 1965 |
sub { |
| 1966 |
my ($io, %hsh) = @_; |
| 1967 |
|
| 1968 |
my @imgs = i_readico_multi($io); |
| 1969 |
unless (@imgs) { |
| 1970 |
Imager->_set_error(Imager->_error_as_msg); |
| 1971 |
return; |
| 1972 |
} |
| 1973 |
return map { |
| 1974 |
bless { IMG => $_, DEBUG => $Imager::DEBUG, ERRSTR => undef }, 'Imager' |
| 1975 |
} @imgs; |
| 1976 |
}, |
| 1977 |
); |
| 1978 |
|
| 1979 |
=item register_writer() |
| 1980 |
|
| 1981 |
Registers single or multiple image write functions. |
| 1982 |
|
| 1983 |
Parameters: |
| 1984 |
|
| 1985 |
=over |
| 1986 |
|
| 1987 |
=item * |
| 1988 |
|
| 1989 |
type - the identifier of the file format. This is typically the |
| 1990 |
extension in lowercase. |
| 1991 |
|
| 1992 |
This parameter is required. |
| 1993 |
|
| 1994 |
=item * |
| 1995 |
|
| 1996 |
single - a code ref to write a single image to a file. This is |
| 1997 |
supplied: |
| 1998 |
|
| 1999 |
=over |
| 2000 |
|
| 2001 |
=item * |
| 2002 |
|
| 2003 |
the object that write() was called on, |
| 2004 |
|
| 2005 |
=item * |
| 2006 |
|
| 2007 |
an Imager::IO object that should be used to write the file, and |
| 2008 |
|
| 2009 |
=item * |
| 2010 |
|
| 2011 |
all the parameters supplied to the write() method. |
| 2012 |
|
| 2013 |
=back |
| 2014 |
|
| 2015 |
The single parameter is required. |
| 2016 |
|
| 2017 |
=item * |
| 2018 |
|
| 2019 |
multiple - a code ref which is called to write multiple images to a |
| 2020 |
file. This is supplied: |
| 2021 |
|
| 2022 |
=over |
| 2023 |
|
| 2024 |
=item * |
| 2025 |
|
| 2026 |
the class name write_multi() was called on, this is typically |
| 2027 |
C<Imager>. |
| 2028 |
|
| 2029 |
=item * |
| 2030 |
|
| 2031 |
an Imager::IO object that should be used to write the file, and |
| 2032 |
|
| 2033 |
=item * |
| 2034 |
|
| 2035 |
all the parameters supplied to the read_multi() method. |
| 2036 |
|
| 2037 |
=back |
| 2038 |
|
| 2039 |
=back |
| 2040 |
|
| 2041 |
=item add_type_extensions($type, $ext, ...) |
| 2042 |
|
| 2043 |
This class method can be used to add extensions to the map used by |
| 2044 |
C<def_guess_type> when working out the file type a filename extension. |
| 2045 |
|
| 2046 |
Imager->add_type_extension(mytype => "mytype", "mytypish"); |
| 2047 |
... |
| 2048 |
$im->write(file => "foo.mytypish") # use the mytype handler |
| 2049 |
|
| 2050 |
=back |
| 2051 |
|
| 2052 |
If you name the reader module C<Imager::File::>I<your-format-name> |
| 2053 |
where I<your-format-name> is a fully upper case version of the type |
| 2054 |
value you would pass to read(), read_multi(), write() or write_multi() |
| 2055 |
then Imager will attempt to load that module if it has no other way to |
| 2056 |
read or write that format. |
| 2057 |
|
| 2058 |
For example, if you create a module Imager::File::GIF and the user has |
| 2059 |
built Imager without it's normal GIF support then an attempt to read a |
| 2060 |
GIF image will attempt to load Imager::File::GIF. |
| 2061 |
|
| 2062 |
If your module can only handle reading then you can name your module |
| 2063 |
C<Imager::File::>I<your-format-name>C<Reader> and Imager will attempt |
| 2064 |
to autoload it. |
| 2065 |
|
| 2066 |
If your module can only handle writing then you can name your module |
| 2067 |
C<Imager::File::>I<your-format-name>C<Writer> and Imager will attempt |
| 2068 |
to autoload it. |
| 2069 |
|
| 2070 |
=head1 PRELOADING FILE MODULES |
| 2071 |
|
| 2072 |
=over |
| 2073 |
|
| 2074 |
=item preload() |
| 2075 |
|
| 2076 |
C<< Imager->preload >> preloads the file support modules included with or |
| 2077 |
that have been included with Imager in the past. |
| 2078 |
|
| 2079 |
It isn't typically needed, but some cases where you might want to use |
| 2080 |
it: |
| 2081 |
|
| 2082 |
=over |
| 2083 |
|
| 2084 |
=item * |
| 2085 |
|
| 2086 |
For use in forking servers such as mod_perl to allow as many pages as |
| 2087 |
possible to be shared between parent and child processes. |
| 2088 |
|
| 2089 |
=item * |
| 2090 |
|
| 2091 |
to avoid runtime loading of a module from delaying output in an |
| 2092 |
animation. |
| 2093 |
|
| 2094 |
=item * |
| 2095 |
|
| 2096 |
if you're loading modules from a relative C<$ENV{PERL5LIB}> and expect |
| 2097 |
to change directories. |
| 2098 |
|
| 2099 |
=back |
| 2100 |
|
| 2101 |
You probably don't need it. |
| 2102 |
|
| 2103 |
If the module is not available no error occurs. |
| 2104 |
|
| 2105 |
Preserves $@. |
| 2106 |
|
| 2107 |
use Imager; |
| 2108 |
Imager->preload; |
| 2109 |
|
| 2110 |
=back |
| 2111 |
|
| 2112 |
=head1 EXAMPLES |
| 2113 |
|
| 2114 |
=head2 Producing an image from a CGI script |
| 2115 |
|
| 2116 |
Once you have an image the basic mechanism is: |
| 2117 |
|
| 2118 |
=for stopwords STDOUT |
| 2119 |
|
| 2120 |
=over |
| 2121 |
|
| 2122 |
=item 1. |
| 2123 |
|
| 2124 |
set STDOUT to autoflush |
| 2125 |
|
| 2126 |
=item 2. |
| 2127 |
|
| 2128 |
output a content-type header, and optionally a content-length header |
| 2129 |
|
| 2130 |
=item 3. |
| 2131 |
|
| 2132 |
put STDOUT into binmode |
| 2133 |
|
| 2134 |
=item 4. |
| 2135 |
|
| 2136 |
call write() with the C<fd> or C<fh> parameter. You will need to |
| 2137 |
provide the C<type> parameter since Imager can't use the extension to |
| 2138 |
guess the file format you want. |
| 2139 |
|
| 2140 |
=back |
| 2141 |
|
| 2142 |
# write an image from a CGI script |
| 2143 |
# using CGI.pm |
| 2144 |
use CGI qw(:standard); |
| 2145 |
$| = 1; |
| 2146 |
binmode STDOUT; |
| 2147 |
print header(-type=>'image/gif'); |
| 2148 |
$img->write(type=>'gif', fd=>fileno(STDOUT)) |
| 2149 |
or die $img->errstr; |
| 2150 |
|
| 2151 |
If you want to send a content length you can send the output to a |
| 2152 |
scalar to get the length: |
| 2153 |
|
| 2154 |
my $data; |
| 2155 |
$img->write(type=>'gif', data=>\$data) |
| 2156 |
or die $img->errstr; |
| 2157 |
binmode STDOUT; |
| 2158 |
print header(-type=>'image/gif', -content_length=>length($data)); |
| 2159 |
print $data; |
| 2160 |
|
| 2161 |
=head2 Writing an animated GIF |
| 2162 |
|
| 2163 |
The basic idea is simple, just use write_multi(): |
| 2164 |
|
| 2165 |
my @imgs = ...; |
| 2166 |
Imager->write_multi({ file=>$filename, type=>'gif' }, @imgs); |
| 2167 |
|
| 2168 |
If your images are RGB images the default quantization mechanism will |
| 2169 |
produce a very good result, but can take a long time to execute. You |
| 2170 |
could either use the standard web color map: |
| 2171 |
|
| 2172 |
Imager->write_multi({ file=>$filename, |
| 2173 |
type=>'gif', |
| 2174 |
make_colors=>'webmap' }, |
| 2175 |
@imgs); |
| 2176 |
|
| 2177 |
or use a median cut algorithm to built a fairly optimal color map: |
| 2178 |
|
| 2179 |
Imager->write_multi({ file=>$filename, |
| 2180 |
type=>'gif', |
| 2181 |
make_colors=>'mediancut' }, |
| 2182 |
@imgs); |
| 2183 |
|
| 2184 |
By default all of the images will use the same global color map, which |
| 2185 |
will produce a smaller image. If your images have significant color |
| 2186 |
differences, you may want to generate a new palette for each image: |
| 2187 |
|
| 2188 |
Imager->write_multi({ file=>$filename, |
| 2189 |
type=>'gif', |
| 2190 |
make_colors=>'mediancut', |
| 2191 |
gif_local_map => 1 }, |
| 2192 |
@imgs); |
| 2193 |
|
| 2194 |
which will set the C<gif_local_map> tag in each image to 1. |
| 2195 |
Alternatively, if you know only some images have different colors, you |
| 2196 |
can set the tag just for those images: |
| 2197 |
|
| 2198 |
$imgs[2]->settag(name=>'gif_local_map', value=>1); |
| 2199 |
$imgs[4]->settag(name=>'gif_local_map', value=>1); |
| 2200 |
|
| 2201 |
and call write_multi() without a C<gif_local_map> parameter, or supply |
| 2202 |
an arrayref of values for the tag: |
| 2203 |
|
| 2204 |
Imager->write_multi({ file=>$filename, |
| 2205 |
type=>'gif', |
| 2206 |
make_colors=>'mediancut', |
| 2207 |
gif_local_map => [ 0, 0, 1, 0, 1 ] }, |
| 2208 |
@imgs); |
| 2209 |
|
| 2210 |
Other useful parameters include C<gif_delay> to control the delay |
| 2211 |
between frames and C<transp> to control transparency. |
| 2212 |
|
| 2213 |
=head2 Reading tags after reading an image |
| 2214 |
|
| 2215 |
This is pretty simple: |
| 2216 |
|
| 2217 |
# print the author of a TIFF, if any |
| 2218 |
my $img = Imager->new; |
| 2219 |
$img->read(file=>$filename, type='tiff') or die $img->errstr; |
| 2220 |
my $author = $img->tags(name=>'tiff_author'); |
| 2221 |
if (defined $author) { |
| 2222 |
print "Author: $author\n"; |
| 2223 |
} |
| 2224 |
|
| 2225 |
=head1 BUGS |
| 2226 |
|
| 2227 |
When saving GIF images the program does NOT try to shave off extra |
| 2228 |
colors if it is possible. If you specify 128 colors and there are |
| 2229 |
only 2 colors used - it will have a 128 color table anyway. |
| 2230 |
|
| 2231 |
=head1 SEE ALSO |
| 2232 |
|
| 2233 |
Imager(3) |
| 2234 |
|
| 2235 |
=head1 AUTHOR |
| 2236 |
|
| 2237 |
Tony Cook <[email protected]>, Arnar M. Hrafnkelsson |
| 2238 |
|
| 2239 |
=cut |