File Coverage

blib/lib/Image/Info/WBMP.pm
Criterion Covered Total %
statement 30 39 76.9
branch 2 8 25.0
condition n/a
subroutine 5 6 83.3
pod 0 2 0.0
total 37 55 67.2


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   552 use strict;
  1         3  
  1         36  
12 1     1   5 use vars qw($VERSION @EXPORT_OK);
  1         2  
  1         478  
13             $VERSION = '0.01';
14              
15             require Exporter;
16             *import = \&Exporter::import;
17              
18             @EXPORT_OK = qw(wbmp_image_info);
19              
20             sub process_file {
21 1     1 0 4 my($info, $fh) = @_;
22              
23             # wbmp files have no magic, so no signature check
24              
25 1         5 $info->push_info(0, 'file_media_type' => 'image/vnd.wap.wbmp');
26 1         4 $info->push_info(0, 'file_ext' => 'wbmp');
27              
28             # logic taken from netpbm's wbmptopbm.c and adapted to perl
29              
30             my $readint = sub {
31 3     3   5 my $sum = 0;
32 3         3 my $pos = 0;
33 3         4 my $c;
34 3         4 do {
35 3         46 $c = ord(getc $fh);
36 3         14 $sum = ($sum << 7*$pos++) | ($c & 0x7f);
37             } while($c & 0x80);
38 3         7 return $sum;
39 1         6 };
40              
41             my $readheader = sub {
42 0     0   0 my $h = shift;
43 0 0       0 if ($h & 0x60 == 0) {
    0          
44             # type 00: read multi-byte bitfield
45 0         0 my $c;
46 0         0 do { $c = ord(getc $fh) } while($c & 0x80);
  0         0  
47             } elsif ($h & 0x60 == 0x60) {
48             # type 11: read name/value pair
49 0         0 for(my $i=0; $i < (($h & 0x70) >> 4) + ($h & 0x0f); $i++) { getc $fh }
  0         0  
50             }
51 1         4 };
52              
53 1         2 my $c;
54 1         3 $c = $readint->();
55 1 50       4 $c == 0
56             or die "Unrecognized WBMP type (got $c)";
57 1         2 $c = ord(getc $fh); # FixHeaderField
58 1         4 while($c & 0x80) { # ExtheaderFields
59 0         0 $c = ord(getc $fh);
60 0         0 $readheader->($c);
61             }
62 1         2 my $w = $readint->();
63 1         2 my $h = $readint->();
64 1         15 $info->push_info(0, 'width', $w);
65 1         3 $info->push_info(0, 'height', $h);
66             }
67              
68             sub wbmp_image_info {
69 1     1 0 1314 my $source = Image::Info::_source(shift);
70 1 50       18 return $source if ref $source eq 'HASH'; # Pass on errors
71              
72 1         5 return Image::Info::_image_info_for_format('WBMP', $source);
73             }
74              
75             1;
76              
77             __END__