| 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 ([email protected]), 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::LUTscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/; |
| 122 | |
| 123 | use strictscip-perl cpan PGPLOT 2.36 strict/; |
| 124 | use warningsscip-perl cpan PGPLOT 2.36 warnings/; |
| 125 | # Just a plain function exporting package |
| 126 | use Exporterscip-perl cpan PGPLOT 2.36 Exporter/; |
| 127 | use Carpscip-perl cpan PGPLOT 2.36 Carp/; |
| 128 | |
| 129 | # attempt to avoid Unix-specific file/directory names |
| 130 | use File::Specscip-perl cpan PGPLOT 2.36 File/Spec/; |
| 131 | use File::Basenamescip-perl cpan PGPLOT 2.36 File/Basename/; |
| 132 | |
| 133 | use PDL::Corescip-perl cpan PGPLOT 2.36 PDL/Core/ qw/:Func :Internal/; # Grab the Core names |
| 134 | use PDL::Basicscip-perl cpan PGPLOT 2.36 PDL/Basic/; |
| 135 | use PDL::Typesscip-perl cpan PGPLOT 2.36 PDL/Types/; |
| 136 | use PDL::Slicesscip-perl cpan PGPLOT 2.36 PDL/Slices/; |
| 137 | use PDL::IO::Miscscip-perl cpan PGPLOT 2.36 PDL/IO/Misc/; |
| 138 | use PDL::IO::FITSscip-perl cpan PGPLOT 2.36 PDL/IO/FITS/; |
| 139 | |
| 140 | # should be careful that $suffix is a valid length on non-Unix systems |
| 141 | our ($suffixscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$suffix., $tabledirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$tabledir., $rampdirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$rampdir.) = ".fits"; |
| 142 | |
| 143 | # should really use EXPORT_OK |
| 144 | our @EXPORTscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`@EXPORT`. = qw( lut_names lut_ramps lut_data ); |
| 145 | our @EXPORT_OKscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`@EXPORT_OK`. = qw( $tabledir $rampdir $suffix ); |
| 146 | our @ISAscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`@ISA`. = qw( Exporter ); |
| 147 | |
| 148 | # can we find the data? |
| 149 | my $d = File::Spec->catdirscip-perl cpan PGPLOT 2.36 File/Spec/catdir().( "PDL", "Graphics", "LUT" ); |
| 150 | my $lutdir = undef; |
| 151 | foreach my $path ( @INCscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`@INC`. ) { |
| 152 | my $check = File::Spec->catdirscip-perl cpan PGPLOT 2.36 File/Spec/catdir().( $path, $d ); |
| 153 | if ( -d $check ) { $lutdir = $check; last; } |
| 154 | } |
| 155 | barfscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/barf(). "Unable to find directory ${d} within the perl libraries.\n" |
| 156 | unless definedscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/defined(). $lutdir; |
| 157 | $tabledirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$tabledir. = File::Spec->catdirscip-perl cpan PGPLOT 2.36 File/Spec/catdir().( $lutdir, "tables" ); |
| 158 | $rampdirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$rampdir. = File::Spec->catdirscip-perl cpan PGPLOT 2.36 File/Spec/catdir().( $lutdir, "ramps" ); |
| 159 | barfscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/barf(). "Unable to find directory $tabledir within the perl libraries.\n" |
| 160 | unless -d $tabledirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$tabledir.; |
| 161 | barfscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/barf(). "Unable to find directory $rampdir within the perl libraries.\n" |
| 162 | unless -d $rampdirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$rampdir.; |
| 163 | |
| 164 | ############################################################################ |
| 165 | |
| 166 | sub _lsdir_basenamescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/_lsdir_basename(). { |
| 167 | my ($dir, $suffix) = @_scip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`@_`.; |
| 168 | confessscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/confess(). "called with undef dir" if !definedscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/defined(). $dir; |
| 169 | opendirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/opendir(). my $fh, $dir or barfscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/barf(). "$dir: $!"; |
| 170 | mapscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/map(). basenamescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/basename().($_scip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$_., $suffix), grepscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/grep(). /\Q$suffix\E\z/, readdirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/readdir(). $fh; |
| 171 | } |
| 172 | |
| 173 | # exported functions |
| 174 | |
| 175 | # Return the list of available tables |
| 176 | sub lut_namesscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/lut_names(). () { _lsdir_basenamescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/_lsdir_basename(). $tabledirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$tabledir., $suffixscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$suffix. } |
| 177 | |
| 178 | # Return the list of available ramps |
| 179 | sub lut_rampsscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/lut_ramps(). () { _lsdir_basenamescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/_lsdir_basename(). $rampdirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$rampdir., $suffixscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$suffix. } |
| 180 | |
| 181 | # Return the requested colour table |
| 182 | sub lut_datascip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/lut_data(). ($;$$) { |
| 183 | my $table = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/shift().; |
| 184 | my $reverse = $#_scip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`$#_`. != -1 ? shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/shift(). : 0; |
| 185 | my $ramp = $#_scip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`$#_`. != -1 ? shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/shift(). : "ramp"; |
| 186 | |
| 187 | my $lfile = File::Spec->catfilescip-perl cpan PGPLOT 2.36 File/Spec/catfile().( $tabledirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$tabledir., "$table$suffix" ); |
| 188 | my $rfile = File::Spec->catfilescip-perl cpan PGPLOT 2.36 File/Spec/catfile().( $rampdirscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$rampdir., "$ramp$suffix" ); |
| 189 | printscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/print(). "Reading colour table and intensity ramp from:\n $lfile\n $rfile\n" |
| 190 | if $PDL::verbosescip-perl cpan PGPLOT 2.36 PDL/$verbose.; |
| 191 | |
| 192 | # unknown table? |
| 193 | unless ( -e $lfile ) { |
| 194 | my @names = lut_namesscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/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_rampsscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/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 = rfitsscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/rfits(). $lfile; |
| 214 | $rgb = floatscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/float().($rgb) if $rgb->get_datatypescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/get_datatype(). != $PDL_Fscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$PDL_F.; |
| 215 | my ( @ldims ) = $rgb->dimsscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/dims().; |
| 216 | barfscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/barf(). "LUT file $lfile is not the correct format (ie n by 3)\n" |
| 217 | unless $#ldimsscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/`$#ldims`. == 1 and $ldims[1] == 3; |
| 218 | |
| 219 | # read in intensity data |
| 220 | my $l = rfitsscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/rfits(). $rfile; |
| 221 | $l = floatscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/float().($l) if $l->get_datatypescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/get_datatype(). != $PDL_Fscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/$PDL_F.; |
| 222 | barfscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/barf(). "Ramp file $rfile does not match the colour table size.\n" |
| 223 | unless $l->nelemscip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/nelem(). == $ldims[0]; |
| 224 | |
| 225 | my $s = $reverse ? "-1:0" : ""; |
| 226 | return ( $l, $rgb->slicescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/slice().("${s},(0)"), $rgb->slicescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/slice().("${s},(1)"), $rgb->slicescip-perl cpan PGPLOT 2.36 PDL/Graphics/LUT/slice().("${s},(2)") ); |
| 227 | |
| 228 | } # sub: lut_data() |
| 229 | |
| 230 | # Exit with OK status |
| 231 | 1; |