root/trunk/devel/FuzzyOcr/Preprocessor.pm

Revision 133, 1.9 kB (checked in by decoder, 1 year ago)

Added License tags too all code files

Line 
1 # <@LICENSE>
2 # Licensed to the Apache Software Foundation (ASF) under one or more
3 # contributor license agreements.  See the NOTICE file distributed with
4 # this work for additional information regarding copyright ownership.
5 # The ASF licenses this file to you under the Apache License, Version 2.0
6 # (the "License"); you may not use this file except in compliance with
7 # the License.  You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # </@LICENSE>
17
18 package FuzzyOcr::Preprocessor;
19
20 sub new {
21     my ($class, $label, $command, $args) = @_;
22
23     bless {
24         "label"     => $label,
25         "command"   => $command,
26         "args"      => $args
27     }, $class;
28 }
29
30 sub run {
31     my ($self, $input) = @_;
32     my $tmpdir = FuzzyOcr::Config::get_tmpdir();
33     my $label = $self->{label};
34     my $output = "$tmpdir/prep.$label.out";
35     my $stderr = ">$tmpdir/prep.$label.err";
36
37     my $stdin = undef;
38     my $stdout = undef;
39     my $args = $self->{args};
40     my $rcmd = $self->{command};
41
42     if (defined $args) {
43         $rcmd .= ' ' . $args;
44     }
45
46     # Does the processor expect input from file or from stdin?
47     if(defined $args and $args =~ /\$input/) {
48         $rcmd =~ s/\$input/$input/;
49     } else {
50         $stdin = "<$input";
51     }
52
53     # Does it output to file or to stdout?
54     if(defined $args and $args =~ /\$output/) {
55         $rcmd =~ s/\$output/$output/;
56     } else {
57         $stdout = ">$output";
58     }
59
60     # Run processor
61     my $retcode = FuzzyOcr::Misc::save_execute($rcmd, $stdin, $stdout, $stderr);
62
63     # Return code
64     return $retcode;
65 }
66
67 1;
Note: See TracBrowser for help on using the browser.