- added many bad BMP files to test various code paths in bmp.c, and
[imager.git] / lib / Imager / Files.pod
CommitLineData
c2188f93
TC
1=head1 NAME
2
3Imager::Files - working with image files
4
5=head1 SYNOPSIS
6
7 my $img = ...;
8 $img->write(file=>$filename, type=>$type)
9 or die "Cannot write: ",$img->errstr;
10
11 $img = Imager->new;
12 $img->read(file=>$filename, type=>$type)
13 or die "Cannot read: ", $img->errstr;
14
15 Imager->write_multi({ file=> $filename, ... }, @images)
16 or die "Cannot write: ", Imager->errstr;
17
18 my @imgs = Imager->read_multi(file=>$filename)
19 or die "Cannot read: ", Imager->errstr;
20
21=head1 DESCRIPTION
22
23You can read and write a variety of images formats, assuming you have
24the appropriate libraries, and images can be read or written to/from
25files, file handles, file descriptors, scalars, or through callbacks.
26
27To see which image formats Imager is compiled to support the following
28code snippet is sufficient:
29
30 use Imager;
31 print join " ", keys %Imager::formats;
32
33This will include some other information identifying libraries rather
34than file formats.
35
36Reading writing to and from files is simple, use the C<read()>
37method to read an image:
38
39 my $img = Imager->new;
40 $img->read(file=>$filename, type=>$type)
41 or die "Cannot read $filename: ", $img->errstr;
42
43and the C<write()> method to write an image:
44
45 $img->write(file=>$filename, type=>$type)
46 or die "Cannot write $filename: ", $img->errstr;
47
48If you're reading from a format that supports multiple images per
49file, use the C<read_multi()> method:
50
51 my @imgs = Imager->read_multi(file=>$filename, type=>$type)
52 or die "Cannot read $filename: ", Imager->errstr;
53
54and if you want to write multiple images to a single file use the
55C<write_multi()> method:
56
57 Imager->write_multi({ file=> $filename, type=>$type }, @images)
58 or die "Cannot write $filename: ", Imager->errstr;
59
60If the I<filename> includes an extension that Imager recognizes, then
61you don't need the I<type>, but you may want to provide one anyway.
62See L</Guessing types> for information on controlling this
63recognition.
64
f6af7cb4
TC
65The C<type> parameter is a lowercase representation of the file type,
66and can be any of the following:
67
68 bmp Windows BitMaP (BMP)
69 gif Graphics Interchange Format (GIF)
70 jpeg JPEG/JFIF
71 png Portable Network Graphics (PNG)
72 pnm Portable aNyMap (PNM)
73 raw Raw
74 rgb SGI .rgb files
75 tga TARGA
76 tiff Tagged Image File Format (TIFF)
77
c2188f93
TC
78When you read an image, Imager may set some tags, possibly including
79information about the spatial resolution, textual information, and
9d1c4956 80animation information. See L<Imager::ImageTypes/Tags> for specifics.
c2188f93
TC
81
82=head2 Input and output
83
84When reading or writing you can specify one of a variety of sources or
85targets:
86
87=over
88
89=item file
90
91The C<file> parameter is the name of the image file to be written to
92or read from. If Imager recognizes the extension of the file you do
93not need to supply a C<type>.
94
95=item fh
96
97C<fh> is a file handle, typically either returned from
98C<<IO::File->new()>>, or a glob from an C<open> call. You should call
99C<binmode> on the handle before passing it to Imager.
100
9d1c4956
TC
101Imager will set the handle to autoflush to make sure any buffered data
102is flushed , since Imager will write to the file descriptor (from
103fileno()) rather than writing at the perl level.
104
c2188f93
TC
105=item fd
106
107C<fd> is a file descriptor. You can get this by calling the
108C<fileno()> function on a file handle, or by using one of the standard
109file descriptor numbers.
110
9d1c4956
TC
111If you get this from a perl file handle, you may need to flush any
112buffered output, otherwise it may appear in the output stream after
113the image.
114
c2188f93
TC
115=item data
116
117When reading data, C<data> is a scalar containing the image file data,
118when writing, C<data> is a reference to the scalar to save the image
119file data too. For GIF images you will need giflib 4 or higher, and
120you may need to patch giflib to use this option for writing.
121
122=item callback
123
124Imager will make calls back to your supplied coderefs to read, write
125and seek from/to/through the image file.
126
127When reading from a file you can use either C<callback> or C<readcb>
128to supply the read callback, and when writing C<callback> or
129C<writecb> to supply the write callback.
130
131When writing you can also supply the C<maxbuffer> option to set the
132maximum amount of data that will be buffered before your write
133callback is called. Note: the amount of data supplied to your
134callback can be smaller or larger than this size.
135
136The read callback is called with 2 parameters, the minimum amount of
137data required, and the maximum amount that Imager will store in it's C
138level buffer. You may want to return the minimum if you have a slow
139data source, or the maximum if you have a fast source and want to
140prevent many calls to your perl callback. The read data should be
141returned as a scalar.
142
143Your write callback takes exactly one parameter, a scalar containing
144the data to be written. Return true for success.
145
146The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
147defined in the same way as perl's seek function.
148
149You can also supply a C<closecb> which is called with no parameters
150when there is no more data to be written. This could be used to flush
151buffered data.
152
153=back
154
155=head2 Guessing types
156
157Imager uses the code reference in $Imager::FORMATGUESS to guess the
158file type when you don't supply a C<type>. The code reference is
159called with a single parameter, the filename of the file. The code
160reference is only called if a C<file> parameter is supplied to the
161file access method.
162
163Return either a valid Imager file type, or undef.
164
165 # I'm writing jpegs to weird filenames
166 local $Imager::FORMATGUESS = sub { 'jpeg' };
167
168=head1 TYPE SPECIFIC INFORMATION
169
170The different image formats can write different image type, and some have
171different options to control how the images are written.
172
97c4effc
TC
173When you call C<write()> or C<write_multi()> with an option that has
174the same name as a tag for the image format you're writing, then the
175value supplied to that option will be used to set the corresponding
176tag in the image. Depending on the image format, these values will be
177used when writing the image.
178
179This replaces the previous options that were used when writing GIF
180images. Currently if you use an obsolete option, it will be converted
181to the equivalent tag and Imager will produced a warning. You can
182suppress these warnings by calling the C<Imager::init()> function with
183the C<warn_obsolete> option set to false:
184
185 Imager::init(warn_obsolete=>0);
186
187At some point in the future these obsolete options will no longer be
188supported.
189
c2188f93
TC
190=head2 PNM (Portable aNy Map)
191
192Imager can write PGM (Portable Gray Map) and PPM (Portable PixMaps)
193files, depending on the number of channels in the image. Currently
194the images are written in binary formats. Only 1 and 3 channel images
195can be written, including 1 and 3 channel paletted images.
196
197 $img->write(file=>'foo.ppm') or die $img->errstr;
198
199Imager can read both the ASCII and binary versions of each of the PBM
200(Portable BitMap), PGM and PPM formats.
201
202 $img->read(file=>'foo.ppm') or die $img->errstr;
203
204PNM does not support the spatial resolution tags.
205
206=head2 JPEG
207
208You can supply a C<jpegquality> parameter (0-100) when writing a JPEG
209file, which defaults to 75%. Only 1 and 3 channel images
210can be written, including 1 and 3 channel paletted images.
211
212 $img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
213
214Imager will read a grayscale JPEG as a 1 channel image and a color
215JPEG as a 3 channel image.
216
217 $img->read(file=>'foo.jpg') or die $img->errstr;
218
219PNM does not support the spatial resolution tags.
220
221=head2 GIF (Graphics Interchange Format)
222
97c4effc
TC
223When writing one of more GIF images you can use the same
224L<Quantization Options|Imager::ImageTypes> as you can when converting
225an RGB image into a paletted image.
61c59c54 226
00424555
TC
227When reading a GIF all of the sub-images are combined using the screen
228size and image positions into one big image, producing an RGB image.
229This may change in the future to produce a paletted image where possible.
230
8889dffd 231When you read a single GIF with C<$img-E<gt>read()> you can supply a
00424555
TC
232reference to a scalar in the C<colors> parameter, if the image is read
233the scalar will be filled with a reference to an anonymous array of
234L<Imager::Color> objects, representing the palette of the image. This
235will be the first palette found in the image. If you want the
236palettes for each of the images in the file, use C<read_multi()> and
237use the C<getcolors()> method on each image.
238
239GIF does not support the spatial resolution tags.
240
97c4effc
TC
241Imager will set the following tags in each image when reading, and can
242use most of them when writing to GIF:
00424555
TC
243
244=over
245
246=item gif_left
247
248the offset of the image from the left of the "screen" ("Image Left
249Position")
250
251=item gif_top
252
253the offset of the image from the top of the "screen" ("Image Top Position")
254
255=item gif_interlace
256
257non-zero if the image was interlaced ("Interlace Flag")
258
259=item gif_screen_width
260
261=item gif_screen_height
262
97c4effc
TC
263the size of the logical screen. When writing this is used as the
264minimum. If any image being written would extend beyond this the
265screen size is extended. ("Logical Screen Width", "Logical Screen
266Height").
267
268When writing this is used as a minimum, if the combination of the
269image size and the image's C<gif_left> and C<gif_top> is beyond this
270size then the screen size will be expanded.
00424555
TC
271
272=item gif_local_map
273
97c4effc
TC
274Non-zero if this image had a local color map. If set for an image
275when writing the image is quantized separately from the other images
276in the file.
00424555
TC
277
278=item gif_background
279
280The index in the global colormap of the logical screen's background
281color. This is only set if the current image uses the global
97c4effc
TC
282colormap. You can set this on write too, but for it to choose the
283color you want, you will need to supply only paletted images and set
284the C<gif_eliminate_unused> tag to 0.
00424555
TC
285
286=item gif_trans_index
287
288The index of the color in the colormap used for transparency. If the
289image has a transparency then it is returned as a 4 channel image with
97c4effc
TC
290the alpha set to zero in this palette entry. This value is not used
291when writing. ("Transparent Color Index")
292
293=item gif_trans_color
294
295A reference to an Imager::Color object, which is the colour to use for
296the palette entry used to represent transparency in the palette. You
297need to set the transp option (see L<Quantization options>) for this
298value to be used.
00424555
TC
299
300=item gif_delay
301
302The delay until the next frame is displayed, in 1/100 of a second.
303("Delay Time").
304
305=item gif_user_input
306
307whether or not a user input is expected before continuing (view dependent)
308("User Input Flag").
309
310=item gif_disposal
311
312how the next frame is displayed ("Disposal Method")
313
314=item gif_loop
315
316the number of loops from the Netscape Loop extension. This may be zero.
317
318=item gif_comment
319
320the first block of the first gif comment before each image.
321
97c4effc
TC
322=item gif_eliminate_unused
323
324If this is true, when you write a paletted image any unused colors
325will be eliminated from its palette. This is set by default.
326
00424555
TC
327=back
328
329Where applicable, the ("name") is the name of that field from the GIF89
330standard.
c2188f93 331
97c4effc
TC
332The following gif writing options are obsolete, you should set the
333corresponding tag in the image, either by using the tags functions, or
334by supplying the tag and value as options.
335
336=over
337
338=item gif_each_palette
339
340Each image in the gif file has it's own palette if this is non-zero.
341All but the first image has a local colour table (the first uses the
342global colour table.
343
344Use C<gif_local_map> in new code.
345
346=item interlace
347
348The images are written interlaced if this is non-zero.
349
350Use C<gif_interlace> in new code.
351
352=item gif_delays
353
354A reference to an array containing the delays between images, in 1/100
355seconds.
356
357Use C<gif_delay> in new code.
358
359=item gif_positions
360
361A reference to an array of references to arrays which represent screen
362positions for each image.
363
364New code should use the C<gif_left> and C<gif_top> tags.
365
366=item gif_loop_count
367
368If this is non-zero the Netscape loop extension block is generated,
369which makes the animation of the images repeat.
370
371This is currently unimplemented due to some limitations in giflib.
372
373=back
374
c2188f93
TC
375=head2 TIFF (Tagged Image File Format)
376
b5dd0159
TC
377Imager can write images to either paletted or RGB TIFF images,
378depending on the type of the source image. Currently if you write a
37916-bit/sample or double/sample image it will be written as an
3808-bit/sample image. Only 1 or 3 channel images can be written.
381
382If you are creating images for faxing you can set the I<class>
383parameter set to C<fax>. By default the image is written in fine
384mode, but this can be overridden by setting the I<fax_fine> parameter
385to zero. Since a fax image is bi-level, Imager uses a threshold to
386decide if a given pixel is black or white, based on a single channel.
387For greyscale images channel 0 is used, for color images channel 1
388(green) is used. If you want more control over the conversion you can
389use $img->to_paletted() to product a bi-level image. This way you can
390use dithering:
391
392 my $bilevel = $img->to_paletted(colors=>[ NC(0,0,0), NC(255,255,255) ],
393 make_colors => 'none',
394 translate => 'errdiff',
395 errdiff => 'stucki');
00424555 396
b5dd0159
TC
397=over
398
399=item class
400
401If set to 'fax' the image will be written as a bi-level fax image.
402
403=item fax_fine
404
405By default when I<class> is set to 'fax' the image is written in fine
406mode, you can select normal mode by setting I<fax_fine> to 0.
407
408=back
409
410Imager should be able to read any TIFF image you supply. Paletted
411TIFF images are read as paletted Imager images, since paletted TIFF
412images have 16-bits/sample (48-bits/color) this means the bottom
4138-bits are lost, but this shouldn't be a big deal. Currently all
414direct color images are read at 8-bits/sample.
415
416TIFF supports the spatial resolution tags. See the
417C<tiff_resolutionunit> tag for some extra options.
00424555 418
5df0fac7
AMH
419The following tags are set in a TIFF image when read, and can be set
420to control output:
421
422=over
423
424=item tiff_resolutionunit
425
426The value of the ResolutionUnit tag. This is ignored on writing if
427the i_aspect_only tag is non-zero.
428
b5dd0159
TC
429The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
430matter tha value of this tag, they will be converted to/from the value
431stored in the TIFF file.
432
5df0fac7
AMH
433=item tiff_documentname
434
435=item tiff_imagedescription
436
437=item tiff_make
438
439=item tiff_model
440
441=item tiff_pagename
442
443=item tiff_software
444
445=item tiff_datetime
446
447=item tiff_artist
448
449=item tiff_hostcomputer
450
451Various strings describing the image. tiff_datetime must be formatted
452as "YYYY:MM:DD HH:MM:SS". These correspond directly to the mixed case
453names in the TIFF specification. These are set in images read from a
b5dd0159 454TIFF and saved when writing a TIFF image.
5df0fac7
AMH
455
456=back
457
b5dd0159 458=head2 BMP (BitMaP)
5df0fac7 459
b5dd0159
TC
460Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
461Windows BMP files. Currently you cannot write compressed BMP files
462with Imager.
5df0fac7 463
b5dd0159
TC
464Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
465Windows BMP files. There is some support for reading 16-bit per pixel
466images, but I haven't found any for testing.
5df0fac7 467
b5dd0159 468BMP has no support for multi-image files.
c2188f93 469
b5dd0159
TC
470BMP files support the spatial resolution tags, but since BMP has no
471support for storing only an aspect ratio, if C<i_aspect_only> is set
472when you write the C<i_xres> and C<i_yres> values are scaled so the
473smaller it 72 DPI.
5df0fac7 474
b5dd0159 475The following tags are set when you read an image from a BMP file:
5df0fac7
AMH
476
477=over
478
479=item bmp_compression
480
b5dd0159
TC
481The type of compression, if any. This can be any of the following
482values:
483
484=over
485
486=item BI_RGB (0)
487
488Uncompressed.
489
490=item BI_RLE8 (1)
491
4928-bits/pixel paletted value RLE compression.
493
494=item BI_RLE4 (2)
495
4964-bits/pixel paletted value RLE compression.
497
498=item BI_BITFIELDS (3)
499
500Packed RGB values.
501
502=back
5df0fac7 503
662e3c02
TC
504=item bmp_compression_name
505
506The bmp_compression value as a BI_* string
507
5df0fac7
AMH
508=item bmp_important_colors
509
510The number of important colors as defined by the writer of the image.
511
662e3c02
TC
512=item bmp_used_colors
513
514Number of color used from the BMP header
515
516=item bmp_filesize
517
518The file size from the BMP header
519
520=item bmp_bit_count
521
522Number of bits stored per pixel. (24, 8, 4 or 1)
523
5df0fac7
AMH
524=back
525
b5dd0159
TC
526=head2 TGA (TarGA)
527
f5fd108b
AMH
528When storing targa images rle compression can be activated with the
529'compress' parameter, the 'idstring' parameter can be used to set the
530targa comment field and the 'wierdpack' option can be used to use the
53115 and 16 bit targa formats for rgb and rgba data. The 15 bit format
532has 5 of each red, green and blue. The 16 bit format in addition
533allows 1 bit of alpha. The most significant bits are used for each
534channel.
535
536
b5dd0159 537Tags:
5df0fac7 538
b5dd0159 539=over
5df0fac7 540
b5dd0159 541=item tga_idstring
5df0fac7 542
b5dd0159 543=item tga_bitspp
5df0fac7 544
b5dd0159 545=item compressed
5df0fac7 546
b5dd0159
TC
547=back
548
f5fd108b
AMH
549
550=head2 RAW
551
552
553When reading raw images you need to supply the width and height of the
554image in the xsize and ysize options:
555
556 $img->read(file=>'foo.raw', xsize=>100, ysize=>100)
557 or die "Cannot read raw image\n";
558
559If your input file has more channels than you want, or (as is common),
560junk in the fourth channel, you can use the datachannels and
561storechannels options to control the number of channels in your input
562file and the resulting channels in your image. For example, if your
563input image uses 32-bits per pixel with red, green, blue and junk
564values for each pixel you could do:
565
566 $img->read(file=>'foo.raw', xsize=>100, ysize=>100, datachannels=>4,
567 storechannels=>3)
568 or die "Cannot read raw image\n";
569
570Normally the raw image is expected to have the value for channel 1
571immediately following channel 0 and channel 2 immediately following
572channel 1 for each pixel. If your input image has all the channel 0
573values for the first line of the image, followed by all the channel 1
574values for the first line and so on, you can use the interleave option:
575
576 $img->read(file=>'foo.raw', xsize=100, ysize=>100, interleave=>1)
577 or die "Cannot read raw image\n";
578
9d1c4956 579=head1 EXAMPLES
f5fd108b 580
9d1c4956 581=head2 Producing an image from a CGI script
f5fd108b 582
9d1c4956
TC
583Once you have an image the basic mechanism is:
584
585=over
586
587=item 1.
588
589set STDOUT to autoflush
590
591=item 2.
592
593output a content-type header, and optionally a content-length header
594
595=item 3.
596
597put STDOUT into binmode
598
599=item 4.
600
601call write() with the C<fd> or C<fh> parameter. You will need to
602provide the C<type> parameter since
603
604=back
605
606 # write an image from a CGI script
607 # using CGI.pm
608 use CGI qw(:standard);
609 $| = 1;
610 binmode STDOUT;
611 print header(-type=>'image/gif');
612 $img->write(type=>'gif', fd=>fileno(STDOUT))
613 or die $img->errstr;
b5dd0159 614
9d1c4956
TC
615If you want to send a content length you can send the output to a
616scalar to get the length:
b5dd0159 617
9d1c4956
TC
618 my $data;
619 $img->write(type=>'gif', data=>\$data)
620 or die $img->errstr;
621 binmode STDOUT;
622 print header(-type=>'image/gif', -content_length=>length($data));
623 print $data;
b5dd0159 624
9d1c4956 625=head2 Writing an animated GIF
c2188f93 626
9d1c4956
TC
627The basic idea is simple, just use write_multi():
628
629 my @imgs = ...;
630 Imager->write_multi({ file=>$filename, type=>'gif' }, @imgs);
631
632If your images are RGB images the default quantization mechanism will
633produce a very good result, but can take a long time to execute. You
634could either use the standard webmap:
635
636 Imager->write_multi({ file=>$filename,
637 type=>'gif',
638 make_colors=>'webmap' },
639 @imgs);
640
641or use a median cut algorithm to built a fairly optimal color map:
642
643 Imager->write_multi({ file=>$filename,
644 type=>'gif',
645 make_colors=>'mediancut' },
646 @imgs);
647
648By default all of the images will use the same global colormap, which
649will produce a smaller image. If your images have significant color
650differences, you may want to generate a new palette for each image:
651
652 Imager->write_multi({ file=>$filename,
653 type=>'gif',
654 make_colors=>'mediancut',
655 gif_local_map => 1 },
656 @imgs);
657
658which will set the C<gif_local_map> tag in each image to 1.
659Alternatively, if you know only some images have different colors, you
660can set the tag just for those images:
661
662 $imgs[2]->settag(name=>'gif_local_map', value=>1);
663 $imgs[4]->settag(name=>'gif_local_map', value=>1);
664
665and call write_multi() without a C<gif_local_map> parameter, or supply
666an arrayref of values for the tag:
667
668 Imager->write_multi({ file=>$filename,
669 type=>'gif',
670 make_colors=>'mediancut',
671 gif_local_map => [ 0, 0, 1, 0, 1 ] },
672 @imgs);
673
674Other useful parameters include C<gif_delay> to control the delay
675between frames and C<transp> to control transparency.
676
677=head2 Reading tags after reading an image
678
679This is pretty simple:
680
681 # print the author of a TIFF, if any
682 my $img = Imager->new;
683 $img->read(file=>$filename, type='tiff') or die $img->errstr;
684 my $author = $img->tags(name=>'tiff_author');
685 if (defined $author) {
686 print "Author: $author\n";
687 }
bac4fcee
AMH
688
689=head1 BUGS
690
691When saving Gif images the program does NOT try to shave of extra
692colors if it is possible. If you specify 128 colors and there are
693only 2 colors used - it will have a 128 colortable anyway.
694
97c4effc
TC
695=head1 SEE ALSO
696
697Imager(3)
bac4fcee 698
c2188f93 699=cut