gs_l2img.ps 5.5 KB

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