blib/lib/PDL/ImageRGB.pm

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