File Coverage

blib/lib/PDL/Graphics/LUT.pm
Criterion Covered Total %
statement 70 74 94.5
branch 18 28 64.2
condition 1 3 33.3
subroutine 17 17 100.0
pod 3 3 100.0
total 109 125 87.2


line stmt bran cond sub pod time code
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.
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 and L),
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             my ( $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     1   640 use Exporter;
  1         2  
  1         43  
126              
127             # attempt to avoid Unix-specific file/directory names
128 1     1   5 use File::Spec;
  1         2  
  1         19  
129 1     1   4 use File::Basename;
  1         2  
  1         110  
130              
131 1     1   604 use autodie;
  1         16406  
  1         4  
132              
133 1     1   6918 use PDL::Core qw/:Func :Internal/; # Grab the Core names
  1         3  
  1         12  
134 1     1   8 use PDL::Basic;
  1         2  
  1         10  
135 1     1   8 use PDL::Types;
  1         3  
  1         141  
136 1     1   7 use PDL::Slices;
  1         2  
  1         8  
137 1     1   682 use PDL::IO::Misc;
  1         2  
  1         7  
138 1     1   729 use PDL::IO::FITS;
  1         4  
  1         10  
139              
140             # should be careful that $suffix is a valid length on non-Unix systems
141             $suffix = ".fits";
142 1     1   8 use vars qw( $tabledir $rampdir $suffix );
  1         3  
  1         92  
143              
144             # should really use EXPORT_OK
145             @EXPORT = qw( lut_names lut_ramps lut_data );
146             @EXPORT_OK = qw( $tabledir $rampdir $suffix );
147             @ISA = qw( Exporter );
148              
149 1     1   8 use strict;
  1         2  
  1         209  
150              
151             ############################################################################
152              
153             # can we find the data?
154             BEGIN {
155 1     1   18 my $d = File::Spec->catdir( "PDL", "Graphics", "LUT" );
156 1         3 my $lutdir = undef;
157 1         2 foreach my $path ( @INC ) {
158 2         13 my $check = File::Spec->catdir( $path, $d );
159 2 100       43 if ( -d $check ) { $lutdir = $check; last; }
  1         4  
  1         2  
160             }
161 1 50       16 barf "Unable to find directory ${d} within the perl libraries.\n"
162             unless defined $lutdir;
163 1         15 $tabledir = File::Spec->catdir( $lutdir, "tables" );
164 1         7 $rampdir = File::Spec->catdir( $lutdir, "ramps" );
165 1 50       19 barf "Unable to find directory ${tabledir} within the perl libraries.\n"
166             unless -d $tabledir;
167 1 50       664 barf "Unable to find directory ${rampdir} within the perl libraries.\n"
168             unless -d $rampdir;
169             }
170              
171             ############################################################################
172              
173             sub _lsdir_basename {
174 2     2   9 my ($dir, $suffix) = @_;
175 2         12 opendir my $fh, $dir;
176 2         2777 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     1 1 88 sub lut_names () { _lsdir_basename $tabledir, $suffix }
183              
184             # Return the list of available ramps
185 1     1 1 5 sub lut_ramps () { _lsdir_basename $rampdir, $suffix }
186              
187             # Return the requested colour table
188             sub lut_data ($;$$) {
189 3     3 1 3157 my $table = shift;
190 3 100       12 my $reverse = $#_ != -1 ? shift : 0;
191 3 100       12 my $ramp = $#_ != -1 ? shift : "ramp";
192              
193 3         52 my $lfile = File::Spec->catfile( $tabledir, "${table}${suffix}" );
194 3         23 my $rfile = File::Spec->catfile( $rampdir, "${ramp}${suffix}" );
195 3 50       12 print "Reading colour table and intensity ramp from:\n $lfile\n $rfile\n"
196             if $PDL::verbose;
197              
198             # unknown table?
199 3 50       77 unless ( -e $lfile ) {
200 0         0 my @names = lut_names();
201 0         0 barf <<"EOD";
202             Unknown colour table $table
203             Available tables:
204             @names
205             EOD
206             }
207              
208             # unknown ramp?
209 3 50       52 unless ( -e $rfile ) {
210 0         0 my @names = lut_ramps();
211 0         0 barf <<"EOD";
212             Unknown intensity ramp $ramp
213             Available ramps:
214             @names
215             EOD
216             }
217              
218             # read in rgb data
219 3         13 my $rgb = rfits $lfile;
220 3 50       18 $rgb = float($rgb) if $rgb->get_datatype != $PDL_F;
221 3         12 my ( @ldims ) = $rgb->dims;
222 3 50 33     16 barf "LUT file $lfile is not the correct format (ie n by 3)\n"
223             unless $#ldims == 1 and $ldims[1] == 3;
224              
225             # read in intensity data
226 3         10 my $l = rfits $rfile;
227 3 50       19 $l = float($l) if $l->get_datatype != $PDL_F;
228 3 50       15 barf "Ramp file $rfile does not match the colour table size.\n"
229             unless $l->nelem == $ldims[0];
230              
231 3 100       12 my $s = $reverse ? "-1:0" : "";
232 3         16 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;
238