Possible fix for use of -a bashism.
[Ale.git] / ale
blob4e8322bf270ba07f07644e4b10412f14fd302ca8
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 chomp ($ale_exec_dir = `dirname $0`);
22 %defaults = (
23 "ALE_BIN" => "$ale_exec_dir/ale-bin",
24 "DCRAW" => "dcraw",
25 "EXIF_UTILITY" => "exiftool",
26 "ALE_COUNT_THREADS" => "0",
27 "PAGER" => "/usr/bin/less",
30 %devices = (
31 "Canon EOS 5D" => "canon_300d",
32 "Canon EOS DIGITAL REBEL" => "canon_300d",
35 for $default (keys %defaults) {
36 if (!defined $ENV{$default}) {
37 $ENV{$default} = $defaults{$default};
42 # Wrap help page requests
45 if (!defined @ARGV || @ARGV == 0 || $ARGV[0] =~ /^--h[a-zA-Z0-9]$/) {
46 $ale_bin = $ENV{"ALE_BIN"};
49 # Find a pager
52 undef $pager;
53 if (-x "$ENV{PAGER}") {
54 $pager = $ENV{"PAGER"};
55 } elsif (`which $ENV{"PAGER"}`) {
56 $pager = `which $ENV{"PAGER"}`;
57 } elsif (`which pager`) {
58 $pager = `which pager`;
59 } elsif (`which less`) {
60 $pager = `which less`;
61 } elsif (`which more`) {
62 $pager = `which more`;
63 } elsif (-x '/usr/bin/less') {
64 $pager = '/usr/bin/less';
65 } elsif (-x '/bin/more') {
66 $pager = '/bin/more';
70 # Set pager options
73 $ENV{"LESS"} = "-X -F";
76 # Fetch help page
79 if (defined $pager) {
80 exec "/bin/bash -c 'exec -a $0 $ale_bin @ARGV | $pager'";
81 } else {
82 exec "/bin/bash -c 'exec -a $0 $ale_bin @ARGV'";
85 exit(0);
89 # Perform metadata extraction and conversion
92 $started_conversion = 0;
93 undef $global_device;
94 for ($i = 0; $i < @ARGV; $i++) {
95 $arg = $ARGV[$i];
97 $i += 10 if $arg =~ /^-?-3dvp$/;
98 $i += 10 if $arg =~ /^-?-3dpp$/;
99 $i += 2 if $arg =~ /^-?-3dp$/;
100 $i += 2 if $arg =~ /^-?-3dv$/;
101 $i += 2 if $arg =~ /^-?-ochain$/;
102 $i += 3 if $arg =~ /^-?-wm$/;
103 $i += 3 if $arg =~ /^-?-wmx$/;
104 $i += 1 if $arg =~ /^-?-trans-load$/;
105 $i += 1 if $arg =~ /^-?-trans-save$/;
107 next if (!-e $arg);
109 if (defined $ENV{"EXIF_UTILITY"} && `which $ENV{"EXIF_UTILITY"}`) {
110 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^EV'` =~ /([0-9\.]+)/) {
111 $ev = $1;
112 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^ISO'` =~ /([0-9\.]+)/) {
113 $ev = $ev - log($1 / 100) / log(2);
115 @ARGV = (@ARGV[0..$i - 1], "--ev", "$ev", @ARGV[$i..$#ARGV]);
116 $i += 2;
120 if (defined $ENV{"DCRAW"} && `which $ENV{"DCRAW"}`) {
121 $device_info = `$ENV{"DCRAW"} -i $arg 2> /dev/null`;
123 next if ($?);
125 if (!$started_conversion) {
126 print "Extracting frame data";
127 $started_conversion = 1;
130 # Make a temporary directory for conversion
133 use File::Temp qw/ tempdir /;
135 $tempdir = tempdir("ale.XXXXXXXXX",
136 CLEANUP => 1,
137 DIR => File::Spec->tmpdir());
141 # Get device information
144 undef $device;
145 foreach $device_string (keys %devices) {
146 if ($device_info =~ /$device_string/) {
147 $device = $devices{$device_string};
148 last;
151 if (!defined $device) {
152 $device = "canon_300d";
156 # Currently, devices must be global.
159 if (defined $global_device && $global_device ne $device) {
160 die "Multiple device types not yet supported.\n";
161 } else {
162 $global_device = $device;
165 # @ARGV = (@ARGV[0..$i - 1], "--device", "$device", @ARGV[$i..$#ARGV]);
166 @ARGV = ("--device", "$device", @ARGV);
168 $i += 2;
171 # Convert
174 $converted_frame = $arg;
175 $converted_frame =~ s/(?:\.[^.]*)?$/.pgm/;
176 $converted_frame =~ s/.*\///;
177 $converted_frame = $tempdir . "/" . $converted_frame;
179 `$ENV{"DCRAW"} -t 0 -d -4 -k 0 -r 1 1 1 1 -c $arg > $converted_frame`;
181 $ARGV[$i] = $converted_frame;
184 # Try to get black level information
187 # XXX: The following approach is not obviously correct.
189 if (`$ENV{"DCRAW"} -d -v -c $arg 2>&1 1>/dev/null` =~ /black=(\d+)/) {
190 $black_unscaled = $1;
191 `$ENV{"DCRAW"} -D -4 -c $arg | pgmhist | tail -1` =~ /(\d+)/;
192 $maxval_unscaled = $1;
193 `pgmhist $converted_frame | tail -1` =~ /(\d+)/;
194 $maxval_scaled = $1;
196 $black_scaled = $black_unscaled
197 * ($maxval_scaled / $maxval_unscaled)
198 / 65535;
200 @ARGV = (@ARGV[0..$i - 1], "--black", "$black_scaled", @ARGV[$i..$#ARGV]);
201 $i += 2;
204 print ".";
208 if ($started_conversion) {
209 print "\n";
212 @ARGV = ($0, @ARGV);
214 if ($ENV{"ALE_COUNT_THREADS"} == 0) {
215 system { $ENV{"ALE_BIN"} } @ARGV;
216 } else {
217 $| = 1;
218 $fork1 = fork();
219 if ($fork1 == 0) {
220 exec { $ENV{"ALE_BIN"} } @ARGV;
223 if (fork() == 0) {
224 $status_file = "/proc/$fork1/status";
225 while (-r $status_file && `cat $status_file 2> /dev/null | grep Threads` =~ /(\d+)/) {
226 if (defined $threads{$1}) {
227 $threads{$1}++;
228 } else {
229 $threads{$1} = 1;
231 sleep 1;
233 foreach $count (sort keys %threads) {
234 print("$count thread(s): $threads{$count} tick(s)\n");
236 exit(0);
239 while (wait() != -1) {}