lib/PDL/IO/Pnm.pd

filter: Statementstmt 98.0% 101 / 103 Branchbran 74.4% 67 / 90 Conditioncond 58.3% 28 / 48 MC/DCmcdc 23.3% 7 / 30 Subroutinesub 100% 12 / 12 Podpod 25.0% 1 / 4 totaltotal 75.2% 216 / 287 CC 62 SCAR 35.5
CC
62
Cov
81%
wpnm
41.7
rpnm
39.5
dmax
2.0
CRAP 34.7
« blib/lib/PDL/DiskCache.pm ↑ Summary blib/lib/PDL/NiceSlice.pm »
1 use strict;
2 use warnings;
3  
4 { no warnings 'once'; # pass info back to Makefile.PL
5 $PDL::Core::Dev::EXTRAS{$::PDLMOD}{OBJECT} .= join '', map " $::PDLBASE/$_\$(OBJ_EXT)", qw(get);
6 $PDL::Core::Dev::EXTRAS{$::PDLMOD}{INC} .= qq{ "-I$::PDLBASE"};
7 }
8  
9 pp_add_exported('',"rpnm wpnm");
10  
11 pp_addpm({At=>'Top'},<<'EOD');
12 15 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 use warnings;
15  
16 =head1 NAME
17  
18 PDL::IO::Pnm -- pnm format I/O for PDL
19  
20 =head1 SYNOPSIS
21  
22 use PDL::IO::Pnm;
23 $im = wpnm $pdl, $file, $format[, $raw];
24 rpnm $stack->slice(':,:,:,(0)'),"PDL.ppm";
25  
26 =head1 DESCRIPTION
27  
28 pnm I/O for PDL.
29  
30 =cut
31 15 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
32 15 ▶use PDL::Core qw/howbig convert/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 15 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 15 ▶use PDL::Basic; # for max/min
Subroutine BEGIN: called CC=1 SCAR=0.0
35 30 ▶use PDL::IO::Misc;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 use File::Temp qw( tempfile );
38  
39 # return the upper limit of data values an integer PDL data type
40 144 ▶# can hold
Subroutine dmax: called CC=2 SCAR=6.9
Pod undocumented
41 144 sub dmax {
42 144 my $type = shift;
43 144 ▶ my $sz = 8*howbig($type);
Branch: 1 branch
BranchTrueFalseTotal
unless "PDL::Type"->new($type)->unsigned 48 96 144
44 144 $sz-- if !PDL::Type->new($type)->unsigned;
45 return ((1 << $sz)-1);
46 }
47 EOD
48  
49 pp_addpm({At=>'Bot'},<<'EOD'); # the rest of FUNCTIONS section
50 =head2 rpnm
51  
52 =for ref
53  
54 Read a pnm (portable bitmap/pixmap, pbm/ppm) file into an ndarray.
55  
56 =for usage
57  
58 Usage: $im = rpnm $file;
59  
60 Reads a file (or open file-handle) in pnm format (ascii or raw) into a pdl (magic numbers P1-P6).
61 Based on the input format it returns pdls with arrays of size (width,height)
62 if binary or grey value data (pbm and pgm) or (3,width,height) if rgb
63 data (ppm). This also means for a palette image that the distinction between
64 an image and its lookup table is lost which can be a problem in cases (but can
65 hardly be avoided when using netpbm/pbmplus). Datatype is dependent
66 on the maximum grey/color-component value (for raw and binary formats
67 always PDL_B). rpnm tries to read chopped files by zero padding the
68 missing data (well it currently doesn't, it barfs; I'll probably fix it
69 when it becomes a problem for me ;). You can also read directly into an
70 existing pdl that has to have the right size(!). This can come in handy
71 when you want to read a sequence of images into a datacube.
72  
73 For details about the formats see appropriate manpages that come with the
74 netpbm/pbmplus packages.
75  
76 =for example
77  
78 $stack = zeroes(byte,3,500,300,4);
79 rpnm $stack->slice(':,:,:,(0)'),"PDL.ppm";
80  
81 reads an rgb image (that had better be of size (500,300)) into the
82 first plane of a 3D RGB datacube (=4D pdl datacube). You can also do
83 inplace transpose/inversion that way.
84  
85 =cut
86  
87 48 ▶sub rpnm {PDL->rpnm(@_)}
Subroutine rpnm: called CC=1 SCAR=0.0
Pod documented
88 48 ▶sub PDL::rpnm {
Subroutine rpnm: called CC=32 SCAR=36.8
Pod undocumented
89 48 ▶ barf 'Usage: $im = rpnm($file) or $im = $pdl->rpnm($file)'
Branch: 1 branch
BranchTrueFalseTotal
if not @_ or @_ > 3 0 48 48
Condition33%1/3
not @_ or @_ > 3
l!l&&r!l&&!r
0048
MC/DC0%0/2
not @_ or @_ > 3
not @_ @_ > 3
Truth table33%1/3
not(A) or B
A= not @_B= @_ > 3
ABresult
1X1
011
000
90 if !@_ || @_>3;
91 48 ▶ my $pdl = ref($_[1]) && UNIVERSAL::isa($_[1], 'PDL')
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] && UNIVERSAL::isa($_[1], 'PDL') ? : 0 48 48
Condition66%2/3
ref $_[1] and UNIVERSAL::isa($_[1], "PDL")
!ll&&!rl&&r
16320
MC/DC0%0/2
ref $_[1] and UNIVERSAL::isa($_[1], "PDL")
ref $_[1] UNIVERSAL::isa($_[1], "PDL")
Truth table66%2/3
A and B
A= ref $_[1]B= UNIVERSAL::isa($_[1], "PDL")
ABresult
0X0
100
111
92 ? (splice @_, 0, 2)[1] : shift->initialize;
93 48 my $file = shift;
94  
95 48 my $fh;
96 48 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 32 16 48
97 32 $fh = $file;
98 } else {
99 16 ▶ open $fh, $file or barf "Can't open pnm file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $file or barf("Can't open pnm file '${file}': $!") 16 0 16
100 }
101 48 binmode $fh;
102  
103 48 read($fh,(my $magic),2);
104 48 ▶ barf "Oops, this is not a PNM file" unless $magic =~ /P([1-6])/;
Branch: 1 branch
BranchTrueFalseTotal
unless $magic =~ /P([1-6])/ 0 48 48
105 48 my $magicno = $1;
106 48 ▶ print "reading pnm file with magic $magic\n" if $PDL::debug>1;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug > 1 0 48 48
107  
108 48 ▶ my $israw = $magicno > 3 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$magicno > 3 ? : 32 16 48
109 48 my $isrgb = ($magicno % 3) == 0;
110 48 my $ispbm = ($magicno % 3) == 1;
111 48 ▶ my ($params, @dims) = ($ispbm ? 2 : 3, 0, 0, $ispbm ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$ispbm ? : 13 35 48
$ispbm ? : 13 35 48
112 # get the header information
113 48 my $pgot = 0;
114 48 ▶ while (($pgot<$params) && defined(my $line=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pgot < $params and defined(my $line = readline $fh)) 131 48 179
Condition66%2/3
$pgot < $params and defined(my $line = readline $fh)
!ll&&!rl&&r
480131
MC/DC50%1/2
$pgot < $params and defined(my $line = readline $fh)
$pgot < $params defined(my $line = readline $fh)
Truth table66%2/3
A and B
A= $pgot < $paramsB= defined(my $line = readline $fh)
ABresult
0X0
100
111
115 131 $line =~ s/#.*$//;
116 131 ▶ next if $line =~ /^\s*$/; # just white space
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 48 83 131
117 83 ▶ while ($line !~ /^\s*$/ && $pgot < $params) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /^\s*$/ and $pgot < $params) 131 83 214
Condition66%2/3
not $line =~ /^\s*$/ and $pgot < $params
!ll&&!rl&&r
830131
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
118 131 ▶ if ($line =~ /\s*(\S+)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\s*(\S+)(.*)$/) { } 131 0 131
119 131 $dims[$pgot++] = $1; $line = $2; }
120 else {
121 0 barf "no valid header info in pnm";}
122 }
123 }
124 # the file ended prematurely
125 48 ▶ barf "no valid header info in pnm" if $pgot < $params;
Branch: 1 branch
BranchTrueFalseTotal
if $pgot < $params 0 48 48
126 48 ▶ barf "Dimensions must be > 0" if ($dims[0] <= 0) || ($dims[1] <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $dims[0] <= 0 or $dims[1] <= 0 0 48 48
Condition33%1/3
$dims[0] <= 0 or $dims[1] <= 0
l!l&&r!l&&!r
0048
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
127  
128 48 my ($type) = grep $dims[2] <= dmax($_), $PDL_B,$PDL_US,$PDL_L;
129 48 ▶ barf "rraw: data from ascii pnm file out of range" if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 48 48
130  
131 48 my @Dims = @dims[0,1];
132 48 ▶ $Dims[0] *= 3 if $isrgb;
Branch: 1 branch
BranchTrueFalseTotal
if $isrgb 8 40 48
133 48 ▶ $pdl = $pdl->zeroes(PDL::Type->new($type),3,@dims[0,1])
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->isnull and $isrgb 8 40 48
Condition66%2/3
$pdl->isnull and $isrgb
!ll&&!rl&&r
0408
MC/DC50%1/2
$pdl->isnull and $isrgb
$pdl->isnull $isrgb
Truth table66%2/3
A and B
A= $pdl->isnullB= $isrgb
ABresult
0X0
100
111
134 if $pdl->isnull and $isrgb;
135 48 ▶ my $npdl = $isrgb ? $pdl->clump(2) : $pdl;
Branch: 1 branch
BranchTrueFalseTotal
$isrgb ? : 8 40 48
136 48 ▶ if ($israw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($israw) { } 32 16 48
137 32 pnminraw (convert(pdl(0),$type), $npdl, $Dims[0], $Dims[1],
138 $ispbm, $fh);
139 } else {
140 16 pnminascii (convert(pdl(0),$type), $npdl, $Dims[0], $Dims[1],
141 $magicno, $fh);
142 }
143 48 ▶ print("loaded pnm file, $dims[0]x$dims[1], gmax: $dims[2]",
Branch: 3 branches
BranchTrueFalseTotal
if $PDL::debug 3 45 48
$isrgb ? : 0 3 3
$israw ? : 3 0 3
144 $isrgb ? ", RGB data":"", $israw ? ", raw" : " ASCII"," data\n")
145 if $PDL::debug;
146  
147 # need to byte swap for little endian platforms
148 48 ▶ $pdl->type->bswap->($pdl) if !isbigendian() and $israw;
Branch: 1 branch
BranchTrueFalseTotal
if not isbigendian() and $israw 32 16 48
Condition66%2/3
not isbigendian() and $israw
!ll&&!rl&&r
01632
MC/DC50%1/2
not isbigendian() and $israw
not isbigendian() $israw
Truth table66%2/3
not(A) and B
A= not isbigendian()B= $israw
ABresult
0X0
100
111
149 48 return $pdl;
150 }
151  
152 =head2 wpnm
153  
154 =for ref
155  
156 Write a pnm (portable bitmap/pixmap, pbm/ppm) file into a file or open file-handle.
157  
158 =for usage
159  
160 Usage: $im = wpnm $pdl, $file, $format[, $raw];
161  
162 Writes data in a pdl into pnm format (ascii or raw) (magic numbers P1-P6).
163 The $format is required (normally produced by B<wpic>) and routine just
164 checks if data is compatible with that format. All conversions should
165 already have been done. If possible, usage of B<wpic> is preferred. Currently
166 RAW format is chosen if compliant with range of input data. Explicit control
167 of ASCII/RAW is possible through the optional $raw argument. If RAW is
168 set to zero it will enforce ASCII mode. Enforcing RAW is
169 somewhat meaningless as the routine will always try to write RAW
170 format if the data range allows (but maybe it should reduce to a RAW
171 supported type when RAW == 'RAW'?). For details about the formats
172 consult appropriate manpages that come with the netpbm/pbmplus
173 packages.
174  
175 =cut
176  
177 my %type2base = (PBM => 1, PGM => 2, PPM => 3);
178 *wpnm = \&PDL::wpnm;
179 42 ▶sub PDL::wpnm {
Subroutine wpnm: called CC=30 SCAR=37.3
Pod undocumented
180 42 ▶ barf ('Usage: wpnm($pdl,$filename,$format[,$raw]) ' .
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 2 0 42 42
181 'or $pdl->wpnm($filename,$format[,$raw])') if $#_ < 2;
182 42 my ($pdl,$file,$type,$raw) = @_;
183 42 ▶ barf "wpnm: unknown format '$type'" if !exists $type2base{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $type2base{$type} 0 42 42
184  
185 # need to copy input arg since bswap[24] work inplace
186 # might be better if the bswap calls detected if run in
187 # void context
188 42 my $swap_inplace = $pdl->is_inplace;
189  
190 # check the data
191 42 my @Dims = $pdl->dims;
192 42 ▶ barf "wpnm: expecting 3D (3,w,h) input"
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /PPM/ and $#Dims != 2 || $Dims[0] != 3 0 42 42
Condition50%3/6
$type =~ /PPM/ and $#Dims != 2 || $Dims[0] != 3
!ll&&!rl&&r
3840
$#Dims != 2 or $Dims[0] != 3
l!l&&r!l&&!r
004
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
193 if ($type =~ /PPM/) && (($#Dims != 2) || ($Dims[0] != 3));
194 42 ▶ barf "wpnm: expecting 2D (w,h) input"
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /P[GB]M/ and $#Dims != 1 0 42 42
Condition66%2/3
$type =~ /P[GB]M/ and $#Dims != 1
!ll&&!rl&&r
4380
MC/DC0%0/2
$type =~ /P[GB]M/ and $#Dims != 1
$type =~ /P[GB]M/ $#Dims != 1
Truth table66%2/3
A and B
A= $type =~ /P[GB]M/B= $#Dims != 1
ABresult
0X0
100
111
195 if ($type =~ /P[GB]M/) && ($#Dims != 1);
196 42 ▶ barf "wpnm: user should convert float etc data to appropriate type"
Branch: 1 branch
BranchTrueFalseTotal
unless $pdl->type->integer 0 42 42
197 if !$pdl->type->integer;
198 42 my $max = $pdl->max;
199 42 ▶ barf "wpnm: expecting prescaled data (0-65535)"
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->min < 0 or $max > 65535 0 42 42
Condition33%1/3
$pdl->min < 0 or $max > 65535
l!l&&r!l&&!r
0042
MC/DC0%0/2
$pdl->min < 0 or $max > 65535
$pdl->min < 0 $max > 65535
Truth table33%1/3
A or B
A= $pdl->min < 0B= $max > 65535
ABresult
1X1
011
000
200 if $pdl->min < 0 or $max > 65535;
201  
202 # check for raw format
203 42 ▶ my $israw =
Branch: 2 branches
BranchTrueFalseTotal
defined $raw && !$raw ? : 16 26 42
$pdl->get_datatype == $PDL::IO::Pnm::PDL_B || $pdl->get_datatype == $PDL::IO::Pnm::PDL_US || $type eq 'PBM' ? : 26 0 26
Condition66%6/9
defined $raw and not $raw
!ll&&!rl&&r
52116
$pdl->get_datatype == $PDL::IO::Pnm::PDL_B or $pdl->get_datatype == $PDL::IO::Pnm::PDL_US or $type eq "PBM"
l!l&&r!l&&!r
2600
$pdl->get_datatype == $PDL::IO::Pnm::PDL_B or $pdl->get_datatype == $PDL::IO::Pnm::PDL_US
l!l&&r!l&&!r
16100
MC/DC100%2/2
defined $raw and not $raw
defined $raw not $raw
MC/DC0%0/3
$pdl->get_datatype == $PDL::IO::Pnm::PDL_B or $pdl->get_datatype == $PDL::IO::Pnm::PDL_US or $type eq "PBM"
$pdl->get_datatype == $PDL::IO::Pnm::PDL_B $pdl->get_datatype == $PDL::IO::Pnm::PDL_US $type eq "PBM"
Truth table100%3/3
A and not(B)
A= defined $rawB= not $raw
ABresult
0X0
100
111
Truth table50%2/4
A or B or C
A= $pdl->get_datatype == $PDL::IO::Pnm::PDL_BB= $pdl->get_datatype == $PDL::IO::Pnm::PDL_USC= $type eq "PBM"
ABCresult
1XX1
01X1
0011
0000
204 (defined($raw) && !$raw) ? 0 :
205 (($pdl->get_datatype == $PDL_B) || ($pdl->get_datatype == $PDL_US) || ($type eq 'PBM')) ? 3 :
206 0;
207  
208 42 my $magic = 'P' . ($type2base{$type} + $israw);
209 42 my $isrgb = $type eq 'PPM';
210  
211 42 ▶ my $pref = ($file !~ /^\s*[|>]/) ? ">" : ""; # test for plain file name
Branch: 1 branch
BranchTrueFalseTotal
!($file =~ /^\s*[|>]/) ? : 32 10 42
212 42 my ($already_open, $fh) = 0;
213 42 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 16 26 42
214 16 $fh = $file, $already_open = 1;
215 } else {
216 26 ▶ open $fh, $pref . $file or barf "Can't open pnm file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $pref . $file or barf("Can't open pnm file: $!") 26 0 26
217 }
218 42 binmode $fh;
219  
220 42 ▶ print "writing ". ($israw ? "raw" : "ascii") .
Branch: 2 branches
BranchTrueFalseTotal
if $PDL::debug 3 39 42
$israw ? : 3 0 3
221 "format with magic $magic, max=$max\n" if $PDL::debug;
222 # write header
223 42 print $fh "$magic\n";
224 42 print $fh "$Dims[-2] $Dims[-1]\n";
225 42 ▶ if ($type ne 'PBM') { # fix maxval for raw output formats
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "PBM") 29 13 42
226 29 my $outmax = 0;
227 29 ▶ if ($max < 256) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max < 256) { } 15 14 29
elsif ($max < 65536) { } 14 0 14
228 15 $outmax = "255";
229 } elsif ($max < 65536) {
230 14 $outmax = "65535";
231 } else {
232 0 $outmax = $max;
233 };
234 29 print $fh "$outmax\n";
235 };
236  
237 # if rgb clump first two dims together
238 42 ▶ my $out = ($isrgb ? $pdl->slice(':,:,-1:0')->clump(2)
Branch: 1 branch
BranchTrueFalseTotal
$isrgb ? : 4 38 42
239 : $pdl->slice(':,-1:0'));
240 # handle byte swap issues for little endian platforms
241 42 ▶ if (!isbigendian() and $israw) {
Branch: 1 branch
BranchTrueFalseTotal
if (not isbigendian() and $israw) 26 16 42
Condition66%2/3
not isbigendian() and $israw
!ll&&!rl&&r
01626
MC/DC50%1/2
not isbigendian() and $israw
not isbigendian() $israw
Truth table66%2/3
not(A) and B
A= not isbigendian()B= $israw
ABresult
0X0
100
111
242 26 ▶ $out = $out->copy unless $swap_inplace;
Branch: 1 branch
BranchTrueFalseTotal
unless $swap_inplace 26 0 26
243 26 $out->type->bswap->($out);
244 }
245 42 pnmout($out,$israw,$type eq "PBM",$fh);
246 # check if our child returned an error (in case of a pipe)
247 42 ▶ barf "wpnm: pbmconverter error: $!" if !$already_open and !close $fh;
Branch: 1 branch
BranchTrueFalseTotal
if not $already_open || close $fh 0 42 42
Condition66%2/3
$already_open || close $fh
l!l&&r!l&&!r
16260
MC/DC0%0/2
$already_open || close $fh
$already_open close $fh
Truth table66%2/3
A || B
A= $already_openB= close $fh
ABresult
1X1
011
000
248 }
249  
250 ;# Exit with OK status
251  
252 1;
253  
254 =head1 BUGS
255  
256 C<rpnm> currently relies on the fact that the header is separated
257 from the image data by a newline. This is not required by the p[bgp]m
258 formats (in fact any whitespace is allowed) but most of the pnm
259 writers seem to comply with that. Truncated files are currently
260 treated ungracefully (C<rpnm> just barfs).
261  
262 =head1 AUTHOR
263  
264 Copyright (C) 1996,1997 Christian Soeller <c.soeller@auckland.ac.nz>
265 All rights reserved. There is no warranty. You are allowed
266 to redistribute this software / documentation under certain
267 conditions. For details, see the file COPYING in the PDL
268 distribution. If this file is separated from the PDL distribution,
269 the copyright notice should be included in the file.
270  
271  
272 =cut
273  
274  
275 ############################## END PM CODE ################################
276 EOD
277  
278  
279 pp_def('pnminraw',
280 Pars => 'type(); byte+ [o] im(m,n); byte [t] buf(llen)',
281 OtherPars => 'IV ms => m; IV ns => n;
282 int isbin; PerlIO *fp',
283 GenericTypes => [qw(B U L)],
284 RedoDimsCode => '
285 $SIZE(llen) = $COMP(isbin) ? ($SIZE(m)+7) / 8 : $SIZE(m) * sizeof($GENERIC());
286 ',
287 Code => '
288 /* with top to bottom inversion */
289 loop (n=::-1) %{
290 if (PerlIO_read($COMP(fp),$P(buf),$SIZE(llen)) != $SIZE(llen))
291 $CROAK("Error reading pnm file");
292 if ($COMP(isbin)) { /* unpack buffer */
293 PDL_Byte *bp=$P(buf); int k=0, bit=0;
294 loop (m) %{
295 bit &= 7;
296 if (!bit) k = *bp++;
297 /* here we do the inversion */
298 $im() = (k&0x80) ? 0 : 1;
299 k = k << 1;
300 bit++;
301 %}
302 } else {
303 $GENERIC() *gbp = ($GENERIC()*)$P(buf);
304 loop(m) %{ $im() = *gbp++; %}
305 }
306 %}
307 ', Doc => '
308 =for ref
309  
310 Read in a raw pnm file.
311  
312 read a raw pnm file. The C<type> argument is only there to
313 determine the type of the operation when creating C<im> or trigger
314 the appropriate type conversion (maybe we want a byte+ here so that
315 C<im> follows I<strictly> the type of C<type>).
316 '
317 );
318  
319  
320 pp_def( 'pnminascii',
321 Pars => 'type(); byte+ [o] im(m,n)',
322 OtherPars => 'IV ms => m; IV ns => n;
323 int format; PerlIO *fp',
324 GenericTypes => [qw(B U S L)],
325 CHeader => qq{#include "get.h"\n},
326 Code => q?
327 int ns = $SIZE(n), s, i;
328 switch ($COMP(format)) {
329 case PBM:
330 broadcastloop %{ /* with top to bottom inversion */
331 for (i=ns-1; i>= 0; i--) {
332 loop(m) %{
333 while ((s = PerlIO_getc($COMP(fp))) != EOF) {
334 switch (s) {
335 case '#': /* comment, skip rest of line */
336 SWALLOWLINE($COMP(fp));
337 break;
338 case '0':
339 case '1':
340 /* invert on the fly */
341 $im(n=>i,m=>m) = 1 - (s - '0');
342 goto $PPSYM()next;
343 break;
344 case ' ':
345 case '\t':
346 case '\r':
347 case '\n':
348 case ',':
349 /* skip whitespace */
350 break;
351 default: /* garbage */
352 $CROAK("found garbage, aborting"); /* for now */
353 break;
354 }
355 }
356 $PPSYM()next: ;
357 %}
358 }
359 %}
360 break;
361 case PGM:
362 case PPM:
363 broadcastloop %{ /* with top to bottom inversion */
364 PDL_Long j;
365 for (i=ns-1; i>= 0; i--) {
366 loop(m) %{
367 if (getint($COMP(fp),&j) <= 0)
368 $CROAK("found garbage, aborting"); /* for now */
369 $im(n=>i,m=>m) = j;
370 %}
371 }
372 %}
373 break;
374 default:
375 $CROAK("unknown PNM format");
376 break;
377 } /* end switch */
378 ?, Doc => '
379 =for ref
380  
381 Read in an ascii pnm file.
382 '
383 );
384  
385  
386 # write a line of data supporting broadcasting !
387 pp_def( 'pnmout',
388 Pars => 'a(m);',
389 'NoPthread' => 1, # Pthreading doesn't make sense for an IO function
390 OtherPars => "int israw; int isbin; PerlIO *fp",
391 GenericTypes => [qw(B U S L)],
392 Code => '
393 if ($COMP(israw)) {
394 if ($COMP(isbin)) {
395 broadcastloop %{
396 int k=0, bit=0;
397 loop(m) %{
398 k = (k << 1) | ($a() < 1);
399 bit++;
400 if (bit==8) {
401 PerlIO_putc($COMP(fp),k);
402 bit = k = 0;
403 }
404 %}
405 if (bit) {
406 k = k << (8-bit);
407 PerlIO_putc($COMP(fp),k);
408 }
409 %}
410 } else {
411 PDL_Indx len = $SIZE(m) * sizeof($GENERIC());
412 broadcastloop %{
413 PDL_Indx this_len = len;
414 while (1) {
415 PDL_Indx wrote = PerlIO_write($COMP(fp),$P(a),this_len);
416 if (wrote < 0)
417 $CROAK("Error writing pnm file: %s", strerror(errno));
418 this_len -= wrote;
419 if (this_len <= 0) break;
420 }
421 %}
422 }
423 } else {
424 PDL_Indx len=0;
425 broadcastloop %{
426 loop(m) %{
427 PerlIO_printf($COMP(fp),"%3d ",$COMP(isbin) ?
428 ($a() < 1) :$a());
429 len +=4;
430 if (len>58) { PerlIO_printf($COMP(fp),"\n"); len=0; }
431 %}
432 if (len<=58)
433 PerlIO_printf($COMP(fp),"\n");
434 %}
435 }
436 ', Doc => '
437 =for ref
438  
439 Write a line of pnm data.
440  
441 This function is implemented this way so that broadcasting works
442 naturally.
443 ');
444  
445 pp_done();
« blib/lib/PDL/DiskCache.pm ↑ Summary blib/lib/PDL/NiceSlice.pm »