| 1 | package Image::Xbm2bmp; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 12 | 2 | my $xbmfilepath = shift; | |||||||||||||||||||||||||||
| 13 | 2 | my($width,$height,@data); | |||||||||||||||||||||||||||
| 14 | 2 | eval{ | |||||||||||||||||||||||||||
| 15 | 2 | ▶ | if(defined($xbmfilepath)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 1 | ($width,$height,@data) = _LoadXbmFile($xbmfilepath); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | else{ | ||||||||||||||||||||||||||||
| 19 | 1 | $width = 0; | |||||||||||||||||||||||||||
| 20 | 1 | $height = 0; | |||||||||||||||||||||||||||
| 21 | 1 | @data = (); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | 2 | ▶ | if($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | die "$@"; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | 2 | my $self = bless { _WIDTH=>$width, | |||||||||||||||||||||||||||
| 28 | _HEIGHT=>$height, | ||||||||||||||||||||||||||||
| 29 | _DATA=>[@data] | ||||||||||||||||||||||||||||
| 30 | }, $class; | ||||||||||||||||||||||||||||
| 31 | 2 | return $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub load_xbm_data($$$){ | ||||||||||||||||||||||||||
|
Subroutine
load_xbm_data: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 1 | my($self,$data_ref,$width,$height) = @_; | |||||||||||||||||||||||||||
| 36 | 1 | eval{ | |||||||||||||||||||||||||||
| 37 | 1 | $self->{_WIDTH} = $width; | |||||||||||||||||||||||||||
| 38 | 1 | $self->{_HEIGHT} = $height; | |||||||||||||||||||||||||||
| 39 | 1 | $self->{_DATA} = [@$data_ref]; | |||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||
| 41 | 1 | ▶ | if($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | die "load_xbm_data failed!:$@"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub to_bmp_file($$){ | ||||||||||||||||||||||||||
|
Subroutine
to_bmp_file: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 2 | my($self,$bmpfilepath) = @_; | |||||||||||||||||||||||||||
| 48 | 2 | my $BMP_PACK = to_bmp_pack($self); | |||||||||||||||||||||||||||
| 49 | 2 | ▶ | open(OUT,">$bmpfilepath") or die "save failed!$!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 2 | binmode(OUT); | |||||||||||||||||||||||||||
| 51 | 2 | print OUT $BMP_PACK; | |||||||||||||||||||||||||||
| 52 | 2 | close(OUT); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub to_bmp_pack($){ | ||||||||||||||||||||||||||
|
Subroutine
to_bmp_pack: called CC=6 SCAR=19.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 2 | my($self) = shift; | |||||||||||||||||||||||||||
| 57 | 2 | my($data_ref,$width,$height,@xbm_data,@source_xbm_data); | |||||||||||||||||||||||||||
| 58 | 2 | $width = $self->{_WIDTH}; | |||||||||||||||||||||||||||
| 59 | 2 | $height = $self->{_HEIGHT}; | |||||||||||||||||||||||||||
| 60 | 2 | $data_ref = $self->{_DATA}; | |||||||||||||||||||||||||||
| 61 | 2 | @source_xbm_data = @$data_ref; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | my($old_row_bytes,$row_bytes); | |||||||||||||||||||||||||||
| 64 | 2 | ▶ | if(($width%32)>0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $row_bytes = ($width/32)*4+4; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else{ | ||||||||||||||||||||||||||||
| 68 | 2 | $row_bytes = $width/8; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 2 | $old_row_bytes = $width/8; | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | if($old_row_bytes==$row_bytes){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 2 | @xbm_data = @source_xbm_data; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | else{ | ||||||||||||||||||||||||||||
| 75 | 0 | @xbm_data = _init_array($row_bytes,0x00); | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | for(my $c1=0; $c1<$height; $c1++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | for(my $c2=0; $c2<$old_row_bytes; $c2++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $xbm_data[$c1*$old_row_bytes+$c2] = $source_xbm_data[$c1*$old_row_bytes+$c2]; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | my @Bitmap_File_size = unpack("C4",pack("C4",(0x3e+$row_bytes*$height))); | |||||||||||||||||||||||||||
| 84 | 2 | my @Bitmap_Data_Offset = (0x3e,0x00,0x00,0x00); | |||||||||||||||||||||||||||
| 85 | 2 | my @Bitmap_Header_Size = (0x28,0x00,0x00,0x00); | |||||||||||||||||||||||||||
| 86 | 2 | my @Bitmap_Width = unpack("C4",pack("C4",$width)); | |||||||||||||||||||||||||||
| 87 | 2 | my @Bitmap_Height = unpack("C4",pack("C4",$height));; | |||||||||||||||||||||||||||
| 88 | 2 | my @Planes = (0x01,0x00); | |||||||||||||||||||||||||||
| 89 | 2 | my @Bits_Per_Pixel = (0x01,0x00); | |||||||||||||||||||||||||||
| 90 | 2 | my @Bitmap_Data_Size = unpack("C4",pack("C4",($row_bytes*$height))); | |||||||||||||||||||||||||||
| 91 | 2 | my @data = (); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | #xbmÊý¾ÝµÄÿ¸ö×Ö½ÚÐèÒª½øÐз´ÐòºÍNOT´¦Àí | ||||||||||||||||||||||||||||
| 94 | 2 | foreach my $d(@xbm_data){ | |||||||||||||||||||||||||||
| 95 | 72 | my $rd = _reverse_byte($d); | |||||||||||||||||||||||||||
| 96 | 72 | $rd = _NOT_byte($rd); | |||||||||||||||||||||||||||
| 97 | 72 | push @data,$rd; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | #xbmÊý¾ÝÐÐÐèÒª½øÐз´Ðò´¦Àí | ||||||||||||||||||||||||||||
| 100 | 2 | @data = _reverse_ex(\@data,$row_bytes); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | my @BITMAPFILE = ( | |||||||||||||||||||||||||||
| 103 | 0x42,0x4d, | ||||||||||||||||||||||||||||
| 104 | (@Bitmap_File_size), | ||||||||||||||||||||||||||||
| 105 | 0x00,0x00,0x00,0x00, | ||||||||||||||||||||||||||||
| 106 | (@Bitmap_Data_Offset), | ||||||||||||||||||||||||||||
| 107 | (@Bitmap_Header_Size), | ||||||||||||||||||||||||||||
| 108 | (@Bitmap_Width), | ||||||||||||||||||||||||||||
| 109 | (@Bitmap_Height), | ||||||||||||||||||||||||||||
| 110 | (@Planes), | ||||||||||||||||||||||||||||
| 111 | (@Bits_Per_Pixel), | ||||||||||||||||||||||||||||
| 112 | 0x00,0x00,0x00,0x00, | ||||||||||||||||||||||||||||
| 113 | (@Bitmap_Data_Size), | ||||||||||||||||||||||||||||
| 114 | 0xc4,0x0e,0x00,0x00, | ||||||||||||||||||||||||||||
| 115 | 0xc4,0x0e,0x00,0x00, | ||||||||||||||||||||||||||||
| 116 | 0x00,0x00,0x00,0x00, | ||||||||||||||||||||||||||||
| 117 | 0x00,0x00,0x00,0x00, | ||||||||||||||||||||||||||||
| 118 | 0x00,0x00,0x00,0x00, | ||||||||||||||||||||||||||||
| 119 | 0xff,0xff,0xff,0x00, | ||||||||||||||||||||||||||||
| 120 | (@data) | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | 2 | return pack('C*',@BITMAPFILE); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 72 | ▶ | sub _reverse_byte($){ | ||||||||||||||||||||||||||
|
Subroutine
_reverse_byte: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 126 | 72 | my $in = shift; | |||||||||||||||||||||||||||
| 127 | 72 | my $out = 0x00; | |||||||||||||||||||||||||||
| 128 | 72 | ▶ | if($in & 0b10000000){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 40 | $out = $out | 0b00000001; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 72 | ▶ | if($in & 0b01000000){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 58 | $out = $out | 0b00000010; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 72 | ▶ | if($in & 0b00100000){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 70 | $out = $out | 0b00000100; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 72 | ▶ | if($in & 0b00010000){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 44 | $out = $out | 0b00001000; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 72 | ▶ | if($in & 0b00001000){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 58 | $out = $out | 0b00010000; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 72 | ▶ | if($in & 0b00000100){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 56 | $out = $out | 0b00100000; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 72 | ▶ | if($in & 0b00000010){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 32 | $out = $out | 0b01000000; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 72 | ▶ | if($in & 0b00000001){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | $out = $out | 0b10000000; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 72 | return $out; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 72 | ▶ | sub _NOT_byte($){ | ||||||||||||||||||||||||||
|
Subroutine
_NOT_byte: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 72 | my $in = shift; | |||||||||||||||||||||||||||
| 157 | 72 | my $out = ~$in; | |||||||||||||||||||||||||||
| 158 | 72 | $out = $out & 0b11111111; | |||||||||||||||||||||||||||
| 159 | 72 | return $out; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | ▶ | sub _reverse_ex($$){ | ||||||||||||||||||||||||||
|
Subroutine
_reverse_ex: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 163 | 2 | my($data_ref,$m) = @_; | |||||||||||||||||||||||||||
| 164 | 2 | ▶ | if(!defined($data_ref)||!defined $m){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $data_ref and defined $m
MC/DC0%0/2
defined $data_ref and defined $m
defined $data_ref
defined $m
Truth table33%1/3
A and B
A= defined $data_refB= defined $m
| |||||||||||||||||||||||||||||
| 165 | 0 | die "method[_reverse_ex] died!"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 2 | my @sdata = @$data_ref; | |||||||||||||||||||||||||||
| 168 | 2 | my $len = scalar(@sdata); | |||||||||||||||||||||||||||
| 169 | 2 | my @tdata= (); | |||||||||||||||||||||||||||
| 170 | 2 | ▶ | for(my $n = 0; $n<$len/$m; $n++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 18 | my $k = $len-$n*$m-$m; | |||||||||||||||||||||||||||
| 172 | 18 | $tdata[$k] = $sdata[$n*$m]; | |||||||||||||||||||||||||||
| 173 | 18 | $tdata[$k+1] = $sdata[$n*$m+1]; | |||||||||||||||||||||||||||
| 174 | 18 | $tdata[$k+2] = $sdata[$n*$m+2]; | |||||||||||||||||||||||||||
| 175 | 18 | $tdata[$k+3] = $sdata[$n*$m+3]; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 2 | return @tdata; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub _hex_value($){ | ||||||||||||||||||||||||||
|
Subroutine
_hex_value: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 181 | 0 | my($list) = @_; | |||||||||||||||||||||||||||
| 182 | 0 | my $value; | |||||||||||||||||||||||||||
| 183 | 0 | my $h = substr($list,0,1); | |||||||||||||||||||||||||||
| 184 | 0 | my $l = substr($list,1,1); | |||||||||||||||||||||||||||
| 185 | 0 | my($h_value,$l_value); | |||||||||||||||||||||||||||
| 186 | 0 | $h = lc($h); | |||||||||||||||||||||||||||
| 187 | 0 | $l = lc($l); | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | if($h=~/[abcdef]/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | $h_value = ord($h)-ord('a')+10; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | if($h=~/[0123456789]/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | $h_value = ord($h)-ord('0'); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if($l=~/[abcdef]/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | $l_value = ord($l)-ord('a')+10; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | if($l=~/[0123456789]/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | $l_value = ord($l)-ord('0'); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 0 | $value = $h_value*16+$l_value; | |||||||||||||||||||||||||||
| 201 | 0 | return $value; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1 | ▶ | sub _LoadXbmFile($){ | ||||||||||||||||||||||||||
|
Subroutine
_LoadXbmFile: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 205 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 206 | 1 | ▶ | open(INPUT,$file) or die "Can't load xbm file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 1 | my $buf; | |||||||||||||||||||||||||||
| 208 | 1 | ▶ | while(<INPUT>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 13 | $buf.= $_; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | 1 | close(INPUT); | |||||||||||||||||||||||||||
| 212 | 1 | my @data = (); | |||||||||||||||||||||||||||
| 213 | 1 | my($height,$width); | |||||||||||||||||||||||||||
| 214 | 1 | ▶ | if($buf=~/#define .*width (\d*)/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 1 | $width=$1; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 1 | ▶ | if($buf=~/#define .*height (\d*)/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 1 | $height=$1; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 1 | ▶ | if($buf=~/{\s*(.*)\s*}/s){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 1 | my $eval_str = qq~\@data=($1);~; | |||||||||||||||||||||||||||
| 222 | 1 | eval $eval_str; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 1 | return $width,$height,@data; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub _init_array($$){ | ||||||||||||||||||||||||||
|
Subroutine
_init_array: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 228 | 0 | my($length,$value) = @_; | |||||||||||||||||||||||||||
| 229 | 0 | my @array; | |||||||||||||||||||||||||||
| 230 | 0 | ▶ | if($length=~/\d/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 0 | ▶ | for(my $count=0;$count<$length;$count++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 0 | $array[$count] = $value; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | else{ | ||||||||||||||||||||||||||||
| 236 | 0 | @array = undef; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 0 | return @array; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 2 | 1; | |||||||||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||||||||||