Image-Info-1.15.tar.gz
[Image-Info.git] / lib / Image / Info.pm
blobba0bb7bbbbca9f2b2a1955c347647f6bcef4fd89
1 # This file is autogenerated from Info.pm.tmpl.
2 # Please do not edit!!
4 package Image::Info;
6 # Copyright 1999-2003, Gisle Aas.
8 # This library is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
11 use strict;
12 use Symbol ();
14 use vars qw($VERSION @EXPORT_OK);
16 $VERSION = '1.15';
18 require Exporter;
19 *import = \&Exporter::import;
21 @EXPORT_OK = qw(image_info dim html_dim);
23 my %mod_failure;
25 sub image_info
27 my($source, %cnf) = @_;
29 if (!ref $source) {
30 require Symbol;
31 my $fh = Symbol::gensym();
32 open($fh, $source) || return _os_err("Can't open $source");
33 ${*$fh} = $source; # keep filename in case somebody wants to know
34 binmode($fh);
35 $source = $fh;
37 elsif (ref($source) eq "SCALAR") {
38 if ($] >= 5.008) {
39 open(my $s, "<", $source) or return _os_err("Can't open string");
40 $source = $s;
42 else {
43 require IO::String;
44 $source = IO::String->new($$source);
47 else {
48 seek($source, 0, 0) or return _os_err("Can't rewind");
51 my $head;
52 read($source, $head, 32) or return _os_err("Can't read head");
53 if (ref($source) eq "IO::String") {
54 # XXX workaround until we can trap seek() with a tied file handle
55 $source->setpos(0);
57 else {
58 seek($source, 0, 0) or _os_err("Can't rewind");
61 if (my $format = determine_file_format($head)) {
62 no strict 'refs';
63 my $mod = "Image::Info::$format";
64 my $sub = "$mod\::process_file";
65 my $info = bless [], "Image::Info::Result";
66 eval {
67 unless (defined &$sub) {
68 if (my $fail = $mod_failure{$mod}) {
69 die $fail;
71 eval "require $mod";
72 if ($@) {
73 $mod_failure{$mod} = $@;
74 die $@;
76 die "$mod did not define &$sub" unless defined &$sub;
79 &$sub($info, $source, \%cnf);
80 $info->clean_up;
82 return { error => $@ } if $@;
83 return wantarray ? @$info : $info->[0];
85 return { error => "Unrecognized file format" };
88 sub _os_err
90 return { error => "$_[0]: $!",
91 Errno => $!+0,
95 sub determine_file_format
97 local($_) = @_;
98 return "BMP" if /^BM/;
99 return "GIF" if /^GIF8[79]a/;
100 return "JPEG" if /^\xFF\xD8/;
101 return "PNG" if /^\x89PNG\x0d\x0a\x1a\x0a/;
102 return "PPM" if /^P[1-6]/;;
103 return "SVG" if /^<\?xml/;
104 return "TIFF" if /^MM\x00\x2a/;
105 return "TIFF" if /^II\x2a\x00/;
106 return "XBM" if /^#define\s+/;
107 return "XPM" if /(^\/\* XPM \*\/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)/;
108 return undef;
111 sub dim
113 my $img = shift || return;
114 my $x = $img->{width} || return;
115 my $y = $img->{height} || return;
116 wantarray ? ($x, $y) : "${x}x$y";
119 sub html_dim
121 my($x, $y) = dim(@_);
122 return "" unless $x;
123 "WIDTH=$x HEIGHT=$y";
126 package Image::Info::Result;
128 sub push_info
130 my($self, $n, $key) = splice(@_, 0, 3);
131 push(@{$self->[$n]{$key}}, @_);
134 sub clean_up
136 my $self = shift;
137 for (@$self) {
138 for my $k (keys %$_) {
139 my $a = $_->{$k};
140 $_->{$k} = $a->[0] if @$a <= 1;
145 sub get_info {
146 my($self, $n, $key, $delete) = @_;
147 my $v = $delete ? delete $self->[$n]{$key} : $self->[$n]{$key};
148 $v ||= [];
149 @$v;
154 __END__
156 =head1 NAME
158 Image::Info - Extract meta information from image files
160 =head1 SYNOPSIS
162 use Image::Info qw(image_info dim);
164 my $info = image_info("image.jpg");
165 if (my $error = $info->{error}) {
166 die "Can't parse image info: $error\n";
168 my $color = $info->{color_type};
170 my($w, $h) = dim($info);
172 =head1 DESCRIPTION
174 This module provide functions to extract various kind of meta
175 information from image files. The following functions are provided by
176 the C<Image::Info> module:
178 =over
180 =item image_info( $file )
182 =item image_info( \$imgdata )
184 =item image_info( $file, key => value,... )
186 This function takes the name of a file or a file handle as argument
187 and will return one or more hashes (actually hash references)
188 describing the images inside the file. If there is only one image in
189 the file only one hash is returned. In scalar context, only the hash
190 for the first image is returned.
192 In case of error, and hash containing the "error" key will be
193 returned. The corresponding value will be an appropriate error
194 message.
196 If a reference to a scalar is passed as argument to this function,
197 then it is assumed that this scalar contains the raw image data
198 directly.
200 The image_info() function also take optional key/value style arguments
201 that can influence what information is returned.
203 =item dim( $info_hash )
205 Takes an hash as returned from image_info() and returns the dimensions
206 ($width, $height) of the image. In scalar context returns the
207 dimensions as a string.
209 =item html_dim( $info_hash )
211 Returns the dimensions as a string suitable for embedding directly
212 into HTML <img>-tags. E.g.:
214 print "<img src="..." @{[html_dim($info)]}>\n";
216 =back
218 =head1 Image descriptions
220 The image_info() function returns meta information about each image in
221 the form of a reference to a hash. The hash keys used are in most
222 cases based on the TIFF element names. All lower case keys are
223 mandatory for all file formats and will always be there unless an
224 error occured (in which case the "error" key will be present.) Mixed
225 case keys will only be present when the corresponding information
226 element is available in the image.
228 The following key names are common for any image format:
230 =over
232 =item file_media_type
234 This is the MIME type that is appropriate for the given file format.
235 The corresponding value is a string like: "image/png" or "image/jpeg".
237 =item file_ext
239 The is the suggested file name extention for a file of the given file
240 format. The value is a 3 letter, lowercase string like "png", "jpg".
242 =item width
244 This is the number of pixels horizontally in the image.
246 =item height
248 This is the number of pixels vertically in the image. (TIFF use the
249 name ImageLength for this field.)
251 =item color_type
253 The value is a short string describing what kind of values the pixels
254 encode. The value can be one of the following:
256 Gray
257 GrayA
259 RGBA
260 CMYK
261 YCbCr
262 CIELab
264 These names can also be prefixed by "Indexed-" if the image is
265 composed of indexes into a palette. Of these, only "Indexed-RGB" is
266 likely to occur.
268 (It is similar to the TIFF field PhotometricInterpretation, but this
269 name was found to be too long, so we used the PNG inpired term
270 instead.)
272 =item resolution
274 The value of this field normally gives the physical size of the image
275 on screen or paper. When the unit specifier is missing then this field
276 denotes the squareness of pixels in the image.
278 The syntax of this field is:
280 <res> <unit>
281 <xres> "/" <yres> <unit>
282 <xres> "/" <yres>
284 The <res>, <xres> and <yres> fields are numbers. The <unit> is a
285 string like C<dpi>, C<dpm> or C<dpcm> (denoting "dots per
286 inch/cm/meter).
288 =item SamplesPerPixel
290 This says how many channels there are in the image. For some image
291 formats this number might be higher than the number implied from the
292 C<color_type>.
294 =item BitsPerSample
296 This says how many bits are used to encode each of samples. The value
297 is a reference to an array containing numbers. The number of elements
298 in the array should be the same as C<SamplesPerPixel>.
300 =item Comment
302 Textual comments found in the file. The value is a reference to an
303 array if there are multiple comments found.
305 =item Interlace
307 If the image is interlaced, then this tell which interlace method is
308 used.
310 =item Compression
312 This tell which compression algorithm is used.
314 =item Gamma
316 A number.
318 =item LastModificationTime
320 A ISO date string
322 =back
324 =head1 Supported Image Formats
326 The following image file formats are currently supported:
328 =over
331 =item BMP
333 This module supports the Microsoft Device Independent Bitmap format
334 (BMP, DIB, RLE).
336 For more information see L<Image::Info::BMP>.
338 =item GIF
340 Both GIF87a and GIF89a are supported and the version number is found
341 as C<GIF_Version> for the first image. GIF files can contain multiple
342 images, and information for all images will be returned if
343 image_info() is called in list context. The Netscape-2.0 extention to
344 loop animation sequences is represented by the C<GIF_Loop> key for the
345 first image. The value is either "forever" or a number indicating
346 loop count.
348 =item JPEG
350 For JPEG files we extract information both from C<JFIF> and C<Exif>
351 application chunks.
353 C<Exif> is the file format written by most digital cameras. This
354 encode things like timestamp, camera model, focal length, exposure
355 time, aperture, flash usage, GPS position, etc. The following web
356 page contain description of the fields that can be present:
358 http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
360 The C<Exif> spec can be found at:
362 http://www.pima.net/standards/it10/PIMA15740/exif.htm
364 =item PNG
366 Information from IHDR, PLTE, gAMA, pHYs, tEXt, tIME chunks are
367 extracted. The sequence of chunks are also given by the C<PNG_Chunks>
368 key.
370 =item PBM/PGM/PPM
372 All information available is extracted.
374 =item SVG
376 SVG also provides (for) a plethora of attributes and metadata of an image.
377 See L<Image::Info::SVG> for details.
379 =item TIFF
381 The C<TIFF> spec can be found at:
382 http://partners.adobe.com/asn/developer/PDFS/TN/TIFF6.pdf
384 Also good writeup on exif spec at:
385 http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
387 =item TIFF
389 =item XBM
391 See L<Image::Info::XBM> for details.
393 =item XPM
395 See L<Image::Info::XPM> for details.
397 =back
399 =head1 SEE ALSO
401 L<Image::Size>
403 =head1 AUTHORS
405 Copyright 1999-2001 Gisle Aas.
407 GIF fixes by Ralf Steines <metamonk@yahoo.com>.
409 ASCII, BMP SVG, XPM and XBM support added by Jerrad Pierce
410 <belg4mit@mit.edu>/<webmaster@pthbb.org>.
412 Exif MakerNote decoding by Jay Soffian <jay@loudcloud.com>.
414 TIFF support by <clarsen@emf.net>.
416 This library is free software; you can redistribute it and/or
417 modify it under the same terms as Perl itself.
419 =cut