6 os.chdir(os.path.dirname(sys.argv[0]))
7 sys.path.insert(0, '..')
9 from lib import mypaintlib, tiledsurface, brush, document, command, helpers
11 def tileConversions():
12 # fully transparent tile stays fully transparent (without noise)
13 N = mypaintlib.TILE_SIZE
14 src = zeros((N, N, 4), 'uint16')
15 dst = ones((N, N, 4), 'uint8')
16 mypaintlib.tile_convert_rgba16_to_rgba8(src, dst)
18 # fully opaque tile stays fully opaque
20 src[:,:,:3] = randint(0, 1<<15, (N, N, 3))
21 dst = zeros((N, N, 4), 'uint8')
22 mypaintlib.tile_convert_rgba16_to_rgba8(src, dst)
23 assert (dst[:,:,3] == 255).all()
27 s = tiledsurface.Surface()
28 events = loadtxt('painting30sec.dat.gz')
31 for t, x, y, pressure in events:
32 r = g = b = 0.5*(1.0+sin(t))
34 s.draw_dab(x, y, 12, r, g, b, pressure, 0.6)
36 s.save('test_directPaint.png')
40 s = tiledsurface.Surface()
42 #b.load_from_string(open('../brushes/s006.myb').read())
43 b.load_from_string(open('../brushes/charcoal.myb').read())
45 events = loadtxt('painting30sec.dat.gz')
47 b.set_color_rgb((0.0, 0.9, 1.0))
53 for t, x, y, pressure in events:
56 b.stroke_to (s, x, y, pressure, 0.0, 0.0, dtime)
58 print 'Brushpaint time:', time()-t0
59 print s.get_bbox(), b.stroke_total_painting_time # FIXME: why is this time so different each run?
61 s.save('test_brushPaint.png')
63 def files_equal(a, b):
64 return open(a, 'rb').read() == open(b, 'rb').read()
68 print a, 'and', b, 'are perfectly equal'
70 im_a = imread(a)*255.0
71 im_b = imread(b)*255.0
72 if im_a.shape != im_b.shape:
73 print a, 'and', b, 'have different size:', im_a.shape, im_b.shape
76 alpha = im_a.shape[-1] == 4
78 diff_alpha = diff[:,:,3]
81 print a, 'and', b, 'are different, analyzing whether it is just the undefined colors...'
82 print 'Average difference (255=white): (R, G, B, A)'
83 print mean(mean(diff, 0), 0)
84 print 'Average difference with premultiplied alpha (255=white): (R, G, B, A)'
87 diff *= imread(a)[:,:,3:4]
88 res = mean(mean(diff, 0), 0)
91 # dithering should make this value nearly zero...
93 print 'Maximum abs difference with premultiplied alpha (255=white): (R, G, B, A)'
94 res = amax(amax(abs(diff), 0), 0)
96 if max(abs(res)) > 1.1:
97 # this error will be visible
98 # - smaller errors are hidden by the weak alpha
99 # (but we should pay attention not to accumulate such errors at each load/save cycle...)
103 print 'Not equal enough!'
107 imshow(im_b[:,:,3], interpolation='nearest')
110 title('Green Error (multiplied with alpha)')
111 imshow(diff[:,:,1], interpolation='nearest')
116 imshow(diff_alpha, interpolation='nearest')
124 b1.load_from_string(open('../brushes/s008.myb').read())
126 b2.load_from_string(open('../brushes/redbrush.myb').read())
127 b2.set_color_hsv((0.3, 0.4, 0.35))
130 doc = document.Document()
132 events = loadtxt('painting30sec.dat.gz')
133 events = events[:len(events)/8]
136 for i, (t, x, y, pressure) in enumerate(events):
140 doc.stroke_to(dtime, x, y, pressure, 0.0, 0.0)
146 assert not doc.get_bbox().empty()
148 assert doc.get_bbox().empty()
162 doc.layers[0].surface.save('test_docPaint_a.png')
163 doc.layers[0].surface.save('test_docPaint_a1.png')
164 # the resulting images will look slightly different because of dithering
165 assert pngs_equal('test_docPaint_a.png', 'test_docPaint_a1.png')
168 doc.save('test_f1.ora')
169 doc2 = document.Document()
170 doc2.load('test_f1.ora')
171 print doc.get_bbox(), doc2.get_bbox()
172 # TODO: fix this one?!
173 #assert doc.get_bbox() == doc2.get_bbox()
174 doc2.layers[0].surface.save('test_docPaint_b.png')
175 assert pngs_equal('test_docPaint_a.png', 'test_docPaint_b.png')
176 doc2.save('test_f2.ora')
177 #check not possible, because PNGs not exactly equal:
178 #assert files_equal('test_f1.ora', 'test_f2.ora')
180 # less strict test than above (just require load-save-load-save not to alter the file)
181 doc3 = document.Document()
182 doc3.load('test_f2.ora')
183 assert doc2.get_bbox() == doc3.get_bbox()
184 doc3.layers[0].surface.save('test_docPaint_c.png')
185 assert pngs_equal('test_docPaint_b.png', 'test_docPaint_c.png')
186 doc2.save('test_f3.ora')
187 #check not possible, because PNGs not exactly equal:
188 #assert files_equal('test_f2.ora', 'test_f3.ora')
190 # note: this is not supposed to be strictly reproducible because
191 # of different random seeds [huh? what does that mean?]
192 bbox = doc.get_bbox()
193 print 'document bbox is', bbox
195 # test for appearance changes (make sure they are intended)
196 doc.save('test_docPaint_flat.png', alpha=False)
197 doc.save('test_docPaint_alpha.png', alpha=True)
198 assert pngs_equal('test_docPaint_flat.png', 'correct_docPaint_flat.png')
199 assert pngs_equal('test_docPaint_alpha.png', 'correct_docPaint_alpha.png')
201 from optparse import OptionParser
202 parser = OptionParser('usage: %prog [options]')
203 options, tests = parser.parse_args()
210 print 'Tests passed.'