PDL/Graphics/LUT.pm untested

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