d2::combine::get_image_dynamic(): Use lrintf() instead of floor() and ceil().
[Ale.git] / ale
blob853df2398baa86b30722a86f9847ecf7fac8fa31
1 #!/usr/bin/perl -w
3 # ALE wrapper script
4 #
5 # Copyright (C) 2007 David Hilvert
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 %defaults = (
21 "ALE_BIN" => "ale-bin",
22 "DCRAW" => "dcraw",
23 "EXIF_UTILITY" => "exiftool",
26 %devices = (
27 "Canon EOS 5D" => "canon_300d",
28 "Canon EOS DIGITAL REBEL" => "canon_300d",
31 for $default (keys %defaults) {
32 if (!defined $ENV{$default}) {
33 $ENV{$default} = $defaults{$default};
38 # Perform metadata extraction and conversion
41 $started_conversion = 0;
42 undef $global_device;
43 for ($i = 0; $i < @ARGV; $i++) {
44 $arg = $ARGV[$i];
46 $i += 10 if $arg =~ /^-?-3dvp$/;
47 $i += 10 if $arg =~ /^-?-3dpp$/;
48 $i += 2 if $arg =~ /^-?-3dp$/;
49 $i += 2 if $arg =~ /^-?-3dv$/;
50 $i += 2 if $arg =~ /^-?-ochain$/;
51 $i += 3 if $arg =~ /^-?-wm$/;
52 $i += 3 if $arg =~ /^-?-wmx$/;
54 next if (!-e $arg);
56 if (defined $ENV{"EXIF_UTILITY"} && `which $ENV{"EXIF_UTILITY"}`) {
57 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^EV'` =~ /([0-9\.]+)/) {
58 $ev = $1;
59 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^ISO'` =~ /([0-9\.]+)/) {
60 $ev = $ev - log($1 / 100) / log(2);
62 @ARGV = (@ARGV[0..$i - 1], "--ev", "$ev", @ARGV[$i..$#ARGV]);
63 $i += 2;
67 if (defined $ENV{"DCRAW"} && `which $ENV{"DCRAW"}`) {
68 $device_info = `$ENV{"DCRAW"} -i $arg 2> /dev/null`;
70 next if ($?);
72 if (!$started_conversion) {
73 print "Extracting frame data";
74 $started_conversion = 1;
77 # Make a temporary directory for conversion
80 use File::Temp qw/ tempdir /;
82 $tempdir = tempdir("ale.XXXXXXXXX",
83 CLEANUP => 1,
84 DIR => File::Spec->tmpdir());
88 # Get device information
91 undef $device;
92 foreach $device_string (keys %devices) {
93 if ($device_info =~ /$device_string/) {
94 $device = $devices{$device_string};
95 last;
98 if (!defined $device) {
99 $device = "canon_300d";
103 # Currently, devices must be global.
106 if (defined $global_device && $global_device ne $device) {
107 die "Multiple device types not yet supported.\n";
108 } else {
109 $global_device = $device;
112 # @ARGV = (@ARGV[0..$i - 1], "--device", "$device", @ARGV[$i..$#ARGV]);
113 @ARGV = ("--device", "$device", @ARGV);
115 $i += 2;
118 # Convert
121 $converted_frame = $arg;
122 $converted_frame =~ s/(?:\.[^.]*)?$/.pgm/;
123 $converted_frame =~ s/.*\///;
124 $converted_frame = $tempdir . "/" . $converted_frame;
126 `$ENV{"DCRAW"} -t 0 -d -4 -k 0 -r 1 1 1 1 -c $arg > $converted_frame`;
128 $ARGV[$i] = $converted_frame;
131 # Try to get black level information
134 # XXX: The following approach is not obviously correct.
136 if (`$ENV{"DCRAW"} -d -v -c $arg 2>&1 1>/dev/null` =~ /black=(\d+)/) {
137 $black_unscaled = $1;
138 `$ENV{"DCRAW"} -D -4 -c $arg | pgmhist | tail -1` =~ /(\d+)/;
139 $maxval_unscaled = $1;
140 `pgmhist $converted_frame | tail -1` =~ /(\d+)/;
141 $maxval_scaled = $1;
143 $black_scaled = $black_unscaled
144 * ($maxval_scaled / $maxval_unscaled)
145 / 65535;
147 @ARGV = (@ARGV[0..$i - 1], "--black", "$black_scaled", @ARGV[$i..$#ARGV]);
148 $i += 2;
151 print ".";
155 if ($started_conversion) {
156 print "\n";
159 @ARGV = ($0, @ARGV);
160 system { $ENV{"ALE_BIN"} } @ARGV;