source: gsdl/trunk/bin/script/pstoimg.pl@ 14958

Last change on this file since 14958 was 10535, checked in by chi, 19 years ago

pstoimg.pl is a wrapper for running convert utility which converts
PS documents to various types of image (e.g. PNG, GIF, JPEG format,
Then use PagedImgPlug to deal with the the images

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1#!/usr/bin/perl -w
2
3
4###########################################################################
5#
6# pstoimg.pl -- convert PS documents to various types of Image format
7#
8# A component of the Greenstone digital library software
9# from the New Zealand Digital Library Project at the
10# University of Waikato, New Zealand.
11#
12# Copyright (C) 2001 New Zealand Digital Library Project
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27#
28###########################################################################
29# pstoimg.pl is a wrapper for running convert utility which converts
30# PS documents to various types of image (e.g. PNG, GIF, JPEG format,
31# Then use PagedImgPlug to deal with the the images
32# Chi-Yu Huang 08.2005
33
34BEGIN {
35 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
37}
38
39use parsargv;
40use util;
41use Cwd;
42use File::Basename
43;
44sub print_usage {
45 # note - we don't actually ever use most of these options...
46 print STDERR
47 ("pstoimg.pl wrapper for pstoimg.\n",
48 "Usage: pstoimg [options] <PS-file> <output>\n",
49 "Options:\n",
50 "\t-convert_to\toutput image type for the PS\n"
51 );
52 exit (1);
53}
54
55sub main {
56 my (@ARGV) = @_;
57 my ($convert_to);
58
59 # read command-line arguments so that
60 # you can change the command in this script
61 if (!parsargv::parse(\@ARGV,
62 'convert_to/.*/^', \$convert_to,
63 )) {
64 print_usage();
65 }
66
67 # Make sure the input file exists and can be opened for reading
68 if (scalar(@ARGV) != 2) {
69 print_usage();
70 }
71
72 my $input_filename = $ARGV[0];
73 my $output_filestem = $ARGV[1];
74
75 # test that the directories exist to create the output file, or
76 # we should exit immediately. (File:: is included by util.pm)
77 &util::mk_dir($output_filestem) if (!-e $output_filestem);
78
79 my @dir = split (/(\/|\\)/, $input_filename);
80 my $input_basename = pop(@dir);
81 $input_basename =~ s/\.ps//i;
82 my $dir = join ("", @dir);
83
84 if (!-r $input_filename) {
85 print STDERR "Error: unable to open $input_filename for reading\n";
86 exit(1);
87 }
88 # don't include path on windows (to avoid having to play about
89 # with quoting when GSDLHOME might contain spaces) but assume
90 # that the PATH is set up correctly.
91 $cmd = "convert";
92 #if ($ENV{'GSDLOS'} =~ /^windows$/);
93 #Convert utility will convert the PS to GIF Animation
94 my $output_filename = &util::filename_cat($output_filestem, $input_basename);
95 if ($convert_to eq "gif") {
96 $cmd .= " \"$input_filename\" \"$output_filename-%02d.$convert_to\"";
97 } else {
98 $cmd .= " \"$input_filename\" \"$output_filename.$convert_to\"";
99 }
100
101 # system() returns -1 if it can't run, otherwise it's $cmds ret val.
102 # note we return 0 if the file is "encrypted"
103 $!=0;
104 if (system($cmd)!=0) {
105 print STDERR "Convert error for $input_filename $!\n";
106 # leave these for gsConvert.pl...
107 #&util::rm("$output_filestem.text") if (-e "$output_filestem.text");
108 #&util::rm("$output_filestem.err") if (-e "$output_filestem.err");
109 return 1;
110 } else {
111 # command execute successfully
112 create_itemfile($output_filestem, $input_basename, $convert_to);
113 }
114 return 0;
115}
116
117sub create_itemfile
118{
119 my ($output_dir, $convert_basename, $convert_to) = @_;
120 opendir(DIR, $output_dir) || die "can't opendir $output_dir: $!";
121 my $item_file = $output_dir."/".$convert_basename.".item";
122 open(FILE,">$item_file");
123
124 print FILE "<PagedDocument>\n";
125
126 my $page_num = "";
127 @dir_files = grep {-f "$output_dir/$_"} readdir(DIR);
128
129 # Sort files in the directory by page_num
130 sub page_num {
131 my ($dir) = @_;
132 my $pagenum = "";
133 if ($ENV{'GSDLOS'} =~ /^windows$/){
134 ($pagenum) =($dir =~ m/^.*\.(\d+)$/i);
135 } else {
136 ($pagenum) =($dir =~ m/^.*-(\d+)\.(.*)$/i);
137 }
138 $pagenum = $pagenum || 1;
139 return $pagenum;
140 }
141
142 # sort the files in the directory in the order of page_num rather than lexically.
143 @dir_files = sort { page_num($a) <=> page_num($b) } @dir_files;
144
145 foreach my $file (@dir_files){
146 if ($ENV{'GSDLOS'} =~ /^windows$/ && $convert_to ne "gif"){
147 ($page_num) =($file =~ m/^.*\.(\d+)$/i);
148 } else {
149 ($page_num) =($file =~ m/^.*-(\d+)\.(.*)/i);
150 }
151 # as the converter will convert the document to image files start from page 0
152 $page_num =$page_num + 1 if defined $page_num;
153 $page_num = 1 unless defined $page_num;
154 if ($file !~ /\.item/i){
155 print FILE " <Page pagenum=\"$page_num\" imgfile=\"$file\" txtfile=\"\"/>\n";
156 }
157 }
158
159 print FILE "</PagedDocument>\n";
160 closedir DIR;
161 return "";
162}
163
164# indicate our error status, 0 = success
165exit (&main(@ARGV));
166
167
168
Note: See TracBrowser for help on using the repository browser.