viewpcx.ps 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. % Copyright (C) 1996, 1999 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: viewpcx.ps,v 1.2 2000/09/19 18:29:11 lpd Exp $
  18. % viewpcx.ps
  19. % Display a PCX file.
  20. % Requires the Level 2 `image' operator (to handle variable pixel widths).
  21. % If SCALE is defined, maps input pixels to output pixels with that scale;
  22. % if SCALE is undefined, scales the image to fit the page.
  23. % ****NOTE: does not handle multi-plane images with palette.
  24. /pcxbytes [
  25. 0 1 255 {
  26. 64 string exch 0 1 63 {
  27. 3 copy exch put pop
  28. } for pop
  29. } for
  30. ] readonly def
  31. /readpcx { % - readpcx <str>
  32. f % gets replaced
  33. dup read not {
  34. pop ()
  35. } {
  36. dup 192 lt {
  37. ( ) dup 0 4 -1 roll put exch pop
  38. } {
  39. 192 sub //pcxbytes 3 -1 roll read pop get exch 0 exch getinterval
  40. } ifelse
  41. } ifelse
  42. } def
  43. /get2 % <string> <index> get2 <int>
  44. { 2 copy get 3 1 roll 1 add get 8 bitshift add
  45. } bind def
  46. /dsproc
  47. { df s readstring pop % s gets filled in
  48. s1 () ne { df s1 readstring pop pop } if % discard padding bytes
  49. } def % don't bind, must be writable
  50. /viewpcx % <filename> viewpcx -
  51. { 100 dict begin
  52. /fname 1 index def
  53. /f exch (r) file def
  54. % Read and unpack the header.
  55. /header f 128 string readstring pop def
  56. /version header 1 get def
  57. /bpp header 3 get def
  58. /w header 8 get2 header 4 get2 sub 1 add def
  59. /h header 10 get2 header 6 get2 sub 1 add def
  60. /nplanes header 65 get def
  61. /bpl header 66 get2 def
  62. /palinfo header 68 get2 def
  63. /nbits bpp nplanes mul def
  64. version 5 eq
  65. { nbits 8 le
  66. { /cspace
  67. [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
  68. f fileposition
  69. 1 nbits bitshift 3 mul string
  70. fname status pop pop pop exch pop
  71. 1 index length sub f exch setfileposition
  72. f exch readstring pop
  73. exch f exch setfileposition
  74. ] def
  75. /decode [0 cspace 2 get] def
  76. }
  77. { /cspace /DeviceRGB def
  78. /decode [0 1 0 1 0 1] def
  79. }
  80. ifelse
  81. }
  82. { /cspace
  83. [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
  84. header 16 1 nbits bitshift 16 min 3 mul getinterval
  85. ] def
  86. /decode [0 cspace 2 get] def
  87. }
  88. ifelse
  89. % Set up scaling.
  90. /SCALE where
  91. { pop
  92. % Map pixels SCALE-for-1. Assume orthogonal transformation.
  93. w 1 0 dtransform add abs div SCALE mul
  94. h 0 1 dtransform add abs div SCALE mul
  95. }
  96. { % Scale the image (uniformly) to fit the page.
  97. clippath pathbbox pop pop translate
  98. pathbbox min exch pop exch pop ceiling
  99. dup h w gt { w mul h div exch } { h mul w div } ifelse
  100. }
  101. ifelse scale
  102. % Since the number of bytes per line is always even,
  103. % it may not match the width specification.
  104. /wbpl w bpp mul 7 add 8 idiv def
  105. % Define the data source procedure.
  106. /s1 bpl wbpl sub string def
  107. /df /readpcx load copyarray dup 0 f put cvx bind readonly
  108. 0 () /SubFileDecode filter def
  109. /dsource [ nplanes
  110. { /dsproc load copyarray
  111. dup 1 wbpl string put
  112. cvx bind readonly
  113. }
  114. repeat ] def
  115. % Construct the image dictionary.
  116. 20 dict begin % image dictionary
  117. /ImageType 1 def
  118. /Width w def
  119. /Height h def
  120. /ImageMatrix [w 0 0 h neg 0 h] def
  121. /BitsPerComponent bpp def
  122. /Decode decode def
  123. /DataSource dsource dup length 1 gt
  124. { /MultipleDataSources true def }
  125. { 0 get }
  126. ifelse def
  127. currentdict end
  128. % Finally, display the image.
  129. cspace setcolorspace
  130. image
  131. showpage
  132. df closefile
  133. f closefile
  134. end
  135. } bind def
  136. % If the program was invoked from the command line, run it now.
  137. [ shellarguments
  138. { counttomark 1 ge
  139. { ] { viewpcx } forall
  140. }
  141. { cleartomark
  142. (Usage: gs -- viewpcx.ps filename.pcx ...\n) print
  143. ( e.g.: gs -- viewpcx.ps my.pcx another.pcx\n) print flush
  144. }
  145. ifelse
  146. }
  147. { pop
  148. }
  149. ifelse