gs_l2img.ps 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. % Copyright (C) 1995, 1996 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: gs_l2img.ps,v 1.2 2000/09/19 18:29:11 lpd Exp $
  18. % Emulate the Level 2 dictionary-based image operator in Level 1,
  19. % except for Interpolate (ignored) and MultipleDataSources = true;
  20. % also, we require that the data source be either a procedure of a
  21. % particular form or a stream, not a string or a general procedure.
  22. % pdf2ps copies the portion of this file from %BEGIN to %END if Level 1
  23. % compatible output is requested.
  24. /currentglobal where
  25. { pop currentglobal { setglobal } true setglobal }
  26. { { } }
  27. ifelse
  28. /packedarray where
  29. { pop }
  30. { /packedarray { array astore readonly } bind def }
  31. ifelse
  32. %BEGIN
  33. 11 dict /.csncompdict 1 index def begin
  34. /DeviceGray { 1 /setgray load } bind def
  35. /DeviceRGB { 3 /setrgbcolor load } bind def
  36. /DeviceCMYK { 4 /setcmykcolor load } bind def
  37. /Indexed
  38. { dup 1 index 1 get //.csncompdict exch get exec
  39. % Stack: [/Indexed base hival map] ncomp basesetcolor
  40. 3 -1 roll 3 get mark 3 1 roll
  41. % Stack: ncomp -mark- basesetcolor map
  42. dup type /stringtype eq
  43. { { -
  44. { exch round cvi get 255 div
  45. }
  46. -
  47. { exch round cvi 3 mul 2 copy 2 copy get 255 div
  48. 3 1 roll 1 add get 255 div
  49. 4 2 roll 2 add get 255 div
  50. }
  51. { exch round cvi 4 mul 2 copy 2 copy 2 copy get 255 div
  52. 3 1 roll 1 add get 255 div
  53. 4 2 roll 2 add get 255 div
  54. 5 3 roll 3 add get 255 div
  55. }
  56. }
  57. 4 index get aload pop counttomark -1 roll
  58. }
  59. { /exec load 3 -1 roll
  60. % Stack: -mark- mapproc --exec-- basesetcolor
  61. }
  62. ifelse .packtomark cvx
  63. exch pop 1 exch
  64. } bind def
  65. /Separation
  66. { dup 2 index //.csncompdict exch get exec
  67. % Stack: [/Separation name alt xform] ncomp altsetcolor
  68. 3 -1 roll 3 get /exec load 3 -1 roll 3 array astore readonly cvx
  69. exch pop 1 exch
  70. } bind def
  71. % Substitute device spaces for CIE spaces.
  72. /CIEBasedA /DeviceGray load def
  73. /CIEBasedABC /DeviceRGB load def
  74. /CIEBasedDEF /DeviceRGB load def
  75. /CIEBasedDEFG /DeviceCMYK load def
  76. end
  77. /.packtomark { counttomark packedarray exch pop } bind def
  78. /.csinextbits % - .csinextbits <bits>
  79. % Uses b, nnb, i, row, mask, BitsPerComponent;
  80. % sets b, nnb, i.
  81. { /nnb nnb BitsPerComponent add
  82. { dup 0 le { exit } if
  83. /b b 8 bitshift row i get add def
  84. /i i 1 add def 8 sub
  85. }
  86. loop def
  87. b nnb bitshift mask and
  88. } bind def
  89. % Note that the ColorSpace key must be present in the image dictionary.
  90. /.colorspaceimage % <imagedict> .colorspaceimage -
  91. { save exch
  92. dup length 15 add dict begin { cvlit def } forall
  93. ColorSpace dup dup type /nametype ne { 0 get } if
  94. .csncompdict exch get exec
  95. /setpixelcolor exch def /ncomp exch def pop
  96. /row ncomp BitsPerComponent mul Width mul 7 add 8 idiv string def
  97. /mask 1 BitsPerComponent bitshift 1 sub def
  98. /nextbits BitsPerComponent 8 eq
  99. { { row i get /i i 1 add def } }
  100. { /.csinextbits load }
  101. ifelse def
  102. /nextpixel mark 0 2 ncomp 1 sub 2 mul
  103. { /nextbits cvx exch
  104. Decode exch 2 getinterval
  105. dup aload pop exch sub
  106. dup mask eq { pop } { mask div /mul load 3 -1 roll } ifelse
  107. 0 get dup 0 eq { pop } { /sub load 3 -1 roll } ifelse
  108. }
  109. for
  110. /setpixelcolor load dup type /operatortype ne { /exec load } if
  111. .packtomark cvx def
  112. /readrow
  113. /DataSource load dup type
  114. dup /arraytype eq exch /packedarraytype eq or
  115. { % Must be { <file> <string> ... }
  116. aload length 1 add array /pop load exch astore
  117. dup 1 row put cvx
  118. }
  119. { pop
  120. % Adobe requires readstring to signal an error if given
  121. % an empty string. Work around this nonsense here.
  122. row length 0 eq
  123. { { } }
  124. { { DataSource row readstring pop pop } }
  125. ifelse
  126. }
  127. ifelse def
  128. ImageMatrix matrix invertmatrix concat
  129. /imat matrix def
  130. 0 1 Height 1 sub
  131. { imat 5 3 -1 roll neg put
  132. readrow
  133. /b 0 def /nnb 0 def /i 0 def
  134. 0 1 Width 1 sub
  135. { imat 4 3 -1 roll neg put nextpixel
  136. 1 1 true imat {<80>} imagemask
  137. }
  138. for
  139. }
  140. for
  141. end restore
  142. } bind def
  143. %END
  144. exec
  145. currentfile closefile
  146. % Patch for testing.
  147. /.cincompdict 3 dict begin
  148. 1 { {0 1} {/DeviceGray} } def
  149. 3 { {0 1 0 1 0 1} {/DeviceRGB} } def
  150. 4 { {0 1 0 1 0 1 0 1} {/DeviceCMYK} } def
  151. currentdict end def
  152. /.imagekeys [
  153. /Decode /DataSource /ImageMatrix /BitsPerComponent /Height /Width
  154. ] def
  155. /colorimage % <width> <height> <bits/comp> <matrix>
  156. % <datasrc> false <ncomp> colorimage -
  157. { 1 index { /colorimage load /rangecheck signalerror } if exch pop
  158. //.cincompdict exch get exec
  159. 7 dict begin /ColorSpace exch cvlit def
  160. .imagekeys { exch cvlit def } forall
  161. currentdict end .colorspaceimage
  162. } bind odef
  163. /image
  164. { dup type /dicttype ne
  165. { 7 dict begin /ColorSpace /DeviceGray def [0 1]
  166. .imagekeys { exch cvlit def } forall
  167. currentdict end
  168. }
  169. { dup length 1 add dict .copydict dup /ColorSpace currentcolorspace put
  170. }
  171. ifelse
  172. .colorspaceimage
  173. } bind odef
  174. exec