File Coverage

blib/lib/Image/Info/WBMP.pm
Criterion Covered Total %
statement 27 36 75.0
branch 2 8 25.0
condition n/a
subroutine 4 5 80.0
pod 0 2 0.0
total 33 51 64.7


line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3             #
4             # Copyright (C) 2013 Slaven Rezic. All rights reserved.
5             # This package is free software; you can redistribute it and/or
6             # modify it under the same terms as Perl itself.
7             #
8              
9             package Image::Info::WBMP;
10              
11 1     1   438 use strict;
  1         1  
  1         392  
12             our $VERSION = '0.02';
13              
14             require Exporter;
15             *import = \&Exporter::import;
16              
17             our @EXPORT_OK = qw(wbmp_image_info);
18              
19             sub process_file {
20 1     1 0 3 my($info, $fh) = @_;
21              
22             # wbmp files have no magic, so no signature check
23              
24 1         25 $info->push_info(0, 'file_media_type' => 'image/vnd.wap.wbmp');
25 1         4 $info->push_info(0, 'file_ext' => 'wbmp');
26              
27             # logic taken from netpbm's wbmptopbm.c and adapted to perl
28              
29             my $readint = sub {
30 3     3   7 my $sum = 0;
31 3         5 my $pos = 0;
32 3         6 my $c;
33 3         4 do {
34 3         76 $c = ord(getc $fh);
35 3         42 $sum = ($sum << 7*$pos++) | ($c & 0x7f);
36             } while($c & 0x80);
37 3         8 return $sum;
38 1         13 };
39              
40             my $readheader = sub {
41 0     0   0 my $h = shift;
42 0 0       0 if ($h & 0x60 == 0) {
    0          
43             # type 00: read multi-byte bitfield
44 0         0 my $c;
45 0         0 do { $c = ord(getc $fh) } while($c & 0x80);
  0         0  
46             } elsif ($h & 0x60 == 0x60) {
47             # type 11: read name/value pair
48 0         0 for(my $i=0; $i < (($h & 0x70) >> 4) + ($h & 0x0f); $i++) { getc $fh }
  0         0  
49             }
50 1         5 };
51              
52 1         2 my $c;
53 1         3 $c = $readint->();
54 1 50       6 $c == 0
55             or die "Unrecognized WBMP type (got $c)";
56 1         3 $c = ord(getc $fh); # FixHeaderField
57 1         6 while($c & 0x80) { # ExtheaderFields
58 0         0 $c = ord(getc $fh);
59 0         0 $readheader->($c);
60             }
61 1         3 my $w = $readint->();
62 1         4 my $h = $readint->();
63 1         6 $info->push_info(0, 'width', $w);
64 1         4 $info->push_info(0, 'height', $h);
65             }
66              
67             sub wbmp_image_info {
68 1     1 0 163247 my $source = Image::Info::_source(shift);
69 1 50       6 return $source if ref $source eq 'HASH'; # Pass on errors
70              
71 1         6 return Image::Info::_image_info_for_format('WBMP', $source);
72             }
73              
74             1;
75              
76             __END__
77              
78             =head1 NAME
79              
80             Image::Info::WBMP - WBMP support for Image::Info
81              
82             =head1 SYNOPSIS
83              
84             use Image::Info qw(dim);
85             use Image::Info::WBMP qw(wbmp_image_info);
86              
87             my $info = wbmp_image_info("image.xpm");
88             if (my $error = $info->{error}) {
89             die "Can't parse image info: $error\n";
90             }
91             my($w, $h) = dim($info);
92              
93             =head1 DESCRIPTION
94              
95             wbmp is a magic-less file format, so using L<Image::Info>'s
96             C<image_info> or C<image_type> does not work here. Instead, the user
97             has to determine the file type himself, e.g. by relying on the file
98             suffix or mime type, and use the C<wbmp_image_info> function instead.
99             The returned value looks the same like L<Image::Info>'s C<image_info>
100             and may be used in a call to the C<dim> function.
101              
102             =head1 AUTHOR
103              
104             Slaven Rezic <srezic@cpan.org>
105              
106             =begin register
107              
108             NO MAGIC: true
109              
110             wbmp files have no magic, so cannot be used with the normal
111             Image::Info functions. See L<Image::Info::WBMP> for more information.
112              
113             =end register
114              
115             =cut