Debian Codegraph @

Resource/Init/gs_img.ps

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