~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to lib/gs_img.ps

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

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