ale, gpu.h, gpu.cc: Add code for initializing the GPU with GLUT, as well
[Ale.git] / ale
blob6f052954e342a4bbde71d4036b68d3d00f1995e1
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 | 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`)) {
39 $ENV{"ALE_GPU_ACCEL_DEFAULT"} = 0;
40 } else {
41 $ENV{"ALE_GPU_ACCEL_DEFAULT"} = 1;
45 # Determine whether to use GL interface by default
48 # Blacklist
50 if (!defined $ENV{"ALE_GL_UI_DEFAULT"}
51 && ($ENV{"ALE_SOFT_GPU"}
52 || !`glxinfo`)) {
53 $ENV{"ALE_GL_UI_DEFAULT"} = 0;
56 # Whitelist
58 if (!defined $ENV{"ALE_GL_UI_DEFAULT"}
59 && (`uname -a | grep Linux`)) {
60 $ENV{"ALE_GL_UI_DEFAULT"} = 1;
63 # Make GL UI non-default if neither blacklisted nor whitelisted
65 if (!defined $ENV{"ALE_GL_UI_DEFAULT"}) {
66 $ENV{"ALE_GL_UI_DEFAULT"} = 0;
70 # Environment variable defaults
73 %defaults = (
74 "ALE_BIN" => "$ale_exec_dir/ale-bin",
75 "DCRAW" => "dcraw",
76 "EXIF_UTILITY" => "exiftool",
77 "ALE_COUNT_THREADS" => "0",
78 "PAGER" => "/usr/bin/less",
81 %devices = (
82 "Canon EOS 5D" => "canon_300d",
83 "Canon EOS DIGITAL REBEL" => "canon_300d",
86 for $default (keys %defaults) {
87 if (!defined $ENV{$default}) {
88 $ENV{$default} = $defaults{$default};
93 # Wrap help page requests
96 if (!defined @ARGV || @ARGV == 0 || $ARGV[0] =~ /^--h[a-zA-Z0-9]$/) {
97 $ale_bin = $ENV{"ALE_BIN"};
100 # Find a pager
103 undef $pager;
104 if (-x "$ENV{PAGER}") {
105 $pager = $ENV{"PAGER"};
106 } elsif (`which $ENV{"PAGER"}`) {
107 $pager = `which $ENV{"PAGER"}`;
108 } elsif (`which pager`) {
109 $pager = `which pager`;
110 } elsif (`which less`) {
111 $pager = `which less`;
112 } elsif (`which more`) {
113 $pager = `which more`;
114 } elsif (-x '/usr/bin/less') {
115 $pager = '/usr/bin/less';
116 } elsif (-x '/bin/more') {
117 $pager = '/bin/more';
121 # Set pager options
124 $ENV{"LESS"} = "-X -F";
127 # Fetch help page
130 if (defined $pager) {
131 exec "exec -a $0 $ale_bin @ARGV | $pager";
132 } else {
133 exec "exec -a $0 $ale_bin @ARGV";
136 exit(0);
140 # Perform metadata extraction and conversion
143 $started_conversion = 0;
144 undef $global_device;
145 $extract_ev = 0;
146 $extract_raw = 0;
147 if ("@ARGV" =~ /exp-meta-only/ && defined $ENV{"EXIF_UTILITY"} && `which $ENV{"EXIF_UTILITY"}`) {
148 $extract_ev = 1;
150 if (defined $ENV{"DCRAW"} && `which $ENV{"DCRAW"}`) {
151 $extract_raw = 1;
153 for ($i = 0; $i < @ARGV; $i++) {
154 $arg = $ARGV[$i];
156 $i += 10 if $arg =~ /^-?-3dvp$/;
157 $i += 10 if $arg =~ /^-?-3dpp$/;
158 $i += 2 if $arg =~ /^-?-3dp$/;
159 $i += 2 if $arg =~ /^-?-3dv$/;
160 $i += 2 if $arg =~ /^-?-ochain$/;
161 $i += 3 if $arg =~ /^-?-wm$/;
162 $i += 3 if $arg =~ /^-?-wmx$/;
163 $i += 1 if $arg =~ /^-?-trans-load$/;
164 $i += 1 if $arg =~ /^-?-trans-save$/;
166 next if (!-e $arg);
168 if ($extract_ev) {
169 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^EV'` =~ /([0-9\.]+)/) {
170 $ev = $1;
171 if (`$ENV{"EXIF_UTILITY"} $arg | grep '^ISO'` =~ /([0-9\.]+)/) {
172 $ev = $ev - log($1 / 100) / log(2);
174 @ARGV = (@ARGV[0..$i - 1], "--ev", "$ev", @ARGV[$i..$#ARGV]);
175 $i += 2;
179 if ($extract_raw) {
180 $device_info = `$ENV{"DCRAW"} -i $arg 2> /dev/null`;
182 next if ($?);
184 if (!$started_conversion) {
185 print "Extracting frame data";
186 $started_conversion = 1;
189 # Make a temporary directory for conversion
192 use File::Temp qw/ tempdir /;
194 $tempdir = tempdir("ale.XXXXXXXXX",
195 CLEANUP => 1,
196 DIR => File::Spec->tmpdir());
200 # Get device information
203 undef $device;
204 foreach $device_string (keys %devices) {
205 if ($device_info =~ /$device_string/) {
206 $device = $devices{$device_string};
207 last;
210 if (!defined $device) {
211 $device = "canon_300d";
215 # Currently, devices must be global.
218 if (defined $global_device && $global_device ne $device) {
219 die "Multiple device types not yet supported.\n";
220 } else {
221 $global_device = $device;
224 # @ARGV = (@ARGV[0..$i - 1], "--device", "$device", @ARGV[$i..$#ARGV]);
225 @ARGV = ("--device", "$device", @ARGV);
227 $i += 2;
230 # Convert
233 $converted_frame = $arg;
234 $converted_frame =~ s/(?:\.[^.]*)?$/.pgm/;
235 $converted_frame =~ s/.*\///;
236 $converted_frame = $tempdir . "/" . $converted_frame;
238 `$ENV{"DCRAW"} -t 0 -d -4 -k 0 -r 1 1 1 1 -c $arg > $converted_frame`;
240 $ARGV[$i] = $converted_frame;
243 # Try to get black level information
246 # XXX: The following approach is not obviously correct.
248 if (`$ENV{"DCRAW"} -d -v -c $arg 2>&1 1>/dev/null` =~ /black=(\d+)/) {
249 $black_unscaled = $1;
250 `$ENV{"DCRAW"} -D -4 -c $arg | pgmhist | tail -1` =~ /(\d+)/;
251 $maxval_unscaled = $1;
252 `pgmhist $converted_frame | tail -1` =~ /(\d+)/;
253 $maxval_scaled = $1;
255 $black_scaled = $black_unscaled
256 * ($maxval_scaled / $maxval_unscaled)
257 / 65535;
259 @ARGV = (@ARGV[0..$i - 1], "--black", "$black_scaled", @ARGV[$i..$#ARGV]);
260 $i += 2;
263 print ".";
267 if ($started_conversion) {
268 print "\n";
271 @ARGV = ($0, @ARGV);
273 if ($ENV{"ALE_COUNT_THREADS"} == 0) {
274 system { $ENV{"ALE_BIN"} } @ARGV;
275 } else {
276 $| = 1;
277 $fork1 = fork();
278 if ($fork1 == 0) {
279 exec { $ENV{"ALE_BIN"} } @ARGV;
282 if (fork() == 0) {
283 $status_file = "/proc/$fork1/status";
284 while (-r $status_file && `cat $status_file 2> /dev/null | grep Threads` =~ /(\d+)/) {
285 if (defined $threads{$1}) {
286 $threads{$1}++;
287 } else {
288 $threads{$1} = 1;
290 sleep 1;
292 foreach $count (sort keys %threads) {
293 print("$count thread(s): $threads{$count} tick(s)\n");
295 exit(0);
298 while (wait() != -1) {}