File: pdfinflt.ps

package info (click to toggle)
ghostscript 9.06~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,732 kB
  • sloc: ansic: 439,964; python: 4,915; cpp: 3,565; sh: 2,520; tcl: 1,482; perl: 1,374; makefile: 421; lisp: 407; awk: 66; yacc: 18
file content (173 lines) | stat: -rw-r--r-- 6,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
% Copyright (C) 2001-2012 Artifex Software, Inc.
% All Rights Reserved.
%
% This software is provided AS-IS with no warranty, either express or
% implied.
%
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
%
% Refer to licensing information at http://www.artifex.com or contact
% Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
% CA  94903, U.S.A., +1(415)492-9861, for further information.


% pdfinflt.ps
%
% Try to decompress any FlateDecode streams in the input file.
%
% Usage: gs -- toolbin/pdfinflt.ps infile outfile
%
% Requires: lib/pdfwrite.ps.
%
%
% It is not yet ready for prime time, but it is available for anyone wants
% to fix it.
%
% The main problem is:
%
% 1. Sometimes the PDF files that are written are broken. When they are
%    broken, GS gets an xref problem.
%
%    This problem is actually due to lib/pdfwrite.ps since even
%    when no conversion is done, the file is may be bad.

% Check to make sure both input and output filename was specified.
[ shellarguments
  { counttomark 2 eq {
      % Arguments ok
      /OutFileName exch def
      /InFileName exch def

      (pdfwrite.ps) runlibfile

      /justcopy /pdfcopystream load def
      /filterstoremove <<
          % list of filters to apply
          %   - kept as a dictionary, for faster searches
          /ASCIIHexDecode   {}
          /AHx              { pop /ASCIIHexDecode } bind
          /ASCII85Decode    {}
          /A85              { pop /ASCII85Decode } bind
          /LZWDecode	    {}
          /LZW              { pop /LZWDecode } bind
          /FlateDecode	    {}
          /Fl               { pop /FlateDecode } bind
        % /RunLengthDecode  {}
        % /RL               { pop /RunLengthDecode } bind
        % /CCITTFaxDecode   {}
        % /CCF              { pop /CCITTFaxDecode } bind
        % /DCTDecode	    {}
        % /DCT              { pop /DCTDecode } bind
        % /JBIG2Decode	    dup /Filter resourcestatus {pop pop (GS-specific)} {pop} ifelse
        % /JPXDecode	    dup /Filter resourcestatus {pop pop (GS-specific)} {pop} ifelse
        % /Crypt	    (no equivalent)
        >> def
      /pdfcopystream {		% <newstreamdict> <file> pdfcopystream -
                                %   (file has been positioned)
        1 index /Filter known {
            10 dict begin
                %stack: <newstreamdict> <file>
                /infile exch def /sdict exch def
                RMap ld_length neg omapnew pop /lenobj exch def
                /copyofsdict sdict dup length dict copy def
                copyofsdict dup /Length lenobj /O cvx 2 packedarray cvx put
                infile sdict /Length oget () /SubFileDecode filter
                % apply filters to the (input) stream
                {
                    %stack: <file>
                    % extract first filter and its parameters
                    {/DecodeParms/Filter} {
                        copyofsdict exch knownoget {
                            dup type /arraytype eq { 0 get } if
                        } {
                            null
                        } ifelse
                    } forall
                    %stack: <file> <decode|null> <filter|null>
                    dup null eq {
                      pop pop exit
                    } if
                    filterstoremove 1 index .knownget {
                        exec
                        % apply the filter
                        exch dup null eq { pop } { exch } ifelse
                        filter
                        %stack: <file>
                        % remove first element from each of /Filter and /DecodeParms
                        copyofsdict dup {/DecodeParms/Filter} {
                            %stack: <file> <copyofsdict> <copyofsdict> </Key>
                            2 copy knownoget {
                                dup type /arraytype eq {
                                    dup length dup 3 ge {
                                        1 exch 1 sub getinterval put
                                    } {
                                        {
                                            {pop undef}	    % []
                                            {pop undef}	    % [elem]
                                            {1 get put}	    % [elem1 elem2]
                                        } exch get exec
                                    } ifelse
                                } {
                                    pop undef
                                } ifelse
                                dup
                            } {
                                pop
                            } ifelse
                        } forall pop pop
                    } {
                        pop pop exit
                    } ifelse
                } loop
                /infile exch def
                copyofsdict pdfwritevalue (stream\n) ows
                /opos OFile fileposition def
                infile dup OFile 16#7fffffff pdfcopybytes closefile
                /len OFile fileposition opos sub def
                (\nendstream\nendobj\n) ows
                lenobj pdfwriteobjheader len pdfwritevalue
            end
        } {
            justcopy
        } ifelse
      } def

      { InFileName (r) file } stopped
      { (\n*** Cannot open input file: ) print InFileName print ( ***\n\n) print flush
        quit
      }
      if
      pdfdict begin pdfopen begin

      { OutFileName (w) file } stopped
      { (\n*** Cannot open output file: ) print OutFileName print ( ***\n\n) print flush
        quit
      }
      if
      (Converting ) print InFileName print ( to ) print OutFileName = flush
      currentdict /Trailer get
      dup /Prev undef		% We don't want a 'Prev' link.
      pdfwrite
      end end
      (Done.\n) print flush
      cleartomark
      true		% success
    }
    {
      (\n*** Incorrect number of file name arguments. ***\n\n) print
      cleartomark false			% incorrect number of arguments
    }
    ifelse
  }
  {
    (\n*** Missing '--' preceding toolbin/pdfinflt.ps ***\n\n) print
    cleartomark false			% not in shell arguments mode (missing -- )
  }
ifelse
not {
   (Usage: gs -- toolbin/pdfinflt.ps infile outfile\n\n) print flush
}
if
quit