]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/ghostscript/gs_l2img.ps
aux/cpuid: decode family and model bitfields
[plan9front.git] / sys / lib / ghostscript / gs_l2img.ps
1 %    Copyright (C) 1995, 1996 Aladdin Enterprises.  All rights reserved.
2
3 % This software is provided AS-IS with no warranty, either express or
4 % implied.
5
6 % This software is distributed under license and may not be copied,
7 % modified or distributed except as expressly authorized under the terms
8 % of the license contained in the file LICENSE in this distribution.
9
10 % For more information about licensing, please refer to
11 % http://www.ghostscript.com/licensing/. For information on
12 % commercial licensing, go to http://www.artifex.com/licensing/ or
13 % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15
16 % $Id: gs_l2img.ps,v 1.4 2002/02/21 21:49:28 giles Exp $
17 % Emulate the Level 2 dictionary-based image operator in Level 1,
18 % except for Interpolate (ignored) and MultipleDataSources = true;
19 % also, we require that the data source be either a procedure of a
20 % particular form or a stream, not a string or a general procedure.
21
22 % pdf2ps copies the portion of this file from %BEGIN to %END if Level 1
23 % compatible output is requested.
24
25 /currentglobal where
26  { pop currentglobal { setglobal } true setglobal }
27  { { } }
28 ifelse
29
30 /packedarray where
31  { pop }
32  { /packedarray { array astore readonly } bind def }
33 ifelse
34
35 %BEGIN
36
37 11 dict /.csncompdict 1 index def begin
38   /DeviceGray { 1 /setgray load } bind def
39   /DeviceRGB { 3 /setrgbcolor load } bind def
40   /DeviceCMYK { 4 /setcmykcolor load } bind def
41   /Indexed
42    { dup 1 index 1 get //.csncompdict exch get exec
43                 % Stack: [/Indexed base hival map] ncomp basesetcolor
44      3 -1 roll 3 get mark 3 1 roll
45                 % Stack: ncomp -mark- basesetcolor map
46      dup type /stringtype eq
47       {  { -
48             { exch round cvi get 255 div
49             }
50            -
51             { exch round cvi 3 mul 2 copy 2 copy get 255 div
52               3 1 roll 1 add get 255 div
53               4 2 roll 2 add get 255 div
54             }
55             { exch round cvi 4 mul 2 copy 2 copy 2 copy get 255 div
56               3 1 roll 1 add get 255 div
57               4 2 roll 2 add get 255 div
58               5 3 roll 3 add get 255 div
59             }
60          }
61         4 index get aload pop counttomark -1 roll
62       }
63       { /exec load 3 -1 roll
64                 % Stack: -mark- mapproc --exec-- basesetcolor
65       }
66      ifelse .packtomark cvx
67      exch pop 1 exch
68    } bind def
69   /Separation
70    { dup 2 index //.csncompdict exch get exec
71                 % Stack: [/Separation name alt xform] ncomp altsetcolor
72      3 -1 roll 3 get /exec load 3 -1 roll 3 array astore readonly cvx
73      exch pop 1 exch
74    } bind def
75         % Substitute device spaces for CIE spaces.
76   /CIEBasedA /DeviceGray load def
77   /CIEBasedABC /DeviceRGB load def
78   /CIEBasedDEF /DeviceRGB load def
79   /CIEBasedDEFG /DeviceCMYK load def
80 end
81
82 /.packtomark { counttomark packedarray exch pop } bind def
83
84 /.csinextbits           % - .csinextbits <bits>
85                         % Uses b, nnb, i, row, mask, BitsPerComponent;
86                         % sets b, nnb, i.
87  { /nnb nnb BitsPerComponent add
88     { dup 0 le { exit } if
89       /b b 8 bitshift row i get add def
90       /i i 1 add def  8 sub
91     }
92    loop def
93    b nnb bitshift mask and
94  } bind def
95
96 % Note that the ColorSpace key must be present in the image dictionary.
97 /.colorspaceimage               % <imagedict> .colorspaceimage -
98  { save exch
99    dup length 15 add dict begin { cvlit def } forall
100    ColorSpace dup dup type /nametype ne { 0 get } if
101    .csncompdict exch get exec
102      /setpixelcolor exch def  /ncomp exch def  pop
103    /row ncomp BitsPerComponent mul Width mul 7 add 8 idiv string def
104       /mask 1 BitsPerComponent bitshift 1 sub def
105       /nextbits BitsPerComponent 8 eq
106        { { row i get /i i 1 add def } }
107        { /.csinextbits load }
108       ifelse def
109       /nextpixel mark 0 2 ncomp 1 sub 2 mul
110        { /nextbits cvx exch
111          Decode exch 2 getinterval
112          dup aload pop exch sub
113          dup mask eq { pop } { mask div /mul load 3 -1 roll } ifelse
114          0 get dup 0 eq { pop } { /sub load 3 -1 roll } ifelse
115        }
116       for
117       /setpixelcolor load dup type /operatortype ne { /exec load } if
118       .packtomark cvx def
119       /readrow
120         /DataSource load dup type
121         dup /arraytype eq exch /packedarraytype eq or
122         {       % Must be { <file> <string> ... }
123           aload length 1 add array /pop load exch astore
124           dup 1 row put cvx
125         }
126         { pop
127                 % Adobe requires readstring to signal an error if given
128                 % an empty string.  Work around this nonsense here.
129           row length 0 eq
130            { { } }
131            { { DataSource row readstring pop pop } }
132           ifelse
133         }
134       ifelse def
135       ImageMatrix matrix invertmatrix concat
136       /imat matrix def
137       0 1 Height 1 sub
138        { imat 5 3 -1 roll neg put
139          readrow
140          /b 0 def  /nnb 0 def  /i 0 def
141          0 1 Width 1 sub
142           { imat 4 3 -1 roll neg put nextpixel
143             1 1 true imat {<80>} imagemask
144           }
145          for
146        }
147       for
148    end restore
149  } bind def
150
151 %END
152 exec
153 currentfile closefile
154
155 % Patch for testing.
156 /.cincompdict 3 dict begin
157   1 { {0 1} {/DeviceGray} } def
158   3 { {0 1 0 1 0 1} {/DeviceRGB} } def
159   4 { {0 1 0 1 0 1 0 1} {/DeviceCMYK} } def
160 currentdict end def
161 /.imagekeys [
162   /Decode /DataSource /ImageMatrix /BitsPerComponent /Height /Width
163 ] def
164 /colorimage             % <width> <height> <bits/comp> <matrix>
165                         %   <datasrc> false <ncomp> colorimage -
166  { 1 index { /colorimage load /rangecheck signalerror } if exch pop
167    //.cincompdict exch get exec
168    7 dict begin /ColorSpace exch cvlit def
169    .imagekeys { exch cvlit def } forall
170    currentdict end .colorspaceimage
171  } bind odef
172 /image
173  { dup type /dicttype ne
174     { 7 dict begin /ColorSpace /DeviceGray def [0 1] 
175       .imagekeys { exch cvlit def } forall
176       currentdict end
177     }
178     { dup length 1 add dict .copydict dup /ColorSpace currentcolorspace put
179     }
180    ifelse
181    .colorspaceimage
182  } bind odef
183
184 exec