blib/lib/PDL/IO/Pnm.pm

filter: Statementstmt 85.2% 144 / 169 Branchbran 66.4% 89 / 134 Conditioncond 53.7% 29 / 54 MC/DCmcdc 21.8% 7 / 32 Subroutinesub 76.4% 13 / 17 Podpod 14.2% 1 / 7 totaltotal 68.5% 283 / 413 CC 87 SCAR 39.0
CC
87
Cov
73%
wpnm
70.7
show_err
56.0
rpnm
45.5
CRAP 49.3
« blib/lib/PDL/PP/PDLCode.pm ↑ Summary blib/lib/PDL/NiceSlice.pm »
1  
2 #
3 # GENERATED WITH PDL::PP! Don't modify!
4 #
5 package PDL::IO::Pnm;
6  
7 49 @EXPORT_OK = qw( rpnm wpnm PDL::PP pnminraw PDL::PP pnminascii PDL::PP pnmout );
8 49 %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
9  
10 49 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 49 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 49 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16
17 49 @ISA = ( 'PDL::Exporter','DynaLoader' );
18 49 push @PDL::Core::PP, __PACKAGE__;
19 49 bootstrap PDL::IO::Pnm ;
20  
21  
22  
23  
24 =head1 NAME
25  
26 PDL::IO::Pnm -- pnm format I/O for PDL
27  
28 =head1 SYNOPSIS
29  
30 use PDL::IO::Pnm;
31 $im = wpnm $pdl, $file, $format[, $raw];
32 rpnm $stack->slice(':,:,:,(0)'),"PDL.ppm";
33  
34 =head1 DESCRIPTION
35  
36 pnm I/O for PDL.
37  
38 =cut
39  
40 49 ▶use PDL::Core qw/howbig convert/;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 49 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 49 ▶use PDL::Basic; # for max/min
Subroutine BEGIN: called CC=1 SCAR=0.0
43 49 ▶use PDL::IO::Misc;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 49 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 49 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # return the upper limit of data values an integer PDL data type
48 # can hold
49 sub dmax {
50 36 ▶ my $type = shift;
Subroutine dmax: called CC=3 SCAR=12.0
Pod undocumented
51 36 my $sz = 8*howbig($type);
52 36 ▶ $sz-- if ($type == $PDL_S || $type == $PDL_L); # signed types
Branch: 1 branch
BranchTrueFalseTotal
if $type == $PDL_S or $type == $PDL_L 0 36 36
Condition33%1/3
$type == $PDL_S or $type == $PDL_L
l!l&&r!l&&!r
0036
MC/DC0%0/2
$type == $PDL_S or $type == $PDL_L
$type == $PDL_S $type == $PDL_L
Truth table33%1/3
A or B
A= $type == $PDL_SB= $type == $PDL_L
ABresult
1X1
011
000
53 36 return ((1 << $sz)-1);
54 }
55  
56 # output any errors that have accumulated
57 sub show_err {
58 0 ▶ my ($file,$showflag) = @_;
Subroutine show_err: not called CC=7 SCAR=40.3
Pod undocumented
59 0 my $err;
60 0 ▶ $showflag = 1 unless defined $showflag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $showflag 0 0 0
61 0 ▶ if (-s "$file") {
Branch: 1 branch
BranchTrueFalseTotal
if (-s "$file") 0 0 0
62 0 ▶ open(INPUT,$file) or barf "Can't open error file";
Branch: 1 branch
BranchTrueFalseTotal
open INPUT, $file or barf("Can't open error file") 0 0 0
63 0 ▶ if ($showerr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($showerr) { } 0 0 0
64 0 ▶ while (<INPUT>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline INPUT)) 0 0 0
65 0 print STDERR "converter: $_";
66 }} else {
67 0 $err = join('',<INPUT>);
68 }
69 }
70 0 close INPUT;
71 0 unlink $file;
72 0 ▶ return $err unless $showflag;
Branch: 1 branch
BranchTrueFalseTotal
unless $showflag 0 0 0
73 }
74  
75 # barf after showing any accumulated errors
76 sub rbarf {
77 0 ▶ my $err = show_err(shift, 0);
Subroutine rbarf: not called CC=2 SCAR=17.9
Pod undocumented
78 0 ▶ $err = '' unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 0 0
79 0 barf @_,"converter error: $err";
80 }
81  
82 # carp after showing any accumulated errors
83 sub rcarp {
84 0 ▶ show_err(shift);
Subroutine rcarp: not called CC=1 SCAR=6.9
Pod undocumented
85 0 carp @_;
86 }
87  
88  
89  
90  
91  
92  
93 =head1 FUNCTIONS
94  
95  
96  
97 =cut
98  
99  
100  
101  
102  
103  
104 =head2 pnminraw
105  
106 =for sig
107  
108 Signature: (type(); byte+ [o] im(m,n); int ms => m; int ns => n;
109 int isbin; char* fd)
110  
111  
112  
113 =for ref
114  
115 Read in a raw pnm file.
116  
117 read a raw pnm file. The C<type> argument is only there to
118 determine the type of the operation when creating C<im> or trigger
119 the appropriate type conversion (maybe we want a byte+ here so that
120 C<im> follows I<strictly> the type of C<type>).
121  
122  
123  
124 =for bad
125  
126 pnminraw does not process bad values.
127 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
128  
129  
130 =cut
131  
132  
133  
134  
135  
136  
137 49 *pnminraw = \&PDL::pnminraw;
138  
139  
140  
141  
142  
143 =head2 pnminascii
144  
145 =for sig
146  
147 Signature: (type(); byte+ [o] im(m,n); int ms => m; int ns => n;
148 int format; char* fd)
149  
150  
151 =for ref
152  
153 Read in an ascii pnm file.
154  
155  
156  
157 =for bad
158  
159 pnminascii does not process bad values.
160 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
161  
162  
163 =cut
164  
165  
166  
167  
168  
169  
170 49 *pnminascii = \&PDL::pnminascii;
171  
172  
173  
174  
175  
176 =head2 pnmout
177  
178 =for sig
179  
180 Signature: (a(m); int israw; int isbin; char *fd)
181  
182  
183 =for ref
184  
185 Write a line of pnm data.
186  
187 This function is implemented this way so that threading works
188 naturally.
189  
190  
191  
192 =for bad
193  
194 pnmout does not process bad values.
195 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
196  
197  
198 =cut
199  
200  
201  
202  
203  
204  
205 49 *pnmout = \&PDL::pnmout;
206  
207  
208  
209 ;
210  
211 =head2 rpnm
212  
213 =for ref
214  
215 Read a pnm (portable bitmap/pixmap, pbm/ppm) file into a piddle.
216  
217 =for usage
218  
219 Usage: $im = rpnm $file;
220  
221 Reads a file in pnm format (ascii or raw) into a pdl (magic numbers P1-P6).
222 Based on the input format it returns pdls with arrays of size (width,height)
223 if binary or grey value data (pbm and pgm) or (3,width,height) if rgb
224 data (ppm). This also means for a palette image that the distinction between
225 an image and its lookup table is lost which can be a problem in cases (but can
226 hardly be avoided when using netpbm/pbmplus). Datatype is dependent
227 on the maximum grey/color-component value (for raw and binary formats
228 always PDL_B). rpnm tries to read chopped files by zero padding the
229 missing data (well it currently doesn't, it barfs; I'll probably fix it
230 when it becomes a problem for me ;). You can also read directly into an
231 existing pdl that has to have the right size(!). This can come in handy
232 when you want to read a sequence of images into a datacube.
233  
234 For details about the formats see appropriate manpages that come with the
235 netpbm/pbmplus packages.
236  
237 =for example
238  
239 $stack = zeroes(byte,3,500,300,4);
240 rpnm $stack->slice(':,:,:,(0)'),"PDL.ppm";
241  
242 reads an rgb image (that had better be of size (500,300)) into the
243 first plane of a 3D RGB datacube (=4D pdl datacube). You can also do
244 inplace transpose/inversion that way.
245  
246 =cut
247  
248 26 ▶sub rpnm {PDL->rpnm(@_)}
Subroutine rpnm: called CC=1 SCAR=0.0
Pod documented
249 sub PDL::rpnm {
250 26 ▶ barf 'Usage: $im = rpnm($file) or $im = $pdl->rpnm($file)'
Subroutine rpnm: called CC=37 SCAR=38.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 0 or $#_ > 2 0 26 26
Condition33%1/3
$#_ < 0 or $#_ > 2
l!l&&r!l&&!r
0026
MC/DC0%0/2
$#_ < 0 or $#_ > 2
$#_ < 0 $#_ > 2
Truth table33%1/3
A or B
A= $#_ < 0B= $#_ > 2
ABresult
1X1
011
000
251 if $#_<0 || $#_>2;
252 26 my ($pdl,$file,$maybe) = @_;
253  
254  
255 26 ▶ if (ref($file)) { # $file is really a pdl in this case
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 10 16 26
256 10 $pdl = $file;
257 10 $file = $maybe;
258 } else {
259 16 $pdl = $pdl->initialize;
260 }
261  
262 26 my ($errfh, $efile) = tempfile();
263 # catch STDERR
264 26 open(SAVEERR, ">&STDERR");
265 26 ▶ open(STDERR, ">$efile") || barf "Can't redirect stderr";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">$efile" or barf("Can't redirect stderr") 0 26 26
266 26 my $succeed = open(PNM, $file);
267 # redirection now in effect for child
268 # close(STDERR);
269 26 open(STDERR, ">&PDL::IO::Pnm::SAVEERR");
270 26 ▶ rbarf $efile,"Can't open pnm file '$file'" unless $succeed;
Branch: 1 branch
BranchTrueFalseTotal
unless $succeed 0 26 26
271 26 binmode PNM;
272  
273 26 read(PNM,(my $magic),2);
274 26 ▶ rbarf $efile, "Oops, this is not a PNM file" unless $magic =~ /P[1-6]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $magic =~ /P[1-6]/ 0 26 26
275 26 ▶ print "reading pnm file with magic $magic\n" if $PDL::debug>1;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug > 1 0 26 26
276  
277 26 my ($isrgb,$israw,$params) = (0,0,3);
278 26 ▶ $israw = 1 if $magic =~ /P[4-6]/;
Branch: 1 branch
BranchTrueFalseTotal
if $magic =~ /P[4-6]/ 18 8 26
279 26 ▶ $isrgb = 1 if $magic =~ /P[3,6]/;
Branch: 1 branch
BranchTrueFalseTotal
if $magic =~ /P[3,6]/ 2 24 26
280 26 ▶ if ($magic =~ /P[1,4]/) { # PBM data
Branch: 1 branch
BranchTrueFalseTotal
if ($magic =~ /P[1,4]/) 7 19 26
281 7 $params = 2;
282 7 $dims[2] = 1; }
283  
284 # get the header information
285 26 my ($line, $pgot, @dims) = ("",0,0,0,0);
286 26 ▶ while (($pgot<$params) && ($line=<PNM>)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pgot < $params and $line = readline PNM) 71 26 97
Condition66%2/3
$pgot < $params and $line = readline PNM
!ll&&!rl&&r
26071
MC/DC50%1/2
$pgot < $params and $line = readline PNM
$pgot < $params $line = readline PNM
Truth table66%2/3
A and B
A= $pgot < $paramsB= $line = readline PNM
ABresult
0X0
100
111
287 71 $line =~ s/#.*$//;
288 71 ▶ next if $line =~ /^\s*$/; # just white space
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 26 45 71
289 45 ▶ while ($line !~ /^\s*$/ && $pgot < $params) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /^\s*$/ and $pgot < $params) 71 45 116
Condition66%2/3
not $line =~ /^\s*$/ and $pgot < $params
!ll&&!rl&&r
45071
MC/DC50%1/2
not $line =~ /^\s*$/ and $pgot < $params
not $line =~ /^\s*$/ $pgot < $params
Truth table66%2/3
not(A) and B
A= not $line =~ /^\s*$/B= $pgot < $params
ABresult
0X0
100
111
290 71 ▶ if ($line =~ /\s*(\S+)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\s*(\S+)(.*)$/) { } 71 0 71
291 71 $dims[$pgot++] = $1; $line = $2; }
292 else {
293 0 rbarf $efile, "no valid header info in pnm";}
294 }
295 }
296  
297 26 my $type = $PDL_B;
298 26 do {
299 26 TYPES: { my $pdlt;
300 26 foreach $pdlt ($PDL_B,$PDL_US,$PDL_L){
301 36 ▶ if ($dims[2] <= dmax($pdlt))
Branch: 1 branch
BranchTrueFalseTotal
if ($dims[2] <= dmax($pdlt)) 26 10 36
302 26 { $type = $pdlt;
303 26 last TYPES;
304 }
305 }
306 0 rbarf $efile, "rraw: data from ascii pnm file out of range";
307 }
308 };
309  
310 # the file ended prematurely
311 26 ▶ rbarf $efile, "no valid header info in pnm" if $pgot < $params;
Branch: 1 branch
BranchTrueFalseTotal
if $pgot < $params 0 26 26
312 26 ▶ rbarf $efile,
Branch: 1 branch
BranchTrueFalseTotal
if $dims[0] <= 0 or $dims[1] <= 0 0 26 26
Condition33%1/3
$dims[0] <= 0 or $dims[1] <= 0
l!l&&r!l&&!r
0026
MC/DC0%0/2
$dims[0] <= 0 or $dims[1] <= 0
$dims[0] <= 0 $dims[1] <= 0
Truth table33%1/3
A or B
A= $dims[0] <= 0B= $dims[1] <= 0
ABresult
1X1
011
000
313 "Dimensions must be > 0" if ($dims[0] <= 0) || ($dims[1] <= 0);
314  
315 26 my @Dims = @dims[0,1];
316 26 ▶ $Dims[0] *= 3 if $isrgb;
Branch: 1 branch
BranchTrueFalseTotal
if $isrgb 2 24 26
317 26 ▶ if ($pdl->getndims==1 && $pdl->getdim(0)==0 && $isrgb) { #input pdl is null
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->getndims == 1 and $pdl->getdim(0) == 0 and $isrgb) 2 24 26
Condition50%3/6
$pdl->getndims == 1 and $pdl->getdim(0) == 0 and $isrgb
!ll&&!rl&&r
0242
$pdl->getndims == 1 and $pdl->getdim(0) == 0
!ll&&!rl&&r
0026
MC/DC33%1/3
$pdl->getndims == 1 and $pdl->getdim(0) == 0 and $isrgb
$pdl->getndims == 1 $pdl->getdim(0) == 0 $isrgb
Truth table50%2/4
A and B and C
A= $pdl->getndims == 1B= $pdl->getdim(0) == 0C= $isrgb
ABCresult
0XX0
10X0
1100
1111
318 2 local $PDL::debug = 0; # shut up
319 2 $pdl = $pdl->zeroes(PDL::Type->new($type),3,@dims[0,1]);
320 }
321 26 ▶ my $npdl = $isrgb ? $pdl->clump(2) : $pdl;
Branch: 1 branch
BranchTrueFalseTotal
$isrgb ? : 2 24 26
322 26 ▶ if ($israw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($israw) { } 18 8 26
323 18 pnminraw (convert(pdl(0),$type), $npdl, $Dims[0], $Dims[1],
324 $magic eq "P4", 'PDL::IO::Pnm::PNM');
325 } else {
326 8 ▶ my $form = $1 if $magic =~ /P([1-3])/;
Branch: 1 branch
BranchTrueFalseTotal
if $magic =~ /P([1-3])/ 8 0 8
327 8 pnminascii (convert(pdl(0),$type), $npdl, $Dims[0], $Dims[1],
328 $form, 'PDL::IO::Pnm::PNM');
329 }
330 26 ▶ print("loaded pnm file, $dims[0]x$dims[1], gmax: $dims[2]",
Branch: 3 branches
BranchTrueFalseTotal
if $PDL::debug 3 23 26
$isrgb ? : 0 3 3
$israw ? : 3 0 3
331 $isrgb ? ", RGB data":"", $israw ? ", raw" : " ASCII"," data\n")
332 if $PDL::debug;
333 26 unlink($efile);
334  
335 # need to byte swap for little endian platforms
336 26 ▶ unless ( isbigendian() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 26 0 26
337 26 ▶ if ($israw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($israw) 18 8 26
338 18 ▶ $pdl->bswap2 if $type==$PDL_US or $pdl->type == ushort;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $PDL_US or $pdl->type == ushort() 8 10 18
Condition66%2/3
$type == $PDL_US or $pdl->type == ushort()
l!l&&r!l&&!r
8010
MC/DC50%1/2
$type == $PDL_US or $pdl->type == ushort()
$type == $PDL_US $pdl->type == ushort()
Truth table66%2/3
A or B
A= $type == $PDL_USB= $pdl->type == ushort()
ABresult
1X1
011
000
339 18 ▶ $pdl->bswap4 if $type==$PDL_L; # not likely, but supported anyway
Branch: 1 branch
BranchTrueFalseTotal
if $type == $PDL_L 0 18 18
340 }
341 }
342 26 return $pdl;
343 }
344  
345  
346 =head2 wpnm
347  
348 =for ref
349  
350 Write a pnm (portable bitmap/pixmap, pbm/ppm) file into a file.
351  
352 =for usage
353  
354 Usage: $im = wpnm $pdl, $file, $format[, $raw];
355  
356 Writes data in a pdl into pnm format (ascii or raw) (magic numbers P1-P6).
357 The $format is required (normally produced by B<wpic>) and routine just
358 checks if data is compatible with that format. All conversions should
359 already have been done. If possible, usage of B<wpic> is preferred. Currently
360 RAW format is chosen if compliant with range of input data. Explicit control
361 of ASCII/RAW is possible through the optional $raw argument. If RAW is
362 set to zero it will enforce ASCII mode. Enforcing RAW is
363 somewhat meaningless as the routine will always try to write RAW
364 format if the data range allows (but maybe it should reduce to a RAW
365 supported type when RAW == 'RAW'?). For details about the formats
366 consult appropriate manpages that come with the netpbm/pbmplus
367 packages.
368  
369 =cut
370  
371 49 *wpnm = \&PDL::wpnm;
372 sub PDL::wpnm {
373 24 ▶ barf ('Usage: wpnm($pdl,$filename,$format[,$raw]) ' .
Subroutine wpnm: called CC=42 SCAR=42.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 2 0 24 24
374 'or $pdl->wpnm($filename,$format[,$raw])') if $#_ < 2;
375 24 my ($pdl,$file,$type,$raw) = @_;
376 24 my ($israw,$max,$isrgb,$magic) = (0,255,0,"");
377  
378 # need to copy input arg since bswap[24] work inplace
379 # might be better if the bswap calls detected if run in
380 # void context
381 24 my $swap_inplace = $pdl->is_inplace;
382  
383 24 ▶ barf "wpnm: unknown format '$type'" if $type !~ /P[P,G,B]M/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /P[P,G,B]M/ 0 24 24
384  
385 # check the data
386 24 my @Dims = $pdl->dims;
387 24 ▶ barf "wpnm: expecting 3D (3,w,h) input"
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /PPM/ and $#Dims != 2 || $Dims[0] != 3 0 24 24
Condition50%3/6
$type =~ /PPM/ and $#Dims != 2 || $Dims[0] != 3
!ll&&!rl&&r
2220
$#Dims != 2 or $Dims[0] != 3
l!l&&r!l&&!r
002
MC/DC0%0/3
$type =~ /PPM/ and $#Dims != 2 || $Dims[0] != 3
$type =~ /PPM/ $#Dims != 2 $Dims[0] != 3
Truth table50%2/4
A and B or C
A= $type =~ /PPM/B= $#Dims != 2C= $Dims[0] != 3
ABCresult
0XX0
1000
11X1
1011
388 if ($type =~ /PPM/) && (($#Dims != 2) || ($Dims[0] != 3));
389 24 ▶ barf "wpnm: expecting 2D (w,h) input"
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /P[G,B]M/ and $#Dims != 1 0 24 24
Condition66%2/3
$type =~ /P[G,B]M/ and $#Dims != 1
!ll&&!rl&&r
2220
MC/DC0%0/2
$type =~ /P[G,B]M/ and $#Dims != 1
$type =~ /P[G,B]M/ $#Dims != 1
Truth table66%2/3
A and B
A= $type =~ /P[G,B]M/B= $#Dims != 1
ABresult
0X0
100
111
390 if ($type =~ /P[G,B]M/) && ($#Dims != 1);
391 24 ▶ barf "wpnm: user should convert float and double data to appropriate type"
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL_F or $pdl->get_datatype == $PDL_D 0 24 24
Condition33%1/3
$pdl->get_datatype == $PDL_F or $pdl->get_datatype == $PDL_D
l!l&&r!l&&!r
0024
MC/DC0%0/2
$pdl->get_datatype == $PDL_F or $pdl->get_datatype == $PDL_D
$pdl->get_datatype == $PDL_F $pdl->get_datatype == $PDL_D
Truth table33%1/3
A or B
A= $pdl->get_datatype == $PDL_FB= $pdl->get_datatype == $PDL_D
ABresult
1X1
011
000
392 if ($pdl->get_datatype == $PDL_F) || ($pdl->get_datatype == $PDL_D);
393 24 ▶ barf "wpnm: expecting prescaled data"
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype != $PDL_B || $pdl->get_datatype != $PDL_US and $pdl->min < 0 0 24 24
Condition50%3/6
$pdl->get_datatype != $PDL_B || $pdl->get_datatype != $PDL_US and $pdl->min < 0
!ll&&!rl&&r
0240
$pdl->get_datatype != $PDL_B or $pdl->get_datatype != $PDL_US
l!l&&r!l&&!r
8160
MC/DC0%0/3
$pdl->get_datatype != $PDL_B || $pdl->get_datatype != $PDL_US and $pdl->min < 0
$pdl->get_datatype != $PDL_B $pdl->get_datatype != $PDL_US $pdl->min < 0
Truth table40%2/5
A or B and C
A= $pdl->get_datatype != $PDL_BB= $pdl->get_datatype != $PDL_USC= $pdl->min < 0
ABCresult
00X0
1X00
0100
1X11
0111
394 if (($pdl->get_datatype != $PDL_B) || ($pdl->get_datatype != $PDL_US)) &&
395 ($pdl->min < 0);
396  
397 # check for raw format
398 24 ▶ $israw = 1 if (($pdl->get_datatype == $PDL_B) || ($pdl->get_datatype == $PDL_US) || ($type =~ /PBM/));
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->get_datatype == $PDL_B or $pdl->get_datatype == $PDL_US or $type =~ /PBM/ 8 0 8
Condition50%3/6
$pdl->get_datatype == $PDL_B or $pdl->get_datatype == $PDL_US or $type =~ /PBM/
l!l&&r!l&&!r
2400
$pdl->get_datatype == $PDL_B or $pdl->get_datatype == $PDL_US
l!l&&r!l&&!r
1680
MC/DC0%0/3
$pdl->get_datatype == $PDL_B or $pdl->get_datatype == $PDL_US or $type =~ /PBM/
$pdl->get_datatype == $PDL_B $pdl->get_datatype == $PDL_US $type =~ /PBM/
Truth table50%2/4
A or B or C
A= $pdl->get_datatype == $PDL_BB= $pdl->get_datatype == $PDL_USC= $type =~ /PBM/
ABCresult
1XX1
01X1
0011
0000
399 24 ▶ $israw = 0 if (defined($raw) && !$raw);
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw and not $raw 8 16 24
Condition100%3/3
defined $raw and not $raw
!ll&&!rl&&r
3138
MC/DC100%2/2
defined $raw and not $raw
defined $raw not $raw
Truth table100%3/3
A and not(B)
A= defined $rawB= not $raw
ABresult
0X0
100
111
400  
401  
402 24 ▶ $magic = $israw ? "P4" : "P1" if $type =~ /PBM/;
Branch: 2 branches
BranchTrueFalseTotal
if $type =~ /PBM/ 7 17 24
$israw ? : 4 3 7
403 24 ▶ $magic = $israw ? "P5" : "P2" if $type =~ /PGM/;
Branch: 2 branches
BranchTrueFalseTotal
if $type =~ /PGM/ 15 9 24
$israw ? : 10 5 15
404 24 ▶ $magic = $israw ? "P6" : "P3" if $type =~ /PPM/;
Branch: 2 branches
BranchTrueFalseTotal
if $type =~ /PPM/ 2 22 24
$israw ? : 2 0 2
405 24 ▶ $isrgb = 1 if $magic =~ /P[3,6]/;
Branch: 1 branch
BranchTrueFalseTotal
if $magic =~ /P[3,6]/ 2 22 24
406  
407 # catch STDERR and sigpipe
408 24 my ($errfh, $efile) = tempfile();
409 0 ▶ local $SIG{"PIPE"} = sub { show_err($efile);
Subroutine __ANON__: not called CC=1 SCAR=2.6
410 24 die "Bad write to pipe $? $!"; };
411  
412 24 ▶ my $pref = ($file !~ /^\s*[|>]/) ? ">" : ""; # test for plain file name
Branch: 1 branch
BranchTrueFalseTotal
!($file =~ /^\s*[|>]/) ? : 16 8 24
413 24 open(SAVEERR, ">&STDERR");
414 24 ▶ open(STDERR, ">$efile") || barf "Can't redirect stderr";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">$efile" or barf("Can't redirect stderr") 0 24 24
415 24 my $succeed = open(PNM, $pref . $file);
416 # close(STDERR);
417 24 open(STDERR, ">&PDL::IO::Pnm::SAVEERR");
418 24 ▶ rbarf $efile, "Can't open pnm file" unless $succeed;
Branch: 1 branch
BranchTrueFalseTotal
unless $succeed 0 24 24
419 24 binmode PNM;
420  
421 24 $max =$pdl->max;
422 24 ▶ print "writing ". ($israw ? "raw" : "ascii") .
Branch: 2 branches
BranchTrueFalseTotal
if $PDL::debug 3 21 24
$israw ? : 3 0 3
423 "format with magic $magic\n" if $PDL::debug;
424 # write header
425 24 print PNM "$magic\n";
426 24 print PNM "$Dims[-2] $Dims[-1]\n";
427 24 ▶ if ($type !~ /PBM/) { # fix maxval for raw output formats
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /PBM/) 17 7 24
428 17 my $outmax = 0;
429  
430 17 ▶ if ($max < 256) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max < 256) { } 9 8 17
elsif ($max < 65536) { } 8 0 8
431 9 $outmax = "255";
432 } elsif ($max < 65536) {
433 8 $outmax = "65535";
434 } else {
435 0 $outmax = $max;
436 };
437  
438 17 ▶ print PNM "$outmax\n" unless $type =~ /PBM/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /PBM/ 17 0 17
439 };
440  
441 # if rgb clump first two dims together
442 24 ▶ my $out = ($isrgb ? $pdl->slice(':,:,-1:0')->clump(2)
Branch: 1 branch
BranchTrueFalseTotal
$isrgb ? : 2 22 24
443 : $pdl->slice(':,-1:0'));
444  
445 # handle byte swap issues for little endian platforms
446 24 ▶ unless ( isbigendian() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isbigendian()) 24 0 24
447 24 ▶ if ($israw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($israw) 16 8 24
448 # make copy if needed
449 16 ▶ $out = $out->copy unless $swap_inplace;
Branch: 1 branch
BranchTrueFalseTotal
unless $swap_inplace 16 0 16
450 16 ▶ if ( (255 < $max) and ($max < 65536)) {
Branch: 2 branches
BranchTrueFalseTotal
if (255 < $max and $max < 65536) { } 6 10 16
elsif ($max >= 65536) { } 0 10 10
Condition66%2/3
255 < $max and $max < 65536
!ll&&!rl&&r
1006
MC/DC50%1/2
255 < $max and $max < 65536
255 < $max $max < 65536
Truth table66%2/3
A and B
A= 255 < $maxB= $max < 65536
ABresult
0X0
100
111
451 6 $out->bswap2;
452 } elsif ($max >= 65536) {
453 0 $out->bswap4;
454 }
455 }
456 }
457 24 pnmout($out,$israw,$type eq "PBM",'PDL::IO::Pnm::PNM');
458  
459 # check if our child returned an error (in case of a pipe)
460 24 ▶ if (!(close PNM)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close PNM) 0 24 24
461 0 my $err = show_err($efile,0);
462 0 barf "wpnm: pbmconverter error: $err";
463 }
464 24 unlink($efile);
465 }
466  
467  
468  
469 ;# Exit with OK status
470  
471 49 1;
472  
473 =head1 BUGS
474  
475 The stderr of the converters is redirected to a file. The filename is
476 currently generated in a probably non-portable way. A method that avoids
477 a file (and is portable) would be preferred.
478  
479 C<rpnm> currently relies on the fact that the header is separated
480 from the image data by a newline. This is not required by the p[bgp]m
481 formats (in fact any whitespace is allowed) but most of the pnm
482 writers seem to comply with that. Truncated files are currently
483 treated ungracefully (C<rpnm> just barfs).
484  
485 =head1 AUTHOR
486  
487 Copyright (C) 1996,1997 Christian Soeller <c.soeller@auckland.ac.nz>
488 All rights reserved. There is no warranty. You are allowed
489 to redistribute this software / documentation under certain
490 conditions. For details, see the file COPYING in the PDL
491 distribution. If this file is separated from the PDL distribution,
492 the copyright notice should be included in the file.
493  
494  
495 =cut
496  
497  
498 ############################## END PM CODE ################################
499  
500  
501  
502  
503 # Exit with OK status
504  
505 49 1;
506  
507
« blib/lib/PDL/PP/PDLCode.pm ↑ Summary blib/lib/PDL/NiceSlice.pm »