bugs: Advantages for incremental library separation by analogy with incremental
[Ale.git] / ale
blob76485ca8e7d63f7c30bc282b783dcc473ed146d2
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`);
23 # Environment variable defaults
26 %defaults = (
27 "ALE_BIN" => "$ale_exec_dir/ale-bin",
28 "DCRAW" => "dcraw",
29 "EXIF_UTILITY" => "exiftool",
30 "ALE_COUNT_THREADS" => "0",
31 "PAGER" => "/usr/bin/less",
34 %devices = (
35 "Canon EOS 5D" => "canon_300d",
36 "Canon EOS DIGITAL REBEL" => "canon_300d",
39 for $default (keys %defaults) {
40 if (!defined $ENV{$default}) {
41 $ENV{$default} = $defaults{$default};
46 # Wrap help page requests
49 if (!defined @ARGV || @ARGV == 0 || $ARGV[0] =~ /^--h[a-zA-Z0-9]$/) {
50 $ale_bin = $ENV{"ALE_BIN"};
53 # Find a pager
56 undef $pager;
57 if (-x "$ENV{PAGER}") {
58 $pager = $ENV{"PAGER"};
59 } elsif (`which $ENV{"PAGER"}`) {
60 $pager = `which $ENV{"PAGER"}`;
61 } elsif (`which pager`) {
62 $pager = `which pager`;
63 } elsif (`which less`) {
64 $pager = `which less`;
65 } elsif (`which more`) {
66 $pager = `which more`;
67 } elsif (-x '/usr/bin/less') {
68 $pager = '/usr/bin/less';
69 } elsif (-x '/bin/more') {
70 $pager = '/bin/more';
74 # Set pager options
77 $ENV{"LESS"} = "-X -F";
80 # Fetch help page
83 if (defined $pager) {
84 exec "bash -c 'exec -a $0 $ale_bin @ARGV | $pager'";
85 } else {
86 exec "bash -c 'exec -a $0 $ale_bin @ARGV'";
89 exit(0);
93 # Perform metadata extraction and conversion
96 $started_conversion = 0;
97 undef $global_device;
98 $extract_ev = 0;
99 $extract_raw = 0;
100 if ("@ARGV" =~ /exp-meta-only/ && defined $ENV{"EXIF_UTILITY"} && `which $ENV{"EXIF_UTILITY"}`) {
101 $extract_ev = 1;
103 if (defined $ENV{"DCRAW"} && `which $ENV{"DCRAW"}`) {
104 $extract_raw = 1;
106 for ($i = 0; $i < @ARGV; $i++) {
107 $arg = $ARGV[$i];
109 $i += 10 if $arg =~ /^-?-3dvp$/;
110 $i += 10 if $arg =~ /^-?-3dpp$/;
111 $i += 2 if $arg =~ /^-?-3dp$/;
112 $i += 2 if $arg =~ /^-?-3dv$/;
113 $i += 2 if $arg =~ /^-?-ochain$/;
114 $i += 3 if $arg =~ /^-?-wm$/;
115 $i += 3 if $arg =~ /^-?-wmx$/;
116 $i += 1 if $arg =~ /^-?-trans-load$/;
117 $i += 1 if $arg =~ /^-?-trans-save$/;
119 next if (!-e $arg);
121 if ($extract_ev) {
122 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^EV'` =~ /([0-9\.]+)/) {
123 $ev = $1;
124 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^ISO'` =~ /([0-9\.]+)/) {
125 $ev = $ev - log($1 / 100) / log(2);
127 @ARGV = (@ARGV[0..$i - 1], "--ev", "$ev", @ARGV[$i..$#ARGV]);
128 $i += 2;
132 if ($extract_raw) {
133 $device_info = `$ENV{"DCRAW"} -i $arg 2> /dev/null`;
135 if ($?) {
137 # For efficiency, stop trying to extract raws if the first
138 # attempt fails.
140 $extract_raw = 0;
142 next;
145 if (!$started_conversion) {
146 print "Extracting frame data";
147 $started_conversion = 1;
150 # Make a temporary directory for conversion
153 use File::Temp qw/ tempdir /;
155 $tempdir = tempdir("ale.XXXXXXXXX",
156 CLEANUP => 1,
157 DIR => File::Spec->tmpdir());
161 # Get device information
164 undef $device;
165 foreach $device_string (keys %devices) {
166 if ($device_info =~ /$device_string/) {
167 $device = $devices{$device_string};
168 last;
171 if (!defined $device) {
172 $device = "canon_300d";
176 # Currently, devices must be global.
179 if (defined $global_device && $global_device ne $device) {
180 die "Multiple device types not yet supported.\n";
181 } else {
182 $global_device = $device;
185 # @ARGV = (@ARGV[0..$i - 1], "--device", "$device", @ARGV[$i..$#ARGV]);
186 @ARGV = ("--device", "$device", @ARGV);
188 $i += 2;
191 # Convert
194 $converted_frame = $arg;
195 $converted_frame =~ s/(?:\.[^.]*)?$/.pgm/;
196 $converted_frame =~ s/.*\///;
197 $converted_frame = $tempdir . "/" . $converted_frame;
199 `$ENV{"DCRAW"} -t 0 -d -4 -k 0 -r 1 1 1 1 -c $arg > $converted_frame`;
201 $ARGV[$i] = $converted_frame;
204 # Try to get black level information
207 # XXX: The following approach is not obviously correct.
209 if (`$ENV{"DCRAW"} -d -v -c $arg 2>&1 1>/dev/null` =~ /black=(\d+)/) {
210 $black_unscaled = $1;
211 `$ENV{"DCRAW"} -D -4 -c $arg | pgmhist | tail -1` =~ /(\d+)/;
212 $maxval_unscaled = $1;
213 `pgmhist $converted_frame | tail -1` =~ /(\d+)/;
214 $maxval_scaled = $1;
216 $black_scaled = $black_unscaled
217 * ($maxval_scaled / $maxval_unscaled)
218 / 65535;
220 @ARGV = (@ARGV[0..$i - 1], "--black", "$black_scaled", @ARGV[$i..$#ARGV]);
221 $i += 2;
224 print ".";
228 if ($started_conversion) {
229 print "\n";
232 @ARGV = ($0, @ARGV);
234 if (!$extract_raw && $ENV{"ALE_COUNT_THREADS"} == 0) {
235 exec { $ENV{"ALE_BIN"} } @ARGV;
236 } elsif ($ENV{"ALE_COUNT_THREADS"} == 0) {
237 system { $ENV{"ALE_BIN"} } @ARGV;
238 } else {
239 $| = 1;
240 $fork1 = fork();
241 if ($fork1 == 0) {
242 exec { $ENV{"ALE_BIN"} } @ARGV;
245 if (fork() == 0) {
246 $status_file = "/proc/$fork1/status";
247 while (-r $status_file && `cat $status_file 2> /dev/null | grep Threads` =~ /(\d+)/) {
248 if (defined $threads{$1}) {
249 $threads{$1}++;
250 } else {
251 $threads{$1} = 1;
253 sleep 1;
255 foreach $count (sort keys %threads) {
256 print("$count thread(s): $threads{$count} tick(s)\n");
258 exit(0);
261 while (wait() != -1) {}