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