lib/PDL/ImageRGB.pd

filter: Statementstmt 80.2% 57 / 71 Branchbran 44.1% 15 / 34 Conditioncond 14.2% 3 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 4 totaltotal 56.4% 87 / 154 CC 25 SCAR 31.7
CC
25
Cov
60%
cquant
56.0
bytescl
30.7
interlrgb
3.1
CRAP 23.8
« blib/lib/PDL/Transform/Cartography.pm ↑ Summary blib/lib/PDL/Matrix.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(ppm_quant);
6 $PDL::Core::Dev::EXTRAS{$::PDLMOD}{INC} .= qq{ "-I$::PDLBASE"};
7 }
8  
9 pp_add_exported('',"interlrgb rgbtogr bytescl cquant ");
10 pp_addhdr('
11 #include "pdlppm.h" /* Local decs */
12 ');
13  
14 pp_addpm({At=>'Top'},<<'EOD');
15 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 PDL::ImageRGB -- some utility functions for RGB image data handling
21  
22 =head1 DESCRIPTION
23  
24 Collection of a few commonly used routines involved in handling of RGB, palette
25 and grayscale images. Not much more than a start. Should be a good place to
26 exercise some of the broadcast/map/clump PP stuff.
27  
28 Other stuff that should/could go here:
29  
30 =over 3
31  
32 =item *
33 color space conversion
34  
35 =item *
36 common image filters
37  
38 =item *
39 image rebinning
40  
41 =back
42  
43 =head1 SYNOPSIS
44  
45 use PDL::ImageRGB;
46  
47 =cut
48  
49 15 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
50 use vars qw( $typecheck $EPS );
51 15 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
52 15 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 30 ▶use PDL::Basic;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 30 ▶use PDL::Primitive;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 use PDL::Types;
56 15 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
57 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 use strict 'vars';
59  
60  
61 $PDL::ImageRGB::EPS = 1e-7; # there is probably a more portable way
62  
63 =head1 FUNCTIONS
64  
65 =head2 cquant
66  
67 =for ref
68  
69 quantize and reduce colours in 8-bit images
70  
71 =for usage
72  
73 ($out, $lut) = cquant($image [,$ncols]);
74  
75 This function does color reduction for <=8bit displays and accepts 8bit RGB
76 and 8bit palette images. It does this through an interface to the ppm_quant
77 routine from the pbmplus package that implements the median cut routine which
78 intelligently selects the 'best' colors to represent your image on a <= 8bit
79 display (based on the median cut algorithm). Optional args: $ncols sets the
80 maximum nunmber of colours used for the output image (defaults to 256).
81 There are images where a different color
82 reduction scheme gives better results (it seems this is true for images
83 containing large areas with very smoothly changing colours).
84  
85 Returns a list containing the new palette image (type PDL_Byte) and the RGB
86 colormap.
87  
88 =cut
89  
90 0 ▶# full broadcasting support intended
Subroutine cquant: not called CC=7 SCAR=40.3
Pod undocumented
91 0 ▶*cquant = \&PDL::cquant;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 0 or $#_ > 1 0 0 0
Condition0%0/3
$#_ < 0 or $#_ > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$#_ < 0 or $#_ > 1
$#_ < 0 $#_ > 1
Truth table0%0/3
A or B
A= $#_ < 0B= $#_ > 1
ABresult
1X1
011
000
92 sub PDL::cquant {
93 0 barf 'Usage: ($out,$olut) = cquant($image[,$ncols])'
94 0 if $#_<0 || $#_>1;
95 0 ▶ my $image = shift;
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) { } 0 0 0
96 0 my $ncols;
97 0 if ($#_ >= 0 ) { $ncols=shift; } else { $ncols = 256; };
98 my @Dims = $image->dims;
99 0 ▶ my ($out, $olut) = (null,null);
Branch: 1 branch
BranchTrueFalseTotal
if @Dims < 2 or $Dims[0] != 3 or $image->get_datatype != $PDL::ImageRGB::PDL_B 0 0 0
Condition0%0/6
@Dims < 2 or $Dims[0] != 3 or $image->get_datatype != $PDL::ImageRGB::PDL_B
l!l&&r!l&&!r
000
@Dims < 2 or $Dims[0] != 3
l!l&&r!l&&!r
000
MC/DC0%0/3
@Dims < 2 or $Dims[0] != 3 or $image->get_datatype != $PDL::ImageRGB::PDL_B
@Dims < 2 $Dims[0] != 3 $image->get_datatype != $PDL::ImageRGB::PDL_B
Truth table0%0/4
A or B or C
A= @Dims < 2B= $Dims[0] != 3C= $image->get_datatype != $PDL::ImageRGB::PDL_B
ABCresult
1XX1
01X1
0011
0000
100  
101 0 barf "input must be byte (3,x,x)" if (@Dims < 2) || ($Dims[0] != 3)
102 0 || ($image->get_datatype != $PDL_B);
103 cquant_c($image,$out,$olut,$ncols);
104 return ($out,$olut);
105 }
106  
107  
108 =head2 interlrgb
109  
110 =for ref
111  
112 Make an RGB image from a palette image and its lookup table.
113  
114 =for usage
115  
116 $rgb = $palette_im->interlrgb($lut)
117  
118 Input should be of an integer type and the lookup table (3,x,...). Will perform
119 the lookup for any N-dimensional input pdl (i.e. 0D, 1D, 2D, ...). Uses the
120 index command but will dataflow only if
121 the $lut ndarray has the dataflow_forward flag set (see L<PDL::Core/fflows>).
122  
123 =cut
124  
125 # interlace a palette image, input as 8bit-image, RGB-lut (3,x,..) to
126 1 ▶# (R,G,B) format for each pixel in the image
Subroutine interlrgb: called CC=3 SCAR=11.2
Pod undocumented
127 1 # should already support broadcasting
128 1 *interlrgb=\&PDL::interlrgb;
129 sub PDL::interlrgb {
130 my ($pdl,$lut) = @_;
131 1 ▶ my $lut_fflows = $lut->fflows;
Branch: 1 branch
BranchTrueFalseTotal
if ($lut->dims)[0] != 3 0 1 1
132 # for our purposes $lut should be (3,z) where z is the number
133 1 # of colours in the lut
134 1 ▶ barf "expecting (3,x) input" if ($lut->dims)[0] != 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $lut_fflows 1 0 1
135 1 # do the conversion as an implicitly broadcasted index lookup
136 my $res = $lut->transpose->index($pdl->dummy(0));
137 $res->sever if !$lut_fflows;
138 return $res;
139 }
140  
141  
142 =head2 rgbtogr
143  
144 =for ref
145  
146 Converts an RGB image to a grey scale using standard transform
147  
148 =for usage
149  
150 $gr = $rgb->rgbtogr
151  
152 Performs a conversion of an RGB input image (3,x,....) to a
153 greyscale image (x,.....) using standard formula:
154  
155 Grey = 0.301 R + 0.586 G + 0.113 B
156  
157 =cut
158  
159 # convert interlaced rgb image to grayscale
160 1 ▶# will convert any (3,...) dim pdl, i.e. also single lines,
Subroutine rgbtogr: called CC=3 SCAR=11.1
Pod undocumented
161 1 ▶# stacks of RGB images, etc since implicit broadcasting takes care of this
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 0 0 1 1
162 1 # should already support broadcasting
163 1 ▶*rgbtogr = \&PDL::rgbtogr;
Branch: 1 branch
BranchTrueFalseTotal
if ($im->dims)[0] != 3 0 1 1
164 sub PDL::rgbtogr {
165 barf "Usage: \$im->rgbtogr" if $#_ < 0;
166 1 my $im = shift;
167 1 barf "rgbtogr: expecting RGB (3,...) input"
168 1 if (($im->dims)[0] != 3);
169 1  
170 my $type = $im->get_datatype;
171 1 my $rgb = float([77,150,29])/256; # vector for rgb conversion
172 my $oim = null; # flag PP we want it to allocate
173 inner($im,$rgb,$oim); # do the conversion as a broadcasted inner prod
174  
175 return $oim->convert($type); # convert back to original type
176 }
177  
178 =head2 bytescl
179  
180 =for ref
181  
182 Scales a pdl into a specified data range (default 0-255)
183  
184 =for usage
185  
186 $scale = $im->bytescl([$top])
187  
188 By default $top=255, otherwise you have to give the desired top value as an
189 argument to C<bytescl>. Normally C<bytescl> doesn't rescale data that fits
190 already in the bounds 0..$top (it only does the type conversion if required).
191 If you want to force it to rescale so that the max of the output is at $top and
192 the min at 0 you give a negative $top value to indicate this.
193  
194 =cut
195  
196 # scale any pdl linearly so that its data fits into the range
197 # 0<=x<=$ncols where $ncols<=255
198 # returns scaled data with type converted to byte
199 3 ▶# doesn't rescale but just typecasts if data already fits into range, i.e.
Subroutine bytescl: called CC=15 SCAR=34.2
Pod undocumented
200 3 ▶# data ist not necessarily stretched to 0..$ncols
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 0 0 3 3
201 3 # needs some changes for full broadcasting support ?? (explicit broadcasting?)
202 3 *bytescl = \&PDL::bytescl;
203 3 ▶sub PDL::bytescl {
Branch: 1 branch
BranchTrueFalseTotal
if $#_ > -1 3 0 3
204 3 ▶ barf 'Usage: bytescl $im[,$top]' if $#_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($top < 0) 2 1 3
205 3 ▶ my $pdl = shift;
Branch: 1 branch
BranchTrueFalseTotal
if $top > 255 0 3 3
206 my ($top,$force) = (255,0);
207 3 ▶ $top = shift if $#_ > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 3 3
208 3 if ($top < 0) { $force=1; $top *= -1; }
209 3 $top = 255 if $top > 255;
210 3  
211 3 ▶ print "bytescl: scaling from 0..$top\n" if $PDL::debug;
Branch: 1 branch
BranchTrueFalseTotal
if $min >= 0 and $max <= $top and not $force 1 2 3
Condition50%3/6
$min >= 0 and $max <= $top and not $force
!ll&&!rl&&r
021
$min >= 0 and $max <= $top
!ll&&!rl&&r
030
MC/DC0%0/3
$min >= 0 and $max <= $top and not $force
$min >= 0 $max <= $top not $force
Truth table0%0/4
A and B and not(C)
A= $min >= 0B= $max <= $topC= not $force
ABCresult
0XX0
10X0
1100
1111
212 my ($max, $min);
213 $max = max $pdl;
214 2 ▶ $min = min $pdl;
Branch: 1 branch
BranchTrueFalseTotal
if ($max - $min < $PDL::ImageRGB::EPS) 0 2 2
215 0 ▶ return byte $pdl if ($min >= 0 && $max <= $top && !$force);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 0 0
216 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if abs $max < $PDL::ImageRGB::EPS or $max >= 0 and $max <= $top 0 0 0
Condition0%0/6
abs $max < $PDL::ImageRGB::EPS or $max >= 0 and $max <= $top
l!l&&r!l&&!r
000
$max >= 0 and $max <= $top
!ll&&!rl&&r
000
MC/DC0%0/3
abs $max < $PDL::ImageRGB::EPS or $max >= 0 and $max <= $top
abs $max < $PDL::ImageRGB::EPS $max >= 0 $max <= $top
Truth table0%0/4
A or B and C
A= abs $max < $PDL::ImageRGB::EPSB= $max >= 0C= $max <= $top
ABCresult
1XX1
0111
00X0
0100
217 # check for pathological cases
218 0 if (($max-$min) < $EPS) {
219 print "bytescl: pathological case\n" if $PDL::debug;
220 return byte $pdl
221 2 ▶ if (abs($max) < $EPS) || ($max >= 0 && $max <= $top);
Branch: 1 branch
BranchTrueFalseTotal
$pdl->get_datatype > $PDL::ImageRGB::PDL_F ? : 1 1 2
222 2 return byte ($pdl/$max);
223 }
224  
225 my $type = $pdl->get_datatype > $PDL_F ? $PDL_D : $PDL_F;
226 return byte ($top*($pdl->convert($type)-$min)/($max-$min)+0.5);
227 }
228  
229 ;# Exit with OK status
230  
231 1;
232  
233 =head1 BUGS
234  
235 This package doesn't yet contain enough useful functions!
236  
237 =head1 AUTHOR
238  
239 Copyright 1997 Christian Soeller <c.soeller@auckland.ac.nz>
240 All rights reserved. There is no warranty. You are allowed
241 to redistribute this software / documentation under certain
242 conditions. For details, see the file COPYING in the PDL
243 distribution. If this file is separated from the PDL distribution,
244 the copyright notice should be included in the file.
245  
246  
247 =cut
248  
249  
250 EOD
251  
252 ################################ XS CODE #################################
253 # a is the input image
254 # b is the output image and c the output lut
255 pp_def(
256 'cquant_c',Doc=>undef,
257 Pars => 'a(m=3,n,o); [o]b(n,o); [o]c(m,p);',
258 GenericTypes => ['B'],
259 OtherPars => 'int psz => p',
260 Code => 'int status;
261 status = ppm_quant($P(a), NULL, NULL, $SIZE(n),
262 $SIZE(o),$P(b), NULL, $P(c), 0,
263 $SIZE(p), 1);
264 if (!status)
265 $CROAK("ppm_quant returned error status");'
266 );
267  
268 pp_done();
« blib/lib/PDL/Transform/Cartography.pm ↑ Summary blib/lib/PDL/Matrix.pm »