1 |
#!/usr/bin/perl |
2 |
# RCSid $Id: rcode2bmp.pl,v 2.5 2024/10/03 18:35:53 greg Exp $ |
3 |
# |
4 |
# Convert one or more rtpict outputs into BMP for convenient viewing |
5 |
# |
6 |
|
7 |
use strict; |
8 |
use warnings; |
9 |
|
10 |
my $xres=0; |
11 |
my $yres=0; |
12 |
my $pfilt=""; |
13 |
|
14 |
# Get options, which must precede input maps |
15 |
while ($#ARGV >= 0 && "$ARGV[0]" =~ /^-[a-zA-Z]/) { |
16 |
if ("$ARGV[0]" eq '-x') { |
17 |
shift @ARGV; |
18 |
$xres = shift(@ARGV); |
19 |
} elsif ("$ARGV[0]" eq '-y') { |
20 |
shift @ARGV; |
21 |
$yres = shift(@ARGV); |
22 |
} else { |
23 |
die "Bad option: $ARGV[0]\n"; |
24 |
} |
25 |
} |
26 |
# Set up resizing operation if requested |
27 |
if ($xres > 0 && $yres > 0) { |
28 |
$pfilt = "pfilt -x $xres -y $yres -pa 1 -1 -r .6"; |
29 |
} |
30 |
# Load/convert each file |
31 |
INPUT_MAP: |
32 |
while ($#ARGV >= 0) { |
33 |
if ("$ARGV[0]" =~ /\.bmp$/i) { |
34 |
print STDERR "Skipping file '$ARGV[0]' already in BMP format\n"; |
35 |
shift @ARGV; |
36 |
next INPUT_MAP; |
37 |
} |
38 |
my $format = `getinfo < '$ARGV[0]' | sed -n 's/^FORMAT= *//p'`; |
39 |
chomp $format; |
40 |
die "Cannot get format from $ARGV[0]\n" if ( $? || ! $format ); |
41 |
my ($dest) = ("$ARGV[0]" =~ /^([^.]+)/); |
42 |
$dest .= ".bmp"; |
43 |
my $cmd=""; |
44 |
if ("$format" =~ /^32-bit_rle_(rgb|xyz)e *$/ || "$format" =~ /^Radiance_spectra *$/) { |
45 |
if ($pfilt) { |
46 |
$cmd = $pfilt . " '$ARGV[0]' | ra_bmp -e auto - '$dest'"; |
47 |
} else { |
48 |
$cmd = "ra_bmp -e auto '$ARGV[0]' '$dest'"; |
49 |
} |
50 |
} elsif ("$format" =~ /^16-bit_encoded_depth *$/) { |
51 |
$cmd = "rcode_depth -r -ff -ho -Ho '$ARGV[0]' "; |
52 |
$cmd .= q{| rcalc -if -of -e 'cond=9e9-$1;$1=$1' | total -if -u}; |
53 |
my $dmax=`$cmd`; |
54 |
$dmax = 2**(int(log($dmax)/log(2))+1); |
55 |
my $unit=`getinfo < '$ARGV[0]' | sed -n 's/^REFDEPTH= *[0-9.]*[^a-zA-Z]*//p'`; |
56 |
chomp $unit; |
57 |
$unit="Depth" if ( ! $unit ); |
58 |
$cmd = "rcode_depth -r -ff '$ARGV[0]' | pvalue -r -df -b "; |
59 |
$cmd .= "| $pfilt " if ($pfilt); |
60 |
$cmd .= "| falsecolor -l '$unit' -m 1 -s $dmax | ra_bmp - '$dest'"; |
61 |
} elsif ("$format" =~ /^[1-9][0-9]*-bit_indexed_name *$/) { |
62 |
$cmd = "rcode_ident -r -n '$ARGV[0]' " . |
63 |
"| getinfo +d -c rcalc -of -e 'cc(x):(.1+.8*rand(x))^2' " . |
64 |
q{-e '$1=cc(.398*$1-11.2);$2=cc(-1.152*$1+41.7);$3=cc(8.571*$1-8.15)' } . |
65 |
"| pvalue -r -df "; |
66 |
$cmd .= "| $pfilt " if ($pfilt); |
67 |
$cmd .= "| ra_bmp - '$dest'"; |
68 |
} elsif ("$format" =~ /^32-bit_encoded_normal *$/) { |
69 |
$cmd = "rcode_norm -r -ff '$ARGV[0]' | getinfo +d -c " . |
70 |
"rcalc -if3 -of -e `vwright v < '$ARGV[0]'` " . |
71 |
q{-e 'dot(vx,vy,vz)=vx*$1+vy*$2+vz*$3' } . |
72 |
"-e 'h=dot(vhx,vhy,vhz)' " . |
73 |
"-e 'v=dot(vvx,vvy,vvz)' " . |
74 |
"-e 'n=-dot(vdx,vdy,vdz)' " . |
75 |
"-f hsv_rgb.cal -e 'hue=mod(180/PI*atan2(v,h),360)' " . |
76 |
"-e 'sat=sqrt(h*h+v*v)' -e 'val=if(n,1,1+n)' " . |
77 |
q{-e '$1=R(hue,sat,val);$2=G(hue,sat,val);$3=B(hue,sat,val)' } . |
78 |
"| pvalue -r -df "; |
79 |
$cmd .= "| $pfilt " if ($pfilt); |
80 |
$cmd .= "| ra_bmp - '$dest'"; |
81 |
} |
82 |
if ($cmd) { |
83 |
system $cmd; |
84 |
die "Cannot convert $ARGV[0]\n" if ( $? ); |
85 |
print "Converted $ARGV[0] to $dest\n"; |
86 |
} else { |
87 |
print STDERR "Skipping unsupported format '$format' for input '$ARGV[0]'\n"; |
88 |
} |
89 |
shift @ARGV; |
90 |
} |
91 |
# EOF |