diff --git a/ChangeLog b/ChangeLog index e3a0ef8b..871fc395 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,7 @@ - support webp and zstd compression in tiff - loaders use "minimise" to close input files earlier - integrate support for oss-fuzz [omira-sch] +- add vips_switch() / vips_case() ... fast many-way ifthenelse 9/7/19 started 8.8.2 - better early shutdown in readers diff --git a/test/test-suite/test_conversion.py b/test/test-suite/test_conversion.py index 73409ee3..afd29b22 100644 --- a/test/test-suite/test_conversion.py +++ b/test/test-suite/test_conversion.py @@ -539,6 +539,27 @@ class TestConversion: result = r(50, 50) assert_almost_equal_objects(result, [3.0, 4.9, 6.9], threshold=0.1) + def test_switch(self): + x = pyvips.Image.grey(256, 256, uchar=True) + + # slice into two at 128, we should get 50% of pixels in each half + index = pyvips.Image.switch([x < 128, x >= 128]) + assert index.avg() == 0.5 + + # slice into four + index = pyvips.Image.switch([ + x < 64, + x >= 64 and x < 128, + x >= 128 and x < 192, + x >= 192 + ]) + assert index.avg() == 1.5 + + # no match should return n + 1 + # FIXME uncomment when we fix relational const + #index = pyvips.Image.switch([x == 1000, x == 2000]) + #assert index.avg() == 2 + def test_insert(self): for x in all_formats: for y in all_formats: diff --git a/test/test-suite/test_histogram.py b/test/test-suite/test_histogram.py index 765b33e1..a709dcfa 100644 --- a/test/test-suite/test_histogram.py +++ b/test/test-suite/test_histogram.py @@ -109,6 +109,25 @@ class TestHistogram: # new mean should be closer to target mean assert abs(im.avg() - 128) > abs(im2.avg() - 128) + def test_case(self): + # slice into two at 128, we should get 50% of pixels in each half + x = pyvips.Image.grey(256, 256, uchar=True) + index = pyvips.Image.switch([x < 128, x >= 128]) + + y = index.case([10, 20]) + assert y.avg() == 15 + + # slice into four + index = pyvips.Image.switch([ + x < 64, + x >= 64 and x < 128, + x >= 128 and x < 192, + x >= 192 + ]) + assert index.case([10, 20, 30, 40]).avg() == 25 + + # values over N should use the last value + assert index.case([10, 20, 30]).avg() == 22.5 if __name__ == '__main__': pytest.main()