viewpcx.ps 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. % Copyright (C) 1996, 1999 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: viewpcx.ps,v 1.5 2002/06/02 12:03:28 mpsuzuki Exp $
  16. % viewpcx.ps
  17. % Display a PCX file.
  18. % Requires the Level 2 `image' operator (to handle variable pixel widths).
  19. % If SCALE is defined, maps input pixels to output pixels with that scale;
  20. % if SCALE is undefined, scales the image to fit the page.
  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. /nplanes header 65 get def
  59. /bpl header 66 get2 def
  60. /palinfo header 68 get2 def
  61. /nbits bpp nplanes mul def
  62. version 5 eq
  63. { nbits 8 le
  64. { /cspace
  65. [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
  66. f fileposition
  67. 1 nbits bitshift 3 mul string
  68. fname status pop pop pop exch pop
  69. 1 index length sub f exch setfileposition
  70. f exch readstring pop
  71. exch f exch setfileposition
  72. ] def
  73. /decode [0 cspace 2 get] def
  74. }
  75. { /cspace /DeviceRGB def
  76. /decode [0 1 0 1 0 1] def
  77. }
  78. ifelse
  79. }
  80. { /cspace
  81. [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
  82. header 16 1 nbits bitshift 16 .min 3 mul getinterval
  83. ] def
  84. /decode [0 cspace 2 get] def
  85. }
  86. ifelse
  87. % Set up scaling.
  88. /SCALE where
  89. { pop
  90. % Map pixels SCALE-for-1. Assume orthogonal transformation.
  91. w 1 0 dtransform add abs div SCALE mul
  92. h 0 1 dtransform add abs div SCALE mul
  93. }
  94. { % Scale the image (uniformly) to fit the page.
  95. clippath pathbbox pop pop translate
  96. pathbbox .min exch pop exch pop ceiling
  97. dup h w gt { w mul h div exch } { h mul w div } ifelse
  98. }
  99. ifelse scale
  100. % Since the number of bytes per line is always even,
  101. % it may not match the width specification.
  102. /wbpl w bpp mul 7 add 8 idiv def
  103. % Define the data source procedure.
  104. /s1 bpl wbpl sub string def
  105. /df /readpcx load copyarray dup 0 f put cvx bind readonly
  106. 0 () /SubFileDecode filter def
  107. /dsource [ nplanes
  108. { /dsproc load copyarray
  109. dup 1 wbpl string put
  110. cvx bind readonly
  111. }
  112. repeat ] def
  113. % Construct the image dictionary.
  114. 20 dict begin % image dictionary
  115. /ImageType 1 def
  116. /Width w def
  117. /Height h def
  118. /ImageMatrix [w 0 0 h neg 0 h] def
  119. /BitsPerComponent bpp def
  120. /Decode decode def
  121. /DataSource dsource dup length 1 gt
  122. { /MultipleDataSources true def }
  123. { 0 get }
  124. ifelse def
  125. currentdict end
  126. % Finally, display the image.
  127. cspace setcolorspace
  128. image
  129. showpage
  130. df closefile
  131. f closefile
  132. end
  133. } bind def
  134. % If the program was invoked from the command line, run it now.
  135. [ shellarguments
  136. { counttomark 1 ge
  137. { ] { viewpcx } forall
  138. }
  139. { cleartomark
  140. (Usage: gs -- viewpcx.ps filename.pcx ...\n) print
  141. ( e.g.: gs -- viewpcx.ps my.pcx another.pcx\n) print flush
  142. }
  143. ifelse
  144. }
  145. { pop
  146. }
  147. ifelse