summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlgs/lib/viewpwg.ps
blob: 4ee0cdb7aed5b09186aab55a60851648abdf2c2f (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
% Copyright (C) 2017-2021 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.,  1305 Grant Avenue - Suite 200, Novato,
% CA 94945, U.S.A., +1(415)492-9861, for further information.
%

% viewpwg.ps
% Display a PWG file.

% VPWGDEBUG can be predefined to be true to get debugging.
% /VPWGDEBUG false def
/VPWGDEBUG where { pop } { /VPWGDEBUG false def } ifelse

/read1			% <file> read1 <int>
{ read pop
} bind def
/read2			% <file> read2 <int>
{ dup read1 8 bitshift exch read1 add
} bind def
/read4			% <file> read4 <int>
{ dup read2 16 bitshift exch read2 add
} bind def
/readString		% <file> readString <string>
{ 0 string
  dup
  0 1 63
  { % <file> <string> <string> <iteration>
    3 index read pop
    % <file> <string> <string> <iteration> <char>
    dup 0 eq {pop pop} {put dup} ifelse
  }
  for
  pop
  exch pop
} bind def
/readReserved           % <file> readReserved -
{ read4 0 ne
  { (Reserved field not zero.\n) print cleartomark stop }
  if
} bind def
/readBoolean           % <file> readBoolean <bool>
{ read4 dup 0 eq
  { pop false }
  { 1 eq
    { 0 }
    { (Boolean not a boolean.\n) print cleartomark stop }
    ifelse
  }
  ifelse
} bind def
 
/readPWGheader		% <file> readPWGheader <dict>
{ 20 dict begin
  dup 4 string readstring pop
  <52615332> eq not
  { (Not a PWG file.\n) print cleartomark stop
  } if
  pop
  currentdict end
} bind def

/readPWGpageHeader	% <file> readPWGpageHeader <dict>
                        % Note: PWG header must be on dict stack
{ 10 dict begin
  dup readString /PwgRaster exch def
  dup readString /MediaColor exch def
  dup readString /MediaType exch def
  dup readString /PrintContentOptimize exch def
  dup readReserved
  dup readReserved
  dup readReserved
  dup read4 /CutMedia exch def
  dup readBoolean /Duplex exch def
  dup read4 /HWResolutionX exch def
  dup read4 /HWResolutionY exch def
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup readBoolean /InsertSheet exch def
  dup read4 /Jog exch def
  dup read4 /LeadingEdge exch def
  dup readReserved
  dup readReserved
  dup readReserved
  dup read4 /MediaPosition exch def
  dup read4 /MediaWeightMetric exch def
  dup readReserved
  dup readReserved
  dup read4 /NumCopies exch def
  dup read4 /Orientation exch def
  dup readReserved
  dup read4 /PageSizeX exch def
  dup read4 /PageSizeY exch def
  dup readReserved
  dup readReserved
  dup read4 /Tumble exch def
  dup read4 /Width exch def
  dup read4 /Height exch def
  dup readReserved
  dup read4 /BitsPerColor exch def
  dup read4 /BitsPerPixel exch def
  dup read4 /BytesPerLine exch def
  dup read4 /ColorOrder exch def
  dup read4 /ColorSpace exch def
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup read4 /NumColors exch def
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup read4 /TotalPageCount exch def
  dup read4 /CrossFeedTransform exch def
  dup read4 /FeedTransform exch def
  dup read4 /ImageBoxLeft exch def
  dup read4 /ImageBoxTop exch def
  dup read4 /ImageBoxRight exch def
  dup read4 /ImageBoxBottom exch def
  dup read4 /AlternatePrimary exch def
  dup read4 /PrintQuality exch def
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup readReserved
  dup read4 /VendorIdentifier exch def
  dup read4 /VendorLength exch def
  % Skip 1088 bytes of VendorData
  1088 { dup read pop pop } repeat
  % 64 Reserved bytes
  dup readReserved dup readReserved dup readReserved dup readReserved
  dup readReserved dup readReserved dup readReserved dup readReserved
  dup readReserved dup readReserved dup readReserved dup readReserved
  dup readReserved dup readReserved dup readReserved dup readReserved
  dup readString /RenderingIntent exch def
  dup readString /PageSizeName exch def
  pop
  currentdict end
} bind def

/viewPWG		% <file|string> viewPWG -
{ save 20 dict begin
  /saved exch def
  dup type /stringtype eq { (r) file } if
  /F exch def
  F readPWGheader /Header exch def

  % We have a dictionary with /saved, /F and /Header on the stack.

  % Debug dump Header fields.
  VPWGDEBUG { Header { exch == == } forall (----------------\n) print flush } if

  {
      % If we have no bytes left, stop
      F bytesavailable 0 le { exit } if

      % Read the page header.
      F readPWGpageHeader /PageHeader exch def

      % Push PageHeader onto the dict stack.
      PageHeader begin

      % Debug dump the Page Header.
      VPWGDEBUG { PageHeader { exch == == } forall (----------------\n) print flush } if

      % Define D to be the decoded data from F.
      /D F
      <</Width Width
        /BPP BitsPerPixel
      >> /PWGDecode filter def

      % Set the Colorspace appropriately.
      ColorSpace dup dup 3 eq exch 18 eq or exch 48 eq or
      { % Black or SGray or Device1
        /DeviceGray setcolorspace
        /NumComponents 1 def
      }
      { ColorSpace dup dup dup 1 eq exch 19 eq or exch 20 eq or exch 50 eq or
        { % Rgb or sRGB or AdobeRGB or Device3
          /DeviceRGB setcolorspace
          /NumComponents 3 def
        }
        { ColorSpace dup 6 eq exch 51 eq or
          { % DeviceCMYK or Device4
            /DeviceCMYK setcolorspace
            /NumComponents 4 def
          }
          { (Unsupported colorspace in PWG file.\n) print cleartomark stop
          }
          ifelse
        }
        ifelse
      }
      ifelse

      % Do some matrixy stuff I don't understand.
      matrix currentmatrix
      0 1 3 { 2 copy get dup 0 ne { dup abs div } if 3 copy put pop pop } for
      setmatrix

      <<
        /ImageType 1
        /ImageMatrix [1 0 0 -1 0 Height]
        /BitsPerComponent BitsPerPixel NumComponents div
        /Width Width
        /Height Height
        /DataSource D
      >> image

      % Bin the PageHeader dictionary
      end

      showpage
  }
  loop
  saved end restore
} bind def

% This lets you do stuff on the command line like:
% gs -sDEVICE=pdfwrite -o stuff%03d.pdf viewurf.ps -c "(image.urf) << /PageSize 2 index viewURFgetsize 2 array astore  >> setpagedevice viewURF"
% so the output size is influenced by the original image.
%/viewURFgetsize		% <file|string> ==> [width height]
%{
%  save 20 dict begin
%  /saved exch def
%  dup type /stringtype eq { (r) file } if
%  /F exch def
%  F readURFheader /Header exch def
%    currentdict Header end begin begin
%  VPWGDEBUG { Header { exch == == } forall (----------------\n) print flush } if
%  F readURFimageHeader /ImageHeader exch def
%    currentdict ImageHeader end begin begin
%  F 0 setfileposition		% reset file pointer
%  Width Height
%  saved end end end restore
%} bind def