Initialize ditz distributed issue tracker.
[Ale.git] / ale
blobd2487ed6b7f990f7446e90b16ff895274222f9b9
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 # Detect software GPU
26 if (!defined $ENV{"ALE_SOFT_GPU"} && `which glxinfo` && `glxinfo 2> /dev/null | grep "direct rendering: No"`) {
27 $ENV{"ALE_SOFT_GPU"} = "1";
28 } elsif (!defined $ENV{"ALE_SOFT_GPU"}) {
29 $ENV{"ALE_SOFT_GPU"} = "0";
33 # Determine whether to use GPU acceleration by default
36 if (!defined $ENV{"ALE_GPU_ACCEL_DEFAULT"}
37 && ($ENV{"ALE_SOFT_GPU"}
38 || !`glxinfo 2> /dev/null`
39 || !`glxinfo 2> /dev/null | grep GL_ARB_fragment_program`
40 || !`glxinfo 2> /dev/null | grep GL_ARB_fragment_shader`
41 || !`glxinfo 2> /dev/null | grep GL_EXT_framebuffer_object`
42 || !`glxinfo 2> /dev/null | grep GL_ARB_texture_float`
43 || !`glxinfo 2> /dev/null | grep GL_ARB_texture_rectangle`)) {
44 $ENV{"ALE_GPU_ACCEL_DEFAULT"} = 0;
45 } elsif (!defined $ENV{"ALE_GPU_ACCEL_DEFAULT"}) {
46 $ENV{"ALE_GPU_ACCEL_DEFAULT"} = 1;
49 # Disable acceleration by default for now, as it doesn't work yet.
51 $ENV{"ALE_GPU_ACCEL_DEFAULT"} = 0;
54 # Environment variable defaults
57 %defaults = (
58 "ALE_BIN" => "$ale_exec_dir/ale-bin",
59 "DCRAW" => "dcraw",
60 "EXIF_UTILITY" => "exiftool",
61 "ALE_COUNT_THREADS" => "0",
62 "PAGER" => "/usr/bin/less",
65 %devices = (
66 "Canon EOS 5D" => "canon_300d",
67 "Canon EOS DIGITAL REBEL" => "canon_300d",
70 for $default (keys %defaults) {
71 if (!defined $ENV{$default}) {
72 $ENV{$default} = $defaults{$default};
77 # Wrap help page requests
80 if (!defined @ARGV || @ARGV == 0 || $ARGV[0] =~ /^--h[a-zA-Z0-9]$/) {
81 $ale_bin = $ENV{"ALE_BIN"};
84 # Find a pager
87 undef $pager;
88 if (-x "$ENV{PAGER}") {
89 $pager = $ENV{"PAGER"};
90 } elsif (`which $ENV{"PAGER"}`) {
91 $pager = `which $ENV{"PAGER"}`;
92 } elsif (`which pager`) {
93 $pager = `which pager`;
94 } elsif (`which less`) {
95 $pager = `which less`;
96 } elsif (`which more`) {
97 $pager = `which more`;
98 } elsif (-x '/usr/bin/less') {
99 $pager = '/usr/bin/less';
100 } elsif (-x '/bin/more') {
101 $pager = '/bin/more';
105 # Set pager options
108 $ENV{"LESS"} = "-X -F";
111 # Fetch help page
114 if (defined $pager) {
115 exec "bash -c 'exec -a $0 $ale_bin @ARGV | $pager'";
116 } else {
117 exec "bash -c 'exec -a $0 $ale_bin @ARGV'";
120 exit(0);
124 # Perform metadata extraction and conversion
127 $started_conversion = 0;
128 undef $global_device;
129 $extract_ev = 0;
130 $extract_raw = 0;
131 if ("@ARGV" =~ /exp-meta-only/ && defined $ENV{"EXIF_UTILITY"} && `which $ENV{"EXIF_UTILITY"}`) {
132 $extract_ev = 1;
134 if (defined $ENV{"DCRAW"} && `which $ENV{"DCRAW"}`) {
135 $extract_raw = 1;
137 for ($i = 0; $i < @ARGV; $i++) {
138 $arg = $ARGV[$i];
140 $i += 10 if $arg =~ /^-?-3dvp$/;
141 $i += 10 if $arg =~ /^-?-3dpp$/;
142 $i += 2 if $arg =~ /^-?-3dp$/;
143 $i += 2 if $arg =~ /^-?-3dv$/;
144 $i += 2 if $arg =~ /^-?-ochain$/;
145 $i += 3 if $arg =~ /^-?-wm$/;
146 $i += 3 if $arg =~ /^-?-wmx$/;
147 $i += 1 if $arg =~ /^-?-trans-load$/;
148 $i += 1 if $arg =~ /^-?-trans-save$/;
150 next if (!-e $arg);
152 if ($extract_ev) {
153 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^EV'` =~ /([0-9\.]+)/) {
154 $ev = $1;
155 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^ISO'` =~ /([0-9\.]+)/) {
156 $ev = $ev - log($1 / 100) / log(2);
158 @ARGV = (@ARGV[0..$i - 1], "--ev", "$ev", @ARGV[$i..$#ARGV]);
159 $i += 2;
163 if ($extract_raw) {
164 $device_info = `$ENV{"DCRAW"} -i $arg 2> /dev/null`;
166 if ($?) {
168 # For efficiency, stop trying to extract raws if the first
169 # attempt fails.
171 $extract_raw = 0;
173 next;
176 if (!$started_conversion) {
177 print "Extracting frame data";
178 $started_conversion = 1;
181 # Make a temporary directory for conversion
184 use File::Temp qw/ tempdir /;
186 $tempdir = tempdir("ale.XXXXXXXXX",
187 CLEANUP => 1,
188 DIR => File::Spec->tmpdir());
192 # Get device information
195 undef $device;
196 foreach $device_string (keys %devices) {
197 if ($device_info =~ /$device_string/) {
198 $device = $devices{$device_string};
199 last;
202 if (!defined $device) {
203 $device = "canon_300d";
207 # Currently, devices must be global.
210 if (defined $global_device && $global_device ne $device) {
211 die "Multiple device types not yet supported.\n";
212 } else {
213 $global_device = $device;
216 # @ARGV = (@ARGV[0..$i - 1], "--device", "$device", @ARGV[$i..$#ARGV]);
217 @ARGV = ("--device", "$device", @ARGV);
219 $i += 2;
222 # Convert
225 $converted_frame = $arg;
226 $converted_frame =~ s/(?:\.[^.]*)?$/.pgm/;
227 $converted_frame =~ s/.*\///;
228 $converted_frame = $tempdir . "/" . $converted_frame;
230 `$ENV{"DCRAW"} -t 0 -d -4 -k 0 -r 1 1 1 1 -c $arg > $converted_frame`;
232 $ARGV[$i] = $converted_frame;
235 # Try to get black level information
238 # XXX: The following approach is not obviously correct.
240 if (`$ENV{"DCRAW"} -d -v -c $arg 2>&1 1>/dev/null` =~ /black=(\d+)/) {
241 $black_unscaled = $1;
242 `$ENV{"DCRAW"} -D -4 -c $arg | pgmhist | tail -1` =~ /(\d+)/;
243 $maxval_unscaled = $1;
244 `pgmhist $converted_frame | tail -1` =~ /(\d+)/;
245 $maxval_scaled = $1;
247 $black_scaled = $black_unscaled
248 * ($maxval_scaled / $maxval_unscaled)
249 / 65535;
251 @ARGV = (@ARGV[0..$i - 1], "--black", "$black_scaled", @ARGV[$i..$#ARGV]);
252 $i += 2;
255 print ".";
259 if ($started_conversion) {
260 print "\n";
263 @ARGV = ($0, @ARGV);
265 if (!$extract_raw && $ENV{"ALE_COUNT_THREADS"} == 0) {
266 exec { $ENV{"ALE_BIN"} } @ARGV;
267 } elsif ($ENV{"ALE_COUNT_THREADS"} == 0) {
268 system { $ENV{"ALE_BIN"} } @ARGV;
269 } else {
270 $| = 1;
271 $fork1 = fork();
272 if ($fork1 == 0) {
273 exec { $ENV{"ALE_BIN"} } @ARGV;
276 if (fork() == 0) {
277 $status_file = "/proc/$fork1/status";
278 while (-r $status_file && `cat $status_file 2> /dev/null | grep Threads` =~ /(\d+)/) {
279 if (defined $threads{$1}) {
280 $threads{$1}++;
281 } else {
282 $threads{$1} = 1;
284 sleep 1;
286 foreach $count (sort keys %threads) {
287 print("$count thread(s): $threads{$count} tick(s)\n");
289 exit(0);
292 while (wait() != -1) {}