]> git.imager.perl.org - imager.git/blob - lib/Imager/Files.pod
1afce5357066e309154c8fab36bb050f041b8969
[imager.git] / lib / Imager / Files.pod
1 =head1 NAME
2
3 Imager::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
23 You can read and write a variety of images formats, assuming you have
24 the appropriate libraries, and images can be read or written to/from
25 files, file handles, file descriptors, scalars, or through callbacks.
26
27 To see which image formats Imager is compiled to support the following
28 code snippet is sufficient:
29
30   use Imager;
31   print join " ", keys %Imager::formats;
32
33 This will include some other information identifying libraries rather
34 than file formats.
35
36 Reading writing to and from files is simple, use the C<read()>
37 method 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
43 and 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
48 If you're reading from a format that supports multiple images per
49 file, 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
54 and if you want to write multiple images to a single file use the
55 C<write_multi()> method:
56
57   Imager->write_multi({ file=> $filename, type=>$type }, @images)
58     or die "Cannot write $filename: ", Imager->errstr;
59
60 If the I<filename> includes an extension that Imager recognizes, then
61 you don't need the I<type>, but you may want to provide one anyway.
62 See L</Guessing types> for information on controlling this
63 recognition.
64
65 When you read an image, Imager may set some tags, possibly including
66 information about the spatial resolution, textual information, and
67 animation information.  See L</Tags> for specifics.
68
69 =head2 Input and output
70
71 When reading or writing you can specify one of a variety of sources or
72 targets:
73
74 =over
75
76 =item file
77
78 The C<file> parameter is the name of the image file to be written to
79 or read from.  If Imager recognizes the extension of the file you do
80 not need to supply a C<type>.
81
82 =item fh
83
84 C<fh> is a file handle, typically either returned from
85 C<<IO::File->new()>>, or a glob from an C<open> call.  You should call
86 C<binmode> on the handle before passing it to Imager.
87
88 =item fd
89
90 C<fd> is a file descriptor.  You can get this by calling the
91 C<fileno()> function on a file handle, or by using one of the standard
92 file descriptor numbers.
93
94 =item data
95
96 When reading data, C<data> is a scalar containing the image file data,
97 when writing, C<data> is a reference to the scalar to save the image
98 file data too.  For GIF images you will need giflib 4 or higher, and
99 you may need to patch giflib to use this option for writing.
100
101 =item callback
102
103 Imager will make calls back to your supplied coderefs to read, write
104 and seek from/to/through the image file.
105
106 When reading from a file you can use either C<callback> or C<readcb>
107 to supply the read callback, and when writing C<callback> or
108 C<writecb> to supply the write callback.
109
110 When writing you can also supply the C<maxbuffer> option to set the
111 maximum amount of data that will be buffered before your write
112 callback is called.  Note: the amount of data supplied to your
113 callback can be smaller or larger than this size.
114
115 The read callback is called with 2 parameters, the minimum amount of
116 data required, and the maximum amount that Imager will store in it's C
117 level buffer.  You may want to return the minimum if you have a slow
118 data source, or the maximum if you have a fast source and want to
119 prevent many calls to your perl callback.  The read data should be
120 returned as a scalar.
121
122 Your write callback takes exactly one parameter, a scalar containing
123 the data to be written.  Return true for success.
124
125 The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
126 defined in the same way as perl's seek function.
127
128 You can also supply a C<closecb> which is called with no parameters
129 when there is no more data to be written.  This could be used to flush
130 buffered data.
131
132 =back
133
134 =head2 Guessing types
135
136 Imager uses the code reference in $Imager::FORMATGUESS to guess the
137 file type when you don't supply a C<type>.  The code reference is
138 called with a single parameter, the filename of the file.  The code
139 reference is only called if a C<file> parameter is supplied to the
140 file access method.
141
142 Return either a valid Imager file type, or undef.
143
144   # I'm writing jpegs to weird filenames
145   local $Imager::FORMATGUESS = sub { 'jpeg' };
146
147 =head1 TYPE SPECIFIC INFORMATION
148
149 The different image formats can write different image type, and some have
150 different options to control how the images are written.
151
152 =head2 PNM (Portable aNy Map)
153
154 Imager can write PGM (Portable Gray Map) and PPM (Portable PixMaps)
155 files, depending on the number of channels in the image.  Currently
156 the images are written in binary formats.  Only 1 and 3 channel images
157 can be written, including 1 and 3 channel paletted images.
158
159   $img->write(file=>'foo.ppm') or die $img->errstr;
160
161 Imager can read both the ASCII and binary versions of each of the PBM
162 (Portable BitMap), PGM and PPM formats.
163
164   $img->read(file=>'foo.ppm') or die $img->errstr;
165
166 PNM does not support the spatial resolution tags.
167
168 =head2 JPEG
169
170 You can supply a C<jpegquality> parameter (0-100) when writing a JPEG
171 file, which defaults to 75%.  Only 1 and 3 channel images
172 can be written, including 1 and 3 channel paletted images.
173
174   $img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
175
176 Imager will read a grayscale JPEG as a 1 channel image and a color
177 JPEG as a 3 channel image.
178
179   $img->read(file=>'foo.jpg') or die $img->errstr;
180
181 PNM does not support the spatial resolution tags.
182
183 =head2 GIF (Graphics Interchange Format)
184
185 You can supply many different options when writing to a GIF file, and
186 you can write a multi-image GIF file, eg. for animation, with the
187 C<write_multi()> method.
188
189 These options can be specified when calling write_multi() or when
190 writing a single image with the C<gifquant> option set to 'gen'
191
192 Note that some viewers will ignore some of these options
193 (C<gif_user_input> in particular).
194
195 =over 4
196
197 =item gif_each_palette
198
199 Each image in the gif file has it's own palette if this is non-zero.
200 All but the first image has a local colour table (the first uses the
201 global colour table.
202
203 =item interlace
204
205 The images are written interlaced if this is non-zero.
206
207 =item gif_delays
208
209 A reference to an array containing the delays between images, in 1/100
210 seconds.
211
212 If you want the same delay for every frame you can simply set this to
213 the delay in 1/100 seconds.
214
215 =item gif_user_input
216
217 A reference to an array contains user input flags.  If the given flag
218 is non-zero the image viewer should wait for input before displaying
219 the next image.
220
221 =item gif_disposal
222
223 A reference to an array of image disposal methods.  These define what
224 should be done to the image before displaying the next one.  These are
225 integers, where 0 means unspecified, 1 means the image should be left
226 in place, 2 means restore to background colour and 3 means restore to
227 the previous value.
228
229 =item gif_tran_color
230
231 A reference to an Imager::Color object, which is the colour to use for
232 the palette entry used to represent transparency in the palette.  You
233 need to set the transp option (see L<Quantization options>) for this
234 value to be used.
235
236 =item gif_positions
237
238 A reference to an array of references to arrays which represent screen
239 positions for each image.
240
241 =item gif_loop_count
242
243 If this is non-zero the Netscape loop extension block is generated,
244 which makes the animation of the images repeat.
245
246 This is currently unimplemented due to some limitations in giflib.
247
248 =item gif_eliminate_unused
249
250 If this is true, when you write a paletted image any unused colors
251 will be eliminated from its palette.  This is set by default.
252
253 =back
254
255 When reading a GIF all of the sub-images are combined using the screen
256 size and image positions into one big image, producing an RGB image.
257 This may change in the future to produce a paletted image where possible.
258
259 When you read a single GIF with C<<$img->read()>> you can supply a
260 reference to a scalar in the C<colors> parameter, if the image is read
261 the scalar will be filled with a reference to an anonymous array of
262 L<Imager::Color> objects, representing the palette of the image.  This
263 will be the first palette found in the image.  If you want the
264 palettes for each of the images in the file, use C<read_multi()> and
265 use the C<getcolors()> method on each image.
266
267 GIF does not support the spatial resolution tags.
268
269 GIF will set the following tags in each image when reading, but does
270 not use them when saving to GIF:
271
272 =over
273
274 =item gif_left
275
276 the offset of the image from the left of the "screen" ("Image Left
277 Position")
278
279 =item gif_top
280
281 the offset of the image from the top of the "screen" ("Image Top Position")
282
283 =item gif_interlace
284
285 non-zero if the image was interlaced ("Interlace Flag")
286
287 =item gif_screen_width
288
289 =item gif_screen_height
290
291 the size of the logical screen ("Logical Screen Width", 
292 "Logical Screen Height")
293
294 =item gif_local_map
295
296 Non-zero if this image had a local color map.
297
298 =item gif_background
299
300 The index in the global colormap of the logical screen's background
301 color.  This is only set if the current image uses the global
302 colormap.
303
304 =item gif_trans_index
305
306 The index of the color in the colormap used for transparency.  If the
307 image has a transparency then it is returned as a 4 channel image with
308 the alpha set to zero in this palette entry. ("Transparent Color Index")
309
310 =item gif_delay
311
312 The delay until the next frame is displayed, in 1/100 of a second. 
313 ("Delay Time").
314
315 =item gif_user_input
316
317 whether or not a user input is expected before continuing (view dependent) 
318 ("User Input Flag").
319
320 =item gif_disposal
321
322 how the next frame is displayed ("Disposal Method")
323
324 =item gif_loop
325
326 the number of loops from the Netscape Loop extension.  This may be zero.
327
328 =item gif_comment
329
330 the first block of the first gif comment before each image.
331
332 =back
333
334 Where applicable, the ("name") is the name of that field from the GIF89 
335 standard.
336
337 =head2 TIFF (Tagged Image File Format)
338
339 Imager can write images to either paletted or RGB TIFF images,
340 depending on the type of the source image.  Currently if you write a
341 16-bit/sample or double/sample image it will be written as an
342 8-bit/sample image.  Only 1 or 3 channel images can be written.
343
344 If you are creating images for faxing you can set the I<class>
345 parameter set to C<fax>.  By default the image is written in fine
346 mode, but this can be overridden by setting the I<fax_fine> parameter
347 to zero.  Since a fax image is bi-level, Imager uses a threshold to
348 decide if a given pixel is black or white, based on a single channel.
349 For greyscale images channel 0 is used, for color images channel 1
350 (green) is used.  If you want more control over the conversion you can
351 use $img->to_paletted() to product a bi-level image.  This way you can
352 use dithering:
353
354   my $bilevel = $img->to_paletted(colors=>[ NC(0,0,0), NC(255,255,255) ],
355                                   make_colors => 'none',
356                                   translate => 'errdiff',
357                                   errdiff => 'stucki');
358
359 =over
360
361 =item class
362
363 If set to 'fax' the image will be written as a bi-level fax image.
364
365 =item fax_fine
366
367 By default when I<class> is set to 'fax' the image is written in fine
368 mode, you can select normal mode by setting I<fax_fine> to 0.
369
370 =back
371
372 Imager should be able to read any TIFF image you supply.  Paletted
373 TIFF images are read as paletted Imager images, since paletted TIFF
374 images have 16-bits/sample (48-bits/color) this means the bottom
375 8-bits are lost, but this shouldn't be a big deal.  Currently all
376 direct color images are read at 8-bits/sample.
377
378 TIFF supports the spatial resolution tags.  See the
379 C<tiff_resolutionunit> tag for some extra options.
380
381 The following tags are set in a TIFF image when read, and can be set
382 to control output:
383
384 =over
385
386 =item tiff_resolutionunit
387
388 The value of the ResolutionUnit tag.  This is ignored on writing if
389 the i_aspect_only tag is non-zero.
390
391 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
392 matter tha value of this tag, they will be converted to/from the value
393 stored in the TIFF file.
394
395 =item tiff_documentname
396
397 =item tiff_imagedescription
398
399 =item tiff_make
400
401 =item tiff_model
402
403 =item tiff_pagename
404
405 =item tiff_software
406
407 =item tiff_datetime
408
409 =item tiff_artist
410
411 =item tiff_hostcomputer
412
413 Various strings describing the image.  tiff_datetime must be formatted
414 as "YYYY:MM:DD HH:MM:SS".  These correspond directly to the mixed case
415 names in the TIFF specification.  These are set in images read from a
416 TIFF and saved when writing a TIFF image.
417
418 =back
419
420 =head2 BMP (BitMaP)
421
422 Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
423 Windows BMP files.  Currently you cannot write compressed BMP files
424 with Imager.
425
426 Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
427 Windows BMP files.  There is some support for reading 16-bit per pixel
428 images, but I haven't found any for testing.
429
430 BMP has no support for multi-image files.
431
432 BMP files support the spatial resolution tags, but since BMP has no
433 support for storing only an aspect ratio, if C<i_aspect_only> is set
434 when you write the C<i_xres> and C<i_yres> values are scaled so the
435 smaller it 72 DPI.
436
437 The following tags are set when you read an image from a BMP file:
438
439 =over
440
441 =item bmp_compression
442
443 The type of compression, if any.  This can be any of the following
444 values:
445
446 =over
447
448 =item BI_RGB (0)
449
450 Uncompressed.
451
452 =item BI_RLE8 (1)
453
454 8-bits/pixel paletted value RLE compression.
455
456 =item BI_RLE4 (2)
457
458 4-bits/pixel paletted value RLE compression.
459
460 =item BI_BITFIELDS (3)
461
462 Packed RGB values.
463
464 =back
465
466 =item bmp_important_colors
467
468 The number of important colors as defined by the writer of the image.
469
470 =back
471
472 =head2 TGA (TarGA)
473
474 Tags:
475
476 =over
477
478 =item tga_idstring
479
480 =item tga_bitspp
481
482 =item compressed
483
484 =back
485
486 =head1 EXAMPLES
487
488 writing an image from CGI (content type, flush, write to fd)
489
490 writing an animated gif
491
492 reading tags after reading an image
493
494 =cut