2014-09-18 09:36:37 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import math
|
|
|
|
|
|
|
|
#import logging
|
|
|
|
#logging.basicConfig(level = logging.DEBUG)
|
|
|
|
|
|
|
|
from gi.repository import Vips
|
|
|
|
|
2015-02-02 12:36:29 +01:00
|
|
|
Vips.leak_set(True)
|
|
|
|
|
2014-09-18 09:36:37 +02:00
|
|
|
unsigned_formats = [Vips.BandFormat.UCHAR,
|
|
|
|
Vips.BandFormat.USHORT,
|
|
|
|
Vips.BandFormat.UINT]
|
|
|
|
signed_formats = [Vips.BandFormat.CHAR,
|
|
|
|
Vips.BandFormat.SHORT,
|
|
|
|
Vips.BandFormat.INT]
|
|
|
|
float_formats = [Vips.BandFormat.FLOAT,
|
|
|
|
Vips.BandFormat.DOUBLE]
|
|
|
|
complex_formats = [Vips.BandFormat.COMPLEX,
|
|
|
|
Vips.BandFormat.DPCOMPLEX]
|
|
|
|
int_formats = unsigned_formats + signed_formats
|
|
|
|
noncomplex_formats = int_formats + float_formats
|
|
|
|
all_formats = int_formats + float_formats + complex_formats
|
|
|
|
|
|
|
|
colour_colourspaces = [Vips.Interpretation.XYZ,
|
|
|
|
Vips.Interpretation.LAB,
|
|
|
|
Vips.Interpretation.LCH,
|
|
|
|
Vips.Interpretation.CMC,
|
|
|
|
Vips.Interpretation.LABS,
|
|
|
|
Vips.Interpretation.SCRGB,
|
|
|
|
Vips.Interpretation.SRGB,
|
|
|
|
Vips.Interpretation.YXY]
|
2014-09-18 16:04:12 +02:00
|
|
|
coded_colourspaces = [Vips.Interpretation.LABQ]
|
2015-02-12 18:50:38 +01:00
|
|
|
mono_colourspaces = [Vips.Interpretation.B_W]
|
|
|
|
sixteenbit_colourspaces = [Vips.Interpretation.GREY16,
|
|
|
|
Vips.Interpretation.RGB16]
|
|
|
|
all_colourspaces = colour_colourspaces + mono_colourspaces + \
|
|
|
|
coded_colourspaces + sixteenbit_colourspaces
|
2014-09-18 09:36:37 +02:00
|
|
|
|
|
|
|
# an expanding zip ... if either of the args is not a list, duplicate it down
|
|
|
|
# the other
|
|
|
|
def zip_expand(x, y):
|
|
|
|
if isinstance(x, list) and isinstance(y, list):
|
2014-12-10 18:02:51 +01:00
|
|
|
return list(zip(x, y))
|
2014-09-18 09:36:37 +02:00
|
|
|
elif isinstance(x, list):
|
|
|
|
return [[i, y] for i in x]
|
|
|
|
elif isinstance(y, list):
|
|
|
|
return [[x, j] for j in y]
|
|
|
|
else:
|
|
|
|
return [[x, y]]
|
|
|
|
|
|
|
|
# run a 1-ary function on a thing -- loop over elements if the
|
|
|
|
# thing is a list
|
|
|
|
def run_fn(fn, x):
|
|
|
|
if isinstance(x, list):
|
|
|
|
return [fn(i) for i in x]
|
|
|
|
else:
|
|
|
|
return fn(x)
|
|
|
|
|
|
|
|
# run a 2-ary function on two things -- loop over elements pairwise if the
|
|
|
|
# things are lists
|
|
|
|
def run_fn2(fn, x, y):
|
|
|
|
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
|
|
|
|
return fn(x, y)
|
|
|
|
elif isinstance(x, list) or isinstance(y, list):
|
|
|
|
return [fn(i, j) for i, j in zip_expand(x, y)]
|
|
|
|
else:
|
|
|
|
return fn(x, y)
|
|
|
|
|
|
|
|
class TestColour(unittest.TestCase):
|
|
|
|
# test a pair of things which can be lists for approx. equality
|
|
|
|
def assertAlmostEqualObjects(self, a, b, places = 4, msg = ''):
|
|
|
|
#print 'assertAlmostEqualObjects %s = %s' % (a, b)
|
|
|
|
for x, y in zip_expand(a, b):
|
|
|
|
self.assertAlmostEqual(x, y, places = places, msg = msg)
|
|
|
|
|
|
|
|
# run a function on an image and on a single pixel, the results
|
|
|
|
# should match
|
|
|
|
def run_cmp(self, message, im, x, y, fn):
|
|
|
|
a = im.getpoint(x, y)
|
|
|
|
v1 = fn(a)
|
|
|
|
im2 = fn(im)
|
|
|
|
v2 = im2.getpoint(x, y)
|
|
|
|
self.assertAlmostEqualObjects(v1, v2, msg = message)
|
|
|
|
|
|
|
|
# run a function on a pair of images and on a pair of pixels, the results
|
|
|
|
# should match
|
|
|
|
def run_cmp2(self, message, left, right, x, y, fn):
|
|
|
|
a = left.getpoint(x, y)
|
|
|
|
b = right.getpoint(x, y)
|
|
|
|
v1 = fn(a, b)
|
|
|
|
after = fn(left, right)
|
|
|
|
v2 = after.getpoint(x, y)
|
|
|
|
self.assertAlmostEqualObjects(v1, v2, msg = message)
|
|
|
|
|
|
|
|
# run a function on a pair of images
|
|
|
|
# 50,50 and 10,10 should have different values on the test image
|
|
|
|
def run_test2(self, message, left, right, fn):
|
|
|
|
self.run_cmp2(message, left, right, 50, 50,
|
|
|
|
lambda x, y: run_fn2(fn, x, y))
|
|
|
|
self.run_cmp2(message, left, right, 10, 10,
|
|
|
|
lambda x, y: run_fn2(fn, x, y))
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-09-22 14:50:09 +02:00
|
|
|
im = Vips.Image.mask_ideal(100, 100, 0.5, reject = True, optical = True)
|
2014-09-18 09:36:37 +02:00
|
|
|
self.colour = im * [1, 2, 3] + [2, 3, 4]
|
|
|
|
self.mono = self.colour.extract_band(1)
|
|
|
|
self.all_images = [self.mono, self.colour]
|
|
|
|
|
|
|
|
def test_colourspace(self):
|
2014-09-18 16:04:12 +02:00
|
|
|
# mid-grey in Lab ... put 42 in the extra band, it should be copied
|
|
|
|
# unmodified
|
|
|
|
test = Vips.Image.black(100, 100) + [50, 0, 0, 42]
|
2014-09-18 09:36:37 +02:00
|
|
|
test = test.copy(interpretation = Vips.Interpretation.LAB)
|
|
|
|
|
2014-09-18 16:04:12 +02:00
|
|
|
# a long series should come in a circle
|
2014-09-18 09:36:37 +02:00
|
|
|
im = test
|
|
|
|
for col in colour_colourspaces + [Vips.Interpretation.LAB]:
|
|
|
|
im = im.colourspace(col)
|
|
|
|
self.assertEqual(im.interpretation, col)
|
2015-02-12 18:50:38 +01:00
|
|
|
|
|
|
|
for i in range(0, 4):
|
|
|
|
l = im.extract_band(i).min()
|
|
|
|
h = im.extract_band(i).max()
|
|
|
|
self.assertAlmostEqual(l, h)
|
|
|
|
|
2014-09-18 16:04:12 +02:00
|
|
|
pixel = im.getpoint(10, 10)
|
|
|
|
self.assertAlmostEqual(pixel[3], 42, places = 2)
|
2014-09-18 09:36:37 +02:00
|
|
|
|
2015-02-12 18:50:38 +01:00
|
|
|
# alpha won't be equal for RGB16, but it should be preserved if we go
|
|
|
|
# there and back
|
|
|
|
im = im.colourspace(Vips.Interpretation.RGB16)
|
|
|
|
im = im.colourspace(Vips.Interpretation.LAB)
|
|
|
|
|
2014-09-18 09:36:37 +02:00
|
|
|
before = test.getpoint(10, 10)
|
|
|
|
after = im.getpoint(10, 10)
|
|
|
|
self.assertAlmostEqualObjects(before, after, places = 1)
|
|
|
|
|
2014-09-27 13:29:23 +02:00
|
|
|
# go between every pair of colour spaces
|
|
|
|
for start in colour_colourspaces:
|
|
|
|
for end in colour_colourspaces:
|
|
|
|
im = test.colourspace(start)
|
|
|
|
im2 = im.colourspace(end)
|
|
|
|
im3 = im2.colourspace(Vips.Interpretation.LAB)
|
|
|
|
|
|
|
|
before = test.getpoint(10, 10)
|
|
|
|
after = im3.getpoint(10, 10)
|
2015-02-10 11:17:01 +01:00
|
|
|
|
2014-09-27 13:29:23 +02:00
|
|
|
self.assertAlmostEqualObjects(before, after, places = 1)
|
|
|
|
|
2014-09-18 16:04:12 +02:00
|
|
|
# test Lab->XYZ on mid-grey
|
|
|
|
# checked against http://www.brucelindbloom.com
|
|
|
|
im = test.colourspace(Vips.Interpretation.XYZ)
|
2014-09-18 09:36:37 +02:00
|
|
|
after = im.getpoint(10, 10)
|
2014-09-18 16:04:12 +02:00
|
|
|
self.assertAlmostEqualObjects(after, [17.5064, 18.4187, 20.0547, 42])
|
|
|
|
|
|
|
|
# grey->colour->grey should be equal
|
|
|
|
for mono_fmt in mono_colourspaces:
|
|
|
|
test_grey = test.colourspace(mono_fmt)
|
|
|
|
im = test_grey
|
|
|
|
for col in colour_colourspaces + [mono_fmt]:
|
|
|
|
im = im.colourspace(col)
|
|
|
|
self.assertEqual(im.interpretation, col)
|
|
|
|
[before, alpha_before] = test_grey.getpoint(10, 10)
|
|
|
|
[after, alpha_after] = im.getpoint(10, 10)
|
|
|
|
self.assertLess(abs(alpha_after - alpha_before), 1)
|
|
|
|
if mono_fmt == Vips.Interpretation.GREY16:
|
|
|
|
# GREY16 can wind up rather different due to rounding
|
|
|
|
self.assertLess(abs(after - before), 30)
|
|
|
|
else:
|
|
|
|
# but 8-bit we should hit exactly
|
|
|
|
self.assertLess(abs(after - before), 1)
|
|
|
|
|
2014-09-22 14:50:09 +02:00
|
|
|
# test results from Bruce Lindbloom's calculator:
|
|
|
|
# http://www.brucelindbloom.com
|
2014-09-18 16:04:12 +02:00
|
|
|
|
|
|
|
def test_dE00(self):
|
2014-09-24 15:52:24 +02:00
|
|
|
# put 42 in the extra band, it should be copied unmodified
|
|
|
|
reference = Vips.Image.black(100, 100) + [50, 10, 20, 42]
|
2014-09-18 16:04:12 +02:00
|
|
|
reference = reference.copy(interpretation = Vips.Interpretation.LAB)
|
|
|
|
sample = Vips.Image.black(100, 100) + [40, -20, 10]
|
|
|
|
sample = sample.copy(interpretation = Vips.Interpretation.LAB)
|
|
|
|
|
|
|
|
difference = reference.dE00(sample)
|
2014-09-24 15:52:24 +02:00
|
|
|
result, alpha = difference.getpoint(10, 10)
|
|
|
|
self.assertAlmostEqual(result, 30.238, places = 3)
|
|
|
|
self.assertAlmostEqual(alpha, 42.0, places = 3)
|
2014-09-18 16:04:12 +02:00
|
|
|
|
|
|
|
def test_dE76(self):
|
2014-09-24 15:52:24 +02:00
|
|
|
# put 42 in the extra band, it should be copied unmodified
|
|
|
|
reference = Vips.Image.black(100, 100) + [50, 10, 20, 42]
|
2014-09-18 16:04:12 +02:00
|
|
|
reference = reference.copy(interpretation = Vips.Interpretation.LAB)
|
|
|
|
sample = Vips.Image.black(100, 100) + [40, -20, 10]
|
|
|
|
sample = sample.copy(interpretation = Vips.Interpretation.LAB)
|
|
|
|
|
|
|
|
difference = reference.dE76(sample)
|
2014-09-24 15:52:24 +02:00
|
|
|
result, alpha = difference.getpoint(10, 10)
|
|
|
|
self.assertAlmostEqual(result, 33.166, places = 3)
|
|
|
|
self.assertAlmostEqual(alpha, 42.0, places = 3)
|
2014-09-18 16:04:12 +02:00
|
|
|
|
2014-09-24 14:16:38 +02:00
|
|
|
# the vips CMC calculation is based on distance in a colorspace derived from
|
|
|
|
# the CMC formula, so it won't match exactly ... see vips_LCh2CMC() for
|
|
|
|
# details
|
2014-09-18 16:04:12 +02:00
|
|
|
def test_dECMC(self):
|
2014-09-24 15:52:24 +02:00
|
|
|
reference = Vips.Image.black(100, 100) + [50, 10, 20, 42]
|
2014-09-18 16:04:12 +02:00
|
|
|
reference = reference.copy(interpretation = Vips.Interpretation.LAB)
|
2014-09-24 14:16:38 +02:00
|
|
|
sample = Vips.Image.black(100, 100) + [55, 11, 23]
|
2014-09-18 16:04:12 +02:00
|
|
|
sample = sample.copy(interpretation = Vips.Interpretation.LAB)
|
|
|
|
|
|
|
|
difference = reference.dECMC(sample)
|
2014-09-24 15:52:24 +02:00
|
|
|
result, alpha = difference.getpoint(10, 10)
|
2014-09-24 14:16:38 +02:00
|
|
|
self.assertLess(abs(result - 4.97), 0.5)
|
2014-09-24 15:52:24 +02:00
|
|
|
self.assertAlmostEqual(alpha, 42.0, places = 3)
|
2014-09-18 09:36:37 +02:00
|
|
|
|
2014-10-01 13:28:25 +02:00
|
|
|
def test_icc(self):
|
|
|
|
test = Vips.Image.new_from_file("images/IMG_4618.jpg")
|
|
|
|
|
|
|
|
im = test.icc_import().icc_export()
|
|
|
|
self.assertLess(im.dE76(test).max(), 6)
|
|
|
|
|
|
|
|
im = test.icc_import()
|
|
|
|
im2 = im.icc_export(depth = 16)
|
|
|
|
self.assertEqual(im2.format, Vips.BandFormat.USHORT)
|
|
|
|
im3 = im2.icc_import()
|
|
|
|
self.assertLess((im - im3).abs().max(), 3)
|
|
|
|
|
|
|
|
im = test.icc_import(intent = Vips.Intent.ABSOLUTE)
|
|
|
|
im2 = im.icc_export(intent = Vips.Intent.ABSOLUTE)
|
|
|
|
self.assertLess(im2.dE76(test).max(), 6)
|
|
|
|
|
|
|
|
im = test.icc_import()
|
|
|
|
im2 = im.icc_export(output_profile = "images/sRGB.icm")
|
|
|
|
im3 = im.colourspace(Vips.Interpretation.SRGB)
|
|
|
|
self.assertLess(im2.dE76(im3).max(), 6)
|
|
|
|
|
2014-10-01 15:59:23 +02:00
|
|
|
before_profile = test.get_value("icc-profile-data")
|
2014-10-01 13:28:25 +02:00
|
|
|
im = test.icc_transform("images/sRGB.icm")
|
2014-10-01 15:59:23 +02:00
|
|
|
after_profile = im.get_value("icc-profile-data")
|
2014-10-01 13:28:25 +02:00
|
|
|
im2 = test.icc_import()
|
|
|
|
im3 = im2.colourspace(Vips.Interpretation.SRGB)
|
|
|
|
self.assertLess(im.dE76(im3).max(), 6)
|
|
|
|
self.assertNotEqual(len(before_profile), len(after_profile))
|
|
|
|
|
|
|
|
im = test.icc_import(input_profile = "images/sRGB.icm")
|
|
|
|
im2 = test.icc_import()
|
|
|
|
self.assertLess(6, im.dE76(im2).max())
|
|
|
|
|
|
|
|
im = test.icc_import(pcs = Vips.PCS.XYZ)
|
|
|
|
self.assertEqual(im.interpretation, Vips.Interpretation.XYZ)
|
|
|
|
im = test.icc_import()
|
|
|
|
self.assertEqual(im.interpretation, Vips.Interpretation.LAB)
|
|
|
|
|
2014-09-18 09:36:37 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|