Debian Codegraph @

bin/extractjpgs.pl

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 package main;
4
5 use warnings;
6 use strict;
7 use CAM::PDF;
8 use Getopt::Long;
9 use Pod::Usage;
10
11 our $VERSION = '1.60';
12
13 my %opts = (
14 template => 'crunchjpg_tmpl.pdf',
15
16 verbose => 0,
17 help => 0,
18 version => 0,
19 skip => {},
20
21 # Temporary values:
22 skipval => [],
23 );
24
25 Getopt::Long::Configure('bundling');
26 GetOptions('S|skip=s' => \@{$opts{skipval}},
27 'v|verbose' => \$opts{verbose},
28 'h|help' => \$opts{help},
29 'V|version' => \$opts{version},
30 ) or pod2usage(1);
31 if ($opts{help})
32 {
33 pod2usage(-exitstatus => 0, -verbose => 2);
34 }
35 if ($opts{version})
36 {
37 print "CAM::PDF v$CAM::PDF::VERSION\n";
38 exit 0;
39 }
40
41 foreach my $flag (qw( skip ))
42 {
43 foreach my $val (@{$opts{$flag.'val'}})
44 {
45 foreach my $key (split /\D+/xms, $val)
46 {
47 $opts{$flag}->{$key} = 1;
48 }
49 }
50 }
51 if (!-f $opts{template})
52 {
53 die "Cannot find the template pdf called $opts{template}\n";
54 }
55
56 if (@ARGV < 2)
57 {
58 pod2usage(1);
59 }
60
61 my $infile = shift;
62 my $outdir = shift;
63
64 my $doc = CAM::PDF->new($infile) || die "$CAM::PDF::errstr\n";
65
66 my $pages = $doc->numPages();
67 my $nimages = 0;
68 my $rimages = 0;
69 my %doneobjs;
70
71 for my $p (1..$pages)
72 {
73 my $c = $doc->getPageContent($p);
74 my @parts = split /(\/[\w]+\s*Do)\b/xms, $c;
75 foreach my $part (@parts)
76 {
77 if ($part =~ m/\A(\/[\w]+)\s*Do\z/xms)
78 {
79 my $ref = $1;
80 my $xobj = $doc->dereference($ref, $p);
81 my $objnum = $xobj->{objnum};
82 my $im = $doc->getValue($xobj);
83 my $w = $im->{Width} || $im->{W} || 0;
84 if ($w)
85 {
86 $w = $doc->getValue($w);
87 }
88 my $h = $im->{Height} || $im->{H} || 0;
89 if ($h)
90 {
91 $h = $doc->getValue($h);
92 }
93
94 next if (exists $doneobjs{$objnum});
95
96 $nimages++;
97 _inform("Image $nimages page $p, $ref = object $objnum, (w,h)=($w,$h)", $opts{verbose});
98
99 if (exists $opts{skip}->{$objnum})
100 {
101 _inform("Skipping object $objnum", $opts{verbose});
102 next;
103 }
104
105 my $isjpg = _isjpg($im);
106
107 if (!$isjpg)
108 {
109 _inform('Not a jpeg', $opts{verbose});
110 }
111 else
112 {
113 my $oldsize = $doc->getValue($im->{Length});
114 if (!$oldsize)
115 {
116 die "PDF error: Failed to get size of image\n";
117 }
118
119 my $tmpl = CAM::PDF->new($opts{template}) || die "$CAM::PDF::errstr\n";
120
121 # Get a handle on the needed data bits from the template
122 my $media_array = $tmpl->getValue($tmpl->getPage(1)->{MediaBox});
123 my $rawpage = $tmpl->getPageContent(1);
124
125 $media_array->[2]->{value} = $w;
126 $media_array->[3]->{value} = $h;
127 my $page = $rawpage;
128 $page =~ s/xxx/$w/igxms;
129 $page =~ s/yyy/$h/igxms;
130 $tmpl->setPageContent(1, $page);
131 $tmpl->replaceObject(9, $doc, $objnum, 1);
132
133 my $ofile = "/tmp/crunchjpg.$$";
134 $tmpl->cleanoutput($ofile);
135
136 if (!-d $outdir)
137 {
138 require File::Path;
139 File::Path::mkpath($outdir);
140 }
141 `convert -quality 50 -density 72x72 -page ${w}x$h pdf:$ofile jpg:$outdir/$objnum.jpg`; ## no critic (Backtick)
142
143 $doneobjs{$objnum} = 1;
144 $rimages++;
145 }
146 }
147 }
148 }
149
150 _inform("Extracted $rimages of $nimages images", $opts{verbose});
151
152 sub _isjpg
153 {
154 my $im = shift;
155 return if (!$im->{Filter});
156
157 my $f = $im->{Filter};
158 my @names = $f->{type} eq 'array' ? @{$f->{value}} : $f;
159 for my $e (@names)
160 {
161 my $name = $doc->getValue($e);
162 if (ref $name)
163 {
164 $name = $name->{value};
165 }
166 #warn "Checking $name\n";
167 if ($name eq 'DCTDecode')
168 {
169 return 1;
170 }
171 }
172 return;
173 }
174
175 sub _inform
176 {
177 my $str = shift;
178 my $verbose = shift;
179
180 if ($verbose)
181 {
182 print STDERR $str, "\n";
183 }
184 return;
185 }
186
187
188 __END__
189
190 =for stopwords extractjpgs.pl ImageMagick JPG
191
192 =head1 NAME
193
194 extractjpgs.pl - Save copies of all PDF JPG images to a directory
195
196 =head1 SYNOPSIS
197
198 extractjpgs.pl [options] infile.pdf outdirectory
199
200 Options:
201 -S --skip=imnum don't output the specified images (can be used mutliple times)
202 -v --verbose print diagnostic messages
203 -h --help verbose help message
204 -V --version print CAM::PDF version
205
206 C<imnum> is a comma-separated list of integers indicating the images
207 in order that they appear in the PDF. Use F<listimages.pl> to retrieve
208 the image numbers.
209
210 =head1 DESCRIPTION
211
212 Requires the ImageMagick B<convert> program to be available
213
214 Searches the PDF for JPG images and saves them as individual files in the
215 specified directory. The files are named C<E<lt>imnumE<gt>.jpg>.
216
217 =head1 SEE ALSO
218
219 CAM::PDF
220
221 F<crunchjpgs.pl>
222
223 F<listimages.pl>
224
225 F<extractallimages.pl>
226
227 F<uninlinepdfimages.pl>
228
229 =head1 AUTHOR
230
231 See L<CAM::PDF>
232
233 =cut