blib/lib/PDL/Graphics/LUT.pm

filter: Statementstmt 94.9% 75 / 79 Branchbran 64.2% 18 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 86.3% 114 / 132 CC 17 SCAR 19.9
CC
17
Cov
85%
lut_data
15.7
BEGIN
6.1
BEGIN
1.0
CRAP 7.3
« blib/lib/PDL/Slices.pm ↑ Summary blib/lib/PDL/IO/Dumper.pm »
1  
2 =head1 NAME
3  
4 PDL::Graphics::LUT - provides access to a number of look-up tables
5  
6 =head1 SYNOPSIS
7  
8 use PDL::Graphics::PGPLOT;
9 use PDL::Graphics::LUT;
10  
11 # what tables are available
12 my @tables = lut_names();
13  
14 # get the reversed colour table 'smooth',
15 # with the gamma intensity ramp
16 my ( $l, $r, $g, $b ) = lut_data( 'smooth', 1, 'gamma' );
17  
18 # use the table idl5 in ctab
19 ctab( lut_data('idl5') );
20  
21 =head1 DESCRIPTION
22  
23 PDL::Graphics::LUT contains a number of colour look-up tables
24 (in rgb format) and intensity ramps, and provides routines to
25 access this data.
26 The format of the data is suitable for use by
27 L<ctab|PDL::Graphics::PGPLOT::Window/ctab>.
28  
29 Unlike the initial release of the package, the data tables are
30 now stored within the PDL distribution as FITS files
31 (see L<$tabledir|/$tabledir> and L<$rampdir|/$rampdir>),
32 rather than in the module itself.
33 Changes to these directories will be picked up on the next call
34 to one of the package functions.
35  
36 =head1 FUNCTIONS
37  
38 =head2 lut_names()
39  
40 =for ref
41  
42 Return, as a list, the names of the available colour tables.
43  
44 =for usage
45  
46 @tables = lut_names();
47  
48 =head2 lut_ramps()
49  
50 =for ref
51  
52 Return, as a list, the names of the available intensity ramps.
53  
54 =for usage
55  
56 @ramps = lut_ramps();
57  
58 =head2 lut_data()
59  
60 =for ref
61  
62 Load in the requested colour table and intensity ramp.
63  
64 =for usage
65  
66 ( $l, $r, $g, $b ) = lut_data( $table, [ $reverse, [ $ramp ] ] );
67  
68 Returns the levels and r, g, b components of the colour table
69 C<$table>. If C<$reverse> is 1 (defaults to B<0>
70 if not supplied),
71 then the r, g, and b components are reversed before being
72 returned.
73 If not supplied, C<$ramp> defaults to B<"ramp">
74 (this is a linear intensity ramp).
75  
76 The returned values are piddles containing values in the range
77 0 to 1 inclusive, and are floats.
78  
79 =head1 VARIABLES
80  
81 =head2 $tabledir
82  
83 =for ref
84  
85 The directory in which the colour tables (in rgb format)
86 are stored.
87  
88 =head2 $rampdir
89  
90 =for ref
91  
92 The directory in which the intensity ramps are stored.
93  
94 =head2 $suffix
95  
96 =for ref
97  
98 The suffix for the data files in C<$tabledir> and
99 C<$rampdir>.
100  
101 =head1 FURTHER INFORMATION
102  
103 The colour tables were taken from the STARLINK GAIA package,
104 and are provided under the GNU copyleft.
105 See http://star-www.rl.ac.uk/ and
106 http://star-www.dur.ac.uk/~pdraper/ for more details.
107  
108 =head1 AUTHOR
109  
110 Doug Burke (djburke@cpan.org), with thanks to
111 Peter Draper/STARLINK for providing the colour-table data,
112 and Christian Soeller and Karl Glazebrook for their help.
113  
114 All rights reserved. There is no warranty. You are allowed
115 to redistribute this software / documentation under certain
116 conditions. For details, see the file COPYING in the PDL
117 distribution. If this file is separated from the PDL distribution,
118 the copyright notice should be included in the file.
119  
120 =cut
121  
122 package PDL::Graphics::LUT;
123  
124 # Just a plain function exporting package
125 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 # attempt to avoid Unix-specific file/directory names
128 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 1 ▶use PDL::Core qw/:Func :Internal/; # Grab the Core names
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1 ▶use PDL::Basic;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 1 ▶use PDL::Slices;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 ▶use PDL::IO::Misc;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 1 ▶use PDL::IO::FITS;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 # should be careful that $suffix is a valid length on non-Unix systems
141 1 $suffix = ".fits";
142 1 ▶use vars qw( $tabledir $rampdir $suffix );
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 # should really use EXPORT_OK
145 1 @EXPORT = qw( lut_names lut_ramps lut_data );
146 1 @EXPORT_OK = qw( $tabledir $rampdir $suffix );
147 1 @ISA = qw( Exporter );
148  
149 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 ############################################################################
152  
153 # can we find the data?
154 BEGIN {
155 1 ▶ my $d = File::Spec->catdir( "PDL", "Graphics", "LUT" );
Subroutine BEGIN: called CC=6 SCAR=18.1
156 1 my $lutdir = undef;
157 1 foreach my $path ( @INC ) {
158 2 my $check = File::Spec->catdir( $path, $d );
159 2 ▶ if ( -d $check ) { $lutdir = $check; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $check) 1 1 2
160 }
161 1 ▶ barf "Unable to find directory ${d} within the perl libraries.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lutdir 0 1 1
162 unless defined $lutdir;
163 1 $tabledir = File::Spec->catdir( $lutdir, "tables" );
164 1 $rampdir = File::Spec->catdir( $lutdir, "ramps" );
165 1 ▶ barf "Unable to find directory ${tabledir} within the perl libraries.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $PDL::Graphics::LUT::tabledir 0 1 1
166 unless -d $tabledir;
167 1 ▶ barf "Unable to find directory ${rampdir} within the perl libraries.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $PDL::Graphics::LUT::rampdir 0 1 1
168 unless -d $rampdir;
169 }
170  
171 ############################################################################
172  
173 sub _lsdir_basename {
174 2 ▶ my ($dir, $suffix) = @_;
Subroutine _lsdir_basename: called CC=1 SCAR=0.0
175 2 opendir my $fh, $dir;
176 2 map basename($_, $suffix), grep /\Q$suffix\E\z/, readdir $fh;
177 }
178  
179 # exported functions
180  
181 # Return the list of available tables
182 1 ▶sub lut_names () { _lsdir_basename $tabledir, $suffix }
Subroutine lut_names: called CC=1 SCAR=0.0
Pod documented
183  
184 # Return the list of available ramps
185 1 ▶sub lut_ramps () { _lsdir_basename $rampdir, $suffix }
Subroutine lut_ramps: called CC=1 SCAR=0.0
Pod documented
186  
187 # Return the requested colour table
188 sub lut_data ($;$$) {
189 3 ▶ my $table = shift;
Subroutine lut_data: called CC=12 SCAR=27.5
Pod documented
190 3 ▶ my $reverse = $#_ != -1 ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
$#_ != -1 ? : 2 1 3
191 3 ▶ my $ramp = $#_ != -1 ? shift : "ramp";
Branch: 1 branch
BranchTrueFalseTotal
$#_ != -1 ? : 1 2 3
192  
193 3 my $lfile = File::Spec->catfile( $tabledir, "${table}${suffix}" );
194 3 my $rfile = File::Spec->catfile( $rampdir, "${ramp}${suffix}" );
195 3 ▶ print "Reading colour table and intensity ramp from:\n $lfile\n $rfile\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 3 3
196 if $PDL::verbose;
197  
198 # unknown table?
199 3 ▶ unless ( -e $lfile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $lfile) 0 3 3
200 0 my @names = lut_names();
201 0 barf <<"EOD";
202 Unknown colour table $table
203 Available tables:
204 @names
205 EOD
206 }
207  
208 # unknown ramp?
209 3 ▶ unless ( -e $rfile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $rfile) 0 3 3
210 0 my @names = lut_ramps();
211 0 barf <<"EOD";
212 Unknown intensity ramp $ramp
213 Available ramps:
214 @names
215 EOD
216 }
217  
218 # read in rgb data
219 3 my $rgb = rfits $lfile;
220 3 ▶ $rgb = float($rgb) if $rgb->get_datatype != $PDL_F;
Branch: 1 branch
BranchTrueFalseTotal
if $rgb->get_datatype != $PDL::Graphics::LUT::PDL_F 0 3 3
221 3 my ( @ldims ) = $rgb->dims;
222 3 ▶ barf "LUT file $lfile is not the correct format (ie n by 3)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $#ldims == 1 and $ldims[1] == 3 0 3 3
Condition33%1/3
$#ldims == 1 and $ldims[1] == 3
!ll&&!rl&&r
003
MC/DC0%0/2
$#ldims == 1 and $ldims[1] == 3
$#ldims == 1 $ldims[1] == 3
Truth table33%1/3
A and B
A= $#ldims == 1B= $ldims[1] == 3
ABresult
0X0
100
111
223 unless $#ldims == 1 and $ldims[1] == 3;
224  
225 # read in intensity data
226 3 my $l = rfits $rfile;
227 3 ▶ $l = float($l) if $l->get_datatype != $PDL_F;
Branch: 1 branch
BranchTrueFalseTotal
if $l->get_datatype != $PDL::Graphics::LUT::PDL_F 0 3 3
228 3 ▶ barf "Ramp file $rfile does not match the colour table size.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $l->nelem == $ldims[0] 0 3 3
229 unless $l->nelem == $ldims[0];
230  
231 3 ▶ my $s = $reverse ? "-1:0" : "";
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 1 2 3
232 3 return ( $l, $rgb->slice("${s},(0)"), $rgb->slice("${s},(1)"), $rgb->slice("${s},(2)") );
233  
234 } # sub: lut_data()
235  
236 # Exit with OK status
237 1 1;
238  
« blib/lib/PDL/Slices.pm ↑ Summary blib/lib/PDL/IO/Dumper.pm »