IO/CommonUtil.pm

filter: Statementstmt 16.0% 21 / 131 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 42.8% 6 / 14 Podpod n/a totaltotal 11.6% 27 / 232 CC 40 SCAR 39.0
CC
40
Cov
10%
_toString
72.0
_getDicomValue
72.0
_showDicomField
72.0
CRAP 49.2
« DB/DicomTagDict.pm ↑ Summary Util/DicomDumper.pm »
1 ##############################################################################
2 # CommonUtil.pm -- a module including common functions for internal use
3 #
4 # Copyright (c) 2010 Baoshe Zhang. All rights reserved.
5 # This file is part of "DicomPack". DicomPack is free software;
6 # you can redistribute it and/or modify it under the same
7 # terms as Perl itself.
8 ##############################################################################
9  
10 package DicomPack::IO::CommonUtil;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use DicomPack::DB::DicomTagDict qw/getTagDesc getTagID/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DicomPack::DB::DicomVRDict qw/getVR/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 @ISA = qw/Exporter/;
21 1 @EXPORT_OK = qw/_getEndian _pack _unpack _isLittleEndian _toString _getDicomValue _showDicomField _parseDicomFieldPath/;
22  
23 # get the endianness of current system. ">" for big endiannes, "<" for little endianness.
24 sub _getEndian
25 0 ▶{
Subroutine _getEndian: not called CC=1 SCAR=6.9
26 0 return ">" if pack("L", 1) eq pack("N", 1);
27 0 return "<";
28 }
29  
30 # a repalcement for pack function. The versions of perl prior to 5.10.0 do not support ">" and "<".
31 sub _pack
32 0 ▶{
Subroutine _pack: not called CC=7 SCAR=40.3
33 0 my ($dataType, $endianness, $data) = @_;
34  
35 0 ▶ if(eval("require 5.0100") or $endianness eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require 5.0100" or $endianness eq "") 0 0 0
Condition0%0/3
eval "require 5.0100" or $endianness eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
eval "require 5.0100" or $endianness eq ""
eval "require 5.0100" $endianness eq ""
Truth table0%0/3
A or B
A= eval "require 5.0100"B= $endianness eq ""
ABresult
1X1
011
000
36 {
37 0 return pack($dataType.$endianness."*", @$data);
38 }
39  
40 0 ▶ if($dataType !~ /^[sSiIlLqQjJfFdDpP]$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dataType =~ /^[sSiIlLqQjJfFdDpP]$/) 0 0 0
41 {
42 0 die "DataType: $dataType, not supporte!!!\n";
43 }
44  
45 0 ▶ if(ref($data) ne "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "ARRAY") 0 0 0
46 {
47 0 die "Data should be an ARRAY Ref!!!\n";
48 }
49  
50 0 ▶ if(_getEndian() eq $endianness)
Branch: 1 branch
BranchTrueFalseTotal
if (_getEndian() eq $endianness) { } 0 0 0
51 {
52 0 return pack($dataType."*", @$data);
53 }
54 else
55 {
56 0 my $data_t = "";
57 0 foreach my $iData (@$data)
58 {
59 0 my $iData_t = pack "C*", reverse unpack("C*", pack($dataType, $iData));
60 0 $data_t .= $iData_t;
61 }
62 0 return $data_t;
63 }
64 }
65  
66 # A replacement for unpack. The versions of perl prior to 5.10.0 do not support ">" and "<".
67 sub _unpack
68 0 ▶{
Subroutine _unpack: not called CC=6 SCAR=37.4
69 0 my ($dataType, $endianness, $data) = @_;
70  
71 0 ▶ if(eval("require 5.0100") or $endianness eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require 5.0100" or $endianness eq "") 0 0 0
Condition0%0/3
eval "require 5.0100" or $endianness eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
eval "require 5.0100" or $endianness eq ""
eval "require 5.0100" $endianness eq ""
Truth table0%0/3
A or B
A= eval "require 5.0100"B= $endianness eq ""
ABresult
1X1
011
000
72 {
73 0 return unpack($dataType.$endianness."*", $data);
74 }
75  
76 0 ▶ if($dataType !~ /^[sSiIlLqQjJfFdDpP]$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dataType =~ /^[sSiIlLqQjJfFdDpP]$/) 0 0 0
77 {
78 0 die "DataType: $dataType, not supporte!!!\n";
79 }
80  
81 0 ▶ if(_getEndian() eq $endianness)
Branch: 1 branch
BranchTrueFalseTotal
if (_getEndian() eq $endianness) { } 0 0 0
82 {
83 0 return unpack($dataType."*", $data);
84 }
85 else # little endianness
86 {
87 0 my @data_t;
88 0 foreach my $iData (unpack($dataType."*", $data))
89 {
90 0 my $iData_t = unpack($dataType, pack("C*", reverse unpack("C*", pack($dataType, $iData))));
91 0 push @data_t, $iData_t;
92 }
93 0 return @data_t;
94 }
95 }
96  
97 # check the endianness of a dicom file according to "0002,0010" of meta info
98 sub _isLittleEndian
99 0 ▶{
Subroutine _isLittleEndian: not called CC=4 SCAR=30.0
100 0 my $dicomFields = shift;
101 0 my $isLittleEndian = 1;
102 0 ▶ if(defined $dicomFields)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dicomFields) 0 0 0
103 {
104 0 ▶ if(defined $dicomFields->{"0002,0010"})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dicomFields->{"0002,0010"}) 0 0 0
105 {
106 0 my ($tt_t, $vv_t) = _getDicomValue($dicomFields->{"0002,0010"});
107 0 my $transferSyntax = $vv_t->[0];
108 0 ▶ if($transferSyntax eq "1.2.840.10008.1.2.2")
Branch: 1 branch
BranchTrueFalseTotal
if ($transferSyntax eq "1.2.840.10008.1.2.2") 0 0 0
109 {
110 0 $isLittleEndian = 0;
111 }
112 }
113 }
114 0 return $isLittleEndian;
115 }
116  
117 # convert a composite dicom value to a string
118 sub _toString
119 0 ▶{
Subroutine _toString: not called CC=8 SCAR=42.8
120 0 my $dicomValue = shift;
121 0 my $isLittleEndian = shift;
122 0 my $verbose = shift;
123 0 my $indent = shift;
124  
125 0 my ($vr, $value) = _getDicomValue($dicomValue, $isLittleEndian);
126  
127 0 my $valueStr = "";
128  
129 0 ▶ if($verbose <= 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose <= 1) { } 0 0 0
130 {
131 0 my $nPrint = scalar @$value;
132 0 ▶ if($nPrint > 15)
Branch: 1 branch
BranchTrueFalseTotal
if ($nPrint > 15) 0 0 0
133 {
134 0 $nPrint = 15;
135 }
136 0 ▶ for(my $i=0; $i<$nPrint; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nPrint) 0 0 0
137 {
138 0 my $t = $value->[$i];
139 0 $valueStr .= $t.' ';
140 }
141 0 ▶ if($nPrint < scalar @$value)
Branch: 1 branch
BranchTrueFalseTotal
if ($nPrint < scalar @$value) 0 0 0
142 {
143 0 $valueStr .= "...\n";
144 }
145  
146 0 ▶ if(length($valueStr) > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (length $valueStr > 255) 0 0 0
147 {
148 0 $valueStr = substr($valueStr, 0, 255);
149 0 $valueStr .= "...\n";
150 }
151 }
152 else
153 {
154 0 ▶ for(my $i=0; $i<scalar @$value; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$value) 0 0 0
155 {
156 0 my $t = $value->[$i];
157 0 $valueStr .= $t.' ';
158 0 ▶ if(($i+1)%16 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (($i + 1) % 16 == 0) 0 0 0
159 {
160 0 $valueStr .= "\n"." ".$indent;
161 }
162 }
163 }
164  
165 0 return $vr.":".$valueStr;
166 }
167  
168 # process a composite dicom value
169 sub _getDicomValue
170 0 ▶{
Subroutine _getDicomValue: not called CC=8 SCAR=42.8
171 0 my $dicomValue = shift;
172 0 my $isLittleEndian = shift;
173  
174 0 my $vr = substr($dicomValue, 0, 2);
175 0 my $value = substr($dicomValue, 3);
176  
177 0 my @t_data;
178  
179 0 my $vrItem = getVR($vr);
180 0 ▶ if(defined $vrItem->{tailing})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vrItem->{"tailing"}) 0 0 0
181 {
182 0 $value =~ s/($vrItem->{tailing})+$//;
183 }
184 0 ▶ if(defined $vrItem->{leading})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vrItem->{"leading"}) 0 0 0
185 {
186 0 $value =~ s/^($vrItem->{leading})+//;
187 }
188  
189 0 ▶ if(defined $vrItem->{type})
Branch: 2 branches
BranchTrueFalseTotal
if (defined $vrItem->{'type'}) { } 0 0 0
elsif (defined $vrItem->{'delimiter'}) { } 0 0 0
190 {
191 0 my $endianness = "<";
192 0 ▶ $endianness = ">" unless $isLittleEndian;
Branch: 1 branch
BranchTrueFalseTotal
unless $isLittleEndian 0 0 0
193 0 ▶ if($vrItem->{type} eq "C" or $vrItem->{type} eq "c")
Branch: 1 branch
BranchTrueFalseTotal
if ($vrItem->{"type"} eq "C" or $vrItem->{"type"} eq "c") 0 0 0
Condition0%0/3
$vrItem->{"type"} eq "C" or $vrItem->{"type"} eq "c"
l!l&&r!l&&!r
000
MC/DC0%0/2
$vrItem->{"type"} eq "C" or $vrItem->{"type"} eq "c"
$vrItem->{"type"} eq "C" $vrItem->{"type"} eq "c"
Truth table0%0/3
A or B
A= $vrItem->{"type"} eq "C"B= $vrItem->{"type"} eq "c"
ABresult
1X1
011
000
194 {
195 0 $endianness = "";
196 }
197 #@t_data = unpack($vrItem->{type}.$endianness."*", $value);
198 0 @t_data = _unpack($vrItem->{type}, $endianness, $value);
199 }
200 elsif (defined $vrItem->{delimiter})
201 {
202 0 @t_data = split quotemeta($vrItem->{delimiter}), $value;
203 }
204 else
205 {
206 0 @t_data = $value;
207 }
208  
209 0 return ($vr, \@t_data);
210 }
211  
212 # show dicom file's structure and field data (recursive)
213 sub _showDicomField
214 0 ▶{
Subroutine _showDicomField: not called CC=8 SCAR=42.8
215 0 my $dicomFields = shift;
216 0 my $depth = shift;
217 0 my $verbose = shift;
218  
219 0 my $isLittleEndian = shift;
220  
221 0 my $tagID = shift;
222  
223 0 my $indent = " " x (4*$depth);
224  
225 0 ▶ if(ref($dicomFields) eq "HASH")
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dicomFields eq 'HASH') { } 0 0 0
elsif (ref $dicomFields eq 'ARRAY') { } 0 0 0
226 {
227 0 foreach my $field_t (sort keys %$dicomFields)
228 {
229 0 my $desc = getTagDesc($field_t);
230 0 print $indent."$field_t"." [".$desc."]"."->\n";
231 0 _showDicomField($dicomFields->{$field_t}, $depth+1, $verbose, $isLittleEndian, $field_t);
232 }
233 }
234 elsif(ref($dicomFields) eq "ARRAY")
235 {
236 0 ▶ for(my $index=0; $index < scalar @$dicomFields; $index++)
Branch: 1 branch
BranchTrueFalseTotal
if ($index < scalar @$dicomFields) 0 0 0
237 {
238 0 print $indent."$index->\n";
239 0 _showDicomField($dicomFields->[$index], $depth+1, $verbose, $isLittleEndian);
240 }
241 }
242 else
243 {
244 0 ▶ if($verbose >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose >= 1) 0 0 0
245 {
246 0 ▶ if(defined $tagID and $tagID =~ /^0002,/)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tagID and $tagID =~ /^0002,/) { } 0 0 0
Condition0%0/3
defined $tagID and $tagID =~ /^0002,/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tagID and $tagID =~ /^0002,/
defined $tagID $tagID =~ /^0002,/
Truth table0%0/3
A and B
A= defined $tagIDB= $tagID =~ /^0002,/
ABresult
0X0
100
111
247 {
248 0 print $indent._toString($dicomFields, 1, $verbose, $indent), "\n";
249 }
250 else
251 {
252 0 print $indent._toString($dicomFields, $isLittleEndian, $verbose, $indent), "\n";
253 }
254 }
255 }
256 }
257  
258 sub _parseDicomFieldPath
259 0 ▶{
Subroutine _parseDicomFieldPath: not called CC=5 SCAR=34.0
260 0 my $fieldPath = shift;
261  
262 0 $fieldPath =~ s/^\s*\/*//;
263 0 $fieldPath =~ s/\/*\s*$//;
264 0 my @fieldID = split /\//, $fieldPath;
265  
266 0 my @tagIDList;
267  
268 0 my $nFields = scalar @fieldID;
269 0 ▶ for(my $i=0; $i<$nFields; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nFields) 0 0 0
270 {
271 0 my $tagID = $fieldID[$i];
272  
273 0 ▶ $tagID = getTagID($tagID) if ($tagID !~ /^\d+$/ and $tagID ne "x");
Branch: 1 branch
BranchTrueFalseTotal
if not $tagID =~ /^\d+$/ and $tagID ne "x" 0 0 0
Condition0%0/3
not $tagID =~ /^\d+$/ and $tagID ne "x"
!ll&&!rl&&r
000
MC/DC0%0/2
not $tagID =~ /^\d+$/ and $tagID ne "x"
not $tagID =~ /^\d+$/ $tagID ne "x"
Truth table0%0/3
not(A) and B
A= not $tagID =~ /^\d+$/B= $tagID ne "x"
ABresult
0X0
100
111
274  
275 0 ▶ die "Tag: $fieldID[$i], not exists!!!" unless defined $tagID;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tagID 0 0 0
276  
277 0 push @tagIDList, $tagID;
278 }
279 0 return @tagIDList;
280 }
281  
282 1 1;
283  
« DB/DicomTagDict.pm ↑ Summary Util/DicomDumper.pm »