blib/lib/Image/Info/XPM.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 118 CC 16 SCAR 50.6
CC
16
Cov
0%
process_file
210.0
_get_rgb_txt
12.0
BEGIN
2.0
CRAP 156.8
1 package Image::Info::XPM;
2 $VERSION = '1.09';
3 use strict;
4 use Image::Xpm 1.09;
5  
6  
7 sub process_file{
8 my($info, $source, $opts) = @_;
9  
10 local $SIG{__WARN__} = sub {
11 $info->push_info(0, "Warn", shift);
12 };
13  
14 my $i = Image::Xpm->new(-width => 0, -height => 0);
15 # loading the file as a separate step avoids a "-r" test, this would
16 # file with in-memory strings (aka fake files)
17 $i->load($source);
18  
19 $info->push_info(0, "color_type" => "Indexed-RGB");
20 $info->push_info(0, "file_ext" => "xpm");
21 $info->push_info(0, "file_media_type" => "image/x-xpixmap");
22 $info->push_info(0, "height", $i->get(-height));
23 $info->push_info(0, "resolution", "1/1");
24 $info->push_info(0, "width", $i->get(-width));
25 $info->push_info(0, "BitsPerSample" => 8);
26 $info->push_info(0, "SamplesPerPixel", 1);
27  
28 $info->push_info(0, "XPM_CharactersPerPixel" => $i->get(-cpp) );
29 # XXX is this always?
30 $info->push_info(0, "ColorResolution", 8);
31 $info->push_info(0, "ColorTableSize" => $i->get(-ncolours) );
32 if( $opts->{ColorPalette} ){
33 $info->push_info(0, "ColorPalette" => [keys %{$i->get(-cindex)}] );
34 }
35 if( $opts->{L1D_Histogram} ){
36 #Do Histograms
37 my(%RGB, @l1dhist, $R, $G, $B, $color);
38 for(my $y=0; $y<$i->get(-height); $y++){
39 for(my $x=0; $x<$i->get(-width); $x++){
40 $color = $i->xy($x, $y);
41 if( $color =~ /^(none|opaque)$/i ) {
42 next;
43 } elsif( $color !~ /^#/ ){
44 unless( exists($RGB{white}) ){
45 local $_;
46 if( open(RGB, _get_rgb_txt()) ){
47 while(<RGB>){
48 next if /^\s*!/;
49 /(\d+)\s+(\d+)\s+(\d+)\s+(.*)/;
50 $RGB{$4}=[$1,$2,$3];
51 }
52 }
53 else{
54 $RGB{white} = "0 but true";
55 $info->push_info(0, "Warn", "Unable to open RGB database, you may need to set \$Image::Info::XPM::RGBLIB");
56 }
57 }
58 $R = $RGB{$color}->[0];
59 $G = $RGB{$color}->[1];
60 $B = $RGB{$color}->[2];
61 }
62 elsif (length $color == 7) {
63 $R = hex(substr($color,1,2));
64 $G = hex(substr($color,3,2));
65 $B = hex(substr($color,5,2));
66 }
67 elsif (length $color == 13) {
68 $R = hex(substr($color,1,2));
69 $G = hex(substr($color,5,2));
70 $B = hex(substr($color,9,2));
71 }
72 elsif (length $color == 4) {
73 $R = hex(substr($color,1,1))*16;
74 $G = hex(substr($color,2,1))*16;
75 $B = hex(substr($color,3,1))*16;
76 }
77 else {
78 warn "Unexpected length in color specification '$color'";
79 }
80 if( $opts->{L1D_Histogram} ){
81 $l1dhist[(.3*$R + .59*$G + .11*$B)]++;
82 }
83 }
84 }
85 if( $opts->{L1D_Histogram} ){
86 $info->push_info(0, "L1D_Histogram", [@l1dhist]);
87 }
88 }
89 $info->push_info(0, "HotSpotX" => $i->get(-hotx) );
90 $info->push_info(0, "HotSpotY" => $i->get(-hoty) );
91 $info->push_info(0, 'XPM_Extension-'.ucfirst($i->get(-extname)) => $i->get(-extlines)) if
92 $i->get(-extname);
93  
94 for (@{$i->get(-comments)}) {
95 $info->push_info(0, "Comment", $_);
96 }
97 }
98  
99 sub _get_rgb_txt{
100 return $Image::Info::XPM::RGBLIB if defined $Image::Info::XPM::RGBLIB;
101 # list taken from Tk::ColorEditor
102 for my $try(
103 '/usr/local/lib/X11/rgb.txt',
104 '/usr/lib/X11/rgb.txt',
105 '/usr/X11R6/lib/X11/rgb.txt',
106 '/usr/local/X11R5/lib/X11/rgb.txt',
107 '/X11/R5/lib/X11/rgb.txt',
108 '/X11/R4/lib/rgb/rgb.txt',
109 '/usr/openwin/lib/X11/rgb.txt',
110 '/usr/share/X11/rgb.txt', # This is the Debian and RH5 location
111 '/usr/X11/share/X11/rgb.txt', # seen on a Mac OS X 10.5.1 system
112 '/usr/X11R6/share/X11/rgb.txt', # seen on a OpenBSD 4.2 system
113 '/etc/X11R6/rgb.txt',
114 '/etc/X11/rgb.txt', # seen on HP-UX 11.31
115 ){
116 if( -r $try ){
117 $Image::Info::XPM::RGBLIB = $try;
118 return $try;
119 }
120 }
121 undef;
122 }
123  
124 1;
125 __END__