home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Impressora_PDF / converter.exe / GPLGS / gs_img.ps < prev    next >
Text File  |  2002-10-08  |  22KB  |  657 lines

  1. % (C) 2002 Artifex, Inc.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_img.ps,v 1.3 2002/10/08 00:49:48 dan Exp $
  14. % image, colorimage, and imagemask implementation
  15.  
  16. %
  17. % The design of the overprint facility in Ghostscript requires that color
  18. % specifications include the color space from which they were expressed,
  19. % even after conversion to the device color model. Directly including this
  20. % information in color specifications is usually not efficient, and is
  21. % difficult to integrate into the existing code structure. The alternative
  22. % approach taken is to extend a state mechanism through the device
  23. % interface, and make the current color space, or more specifically,
  24. % certain information about the current color space, a property of this
  25. % state.
  26. %
  27. % For such a mechanism to work, it is necessary to identify all changes
  28. % to the current color space. This is accomplished in the graphic library
  29. % by funneling all changes to the current color space through the
  30. % gs_setcolorspace procedure. At the PostScript interpreter level, this
  31. % result is achieved by forcing color space changes through the
  32. % setcolorspace operator.
  33. %
  34. % Aside from explicit use of setcolorspace, PostScript provides a few
  35. % implicit methods of changing the current color space. The setgray,
  36. % setrgbcolor, and setcmykcolor operators implicitly set the color space
  37. % while explicitly setting the current color. Similarly, the colorimage
  38. % operator and the traditional form of the image operator (5 operands)
  39. % both temporarily modify the current color space while an image is
  40. % being processed. The current file is concerned with the implementation
  41. % of these two operators. In addition, the traditional form of the
  42. % imagemask operator (5 operands), while it does not affect the current
  43. % color space, is closely related to the image operator and thus is
  44. % implemented in this file as well.
  45. %
  46. % In this implementation, all sampled objects are passed through one of
  47. % the internal operators .image1, .imagemask1, .image1alpha, .image2,
  48. % .image3, or .image4, each of which handles a specific ImageType value.
  49. %
  50. % The procedures in this file are responsible for constructing
  51. % image dictionaries from a set of stack entries. This is, in principle,
  52. % a trivial exercise. In practice it appears to be far more complex,
  53. % primarily due to the need to reconstruct the original state in the
  54. % event of an error. This is a particular problem for operators such as
  55. % image, which include data source objects that may, directly or
  56. % indirectly, be procedures. When these procedures are executed, the
  57. % image operator's operands must have been cleared from the operand
  58. % stack. Hence, the operand stack cannot be used to store state
  59. % information. Similarly, the dictionary stack also cannot be used to
  60. % store state information, as the data source procedures may depend on
  61. % a particular dictionary being on the top of this stack.
  62. %
  63. % Adobe's PostScript implementations determine the extent to which the
  64. % interpreter state is restored in the event of an error by the point at
  65. % which the error is detected. Errors in the image/colorimage/imagemask
  66. % operators that are detected before the data source procedures are
  67. % executed restore the state in effect before the image was processed.
  68. % Those that are detected as part of running the data source procedures
  69. % only attempt to restore the state to that in effect at the start of
  70. % the operator that failed (or at the conclusion of the data source
  71. % procedure, if this procedure failed to push a string).
  72. %
  73. % The implementation given here follows the Adobe convention. The
  74. % mechanism used is as follows:
  75. %
  76. %   1. Check that the stack has a sufficient number of operands, and
  77. %      that enough of them have the proper type to allow construction
  78. %      of the image dictionary. Any errors at this point are handled
  79. %      in the conventional manner.
  80. %
  81. %   2. Build the image dictionary, in the process clearing the image/
  82. %      colorimage/imagemask operands from the stack. No errors can
  83. %      occur during this process.
  84. %
  85. %      (Special precautions could be taken during this step to handle
  86. %      a limitcheck or VMError during the building of the image
  87. %      dictionary, but this essentially never occurs in practice and, if
  88. %      it did, is very unlikely to leave a useable state. Hence, we don't
  89. %      bother with this possibility.)
  90. %
  91. %   3. The .image operator is executed in a stopped context. If it
  92. %      returns abnormally, a check is made to see if the uppermost
  93. %      operand on the stack is a color image dictionary. If so, the
  94. %      original stack is created anew using this dictionary. (Because
  95. %      the image operand works via colorimage, some additional special
  96. %      handling is required in this case.)
  97. %
  98.  
  99.  
  100. %
  101. % Create a dictionary of operators for specific image and image mask types.
  102. % Each of these will always handle ImageType 1. Additional types are added
  103. % as they are supported in specific interpreter levels or versions.
  104. %
  105. % These dictionaries are in systemdict for historical reasons.
  106. %
  107. .currentglobal true .setglobal
  108. systemdict begin
  109. /.imagetypes
  110.   5 dict
  111.   dup 1 /.image1 load put
  112. def
  113. /.imagemasktypes
  114.   5 dict
  115.   dup 1 /.imagemask1 load put
  116. def
  117. end
  118. .setglobal
  119.  
  120. % Build a dictionary of utility procedures and constants for use in
  121. % impelementing the image operators. This dictionary is in global VM but
  122. % is maintained (during initialization) in userdict. It should be pushed
  123. % onto the dictionary stack when constructing image-related procedures
  124. % and pseudo-operators.
  125. %
  126. % This dictionary is removed from userdict when initialization is
  127. % completed.
  128. %
  129. .currentglobal true .setglobal
  130. userdict /img_utils_dict 30 dict put
  131. img_utils_dict begin
  132.  
  133.  
  134. %
  135. % Some useful local data structures:
  136. %
  137. %   img_csary maps the number of components in an image to the implied
  138. %       color space.
  139. %
  140. %   img_decary is a prototype Decode array; subintervals of this array
  141. %       may be used for fewer than 4 color components.
  142. %
  143. %   img_params_ary is a list of the parameters to be built in the image
  144. %       dictionary for a colorimage invocation. ImageType is given a
  145. %       fixed value; the other parameters are in stack order (IMG_NComps
  146. %       is the number of components).
  147. %
  148. %   img_mask_params_ary is the equivalent of img_params_ary for imagemask
  149. %       invocations. Polarity is a proxy for Decode, and is replaced
  150. %       by the Decode key in the image dictionary.
  151. %
  152. %   img_mask_check_ary is the set of parameters that must be present in
  153. %       an image dictionary generated by an imagemask invocation. This
  154. %       differs from img_mask_params_ary in that Decode replaces Polarity.
  155. %
  156. /img_csary [ null /DeviceGray null /DeviceRGB /DeviceCMYK ] def
  157. /img_decary [ 0 1  0 1  0 1  0 1 ] def
  158.  
  159. /img_params_ary
  160.   [
  161.     /ImageType  /IMG_NComps  /MultipleDataSources  /DataSource
  162.     /ImageMatrix  /BitsPerComponent  /Height  /Width   /Decode
  163.   ]
  164. def
  165. /img_check_ary //img_params_ary def
  166. /img_unbuild_ary
  167.  //img_params_ary 1 1 index length 2 sub getinterval
  168. def
  169.  
  170. /img_mask_params_ary
  171.   [ /ImageType  /DataSource  /ImageMatrix  /Polarity  /Height  /Width ]
  172. def
  173. /img_mask_check_ary
  174.   [
  175.     /ImageType  /BitsPerComponent
  176.     /DataSource  /ImageMatrix  /Decode  /Height  /Width
  177.   ]
  178. def
  179. /img_mask_unbuild_ary
  180.  //img_mask_check_ary 2 1 index length 2 sub getinterval
  181. def
  182.  
  183.  
  184. %
  185. %   <?any?>  <array>   img_check_keys   <?any?>  <bool>
  186. %
  187. % Verify that:
  188. %   that there are at least two entries on the stack, and
  189. %   the second (lower) entry is a dictionary, and
  190. %   that dictionary contains all of the keys in the array
  191. %
  192. % If any one of these conditions does not hold, pop the array and push
  193. % false; otherwise pop the array and push true. This utility is used by
  194. % the colorimage and imagematrix procedures to determine if .image left
  195. % the image dictionary on the stack after an abnormal return.
  196. %
  197. /img_check_keys
  198.   {
  199.     count 2 ge
  200.       {
  201.         1 index type /dicttype eq
  202.           {
  203.             true exch
  204.               {
  205.                 2 index exch known and
  206.                 dup not
  207.                   { exit }
  208.                 if
  209.               }
  210.             forall
  211.           }
  212.           { pop //false }
  213.         ifelse
  214.       }
  215.       { pop //false }
  216.     ifelse
  217.   }
  218. .bind def
  219.  
  220. %
  221. % Procedures to convert a set of stack entries to a dictionary. There is
  222. % a procedure associated with each key, though most keys use the same
  223. % procedure. The dictionary to be built is at the top of the dictionary
  224. % stack. Stack handling for the procedures is:
  225. %
  226. %   <?val0?> ... <?val(n - 1)?>  <key>   proc   -
  227. %
  228. % Parameters are handle in inverse-stack order, so inter-parameter
  229. % dependencies that on the stack can generally be used here.
  230. %
  231. /img_params_dict
  232.   mark
  233.     /ImageType { 1 def } .bind
  234.  
  235.     /IMG_NComps { exch def } .bind      % number of components
  236.     /MultipleDataSources 1 index
  237.     /Width 1 index
  238.     /Height 1 index
  239.     /ImageMatrix 1 index
  240.     /BitsPerComponent 1 index
  241.     /DataSource 1 index
  242.  
  243.     % Polarity is a proxy for Decode; it never appears in a dictionary
  244.     /Polarity
  245.       {
  246.         pop
  247.           { { 1 0 } }
  248.           { { 0 1 } }
  249.         ifelse
  250.         /Decode exch cvlit def
  251.       }
  252.     .bind
  253.  
  254.     % the definition of Decode is based on the number of components
  255.     /Decode { //img_decary 0 IMG_NComps 2 mul getinterval def } .bind
  256.   .dicttomark
  257. def
  258.  
  259. %
  260. %    <oper_0>  ...  <oper_n>  <array>   img_build_dict   <dict>
  261. %
  262. % Build a dictionary. This will always be done in local VM. The array is
  263. % a list of the keys to be associated with operands on the stack, in
  264. % inverse stack order (topmost element first). The caller should verify
  265. % that the dictionary can be built successfully (except for a possible
  266. % VMerror) before calling this routine.
  267. %
  268. /img_build_dict
  269.   {
  270.     % build the dictionary in local VM; all for 2 extra entries
  271.     .currentglobal false .setglobal
  272.     1 index length 2 add dict
  273.     exch .setglobal
  274.     begin
  275.  
  276.     % process all keys in the array
  277.       { //img_params_dict 1 index get exec }
  278.     forall
  279.  
  280.     % if BitsPerComponent is not yet defined, define it to be 1
  281.     currentdict /BitsPerComponent known not
  282.       { /BitsPerComponent 1 def }
  283.     if
  284.  
  285.     currentdict end
  286.   }
  287. .bind def
  288.  
  289. %
  290. %   <dict>  <array>   img_unbuild_dict   <oper_0>  ...  <oper_n>
  291. %
  292. % "Unbuild" a dictionary: spread the contents the dictionary back onto the
  293. % stack, in the inverse of the order indicated in the array (inverse is
  294. % used as this order is more convenient for img_build_dict, which is
  295. % expected to be invoked far more frequently).
  296. %
  297. /img_unbuild_dict
  298.   {
  299.     exch begin
  300.     dup length 1 sub -1 0
  301.       { 1 index exch get load exch }
  302.     for
  303.     pop
  304.     end
  305.   }
  306. .bind def
  307.  
  308.  
  309. %
  310. %   <width>  <height>  <bits/component>  <matrix>  <dsrc0> ...
  311. %   <multi>  <ncomp>  <has_alpha>
  312. %   img_build_image_dict
  313. %   <dict>  <has_alpha>
  314. %
  315. % Build the dictionary corresponding to a colorimage operand stack. This
  316. % routine will check just enough of the stack to verify that the
  317. % dictionary can be built, and will generate the appropriate error if this
  318. % is not the case.
  319. %
  320. % The <has_alpha> boolean is used to support the Next alphaimage extension.
  321. %
  322. % At the first level, errors in this procedure are reported as colorimage
  323. % errors. The error actually reported will usually be determined by the
  324. % pseudo-operator which invokes this routine.
  325. %
  326. /img_build_image_dict
  327.   {
  328.     % Veify that at least 8 operands are available, and that the top three
  329.     % operands have the expected types
  330.     count 8 lt
  331.       { /.colorimage cvx /stackunderflow signalerror }
  332.     if
  333.     3 copy
  334.     type /booleantype ne exch
  335.     type /integertype ne or exch
  336.     type /booleantype ne or
  337.       { /.colorimage cvx /typecheck signalerror }
  338.     if
  339.  
  340.     % verify that the number of components is 1, 3, or 4
  341.     1 index 1 lt 2 index 2 eq or 2 index 4 gt or
  342.       { /.colorimage cvx /rangecheck signalerror }
  343.     if
  344.  
  345.     % Verify that the required number of operands are present if multiple
  346.     % data sources are being used. If this test is successful, convert
  347.     % the data sources to an array (in local VM).
  348.     2 index
  349.       {
  350.         % if an alpha component is present, this adds one more component
  351.         2 copy
  352.           { 1 add }
  353.         if
  354.         dup count 9 sub gt
  355.           { /.colorimage cvx /stackunderflow signalerror }
  356.         if
  357.  
  358.         % build the DataSource array in local VM
  359.         dup .currentglobal false .setglobal exch array exch .setglobal
  360.  
  361.         % stack: <w> <h> <bps> <mtx> <d0> ... <multi> <n> <alpha> <n'> <array>
  362.         5 1 roll 4 add 3 roll astore 4 1 roll
  363.       }
  364.     if
  365.  
  366.     % the image dictionary can be built; do so
  367.     % stack: <w> <h> <bps> <mtx> <dsrc|dsrc_array> <multi> <n> <alpha>
  368.     8 1 roll //img_params_ary //img_build_dict exec exch
  369.   }
  370. .bind def
  371.  
  372. %
  373. %   <?dict?>
  374. %   img_unbuild_image_dict
  375. %   <width>  <height>  <bits/component>  <matrix>  <dsrc0> ...
  376. %   <multi>  <ncomp>
  377. %
  378. % If the top entry of the stack is a dictionary that has the keys required
  379. % by a colorimage dictionary, unpack that dictionary onto the stack.
  380. % Otherwise just leave things as they are. Note that the <has_alpha>
  381. % parameter is not pushd onto the stack.
  382. %
  383. /img_unbuild_image_dict
  384.   {
  385.     //img_check_ary //img_check_keys exec
  386.       {
  387.         //img_unbuild_ary //img_unbuild_dict exec
  388.         1 index type /booleantype eq
  389.           {
  390.             1 index
  391.               { 3 1 roll aload length 2 add -2 roll }
  392.             if
  393.           }
  394.         if
  395.       }
  396.     if
  397.   }
  398. .bind def
  399.  
  400.  
  401. %
  402. %   <width>  <height>  <polarity>  <matrix>  <dsrc>
  403. %   img_unbuild_imagemask_dict
  404. %   <dict>
  405. %
  406. % Build the dictionary corresponding to an imagemask stack. This routine
  407. % will verify that the appropriate number of operands are on the stack,
  408. % and that polarity is a boolean. This is all that is necessary to build
  409. % the dictionary.
  410. %
  411. /img_build_imagemask_dict
  412.   {
  413.     % check for proper number of operands
  414.     count 5 lt
  415.       { /imagemask load /stackunderflow signalerror }
  416.     if
  417.  
  418.     % verify that polarity is a boolean
  419.     2 index type /booleantype ne
  420.       { /imagemask load /typecheck signalerror }
  421.     if
  422.  
  423.     % the imagemask dictionary can be built; do so
  424.     //img_mask_params_ary //img_build_dict exec
  425.   }
  426. .bind def
  427.  
  428. %
  429. %   <?dict?>
  430. %   img_unbuild_imagemask_dict
  431. %   <width>  <height>  <polarity>  <matrix>  <dsrc>
  432. %
  433. % If the top entry of the stack is a dictionary that has the keys rquired
  434. % by an imagemask dictionary, unpack that dictionary onto the stack.
  435. % Otherwise just leave things as they are.
  436. %
  437. /img_unbuild_imagemask_dict
  438.   {
  439.     //img_mask_check_ary //img_check_keys exec
  440.       {
  441.         //img_mask_unbuild_ary //img_unbuild_dict exec
  442.         3 -1 roll
  443.         dup type dup /arraytype eq exch /packedarraytype eq or
  444.         1 index rcheck and
  445.           { 0 get 1 eq }
  446.         if
  447.         3 1 roll
  448.       }
  449.     if
  450.   }
  451. .bind def
  452.  
  453.  
  454. %
  455. %   <width>  <height>  <bits/component>  <matrix>  <dsrc_0> ...
  456. %   <multi>  <ncomp>  <has_alpha>
  457. %   .colorimage 
  458. %   -
  459. %
  460. % Convert the image/colorimage operator from their traditional form to
  461. % the dictionary form. The <has_alpha> operand is used ot support the
  462. % Next alphaimage extension.
  463. %
  464. % Error handling for these operators is a bit complex, due to the stack
  465. % handling required of operators that potentially invoke procedures.
  466. % This problem is discussed in the comment above. The facts relevant to
  467. % this particular implementation are:
  468. %
  469. %   1. The .image1 (or .alphaimage) operator is executed in a stopped
  470. %      context, so that we can undo the gsave context in the event of
  471. %      an error.
  472. %
  473. %   2. In the event of an error, the stack is examined to see if the
  474. %      dictionary passed to .image1 (.alphaimage) is still present.
  475. %      If so, this dictionary is "unpacked" onto the stack to re-
  476. %      create the original stack. The <has_alpha> parameter is not
  477. %      pushed onto the stack, as it is not required for any of the
  478. %      pseudo-operators than invoke this procedure.
  479. %
  480. %   3. The use of pseudo-operators in this case may yield incorrect
  481. %      results for late-detected errors, as the stack depth will be
  482. %      restored (even though the stack is not). This is, however, no
  483. %      worse than the prior (level >= 2) code, so it should cause no
  484. %      new problems.
  485. %
  486. /.colorimage
  487.   {
  488.     % build the image dictionary
  489.     //img_build_image_dict exec
  490.  
  491.     % execute .image1 in a stopped context
  492.       {
  493.         gsave
  494.         0 .setoverprintmode             % disable overprint mode for images
  495.         //img_csary 2 index /IMG_NComps get get setcolorspace
  496.           { .alphaimage }
  497.           { .image1 }
  498.         ifelse
  499.       }
  500.     stopped
  501.     grestore
  502.       {
  503.         //img_unbuild_image_dict exec
  504.         /.colorimage cvx $error /errorname get
  505.         signalerror
  506.       }
  507.     if
  508.   }
  509. .bind def
  510.  
  511.  
  512. %   <width>  <height>  <bits/component>  <matrix>  <dsrc_0> ...
  513. %   <multi>  <ncomp>
  514. %   colorimage 
  515. %   -
  516. %
  517. % Build the colorimage pseudo-operator only if setcolorscreen is visible.
  518. %
  519. systemdict /setcolorscreen .knownget
  520.   {
  521.     type /operatortype eq
  522.       {
  523.         /colorimage 
  524.           {
  525.              //false
  526.                //.colorimage
  527.              stopped
  528.                { /colorimage load  $error /errorname get signalerror }
  529.              if
  530.           }
  531.         .bind systemdict begin odef end
  532.       }
  533.     if
  534.   }
  535. if
  536.  
  537.  
  538. %
  539. %   width  height  bits_per_component  matrix  data_src   image   -
  540. %   
  541. %   <dict>   image   -
  542. %
  543. % Some special handling is required for ImageType 2 (Display PostScript
  544. % pixmap images) so as to set the appropriate color space as the current
  545. % color space.
  546. %
  547. /image
  548.   {
  549.     dup type /dicttype eq languagelevel 2 ge and
  550.       { 
  551.         dup /ImageType get dup 2 eq
  552.           {
  553.             % verify the ImageType 2 is supported
  554.             //.imagetypes exch known
  555.               {
  556.                 %
  557.                 % Set either DevicePixel or DeviceRGB as the current
  558.                 % color space. DevicePixel is used if the image data is
  559.                 % to be copied directly, with only a geometric
  560.                 % transformation (PixelCopy true). The use of DeviceRGB
  561.                 % in the alternate case is not, in general, correct, and
  562.                 % reflects a current implementation limitation. Ideally,
  563.                 % an intermediate color space should be used only if
  564.                 % the source and destination color models vary; otherwise
  565.                 % the native color space corresponding to the color model
  566.                 % should be used.
  567.                 %
  568.                 % The mechanism to determine depth for the DevicePixel
  569.                 % color space when BitsPerPixel is not available is 
  570.                 % somewhat of a hack.
  571.                 %
  572.                 gsave
  573.                 0 .setoverprintmode     % disable overprintmode for images
  574.                 dup /PixelCopy .knownget dup
  575.                   { pop }
  576.                 if
  577.                   {
  578.                       [
  579.                         /DevicePixel
  580.                         currentpagedevice dup /BitsPerPixel .knownget
  581.                           { exch pop }
  582.                           {
  583.                             /GrayValues .knownget not
  584.                               { 2 }     % try a guess
  585.                             if
  586.                             ln 2 ln div round cvi
  587.                           }
  588.                         ifelse
  589.                       ]
  590.                   }
  591.                   { /DeviceRGB }
  592.                 ifelse
  593.                 setcolorspace
  594.                 //.imagetypes 2 get
  595.                 stopped
  596.                 grestore
  597.                   { /image load $error /errorname get signalerror }
  598.                 if
  599.               }
  600.               { /image load /undefined signalerror }
  601.             ifelse
  602.           }
  603.           {
  604.             gsave
  605.             0 .setoverprintmode         % disable overprintmode for images
  606.             //.imagetypes exch get
  607.             stopped
  608.             grestore
  609.               { /image load $error /errorname get signalerror }
  610.             if
  611.           }
  612.         ifelse
  613.       }
  614.       { 
  615.         //false 1 //false
  616.           //.colorimage
  617.         stopped
  618.           { /image load $error /errorname get signalerror }
  619.         if
  620.       }
  621.     ifelse
  622.   }
  623. .bind systemdict begin odef end
  624.  
  625.  
  626. %
  627. %   width  height  polarity  matrix  datasrc   imagemask   -
  628. %
  629. % See the comment preceding the definition of .colorimage for information
  630. % as to the handling of error conditions.
  631. %
  632. /imagemask
  633.   {
  634.     dup type /dicttype eq languagelevel 2 ge and
  635.       { dup /ImageType get //.imagemasktypes exch get exec }
  636.       {
  637.         //img_build_imagemask_dict exec
  638.           { .imagemask1 }
  639.         stopped
  640.           {
  641.             //img_unbuild_imagemask_dict exec
  642.             /imagemask load $error /errorname get signalerror
  643.           }
  644.         if
  645.       }
  646.     ifelse
  647.   }
  648. .bind systemdict begin odef end
  649.  
  650. end        % img_utils_dict
  651. .setglobal  % restore VM mode
  652.