-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.ps
174 lines (162 loc) · 4 KB
/
util.ps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
% PostScript General Utility Procedures
% by Raymond Luckhurst, scriptit.uk
% test if running ghostscript interpreter
% - is_gs <bool>
/is_gs {
product (Ghostscript) search { pop pop pop true }{ pop false } ifelse
} bind def
% maximum / minimum value
/max where {
pop % gs
}{
/max { 2 copy lt { exch } if pop } bind def
/min { 2 copy gt { exch } if pop } bind def
} ifelse
% mm to points
/mm {
360 mul 127 div % x 72 / 25.4
} bind def
% points to mm
/pt {
127 mul 360 div % x 25.4 / 72
} bind def
% current x point
/x {
currentpoint pop
} bind def
% current y point
/y {
currentpoint exch pop
} bind def
% 24-bit RGB to red green blue values 0.0 - 1.0
% <int> rgb <red> <green> <blue>
/rgb {
16#FFFFFF and
dup dup
-16 bitshift 255 div
exch -8 bitshift 16#FF and 255 div
3 -1 roll 16#FF and 255 div
} bind def
% test if RGB is gray and leave value 0.0 - 1.0
% <red> <green> <blue> is_gray <gray> true
% or <red> <green> <blue> false
/is_gray {
2 copy eq 3 index 3 index eq and
dup { 3 1 roll pop pop } if
} bind def
% create chequered image data source
% TODO: do it in PaintProc instead
% <size> <colour1> <colour2> chequereddata <width> <height> <bits> <components> <samples>
/chequereddata {
2 {
[
exch dup 16#FFFFFF and
-16 bitshift exch dup 16#FFFF and
-8 bitshift exch 16#FF and
3 copy eq exch 2 index eq and { pop pop } if % grey
] exch
} repeat
2 copy length exch length eq not {
2 {
exch dup length 1 eq { aload pop dup dup 3 array astore } if
} repeat
} if
dup length 3 index dup add dup mul mul string
0
2 {
4 index {
4 index { 3 index { 3 copy put pop 1 add } forall } repeat
4 index { 2 index { 3 copy put pop 1 add } forall } repeat
} repeat
4 2 roll exch 4 2 roll
} repeat
pop
4 -1 roll dup add dup
8
6 -2 roll pop length
5 -1 roll
} bind def
% extract width, height, bits per sample, number of components, sample data from JPEG file
% <filename> jpegdata <width> <height> <bits> <components> <samples>
/jpegdata {
dup status pop pop pop exch pop exch % file size
(r) file exch string readstring pop % raw file data
dup
dup length
2
{
2 copy lt { 0 0 0 0 7 4 roll exit } if % EOF
2 index 1 index get
16#FF ne { 0 0 0 0 7 4 roll exit } if % error
1 add
2 index 1 index get
16#C0 eq { % SOF0 marker
3 add
2 index 1 index get % precision
4 1 roll % bits
1 add
2 index 1 index get 8 bitshift 3 index 2 index 1 add get or % height
5 1 roll
2 add
2 index 1 index get 8 bitshift 3 index 2 index 1 add get or % width
6 1 roll
2 add
2 index 1 index get % components
4 1 roll
exit
} if
1 add
2 index 1 index get 8 bitshift 3 index 2 index 1 add get or
add
} loop
pop pop pop
5 -1 roll
1 index 5 index 5 index mul mul dup 0 eq { pop 65535 } if string
exch /DCTDecode filter exch readstring pop % decoded string
} bind def
% make tiling pattern from image data
% <width> <height> <bits> <samples> imagetile <pattern>
/imagetile {
5 dict begin
/$samples exch def
/$cpts exch def
/$bits exch def
/$height exch def
/$width exch def
/$id << % image dictionary
/ImageType 1 % opaque
/Width $width
/Height $height
/ImageMatrix [$width 0 0 $height neg 0 $height]
/DataSource $samples % image
/BitsPerComponent $bits
/Decode $cpts 1 eq { [0 1] }{ [0 1 0 1 0 1] } ifelse % as recommended
>> def
$cpts 1 eq { /DeviceGray }{ /DeviceRGB } ifelse setcolorspace
<< % pattern dictionary
/PatternType 1 % tiling
/PaintType 1 % coloured
/TilingType 3 % faster
/BBox [0 0 1 1]
/XStep 1
/YStep 1
/id $id % additional
/PaintProc { begin id image end } bind
>>
$width $height neg idtransform matrix scale makepattern
end
} bind def
% set colour space and colour/pattern
% <int or filename> colour -
/colour {
dup type /integertype eq { % plain colour
rgb is_gray { setgray }{ setrgbcolor } ifelse
}{ % tiled image
dup type /stringtype eq { % filename
jpegdata
}{ % [size colour1 colour2] chequered
aload pop chequereddata
} ifelse
imagetile setpattern
} ifelse
} bind def