viewpbm.ps 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. % Copyright (C) 1992, 1995, 1996, 1998, 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: viewpbm.ps,v 1.5 2002/06/02 12:03:28 mpsuzuki Exp $
  16. % viewpbm.ps
  17. % Display a PBM/PGM/PPM 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. /s 100 string def
  22. /readmaxv { % <file> readmaxv -
  23. 10 string readline pop cvx exec /maxv exch def
  24. } bind def
  25. /readrow { % <file> <row> readrow <row>
  26. 0 1 2 index length 1 sub {
  27. 1 index exch 3 index token pop put
  28. } for exch pop
  29. } bind def
  30. /read01 { % <file> <count> read01 <byte>
  31. 0 exch {
  32. 1 index read pop 48 xor dup 1 le { exch dup add add } { pop } ifelse
  33. } repeat
  34. } bind def
  35. /readrow01 { % <file> <row> readrow01 <row>
  36. 0 1 w 8 idiv {
  37. 1 index exch 3 index 8 read01 put
  38. } for
  39. wrem 0 ne {
  40. dup rsize 1 sub wrem read01 8 wrem sub bitshift put
  41. } if
  42. exch pop
  43. } bind def
  44. /readwh { % <file> readwh <w> <h>
  45. dup s readline pop % check for comment
  46. (#) anchorsearch {
  47. pop pop dup s readline pop
  48. } if
  49. cvx exec
  50. } bind def
  51. /pbmtypes mark
  52. % The procedures in this dictionary are called as
  53. % <file> Pn <w> <h> <readproc>
  54. /P1 { % ASCII 1-bit white/black
  55. /bpc 1 def /maxv 1 def /rsize w 7 add 8 idiv def
  56. /wrem w 8 mod def
  57. /ncomp 1 def /invert true def /DeviceGray setcolorspace
  58. readwh
  59. { readrow01 }
  60. } bind
  61. /P2 { % ASCII 8-bit gray
  62. readwh
  63. /bpc 8 def 2 index readmaxv /rsize 2 index def
  64. /ncomp 1 def /invert false def /DeviceGray setcolorspace
  65. { readrow }
  66. } bind
  67. /P3 { % ASCII 8-bit RGB
  68. readwh
  69. /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
  70. /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  71. { readrow }
  72. } bind
  73. /P4 { % Binary 1-bit white/black
  74. readwh
  75. /bpc 1 def /maxv 1 def /rsize 2 index 7 add 8 idiv def
  76. /ncomp 1 def /invert true def /DeviceGray setcolorspace
  77. { readstring pop }
  78. } bind
  79. /P5 { % Binary 8-bit gray
  80. readwh
  81. /bpc 8 def 2 index readmaxv /rsize 2 index def
  82. /ncomp 1 def /invert false def /DeviceGray setcolorspace
  83. { readstring pop }
  84. } bind
  85. /P6 { % Binary 8-bit RGB
  86. readwh
  87. /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
  88. /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  89. { readstring pop }
  90. } bind
  91. .dicttomark readonly def
  92. /pbmsetup { % <file> <w> <h> <readproc> runpbm -
  93. /readproc exch def
  94. /h exch def
  95. /w exch def
  96. /f exch def
  97. 20 dict begin % image dictionary
  98. /ImageType 1 def
  99. /Width w def
  100. /Height h def
  101. /ImageMatrix [w 0 0 h neg 0 h] def
  102. /BitsPerComponent bpc def
  103. /Decode [ 0 255 maxv div invert { exch } if ncomp 1 sub { 2 copy } repeat ] def
  104. /DataSource [ f rsize string /readproc load /exec load ] cvx def
  105. currentdict end
  106. } def
  107. /imagescale { % <imagedict> imagescale -
  108. begin
  109. /SCALE where {
  110. pop
  111. % Map pixels SCALE-for-1. Assume orthogonal transformation.
  112. Width 1 0 dtransform add abs div SCALE mul
  113. Height 0 1 dtransform add abs div SCALE mul
  114. } {
  115. % Scale the image (uniformly) to fit the page.
  116. clippath pathbbox pop pop translate
  117. pathbbox .min exch pop exch pop ceiling
  118. dup Height Width gt {
  119. Width mul Height div exch
  120. } {
  121. Height mul Width div
  122. } ifelse
  123. }
  124. ifelse scale
  125. end
  126. } def
  127. % Image a PBM file page by page.
  128. /viewpbm { % <filename> viewpbm -
  129. 20 dict begin
  130. (r) file /pf exch def {
  131. pf token not { exit } if
  132. pbmtypes exch get pf exch exec pbmsetup
  133. dup imagescale image showpage
  134. } loop
  135. end
  136. } def
  137. % Reassemble a composite PBM file from the CMYK separations.
  138. /viewpsm {
  139. 20 dict begin
  140. /fname exch def
  141. /sources [ 0 1 3 {
  142. /plane exch def
  143. /pf fname (r) file def
  144. pf pbmtypes pf token pop get exec
  145. % Stack: pf w h readproc
  146. plane {
  147. /readproc exch def /h exch def /w exch def pop
  148. /row rsize string def
  149. h { pf row readproc pop } repeat
  150. pf pbmtypes pf token pop get exec
  151. } repeat
  152. pbmsetup
  153. } for ] def
  154. /datas [ sources { /DataSource get 0 get } forall ] def
  155. /decode sources 0 get /Decode get
  156. dup 0 get exch 1 get add cvi 0 exch
  157. 2 copy 4 copy 8 array astore def
  158. sources 0 get
  159. dup /MultipleDataSources true put
  160. dup /DataSource datas put
  161. dup /Decode decode put
  162. /DeviceCMYK setcolorspace
  163. dup imagescale image showpage
  164. end
  165. } def
  166. % If the program was invoked from the command line, run it now.
  167. [ shellarguments
  168. { counttomark 1 ge
  169. { ] { viewpbm } forall
  170. }
  171. { cleartomark
  172. (Usage: gs [--] viewpbm.ps filename.p*m ...\n) print
  173. ( e.g.: gs [--] viewpbm.ps my.ppm another.ppm\n) print flush
  174. }
  175. ifelse
  176. }
  177. { pop
  178. }
  179. ifelse