1
0

viewpbm.ps 5.4 KB

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