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.

193 lines
5.3 KiB

1 month ago
  1. % Copyright (C) 2001-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. % viewpcx.ps
  16. % Display a PCX file.
  17. % Requires the Level 2 `image' operator (to handle variable pixel widths).
  18. % If SCALE is defined, maps input pixels to output pixels with that scale;
  19. % if SCALE is undefined, scales the image to fit the page.
  20. % If FITPAGE is true it fits the output page size to the image, honouring SCALE
  21. % ****NOTE: does not handle multi-plane images with palette.
  22. /pcxbytes [
  23. 0 1 255 {
  24. 64 string exch 0 1 63 {
  25. 3 copy exch put pop
  26. } for pop
  27. } for
  28. ] readonly def
  29. /readpcx { % - readpcx <str>
  30. f % gets replaced
  31. dup read not {
  32. pop ()
  33. } {
  34. dup 192 lt {
  35. ( ) dup 0 4 -1 roll put exch pop
  36. } {
  37. 192 sub //pcxbytes 3 -1 roll read pop get exch 0 exch getinterval
  38. } ifelse
  39. } ifelse
  40. } def
  41. /get2 % <string> <index> get2 <int>
  42. { 2 copy get 3 1 roll 1 add get 8 bitshift add
  43. } bind def
  44. /dsproc
  45. { df s readstring pop % s gets filled in
  46. s1 () ne { df s1 readstring pop pop } if % discard padding bytes
  47. } def % don't bind, must be writable
  48. /viewpcx % <filename> viewpcx -
  49. { 100 dict begin
  50. /fname 1 index def
  51. /f exch (r) file def
  52. % Read and unpack the header.
  53. /header f 128 string readstring pop def
  54. /version header 1 get def
  55. /bpp header 3 get def
  56. /w header 8 get2 header 4 get2 sub 1 add def
  57. /h header 10 get2 header 6 get2 sub 1 add def
  58. /FITPAGE where
  59. {
  60. /FITPAGE get
  61. {
  62. 5 dict begin
  63. /SCALE where
  64. {
  65. pop
  66. /Width w SCALE mul def
  67. /Height h SCALE mul def
  68. }
  69. {
  70. /Width w def
  71. /Height h def
  72. } ifelse
  73. % we've already set the image color space, so
  74. % push it on the stack, and set it again after
  75. % setting the page size
  76. <</PageSize [Width Height] >> setpagedevice
  77. end
  78. } if
  79. }
  80. {
  81. /FITPAGE false def
  82. } ifelse
  83. /nplanes header 65 get def
  84. /bpl header 66 get2 def
  85. /palinfo header 68 get2 def
  86. /nbits bpp nplanes mul def
  87. version 5 eq
  88. { nbits 8 le
  89. { /cspace
  90. [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
  91. f fileposition
  92. 1 nbits bitshift 3 mul string
  93. fname status pop pop pop exch pop
  94. 1 index length sub f exch setfileposition
  95. f exch readstring pop
  96. exch f exch setfileposition
  97. ] def
  98. /decode [0 cspace 2 get] def
  99. }
  100. { /cspace /DeviceRGB def
  101. /decode [0 1 0 1 0 1] def
  102. }
  103. ifelse
  104. }
  105. { /cspace
  106. [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
  107. header 16 1 nbits bitshift 16 .min 3 mul getinterval
  108. ] def
  109. /decode [0 cspace 2 get] def
  110. }
  111. ifelse
  112. % Set up scaling.
  113. /SCALE where
  114. {
  115. pop
  116. FITPAGE
  117. {
  118. % Map pixels SCALE-for-1. Assume orthogonal transformation.
  119. w SCALE mul
  120. h SCALE mul
  121. }
  122. {
  123. % Map pixels SCALE-for-1. Assume orthogonal transformation.
  124. w 1 0 dtransform add abs div SCALE mul
  125. h 0 1 dtransform add abs div SCALE mul
  126. } ifelse
  127. }
  128. {
  129. FITPAGE
  130. {
  131. w h
  132. }
  133. {
  134. % Scale the image (uniformly) to fit the page.
  135. clippath pathbbox pop pop translate
  136. pathbbox .min exch pop exch pop ceiling
  137. dup h w gt { w mul h div exch } { h mul w div } ifelse
  138. } ifelse
  139. }
  140. ifelse scale
  141. % Since the number of bytes per line is always even,
  142. % it may not match the width specification.
  143. /wbpl w bpp mul 7 add 8 idiv def
  144. % Define the data source procedure.
  145. /s1 bpl wbpl sub string def
  146. /df /readpcx load copyarray dup 0 f put cvx bind readonly
  147. 0 () /SubFileDecode filter def
  148. /dsource [ nplanes
  149. { /dsproc load copyarray
  150. dup 1 wbpl string put
  151. cvx bind readonly
  152. }
  153. repeat ] def
  154. % Construct the image dictionary.
  155. 20 dict begin % image dictionary
  156. /ImageType 1 def
  157. /Width w def
  158. /Height h def
  159. /ImageMatrix [w 0 0 h neg 0 h] def
  160. /BitsPerComponent bpp def
  161. /Decode decode def
  162. /DataSource dsource dup length 1 gt
  163. { /MultipleDataSources true def }
  164. { 0 get }
  165. ifelse def
  166. currentdict end
  167. % Finally, display the image.
  168. cspace setcolorspace
  169. image
  170. showpage
  171. df closefile
  172. f closefile
  173. end
  174. } bind def
  175. % If the program was invoked from the command line, run it now.
  176. [ .shellarguments
  177. { counttomark 1 ge
  178. { ] { viewpcx } forall
  179. }
  180. { cleartomark
  181. (Usage: gs -- viewpcx.ps filename.pcx ...\n) print
  182. ( e.g.: gs -- viewpcx.ps my.pcx another.pcx\n) print flush
  183. (From version 9.50 you must supply permissions for this program to read the input file(s)\n) print flush
  184. (either by using -dNOSAFER or by supplying --permit-file-read=<filename>\n) = flush
  185. }
  186. ifelse
  187. }
  188. { pop
  189. }
  190. ifelse