You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
6.9 KiB

1 month ago
  1. % Copyright (C) 2017-2023 Artifex Software, Inc.
  2. % All Rights Reserved.
  3. %
  4. % This software is provided AS-IS with no warranty, either express or
  5. % implied.
  6. %
  7. % This software is distributed under license and may not be copied,
  8. % modified or distributed except as expressly authorized under the terms
  9. % of the license contained in the file LICENSE in this distribution.
  10. %
  11. % Refer to licensing information at http://www.artifex.com or contact
  12. % Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  13. % CA 94129, USA, for further information.
  14. %
  15. % viewpwg.ps
  16. % Display a PWG file.
  17. % VPWGDEBUG can be predefined to be true to get debugging.
  18. % /VPWGDEBUG false def
  19. /VPWGDEBUG where { pop } { /VPWGDEBUG false def } ifelse
  20. /read1 % <file> read1 <int>
  21. { read pop
  22. } bind def
  23. /read2 % <file> read2 <int>
  24. { dup read1 8 bitshift exch read1 add
  25. } bind def
  26. /read4 % <file> read4 <int>
  27. { dup read2 16 bitshift exch read2 add
  28. } bind def
  29. /readString % <file> readString <string>
  30. { 0 string
  31. dup
  32. 0 1 63
  33. { % <file> <string> <string> <iteration>
  34. 3 index read pop
  35. % <file> <string> <string> <iteration> <char>
  36. dup 0 eq {pop pop} {put dup} ifelse
  37. }
  38. for
  39. pop
  40. exch pop
  41. } bind def
  42. /readReserved % <file> readReserved -
  43. { read4 0 ne
  44. { (Reserved field not zero.\n) print cleartomark stop }
  45. if
  46. } bind def
  47. /readBoolean % <file> readBoolean <bool>
  48. { read4 dup 0 eq
  49. { pop false }
  50. { 1 eq
  51. { 0 }
  52. { (Boolean not a boolean.\n) print cleartomark stop }
  53. ifelse
  54. }
  55. ifelse
  56. } bind def
  57. /readPWGheader % <file> readPWGheader <dict>
  58. { 20 dict begin
  59. dup 4 string readstring pop
  60. <52615332> eq not
  61. { (Not a PWG file.\n) print cleartomark stop
  62. } if
  63. pop
  64. currentdict end
  65. } bind def
  66. /readPWGpageHeader % <file> readPWGpageHeader <dict>
  67. % Note: PWG header must be on dict stack
  68. { 10 dict begin
  69. dup readString /PwgRaster exch def
  70. dup readString /MediaColor exch def
  71. dup readString /MediaType exch def
  72. dup readString /PrintContentOptimize exch def
  73. dup readReserved
  74. dup readReserved
  75. dup readReserved
  76. dup read4 /CutMedia exch def
  77. dup readBoolean /Duplex exch def
  78. dup read4 /HWResolutionX exch def
  79. dup read4 /HWResolutionY exch def
  80. dup readReserved
  81. dup readReserved
  82. dup readReserved
  83. dup readReserved
  84. dup readBoolean /InsertSheet exch def
  85. dup read4 /Jog exch def
  86. dup read4 /LeadingEdge exch def
  87. dup readReserved
  88. dup readReserved
  89. dup readReserved
  90. dup read4 /MediaPosition exch def
  91. dup read4 /MediaWeightMetric exch def
  92. dup readReserved
  93. dup readReserved
  94. dup read4 /NumCopies exch def
  95. dup read4 /Orientation exch def
  96. dup readReserved
  97. dup read4 /PageSizeX exch def
  98. dup read4 /PageSizeY exch def
  99. dup readReserved
  100. dup readReserved
  101. dup read4 /Tumble exch def
  102. dup read4 /Width exch def
  103. dup read4 /Height exch def
  104. dup readReserved
  105. dup read4 /BitsPerColor exch def
  106. dup read4 /BitsPerPixel exch def
  107. dup read4 /BytesPerLine exch def
  108. dup read4 /ColorOrder exch def
  109. dup read4 /ColorSpace exch def
  110. dup readReserved
  111. dup readReserved
  112. dup readReserved
  113. dup readReserved
  114. dup read4 /NumColors exch def
  115. dup readReserved
  116. dup readReserved
  117. dup readReserved
  118. dup readReserved
  119. dup readReserved
  120. dup readReserved
  121. dup readReserved
  122. dup read4 /TotalPageCount exch def
  123. dup read4 /CrossFeedTransform exch def
  124. dup read4 /FeedTransform exch def
  125. dup read4 /ImageBoxLeft exch def
  126. dup read4 /ImageBoxTop exch def
  127. dup read4 /ImageBoxRight exch def
  128. dup read4 /ImageBoxBottom exch def
  129. dup read4 /AlternatePrimary exch def
  130. dup read4 /PrintQuality exch def
  131. dup readReserved
  132. dup readReserved
  133. dup readReserved
  134. dup readReserved
  135. dup readReserved
  136. dup read4 /VendorIdentifier exch def
  137. dup read4 /VendorLength exch def
  138. % Skip 1088 bytes of VendorData
  139. 1088 { dup read pop pop } repeat
  140. % 64 Reserved bytes
  141. dup readReserved dup readReserved dup readReserved dup readReserved
  142. dup readReserved dup readReserved dup readReserved dup readReserved
  143. dup readReserved dup readReserved dup readReserved dup readReserved
  144. dup readReserved dup readReserved dup readReserved dup readReserved
  145. dup readString /RenderingIntent exch def
  146. dup readString /PageSizeName exch def
  147. pop
  148. currentdict end
  149. } bind def
  150. /viewPWG % <file|string> viewPWG -
  151. { save 20 dict begin
  152. /saved exch def
  153. dup type /stringtype eq { (r) file } if
  154. /F exch def
  155. F readPWGheader /Header exch def
  156. % We have a dictionary with /saved, /F and /Header on the stack.
  157. % Debug dump Header fields.
  158. VPWGDEBUG { Header { exch == == } forall (----------------\n) print flush } if
  159. {
  160. % If we have no bytes left, stop
  161. F bytesavailable 0 le { exit } if
  162. % Read the page header.
  163. F readPWGpageHeader /PageHeader exch def
  164. % Push PageHeader onto the dict stack.
  165. PageHeader begin
  166. % Debug dump the Page Header.
  167. VPWGDEBUG { PageHeader { exch == == } forall (----------------\n) print flush } if
  168. % Define D to be the decoded data from F.
  169. /D F
  170. <</Width Width
  171. /BPP BitsPerPixel
  172. >> /PWGDecode filter def
  173. % Set the Colorspace appropriately.
  174. ColorSpace dup dup 3 eq exch 18 eq or exch 48 eq or
  175. { % Black or SGray or Device1
  176. /DeviceGray setcolorspace
  177. /NumComponents 1 def
  178. }
  179. { ColorSpace dup dup dup 1 eq exch 19 eq or exch 20 eq or exch 50 eq or
  180. { % Rgb or sRGB or AdobeRGB or Device3
  181. /DeviceRGB setcolorspace
  182. /NumComponents 3 def
  183. }
  184. { ColorSpace dup 6 eq exch 51 eq or
  185. { % DeviceCMYK or Device4
  186. /DeviceCMYK setcolorspace
  187. /NumComponents 4 def
  188. }
  189. { (Unsupported colorspace in PWG file.\n) print cleartomark stop
  190. }
  191. ifelse
  192. }
  193. ifelse
  194. }
  195. ifelse
  196. % Do some matrixy stuff I don't understand.
  197. matrix currentmatrix
  198. 0 1 3 { 2 copy get dup 0 ne { dup abs div } if 3 copy put pop pop } for
  199. setmatrix
  200. <<
  201. /ImageType 1
  202. /ImageMatrix [1 0 0 -1 0 Height]
  203. /BitsPerComponent BitsPerPixel NumComponents div
  204. /Width Width
  205. /Height Height
  206. /DataSource D
  207. >> image
  208. % Bin the PageHeader dictionary
  209. end
  210. showpage
  211. }
  212. loop
  213. saved end restore
  214. } bind def
  215. % This lets you do stuff on the command line like:
  216. % gs -sDEVICE=pdfwrite -o stuff%03d.pdf viewurf.ps -c "(image.urf) << /PageSize 2 index viewURFgetsize 2 array astore >> setpagedevice viewURF"
  217. % so the output size is influenced by the original image.
  218. %/viewURFgetsize % <file|string> ==> [width height]
  219. %{
  220. % save 20 dict begin
  221. % /saved exch def
  222. % dup type /stringtype eq { (r) file } if
  223. % /F exch def
  224. % F readURFheader /Header exch def
  225. % currentdict Header end begin begin
  226. % VPWGDEBUG { Header { exch == == } forall (----------------\n) print flush } if
  227. % F readURFimageHeader /ImageHeader exch def
  228. % currentdict ImageHeader end begin begin
  229. % F 0 setfileposition % reset file pointer
  230. % Width Height
  231. % saved end end end restore
  232. %} bind def