-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshadow_mask
executable file
·242 lines (230 loc) · 7.07 KB
/
shadow_mask
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/python3
#+
# Simulation of pixelation of an image as per an old RGB CRT.
#
# Copyright 2015 by Lawrence D'Oliveiro <ldo@geek-central.gen.nz>. This
# script is licensed CC0
# <https://creativecommons.org/publicdomain/zero/1.0/>; do with it
# what you will.
#-
import sys
import os
import math
import qahirah as qah
from qahirah import \
CAIRO, \
Colour, \
Matrix, \
Rect, \
Vector
#+
# Constructing the shadow masks
#
# These are made deliberately coarse, so the pixel components are easily visible.
#-
component_gap = 0.1 # relative gap between R, G, B components
primary = \
( # primary colour corresponding to component index 0, 1 or 2
Colour.from_rgba((1, 0, 0)),
Colour.from_rgba((0, 1, 0)),
Colour.from_rgba((0, 0, 1)),
)
def make_triangular_shadow_mask(component) :
"constructs a mask with circular pixel components arranged in a triangle."
scale = 3
dimensions = Vector(3, math.sqrt(3))
spot_radius = 0.5 * (1 - component_gap)
spot1_pos = \
(
Vector(1, 0.5),
Vector(0.5, 0.5 + 0.5 * math.sqrt(3)),
Vector(1.5, 0.5 + 0.5 * math.sqrt(3)),
)[component]
spot_positions = list \
(
spot1_pos + offset * dimensions / 2
for offset in (Vector(0, 0), Vector(1, 1), Vector(2, 0), Vector(-1, -1), Vector(1, -1), Vector(0, -2))
# cover all the possible positions that contribute something to the pattern
)
mask = qah.ImageSurface.create \
(
format = CAIRO.FORMAT_RGB24,
dimensions = round(dimensions * scale)
)
g = \
(qah.Context.create(mask)
.set_matrix(Matrix.scale(scale))
.set_source_colour(primary[component])
.set_operator(CAIRO.OPERATOR_SOURCE)
)
for pos in spot_positions :
g.circle(pos, spot_radius)
g.fill()
#end for
return \
qah.Pattern.create_for_surface(mask).set_extend(CAIRO.EXTEND_REPEAT)
#end make_triangular_shadow_mask
def make_planar_shadow_mask(component) :
"constructs a mask with rectangular pixel components arranged horizontally."
scale = 5
dimensions = Vector(1, 1)
spot = Rect \
(
left = component_gap / 2 + component / 3,
top = component_gap / 2,
width = 1 / 3 - component_gap,
height = 1 - component_gap
)
mask = qah.ImageSurface.create \
(
format = CAIRO.FORMAT_RGB24,
dimensions = dimensions * scale
)
(qah.Context.create(mask)
.set_matrix(Matrix.scale(scale))
.set_source_colour(primary[component])
.set_operator(CAIRO.OPERATOR_SOURCE)
.rectangle(spot)
.fill()
)
return \
qah.Pattern.create_for_surface(mask).set_extend(CAIRO.EXTEND_REPEAT)
#end make_planar_shadow_mask
mask_makers = \
(
make_triangular_shadow_mask,
make_planar_shadow_mask,
)
def apply_mask(image, make_mask) :
"returns a new ImageSurface which applies the mask generated by make_mask to image.\n" \
"\n"
"To be strictly correct, each pixel element should have a uniform intensity, rather" \
" than varying based on that area of the original image.\n" \
"\n"
"Achieving this is left as an exercise for the reader. :)\n"
result_pix = image.create_like()
result = \
(qah.Context.create(result_pix)
.set_operator(CAIRO.OPERATOR_ADD)
)
mask_pix = image.create_like()
mask = qah.Context.create(mask_pix)
for component in (0, 1, 2) :
(mask
.set_source(make_mask(component))
.set_operator(CAIRO.OPERATOR_SOURCE)
.paint()
.set_source_surface(image, (0, 0))
.set_operator(CAIRO.OPERATOR_MULTIPLY)
.paint()
)
(result
.set_source_surface(mask_pix, (0, 0))
.paint()
)
#end for
result_pix.flush()
return \
result_pix
#end apply_mask
#+
# Sample image
#-
def make_sample_image() :
aspect = Vector(4, 3)
res_scale = 192
circle_radius = 1.4
grid_unit = circle_radius / 6
src = qah.ImageSurface.create \
(
format = CAIRO.FORMAT_RGB24,
dimensions = aspect * res_scale
)
ctx = \
(qah.Context.create(src)
.set_matrix(Matrix.scale(res_scale) * Matrix.translate(aspect / 2))
.set_source_colour(Colour.grey(.5))
.set_operator(CAIRO.OPERATOR_SOURCE)
.paint()
.circle(centre = (0, 0), radius = circle_radius)
.clip()
.set_source_colour(Colour.grey(0))
.paint()
.set_source_colour(Colour.grey(1))
.rectangle(Rect(- circle_radius, - grid_unit * 6.5, 2 * circle_radius, 3 * grid_unit))
.fill()
.rectangle(Rect(- circle_radius, 3 * grid_unit, 2 * circle_radius, grid_unit))
.fill()
)
for i in range(6) :
ctx.rectangle \
(
Rect(0, 0, grid_unit * 2, grid_unit * 2)
+
Vector(i * grid_unit * 2 - circle_radius, - grid_unit * 2.5)
)
ctx.source_colour = Colour.from_rgba \
(
(
(1, 0, 0, 1, 1, 0)[i],
(1, 1, 1, 0, 0, 0)[i],
(0, 1, 0, 1, 0, 1)[i],
)
)
ctx.fill()
ctx.rectangle \
(
Rect(0, 0, grid_unit * 2, grid_unit)
+
Vector(i * grid_unit * 2 - circle_radius, grid_unit * 2)
)
ctx.source_colour = Colour.grey(i / 5)
ctx.fill()
#end for
ctx.reset_clip()
pastel_blue = Colour.from_hsva((0.58, 0.66, 0.74))
pastel_green_dark = Colour.from_hsva((0.42, 0.66, 0.74))
pastel_pink = Colour.from_hsva((0.91, 0.66, 0.74))
pastel_green_light = Colour.from_hsva((0.25, 0.66, 0.74))
pastel_purple = Colour.from_hsva((0.75, 0.66, 0.74))
pastel_gold = Colour.from_hsva((0.08, 0.66, 0.74))
for \
rect, colour \
in \
(
(Rect(-7, -6, 0.95, 1.95), pastel_blue),
(Rect(-8, -6, 0.95, 5.95), pastel_green_dark),
(Rect(-8, 0, 0.95, 5.95), pastel_pink),
(Rect(-7, 4, 0.95, 1.95), pastel_gold),
(Rect(6, -6, 0.95, 1.95), pastel_blue),
(Rect(7, -6, 0.95, 5.95), pastel_green_light),
(Rect(7, 0, 0.95, 5.95), pastel_purple),
(Rect(6, 4, 0.95, 1.95), pastel_gold),
) \
:
(ctx.rectangle(rect * grid_unit)
.set_source_colour(colour)
.fill()
)
#end for
src.flush()
return \
src
#end make_sample_image
#+
# Mainline
#-
image = make_sample_image()
result_pix = qah.ImageSurface.create \
(
format = CAIRO.FORMAT_RGB24,
dimensions = Vector(len(mask_makers), 1) * image.dimensions
)
result = qah.Context.create(result_pix)
for i, make_mask in enumerate(mask_makers) :
(result
.set_source_surface(apply_mask(image, make_mask), Vector(i, 0) * image.dimensions)
.paint()
)
#end for
result_pix.write_to_png("{}.png".format(os.path.basename(sys.argv[0])))