gs_sepr.ps 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. % Copyright (C) 2001, 2002 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_sepr.ps,v 1.3 2003/04/26 18:40:16 dan Exp $
  16. % Separation color space method dictionary
  17. % verify that Separation color spaces are supported
  18. /.setseparationspace where
  19. { pop }
  20. { currentfile closefile }
  21. ifelse
  22. .currentglobal true .setglobal
  23. .cspace_util begin
  24. %
  25. % <c1> <array> apply_sepr_tint_xform <c1>' ... <cn>' <array>
  26. %
  27. % Apply the tint transform for a Separation color space color intensity
  28. % value.
  29. %
  30. /apply_sepr_tint_xform
  31. {
  32. exch //bound_0_1 exec
  33. mark exch 2 index 3 get exec
  34. counttomark 2 add -2 roll pop
  35. }
  36. bind def
  37. %
  38. % <cspace_array> build_tintxform_function <cspace_array> <proc>
  39. %
  40. % Convert a tint transformation into a function, if possible. If this is
  41. % successful, <proc> will be a function procedure (a two-element,
  42. % execute-only, executable array whose first element is the function
  43. % data structure and whose second element is the operator %execfunction.
  44. % If it is not successful, <proc> will be the same as the tint transform
  45. % in <cspace_array>.
  46. %
  47. % Note that, for PDF files, the tint transform will likely already be a
  48. % function-array. In this case, <proc> will be this array, and thus will
  49. % also be identical to the original tint transform.
  50. %
  51. % This procedure is used for both Separation and DeviceN color spaces.
  52. %
  53. /build_tintxform_function
  54. {
  55. dup 3 get dup .isencapfunction not
  56. {
  57. % Try a FunctionType 4 function first; build the function dictionary
  58. % in local VM.
  59. .currentglobal exch //false .setglobal
  60. % Make the dictionary a couple of elements large in case this fails
  61. % and we need the dictionary to build a sampled function.
  62. 6 dict begin
  63. /FunctionType 4 def
  64. /Function exch def
  65. /Domain 2 index //.cs_get_range exec def
  66. /Range 2 index 2 get //.cs_get_range exec def
  67. currentdict end
  68. { .buildfunction }
  69. .internalstopped % If .buildfunction fails then not type 4 func.
  70. { % If not then try building a sampled (type 0) func.
  71. % We are using the dictionary which was created before .buildfunction
  72. dup /Order 3 put
  73. dup /BitsPerSample 16 put
  74. { .buildsampledfunction }
  75. .internalstopped % If .buildsamplefunction fails then invalid func.
  76. { % Failed - Create a dummy tint transform function
  77. % We are using the dictionary which was created before .buildfunction
  78. dup /Function {} put % Replace invalid tint transform with dummy
  79. .buildfunction
  80. }
  81. if % Check status from .buildsamplefunction/.internalstopped
  82. }
  83. if % check status from .buildfunction/.intermalstopped
  84. % restore the VM mode
  85. exch .setglobal
  86. }
  87. if
  88. }
  89. bind def
  90. %
  91. % <array1> <array2> converttinttransform <array1> <array2'>
  92. %
  93. % Convert a Separation/DeviceN color space to use a function as a tint
  94. % transformation, if this is possible. Possible outcomes are:
  95. %
  96. % 1. The tint transform already is a function, or is a procedure that
  97. % cannot be converted to a function. In either case, <array2> is
  98. % left unchanged (<array2> == <array2'>).
  99. %
  100. % 2. The tint transform is not already a function but can be converted
  101. % to a function, and <array1> != <array2>. In this case, <array2>
  102. % is modified directly.
  103. %
  104. % 3. The tint transform is not already a function but can be converted
  105. % to a function, and <array1> == <array2>. In this case, <array2>
  106. % is copied, and the copy is modified (i.e., after the operation
  107. % <array1> != <array2>
  108. %
  109. % This slightly complex approach avoids creating an extra color space
  110. % array unnecessarily.
  111. %
  112. /converttinttransform
  113. {
  114. % convert the tint transform to a fucntion
  115. //build_tintxform_function exec
  116. % see if anything needs to be modified
  117. 1 index 3 get 2 copy eq
  118. { pop pop }
  119. {
  120. pop
  121. % see if the color space must be copied
  122. 3 copy pop eq
  123. {
  124. % copy the array into local VM
  125. .currentglobal //false .setglobal
  126. 3 -1 roll dup length array copy 3 1 roll
  127. .setglobal
  128. }
  129. if
  130. 1 index exch 3 exch put
  131. }
  132. ifelse
  133. }
  134. bind def
  135. colorspacedict
  136. /Separation
  137. mark
  138. /cs_potential_indexed_base true
  139. /cs_potential_pattern_base true
  140. /cs_potential_alternate false
  141. /cs_potential_icc_alternate false
  142. /cs_get_ncomps //ncomps_1
  143. /cs_get_range //get_range_1
  144. /cs_get_default_color { pop 1 } bind
  145. /cs_get_currentgray
  146. { //apply_sepr_tint_xform exec 2 get //.cs_get_currentgray exec }
  147. bind
  148. /cs_get_currentrgb
  149. { //apply_sepr_tint_xform exec 2 get //.cs_get_currentrgb exec }
  150. bind
  151. /cs_get_currentcmyk
  152. { //apply_sepr_tint_xform exec 2 get //.cs_get_currentcmyk exec }
  153. bind
  154. % a lot of validation is done by the cs_validate method
  155. /cs_validate
  156. {
  157. //check_array exec
  158. dup 1 get type dup /nametype ne exch /stringtype ne and
  159. //setcspace_typecheck
  160. if
  161. dup 2 get //.cs_validate exec //.cs_potential_alternate exec not
  162. //setcspace_rangecheck
  163. if
  164. dup 3 get //check_array exec xcheck not
  165. //setcspace_typecheck
  166. if
  167. }
  168. bind
  169. % substitute the base space if appropriate
  170. /cs_substitute
  171. {
  172. dup 2 get //.cs_substitute exec 2 copy eq
  173. { pop pop dup }
  174. {
  175. % retain only the new alternate space
  176. exch pop
  177. % build all new structures in local VM
  178. .currentglobal 3 1 roll //false .setglobal
  179. % construct a new array and insert the new base color space
  180. 1 index dup length array copy dup 2 4 -1 roll put
  181. % restore VM mode
  182. 3 -1 roll .setglobal
  183. }
  184. ifelse
  185. }
  186. bind
  187. %
  188. % The Ghostscript interpreter works better when tinttransform procedures
  189. % are translated into functions. Attempt to do that here.
  190. %
  191. /cs_prepare //converttinttransform
  192. %
  193. % Install the current color space.
  194. %
  195. % The current Ghostscript color space implementation requires that
  196. % color spaces that provide a base or alternative color space set
  197. % that base/alternative color space to be the current color space
  198. % before attempting to set the original color space.
  199. %
  200. /cs_install
  201. {
  202. % save the current color space
  203. currentcolorspace
  204. % set the base color space as the current color space
  205. 1 index 2 get //forcesetcolorspace
  206. % set the indexed color space; restore the earlier space on error
  207. mark 2 index
  208. { .setseparationspace }
  209. stopped
  210. { cleartomark setcolorspace stop }
  211. { pop pop pop }
  212. ifelse
  213. }
  214. bind
  215. /cs_prepare_color //validate_1
  216. %
  217. % If a Separation color space is not supported in native mode by
  218. % the current process color model, Adobe implementations will always
  219. % execute the tint transform procedure when setcolor is invoked.
  220. % Ghostscript may have turned this transform into a sampled function,
  221. % and even if this is not the case, will have sampled the transform
  222. % when the color space is first set. Some applications may depend
  223. % on the Adobe behavior, so we implement it via the
  224. % cs_complete_setcolor method.
  225. %
  226. /cs_complete_setcolor
  227. {
  228. .usealternate
  229. {
  230. currentcolor exch 3 get exec
  231. currentcolorspace 2 get //clear_setcolor_operands exec
  232. }
  233. { pop }
  234. ifelse
  235. }
  236. bind
  237. .dicttomark
  238. put
  239. end % .cspace_util
  240. .setglobal