more tests

This commit is contained in:
John Cupitt 2014-09-26 15:01:56 +01:00
parent 7d19abbae3
commit 61bae5eeff
3 changed files with 125 additions and 40 deletions

5
TODO
View File

@ -12,6 +12,11 @@
easier to get into CPAN or whatever the python thing is called
- [put exif autorotate into jpeg load
see
https://github.com/jcupitt/ruby-vips/issues/53
- can we pick the vipsthumbnail int shrink factor more intelligently?

View File

@ -401,10 +401,11 @@ vips_flatten_init( VipsFlatten *flatten )
* Take the last band of @in as an alpha and use it to blend the
* remaining channels with @background.
*
* The alpha channel is 0 - 255 for
* integer images and 0 - 1 for float images, where 255 means 100% image and 0
* The alpha channel is 0 - MAX for
* integer images and 0 - 1 for float images, where MAX means 100% image and 0
* means 100% background. Non-complex images only.
* @background defaults to zero (black).
* @background defaults to zero (black). MAX is the largest possible positive
* value for that int type.
*
* Useful for flattening PNG images to RGB.
*

View File

@ -135,9 +135,11 @@ class TestConversion(unittest.TestCase):
def test_bandrank(self):
def median(x, y):
joined = zip(x, y)
map(lambda x: list(x).sort(), joined)
return map(lambda x: x[len(x) / 2], joined)
joined = [[a, b] for a, b in zip(x, y)]
# .sort() isn't a function, so we have to run this as a separate
# pass
[x.sort() for x in joined]
return [x[len(x) / 2] for x in joined]
def bandrank(x, y):
if isinstance(x, Vips.Image) and isinstance(y, Vips.Image):
@ -179,43 +181,120 @@ class TestConversion(unittest.TestCase):
self.assertEqual(x.height, 42)
def test_embed(self):
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [0, 0, 0])
pixel = im.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [0, 0, 0])
for fmt in all_formats:
test = self.colour.cast(fmt)
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.COPY)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
im = test.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [0, 0, 0])
pixel = im.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [0, 0, 0])
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.BACKGROUND,
background = [7, 8, 9])
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [7, 8, 9])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [7, 8, 9])
im = test.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.COPY)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
im = test.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.BACKGROUND,
background = [7, 8, 9])
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [7, 8, 9])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [7, 8, 9])
im = test.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.WHITE)
pixel = im.getpoint(10, 10)
# uses 255 in all bytes of ints, 255.0 for float
pixel = [int(x) & 0xff for x in pixel]
self.assertAlmostEqualObjects(pixel, [255, 255, 255])
pixel = im.getpoint(im.width - 10, im.height - 10)
pixel = [int(x) & 0xff for x in pixel]
self.assertAlmostEqualObjects(pixel, [255, 255, 255])
def test_extract(self):
for fmt in all_formats:
test = self.colour.cast(fmt)
pixel = test.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
sub = test.extract_area(25, 25, 10, 10)
pixel = sub.getpoint(5, 5)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
sub = test.extract_band(1, n = 2)
pixel = sub.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [3, 4])
def test_crop(self):
for fmt in all_formats:
test = self.colour.cast(fmt)
pixel = test.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
sub = test.crop(25, 25, 10, 10)
pixel = sub.getpoint(5, 5)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
def test_falsecolour(self):
for fmt in all_formats:
test = self.colour.cast(fmt)
im = test.falsecolour()
self.assertEqual(im.width, test.width)
self.assertEqual(im.height, test.height)
self.assertEqual(im.bands, 3)
pixel = im.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [20, 0, 41])
def test_flatten(self):
max_value = {Vips.BandFormat.UCHAR: 0xff,
Vips.BandFormat.USHORT: 0xffff,
Vips.BandFormat.UINT: 0xffffffff,
Vips.BandFormat.CHAR: 0x7f,
Vips.BandFormat.SHORT: 0x7fff,
Vips.BandFormat.INT: 0x7fffffff,
Vips.BandFormat.FLOAT: 1.0,
Vips.BandFormat.DOUBLE: 1.0,
Vips.BandFormat.COMPLEX: 1.0,
Vips.BandFormat.DPCOMPLEX: 1.0}
black = self.mono * 0.0
for fmt in all_formats:
test = self.colour.bandjoin2(black + max_value[fmt] / 2]).cast(fmt)
im = test.flatten()
self.assertEqual(im.bands, 3)
pixel = im.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [2, 2, 3])
im = test.flatten(background = [100, 100, 100])
self.assertEqual(im.bands, 3)
pixel = im.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [50, 50, 51])
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.WHITE)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [255, 255, 255])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [255, 255, 255])
if __name__ == '__main__':