File: compositing_test.py

package info (click to toggle)
python-mapnik 1%3A0.0~20200224-7da019cf9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,784 kB
  • sloc: python: 12,085; cpp: 5,717; sh: 101; makefile: 18
file content (321 lines) | stat: -rw-r--r-- 12,364 bytes parent folder | download | duplicates (4)
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
# encoding: utf8

from __future__ import print_function

import os

from nose.tools import eq_
from nose.plugins.skip import SkipTest

import mapnik

from .utilities import (execution_path, get_unique_colors, pixel2channels,
                        run_all, side_by_side_image)


def setup():
    # All of the paths used are relative, if we run the tests
    # from another directory we need to chdir()
    os.chdir(execution_path('.'))


def is_pre(color, alpha):
    return (color * 255.0 / alpha) <= 255


def debug_image(image, step=2):
    for x in range(0, image.width(), step):
        for y in range(0, image.height(), step):
            pixel = image.get_pixel(x, y)
            red, green, blue, alpha = pixel2channels(pixel)
            print(
                "rgba(%s,%s,%s,%s) at %s,%s" %
                (red, green, blue, alpha, x, y))


def replace_style(m, name, style):
    m.remove_style(name)
    m.append_style(name, style)

# note: it is impossible to know for all pixel colors
# we can only detect likely cases of non premultiplied colors


def validate_pixels_are_not_premultiplied(image):
    over_alpha = False
    transparent = True
    fully_opaque = True
    for x in range(0, image.width(), 2):
        for y in range(0, image.height(), 2):
            pixel = image.get_pixel(x, y)
            red, green, blue, alpha = pixel2channels(pixel)
            if alpha > 0:
                transparent = False
                if alpha < 255:
                    fully_opaque = False
                color_max = max(red, green, blue)
                if color_max > alpha:
                    over_alpha = True
    return over_alpha or transparent or fully_opaque


def validate_pixels_are_not_premultiplied2(image):
    looks_not_multiplied = False
    for x in range(0, image.width(), 2):
        for y in range(0, image.height(), 2):
            pixel = image.get_pixel(x, y)
            red, green, blue, alpha = pixel2channels(pixel)
            # each value of the color channels will never be bigger than that
            # of the alpha channel.
            if alpha > 0:
                if red > 0 and red > alpha:
                    print('red: %s, a: %s' % (red, alpha))
                    looks_not_multiplied = True
    return looks_not_multiplied


def validate_pixels_are_premultiplied(image):
    bad_pixels = []
    for x in range(0, image.width(), 2):
        for y in range(0, image.height(), 2):
            pixel = image.get_pixel(x, y)
            red, green, blue, alpha = pixel2channels(pixel)
            if alpha > 0:
                pixel = image.get_pixel(x, y)
                is_valid = ((0 <= red <= alpha) and is_pre(red, alpha)) \
                    and ((0 <= green <= alpha) and is_pre(green, alpha)) \
                    and ((0 <= blue <= alpha) and is_pre(blue, alpha)) \
                    and (alpha >= 0 and alpha <= 255)
                if not is_valid:
                    bad_pixels.append(
                        "rgba(%s,%s,%s,%s) at %s,%s" %
                        (red, green, blue, alpha, x, y))
    num_bad = len(bad_pixels)
    return (num_bad == 0, bad_pixels)


def test_compare_images():
    b = mapnik.Image.open('./images/support/b.png')
    b.premultiply()
    num_ops = len(mapnik.CompositeOp.names)
    successes = []
    fails = []
    for name in mapnik.CompositeOp.names:
        a = mapnik.Image.open('./images/support/a.png')
        a.premultiply()
        a.composite(b, getattr(mapnik.CompositeOp, name))
        actual = '/tmp/mapnik-comp-op-test-' + name + '.png'
        expected = 'images/composited/' + name + '.png'
        valid = validate_pixels_are_premultiplied(a)
        if not valid[0]:
            fails.append(
                '%s not validly premultiplied!:\n\t %s pixels (%s)' %
                (name, len(
                    valid[1]), valid[1][0]))
        a.demultiply()
        if not validate_pixels_are_not_premultiplied(a):
            fails.append('%s not validly demultiplied' % (name))
        a.save(actual, 'png32')
        if not os.path.exists(expected) or os.environ.get('UPDATE'):
            print('generating expected test image: %s' % expected)
            a.save(expected, 'png32')
        expected_im = mapnik.Image.open(expected)
        # compare them
        if a.tostring('png32') == expected_im.tostring('png32'):
            successes.append(name)
        else:
            fails.append(
                'failed comparing actual (%s) and expected(%s)' %
                (actual, 'tests/python_tests/' + expected))
            fail_im = side_by_side_image(expected_im, a)
            fail_im.save(
                '/tmp/mapnik-comp-op-test-' +
                name +
                '.fail.png',
                'png32')
    eq_(len(successes), num_ops, '\n' + '\n'.join(fails))
    b.demultiply()
    # b will be slightly modified by pre and then de multiplication rounding errors
    # TODO - write test to ensure the image is 99% the same.
    #expected_b = mapnik.Image.open('./images/support/b.png')
    # b.save('/tmp/mapnik-comp-op-test-original-mask.png')
    #eq_(b.tostring('png32'),expected_b.tostring('png32'), '/tmp/mapnik-comp-op-test-original-mask.png is no longer equivalent to original mask: ./images/support/b.png')


def test_pre_multiply_status():
    b = mapnik.Image.open('./images/support/b.png')
    # not premultiplied yet, should appear that way
    result = validate_pixels_are_not_premultiplied(b)
    eq_(result, True)
    # not yet premultiplied therefore should return false
    result = validate_pixels_are_premultiplied(b)
    eq_(result[0], False)
    # now actually premultiply the pixels
    b.premultiply()
    # now checking if premultiplied should succeed
    result = validate_pixels_are_premultiplied(b)
    eq_(result[0], True)
    # should now not appear to look not premultiplied
    result = validate_pixels_are_not_premultiplied(b)
    eq_(result, False)
    # now actually demultiply the pixels
    b.demultiply()
    # should now appear demultiplied
    result = validate_pixels_are_not_premultiplied(b)
    eq_(result, True)


def test_pre_multiply_status_of_map1():
    m = mapnik.Map(256, 256)
    im = mapnik.Image(m.width, m.height)
    eq_(validate_pixels_are_not_premultiplied(im), True)
    mapnik.render(m, im)
    eq_(validate_pixels_are_not_premultiplied(im), True)


def test_pre_multiply_status_of_map2():
    m = mapnik.Map(256, 256)
    m.background = mapnik.Color(1, 1, 1, 255)
    im = mapnik.Image(m.width, m.height)
    eq_(validate_pixels_are_not_premultiplied(im), True)
    mapnik.render(m, im)
    eq_(validate_pixels_are_not_premultiplied(im), True)

if 'shape' in mapnik.DatasourceCache.plugin_names():
    def test_style_level_comp_op():
        if not os.path.exists('../data/good_maps/style_level_comp_op.xml'):
            raise SkipTest

        m = mapnik.Map(256, 256)
        mapnik.load_map(m, '../data/good_maps/style_level_comp_op.xml')
        m.zoom_all()
        successes = []
        fails = []
        for name in mapnik.CompositeOp.names:
            # find_style returns a copy of the style object
            style_markers = m.find_style("markers")
            style_markers.comp_op = getattr(mapnik.CompositeOp, name)
            # replace the original style with the modified one
            replace_style(m, "markers", style_markers)
            im = mapnik.Image(m.width, m.height)
            mapnik.render(m, im)
            actual = '/tmp/mapnik-style-comp-op-' + name + '.png'
            expected = 'images/style-comp-op/' + name + '.png'
            im.save(actual, 'png32')
            if not os.path.exists(expected) or os.environ.get('UPDATE'):
                print('generating expected test image: %s' % expected)
                im.save(expected, 'png32')
            expected_im = mapnik.Image.open(expected)
            # compare them
            if im.tostring('png32') == expected_im.tostring('png32'):
                successes.append(name)
            else:
                fails.append(
                    'failed comparing actual (%s) and expected(%s)' %
                    (actual, 'tests/python_tests/' + expected))
                fail_im = side_by_side_image(expected_im, im)
                fail_im.save(
                    '/tmp/mapnik-style-comp-op-' +
                    name +
                    '.fail.png',
                    'png32')
        eq_(len(fails), 0, '\n' + '\n'.join(fails))

    def test_style_level_opacity():
        if not os.path.exists('../data/good_maps/style_level_opacity_and_blur.xml'):
            raise SkipTest

        m = mapnik.Map(512, 512)
        mapnik.load_map(
            m, '../data/good_maps/style_level_opacity_and_blur.xml')
        m.zoom_all()
        im = mapnik.Image(512, 512)
        mapnik.render(m, im)
        actual = '/tmp/mapnik-style-level-opacity.png'
        expected = 'images/support/mapnik-style-level-opacity.png'
        im.save(actual, 'png32')
        expected_im = mapnik.Image.open(expected)
        eq_(im.tostring('png32'),
            expected_im.tostring('png32'),
            'failed comparing actual (%s) and expected (%s)' % (actual,
                                                                'tests/python_tests/' + expected))


def test_rounding_and_color_expectations():
    if not os.path.exists('../data/images/stripes_pattern.png'):
        raise SkipTest

    m = mapnik.Map(1, 1)
    m.background = mapnik.Color('rgba(255,255,255,.4999999)')
    im = mapnik.Image(m.width, m.height)
    mapnik.render(m, im)
    eq_(get_unique_colors(im), ['rgba(255,255,255,127)'])
    m = mapnik.Map(1, 1)
    m.background = mapnik.Color('rgba(255,255,255,.5)')
    im = mapnik.Image(m.width, m.height)
    mapnik.render(m, im)
    eq_(get_unique_colors(im), ['rgba(255,255,255,128)'])
    im_file = mapnik.Image.open('../data/images/stripes_pattern.png')
    eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,255)'])
    # should have no effect
    im_file.premultiply()
    eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,255)'])
    im_file.apply_opacity(.5)
    # should have effect now that image has transparency
    im_file.premultiply()
    eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(37,37,37,127)'])
    # should restore to original nonpremultiplied colors
    im_file.demultiply()
    eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,127)'])


def test_background_image_and_background_color():
    if not os.path.exists('../data/images/stripes_pattern.png'):
        raise SkipTest

    m = mapnik.Map(8, 8)
    m.background = mapnik.Color('rgba(255,255,255,.5)')
    m.background_image = '../data/images/stripes_pattern.png'
    im = mapnik.Image(m.width, m.height)
    mapnik.render(m, im)
    eq_(get_unique_colors(im), ['rgba(255,255,255,128)', 'rgba(74,74,74,255)'])


def test_background_image_with_alpha_and_background_color():
    if not os.path.exists('../data/images/yellow_half_trans.png'):
        raise SkipTest

    m = mapnik.Map(10, 10)
    m.background = mapnik.Color('rgba(255,255,255,.5)')
    m.background_image = '../data/images/yellow_half_trans.png'
    im = mapnik.Image(m.width, m.height)
    mapnik.render(m, im)
    eq_(get_unique_colors(im), ['rgba(255,255,85,191)'])


def test_background_image_with_alpha_and_background_color_against_composited_control():
    if not os.path.exists('../data/images/yellow_half_trans.png'):
        raise SkipTest

    m = mapnik.Map(10, 10)
    m.background = mapnik.Color('rgba(255,255,255,.5)')
    m.background_image = '../data/images/yellow_half_trans.png'
    im = mapnik.Image(m.width, m.height)
    mapnik.render(m, im)
    # create and composite the expected result
    im1 = mapnik.Image(10, 10)
    im1.fill(mapnik.Color('rgba(255,255,255,.5)'))
    im1.premultiply()
    im2 = mapnik.Image(10, 10)
    im2.fill(mapnik.Color('rgba(255,255,0,.5)'))
    im2.premultiply()
    im1.composite(im2)
    im1.demultiply()
    # compare image rendered (compositing in `agg_renderer<T>::setup`)
    # vs image composited via python bindings
    #raise Todo("looks like we need to investigate PNG color rounding when saving")
    # eq_(get_unique_colors(im),get_unique_colors(im1))

if __name__ == "__main__":
    setup()
    exit(run_all(eval(x) for x in dir() if x.startswith("test_")))