d2::align::_align(): Limit skipping of the alignment process based on
[Ale.git] / ale
blob4bc6517a9796457a59760460bda121ffd4ffa72e
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",
29 %devices = (
30 "Canon EOS 5D" => "canon_300d",
31 "Canon EOS DIGITAL REBEL" => "canon_300d",
34 for $default (keys %defaults) {
35 if (!defined $ENV{$default}) {
36 $ENV{$default} = $defaults{$default};
41 # Perform metadata extraction and conversion
44 $started_conversion = 0;
45 undef $global_device;
46 for ($i = 0; $i < @ARGV; $i++) {
47 $arg = $ARGV[$i];
49 $i += 10 if $arg =~ /^-?-3dvp$/;
50 $i += 10 if $arg =~ /^-?-3dpp$/;
51 $i += 2 if $arg =~ /^-?-3dp$/;
52 $i += 2 if $arg =~ /^-?-3dv$/;
53 $i += 2 if $arg =~ /^-?-ochain$/;
54 $i += 3 if $arg =~ /^-?-wm$/;
55 $i += 3 if $arg =~ /^-?-wmx$/;
57 next if (!-e $arg);
59 if (defined $ENV{"EXIF_UTILITY"} && `which $ENV{"EXIF_UTILITY"}`) {
60 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^EV'` =~ /([0-9\.]+)/) {
61 $ev = $1;
62 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^ISO'` =~ /([0-9\.]+)/) {
63 $ev = $ev - log($1 / 100) / log(2);
65 @ARGV = (@ARGV[0..$i - 1], "--ev", "$ev", @ARGV[$i..$#ARGV]);
66 $i += 2;
70 if (defined $ENV{"DCRAW"} && `which $ENV{"DCRAW"}`) {
71 $device_info = `$ENV{"DCRAW"} -i $arg 2> /dev/null`;
73 next if ($?);
75 if (!$started_conversion) {
76 print "Extracting frame data";
77 $started_conversion = 1;
80 # Make a temporary directory for conversion
83 use File::Temp qw/ tempdir /;
85 $tempdir = tempdir("ale.XXXXXXXXX",
86 CLEANUP => 1,
87 DIR => File::Spec->tmpdir());
91 # Get device information
94 undef $device;
95 foreach $device_string (keys %devices) {
96 if ($device_info =~ /$device_string/) {
97 $device = $devices{$device_string};
98 last;
101 if (!defined $device) {
102 $device = "canon_300d";
106 # Currently, devices must be global.
109 if (defined $global_device && $global_device ne $device) {
110 die "Multiple device types not yet supported.\n";
111 } else {
112 $global_device = $device;
115 # @ARGV = (@ARGV[0..$i - 1], "--device", "$device", @ARGV[$i..$#ARGV]);
116 @ARGV = ("--device", "$device", @ARGV);
118 $i += 2;
121 # Convert
124 $converted_frame = $arg;
125 $converted_frame =~ s/(?:\.[^.]*)?$/.pgm/;
126 $converted_frame =~ s/.*\///;
127 $converted_frame = $tempdir . "/" . $converted_frame;
129 `$ENV{"DCRAW"} -t 0 -d -4 -k 0 -r 1 1 1 1 -c $arg > $converted_frame`;
131 $ARGV[$i] = $converted_frame;
134 # Try to get black level information
137 # XXX: The following approach is not obviously correct.
139 if (`$ENV{"DCRAW"} -d -v -c $arg 2>&1 1>/dev/null` =~ /black=(\d+)/) {
140 $black_unscaled = $1;
141 `$ENV{"DCRAW"} -D -4 -c $arg | pgmhist | tail -1` =~ /(\d+)/;
142 $maxval_unscaled = $1;
143 `pgmhist $converted_frame | tail -1` =~ /(\d+)/;
144 $maxval_scaled = $1;
146 $black_scaled = $black_unscaled
147 * ($maxval_scaled / $maxval_unscaled)
148 / 65535;
150 @ARGV = (@ARGV[0..$i - 1], "--black", "$black_scaled", @ARGV[$i..$#ARGV]);
151 $i += 2;
154 print ".";
158 if ($started_conversion) {
159 print "\n";
162 @ARGV = ($0, @ARGV);
164 if ($ENV{"ALE_COUNT_THREADS"} == 0) {
165 system { $ENV{"ALE_BIN"} } @ARGV;
166 } else {
167 $| = 1;
168 $fork1 = fork();
169 if ($fork1 == 0) {
170 exec { $ENV{"ALE_BIN"} } @ARGV;
173 if (fork() == 0) {
174 $status_file = "/proc/$fork1/status";
175 while (-r $status_file && `cat $status_file 2> /dev/null | grep Threads` =~ /(\d+)/) {
176 if (defined $threads{$1}) {
177 $threads{$1}++;
178 } else {
179 $threads{$1} = 1;
181 sleep 1;
183 foreach $count (sort keys %threads) {
184 print("$count thread(s): $threads{$count} tick(s)\n");
186 exit(0);
189 while (wait() != -1) {}