File: test_brush.py

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (336 lines) | stat: -rw-r--r-- 8,886 bytes parent folder | download | duplicates (2)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

"""
Python scripts to test GimpBrush class of libgimp API.

See comments about usage and setup in test-resource-class.py

Setup: "2. Hardness 050" brush is selected.

Testing context_set_brush
and a few other tests of class methods
are in test_resource_class.py

FUTURE: revise comparison of floats to within epsilon
"""

"""
Test a known, stock, parametric brush "2. Hardness 050"
********************************************************************************

Numeric literals might change if gimp devs change defaults.
"""
brush_default = Gimp.context_get_brush()
print( brush_default.get_id() )
assert brush_default.get_id() == "2. Hardness 050"

assert brush_default.is_generated()
assert not brush_default.is_editable()

"""
Test getters of default, stock (not editable), parametric brush
"""
success, width, height, mask_bpp, color_bpp = brush_default.get_info()
print( width, height, mask_bpp, color_bpp )
assert success
assert width     == 51
assert height    == 51
assert mask_bpp  == 1
# !!! Since parametric, not pixmap and color_bpp is zero
assert color_bpp == 0

success, width, height, mask_bpp, mask,  color_bpp, pixels = brush_default.get_pixels()
print( width, height, mask_bpp, len(mask), color_bpp, len(pixels) )
assert success
assert width     == 51
assert height    == 51
assert mask_bpp  == 1
# !!! Since parametric, not pixmap and color_bpp is zero
assert color_bpp == 0
assert len(mask)   == 2601
assert len(pixels) == 0

# !!! spacing does not return success, only a value
spacing = brush_default.get_spacing()
print(spacing)
assert spacing == 10

# shape, radius, spikes, hardness, aspect_ratio, angle
success, shape = brush_default.get_shape()
assert success
print(shape)
assert shape == Gimp.BrushGeneratedShape.CIRCLE

success, radius = brush_default.get_radius()
assert success
print(radius)
assert radius == 25.0

success, spikes = brush_default.get_spikes()
assert success
print(spikes)
assert spikes == 2

success, hardness = brush_default.get_hardness()
assert success
print(hardness)
assert hardness == 0.5

success, aspect_ratio = brush_default.get_aspect_ratio()
assert success
print(aspect_ratio)
assert aspect_ratio == 1.0

success, angle = brush_default.get_angle()
assert success
print(angle)
assert angle == 0.0


"""
Test set_spacing fails on not editable brush.
"""
success = brush_default.set_spacing(1)
assert not success
# spacing did not change
assert brush_default.get_spacing() == 10


"""
Test setters fail on non-editable brush.

When they fail, they return a value that is nullish
"""
success, returned_shape = brush_default.set_shape(Gimp.BrushGeneratedShape.DIAMOND)
print(success, returned_shape)
assert not success
assert returned_shape is Gimp.BrushGeneratedShape.CIRCLE

success, returned_radius = brush_default.set_radius(1.0)
print(success, returned_radius)
assert not success
assert returned_radius == 0.0

success, returned_spikes = brush_default.set_spikes(1.0)
print(success, returned_spikes)
assert not success
assert returned_spikes == 0

success, returned_hardness = brush_default.set_hardness(1.0)
print(success, returned_hardness)
assert not success
assert returned_hardness == 0.0

success, returned_aspect_ratio = brush_default.set_aspect_ratio(1.0)
print(success, returned_aspect_ratio)
assert not success

success, returned_angle = brush_default.set_angle(1.0)
assert not success


"""
Test non-parametric brush i.e. raster, and stock i.e. not editable
********************************************************************************

No method exists to get a specific brush.
But we can set the ID of a new brush.
"""
brush_raster = Gimp.Brush()
brush_raster.set_property("id", "Acrylic 01")
assert brush_raster.get_id()  == "Acrylic 01"
assert brush_raster.is_valid()

# raster brush is not parametric
assert not brush_raster.is_generated()

# This brush is not editable
assert not brush_raster.is_editable()

# Raster brush has spacing
spacing = brush_raster.get_spacing()
print(spacing)
assert spacing == 25

# Getters of attributes of parametric brush fail for raster brush
success, shape = brush_raster.get_shape()
assert not success
success, radius = brush_raster.get_radius()
assert not success
success, spikes = brush_raster.get_spikes()
assert not success
success, hardness = brush_raster.get_hardness()
assert not success
success, aspect_ratio = brush_raster.get_aspect_ratio()
assert not success
success, angle = brush_raster.get_angle()
assert not success


"""
Test raster brush of kind color has pixel data.
"""
brush_color = Gimp.Brush()
brush_color.set_property("id", "z Pepper")
assert brush_color.is_valid()
success, width, height, mask_bpp, mask,  color_bpp, pixels = brush_color.get_pixels()
print( width, height, mask_bpp, len(mask), color_bpp, len(pixels) )
assert success
# !!! Since is color and raster, has pixmap and color_bpp is non-zero
assert color_bpp == 3
assert len(pixels) > 0

"""
Test setting spacing on not editable, raster brush
"""
success = brush_raster.set_spacing(1)
assert not success
# spacing did not change
assert brush_raster.get_spacing() == 25


"""
Test setters fails on raster brush.
"""
success, returned_shape = brush_raster.set_shape(Gimp.BrushGeneratedShape.DIAMOND)
print(success, returned_shape)
assert not success
# Returned value is integer 0, corresponding to CIRCLE enum
assert returned_shape is Gimp.BrushGeneratedShape.CIRCLE

success, returned_radius = brush_raster.set_radius(1.0)
assert not success
success, returned_spikes = brush_raster.set_spikes(1.0)
assert not success
success, returned_hardness = brush_raster.set_hardness(1.0)
assert not success
success, returned_aspect_ratio = brush_raster.set_aspect_ratio(1.0)
assert not success
success, returned_angle = brush_raster.set_angle(1.0)
assert not success

"""
Test new brush
********************************************************************************

Parametric and editable
"""

brush_new = Gimp.Brush.new("New Brush")

# is generated and editable
assert brush_new.is_generated()
assert brush_new.is_editable()

"""
Test setters on editable brush.
"""
success = brush_new.set_spacing(20)
assert success == True
spacing = brush_new.get_spacing()
assert spacing == 20

success, returned_shape = brush_new.set_shape(Gimp.BrushGeneratedShape.DIAMOND)
print(success)
assert success == True
assert returned_shape == Gimp.BrushGeneratedShape.DIAMOND
success, shape = brush_new.get_shape()
assert success == True
assert shape == Gimp.BrushGeneratedShape.DIAMOND

success, returned_radius = brush_new.set_radius(4.0)
assert success == True
print(returned_radius)
assert returned_radius == 4.0
success, radius = brush_new.get_radius()
assert success == True
assert radius == 4.0

success, returned_spikes = brush_new.set_spikes(2)
assert success == True
assert returned_spikes == 2
success, spikes = brush_new.get_spikes()
assert success == True
assert spikes == 2

success, returned_hardness = brush_new.set_hardness(0.5)
assert success == True
print(returned_hardness)
assert returned_hardness == 0.5
success, hardness = brush_new.get_hardness()
assert success == True
assert hardness == 0.5

success, returned_aspect_ratio = brush_new.set_aspect_ratio(5.0)
assert success == True
print(returned_aspect_ratio)
assert returned_aspect_ratio == 5.0
success, aspect_ratio = brush_new.get_aspect_ratio()
assert success == True
assert aspect_ratio == 5.0

success, returned_angle = brush_new.set_angle(90.0)
assert success == True
print(returned_angle)
assert returned_angle == 90.0
success, angle = brush_new.get_angle()
assert success == True
assert angle == 90.0

"""
Test invalid enum for shape.
"""
# TODO it yields a TypeError

"""
Test clamping at upper limits.
"""

'''
Omitting this test for now.  Possible bug.
The symptoms are that it takes a long time,
and then GIMP crashes.

success, returned_radius = brush_new.set_radius(40000)
assert success == True
# upper clamp
assert returned_radius == 32767.0
success, radius = brush_new.get_radius()
assert success == True
assert radius == 32767.0
'''

success, returned_spikes = brush_new.set_spikes(22)
assert success == True
assert returned_spikes == 20
success, spikes = brush_new.get_spikes()
assert success == True
assert spikes == 20

success, returned_hardness = brush_new.set_hardness(2.0)
assert success == True
assert returned_hardness == 1.0
success, hardness = brush_new.get_hardness()
assert success == True
assert hardness == 1.0

success, returned_aspect_ratio = brush_new.set_aspect_ratio(2000)
assert success == True
assert returned_aspect_ratio == 1000.0
success, aspect_ratio = brush_new.get_aspect_ratio()
assert success == True
assert aspect_ratio == 1000.0

success, returned_angle = brush_new.set_angle(270)
assert success == True
assert returned_angle == 90
success, angle = brush_new.get_angle()
assert success == True
assert angle == 90



"""
Cleanup
"""
success = brush_new.delete()
assert success == True