| 1 |
import math |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from DisplayCAL.colormath import ( |
| 6 |
CAT_MATRICES, |
| 7 |
get_cat_matrix, |
| 8 |
get_standard_illuminant, |
| 9 |
Matrix3x3, |
| 10 |
smooth_avg_old, |
| 11 |
smooth_avg, |
| 12 |
RGB2XYZ, |
| 13 |
special_pow, |
| 14 |
linmin, |
| 15 |
REC709_P, |
| 16 |
REC709_K0, |
| 17 |
SMPTE240M_P, |
| 18 |
SMPTE240M_K0, |
| 19 |
SRGB_P, |
| 20 |
SRGB_K0, |
| 21 |
LSTAR_K, |
| 22 |
LSTAR_E, |
| 23 |
) |
| 24 |
from tests.data.display_data import DisplayData |
| 25 |
|
| 26 |
|
| 27 |
def test_smooth_avg_1(): |
| 28 |
"""testing if the smooth_avg function is working properly""" |
| 29 |
test_values = DisplayData.values_to_smooth |
| 30 |
expected_result = DisplayData.expected_smooth_values |
| 31 |
passes = 1 |
| 32 |
window = None |
| 33 |
protect = None |
| 34 |
result = smooth_avg(test_values, passes, window, protect) |
| 35 |
assert result == pytest.approx(expected_result) |
| 36 |
|
| 37 |
|
| 38 |
def test_smooth_avg_is_matching_old_implementation_1(): |
| 39 |
"""testing if the ``smooth_avg`` function is matching ``smooth_avg_old``""" |
| 40 |
test_values = DisplayData.values_to_smooth |
| 41 |
expected_result = DisplayData.expected_smooth_values |
| 42 |
passes = 1 |
| 43 |
window = None |
| 44 |
protect = None |
| 45 |
result_1 = smooth_avg_old(test_values, passes, window, protect) |
| 46 |
result_2 = smooth_avg(test_values, passes, window, protect) |
| 47 |
|
| 48 |
assert len(result_1) == len(test_values) |
| 49 |
assert len(result_2) == len(test_values) |
| 50 |
assert result_1 == pytest.approx(result_2) |
| 51 |
assert result_1 == pytest.approx(expected_result) |
| 52 |
assert result_2 == pytest.approx(expected_result) |
| 53 |
|
| 54 |
|
| 55 |
def test_smooth_avg_is_matching_old_implementation_2(): |
| 56 |
"""testing if the ``smooth_avg`` function is matching ``smooth_avg_old``""" |
| 57 |
test_values = DisplayData.values_to_smooth |
| 58 |
passes = 1 |
| 59 |
window = tuple([1] * 5) |
| 60 |
window_size = len(window) |
| 61 |
half_window_size = int(window_size / 2) |
| 62 |
protect = None |
| 63 |
result_1 = smooth_avg_old(test_values, passes, window, protect) |
| 64 |
result_2 = smooth_avg(test_values, passes, window, protect) |
| 65 |
|
| 66 |
assert len(result_1) == len(test_values) |
| 67 |
assert len(result_2) == len(test_values) |
| 68 |
# unfortunately the first value after start and first value after end are not |
| 69 |
# matching, but the rest are perfectly matching |
| 70 |
assert result_1[half_window_size:-half_window_size] == pytest.approx( |
| 71 |
result_2[half_window_size:-half_window_size] |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
def test_smooth_avg_is_matching_old_implementation_3(): |
| 76 |
"""testing if the ``smooth_avg`` function is matching ``smooth_avg_old``""" |
| 77 |
test_values = DisplayData.values_to_smooth |
| 78 |
passes = 1 |
| 79 |
window = tuple([1] * 7) |
| 80 |
window_size = len(window) |
| 81 |
half_window_size = int(window_size / 2) |
| 82 |
protect = None |
| 83 |
result_1 = smooth_avg_old(test_values, passes, window, protect) |
| 84 |
result_2 = smooth_avg(test_values, passes, window, protect) |
| 85 |
|
| 86 |
assert len(result_1) == len(test_values) |
| 87 |
assert len(result_2) == len(test_values) |
| 88 |
# unfortunately the first value after start and first value after end are not |
| 89 |
# matching, but the rest are perfectly matching |
| 90 |
assert result_1[half_window_size:-half_window_size] == pytest.approx( |
| 91 |
result_2[half_window_size:-half_window_size] |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
def test_smooth_avg_protetced_values_1(): |
| 96 |
"""Testing ``smooth_avg`` ``protect`` is working as expected.""" |
| 97 |
test_values = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0] |
| 98 |
passes = 1 |
| 99 |
window = (1, 1, 1) |
| 100 |
protect = [7] |
| 101 |
result = smooth_avg(test_values, passes, window, protect) |
| 102 |
expected_result = [ |
| 103 |
0, |
| 104 |
0.0, |
| 105 |
0.0, |
| 106 |
0.0, |
| 107 |
0.0, |
| 108 |
0.0, |
| 109 |
0.3333333333333333, |
| 110 |
1, |
| 111 |
0.3333333333333333, |
| 112 |
0.0, |
| 113 |
0.0, |
| 114 |
0.0, |
| 115 |
0.0, |
| 116 |
0, |
| 117 |
] |
| 118 |
assert result == expected_result |
| 119 |
|
| 120 |
|
| 121 |
def test_smooth_avg_protetced_values_2(): |
| 122 |
"""Testing ``smooth_avg`` ``protect`` is working as expected.""" |
| 123 |
test_values = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0] |
| 124 |
passes = 1 |
| 125 |
window = (1, 1, 1) |
| 126 |
protect = [6, 7] |
| 127 |
result = smooth_avg(test_values, passes, window, protect) |
| 128 |
expected_result = [ |
| 129 |
0, |
| 130 |
0.0, |
| 131 |
0.0, |
| 132 |
0.0, |
| 133 |
0.0, |
| 134 |
0.0, |
| 135 |
0, |
| 136 |
1, |
| 137 |
0.3333333333333333, |
| 138 |
0.0, |
| 139 |
0.0, |
| 140 |
0.0, |
| 141 |
0.0, |
| 142 |
0, |
| 143 |
] |
| 144 |
assert result == expected_result |
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.parametrize( |
| 148 |
"test_value, expected", |
| 149 |
[ |
| 150 |
[ |
| 151 |
"Bradford", |
| 152 |
Matrix3x3( |
| 153 |
[ |
| 154 |
[0.89510, 0.26640, -0.16140], |
| 155 |
[-0.75020, 1.71350, 0.03670], |
| 156 |
[0.03890, -0.06850, 1.02960], |
| 157 |
] |
| 158 |
), |
| 159 |
], |
| 160 |
[ |
| 161 |
"CAT02", |
| 162 |
Matrix3x3( |
| 163 |
[ |
| 164 |
[0.7328, 0.4296, -0.1624], |
| 165 |
[-0.7036, 1.6975, 0.0061], |
| 166 |
[0.0030, 0.0136, 0.9834], |
| 167 |
] |
| 168 |
), |
| 169 |
], |
| 170 |
[ |
| 171 |
"CAT02BS", |
| 172 |
Matrix3x3( |
| 173 |
[ |
| 174 |
[0.7328, 0.4296, -0.1624], |
| 175 |
[-0.7036, 1.6975, 0.0061], |
| 176 |
[0.0000, 0.0000, 1.0000], |
| 177 |
] |
| 178 |
), |
| 179 |
], |
| 180 |
[ |
| 181 |
"CAT97s", |
| 182 |
Matrix3x3( |
| 183 |
[ |
| 184 |
[0.8562, 0.3372, -0.1934], |
| 185 |
[-0.8360, 1.8327, 0.0033], |
| 186 |
[0.0357, -0.0469, 1.0112], |
| 187 |
] |
| 188 |
), |
| 189 |
], |
| 190 |
[ |
| 191 |
"CMCCAT2000", |
| 192 |
Matrix3x3( |
| 193 |
[ |
| 194 |
[0.7982, 0.3389, -0.1371], |
| 195 |
[-0.5918, 1.5512, 0.0406], |
| 196 |
[0.0008, 0.0239, 0.9753], |
| 197 |
] |
| 198 |
), |
| 199 |
], |
| 200 |
[ |
| 201 |
"HPE E", |
| 202 |
Matrix3x3( |
| 203 |
[ |
| 204 |
[0.38971, 0.68898, -0.07868], |
| 205 |
[-0.22981, 1.18340, 0.04641], |
| 206 |
[0.00000, 0.00000, 1.00000], |
| 207 |
] |
| 208 |
), |
| 209 |
], |
| 210 |
[ |
| 211 |
"Sharp", |
| 212 |
Matrix3x3( |
| 213 |
[ |
| 214 |
[1.2694, -0.0988, -0.1706], |
| 215 |
[-0.8364, 1.8006, 0.0357], |
| 216 |
[0.0297, -0.0315, 1.0018], |
| 217 |
] |
| 218 |
), |
| 219 |
], |
| 220 |
[ |
| 221 |
"HPE D65", |
| 222 |
Matrix3x3( |
| 223 |
[ |
| 224 |
[0.40024, 0.70760, -0.08081], |
| 225 |
[-0.22630, 1.16532, 0.04570], |
| 226 |
[0.00000, 0.00000, 0.91822], |
| 227 |
] |
| 228 |
), |
| 229 |
], |
| 230 |
["XYZ scaling", Matrix3x3([[1, 0, 0], [0, 1, 0], [0, 0, 1]])], |
| 231 |
[ |
| 232 |
"IPT", |
| 233 |
Matrix3x3( |
| 234 |
[ |
| 235 |
[0.4002, 0.7075, -0.0807], |
| 236 |
[-0.2280, 1.1500, 0.0612], |
| 237 |
[0.0000, 0.0000, 0.9184], |
| 238 |
] |
| 239 |
), |
| 240 |
], |
| 241 |
[ |
| 242 |
"CIE2012_2", |
| 243 |
Matrix3x3( |
| 244 |
[ |
| 245 |
[0.2052445519046028, 0.8334486497310412, -0.0386932016356441], |
| 246 |
[-0.4972221301804286, 1.4034846060306130, 0.0937375241498157], |
| 247 |
[0.0000000000000000, 0.0000000000000000, 1.0000000000000000], |
| 248 |
] |
| 249 |
), |
| 250 |
], |
| 251 |
[ |
| 252 |
"BS", |
| 253 |
Matrix3x3( |
| 254 |
[ |
| 255 |
[0.8752, 0.2787, -0.1539], |
| 256 |
[-0.8904, 1.8709, 0.0195], |
| 257 |
[-0.0061, 0.0162, 0.9899], |
| 258 |
] |
| 259 |
), |
| 260 |
], |
| 261 |
[ |
| 262 |
"BS-PC", |
| 263 |
Matrix3x3( |
| 264 |
[ |
| 265 |
[0.6489, 0.3915, -0.0404], |
| 266 |
[-0.3775, 1.3055, 0.0720], |
| 267 |
[-0.0271, 0.0888, 0.9383], |
| 268 |
] |
| 269 |
), |
| 270 |
], |
| 271 |
], |
| 272 |
) |
| 273 |
def test_get_cat_matrix_with_str_input(test_value, expected): |
| 274 |
"""Testing get_cat_matrix with str input.""" |
| 275 |
result = get_cat_matrix(test_value) |
| 276 |
assert result == expected |
| 277 |
|
| 278 |
|
| 279 |
@pytest.mark.skip( |
| 280 |
reason="TODO: This test is moved from the module, properly implement it." |
| 281 |
) |
| 282 |
def test_from_module(): |
| 283 |
for i in range(4): |
| 284 |
if i == 0: |
| 285 |
wp = "native" |
| 286 |
elif i == 1: |
| 287 |
wp = "D50" |
| 288 |
XYZ = get_standard_illuminant(wp) |
| 289 |
elif i == 2: |
| 290 |
wp = "D65" |
| 291 |
XYZ = get_standard_illuminant(wp) |
| 292 |
elif i == 3: |
| 293 |
XYZ = get_standard_illuminant("D65", ("ASTM E308-01",)) |
| 294 |
wp = " ".join([str(v) for v in XYZ]) |
| 295 |
print( |
| 296 |
f"RGB and corresponding XYZ (nominal range 0.0 - 1.0) with whitepoint {wp}" |
| 297 |
) |
| 298 |
for name in rgb_spaces: |
| 299 |
spc = rgb_spaces[name] |
| 300 |
if i == 0: |
| 301 |
XYZ = CIEDCCT2XYZ(spc[1]) |
| 302 |
spc = spc[0], XYZ, spc[2], spc[3], spc[4] |
| 303 |
print( |
| 304 |
f"{name} 1.0, 1.0, 1.0 = XYZ", |
| 305 |
[str(round(v, 4)) for v in RGB2XYZ(1.0, 1.0, 1.0, spc)], |
| 306 |
) |
| 307 |
print( |
| 308 |
f"{name} 1.0, 0.0, 0.0 = XYZ", |
| 309 |
[str(round(v, 4)) for v in RGB2XYZ(1.0, 0.0, 0.0, spc)], |
| 310 |
) |
| 311 |
print( |
| 312 |
f"{name} 0.0, 1.0, 0.0 = XYZ", |
| 313 |
[str(round(v, 4)) for v in RGB2XYZ(0.0, 1.0, 0.0, spc)], |
| 314 |
) |
| 315 |
print( |
| 316 |
f"{name} 0.0, 0.0, 1.0 = XYZ", |
| 317 |
[str(round(v, 4)) for v in RGB2XYZ(0.0, 0.0, 1.0, spc)], |
| 318 |
) |
| 319 |
print("") |
| 320 |
|
| 321 |
|
| 322 |
@pytest.mark.parametrize( |
| 323 |
"a, b, expected", |
| 324 |
[ |
| 325 |
(2.0, 3.0, 8.0), # simple power |
| 326 |
(-2.0, 3.0, -8.0), # negative base, odd exponent |
| 327 |
(2.0, 0.5, math.sqrt(2.0)), # square root |
| 328 |
(0.0, 2.0, 0.0), # zero base |
| 329 |
(2.0, 1.0, 2.0), # identity |
| 330 |
], |
| 331 |
) |
| 332 |
def test_special_pow_basic_power(a, b, expected): |
| 333 |
assert special_pow(a, b) == pytest.approx(expected) |
| 334 |
|
| 335 |
|
| 336 |
def test_special_pow_slope_limit_negative_input(): |
| 337 |
"""slope_limit should limit the result for negative input.""" |
| 338 |
a = -0.5 |
| 339 |
b = 2.0 |
| 340 |
slope_limit = 0.1 |
| 341 |
result = special_pow(a, b, slope_limit) |
| 342 |
assert result == pytest.approx(min(-math.pow(-a, b), a / slope_limit)) |
| 343 |
|
| 344 |
|
| 345 |
def test_special_pow_slope_limit_positive_input(): |
| 346 |
"""slope_limit should limit the result for positive input.""" |
| 347 |
a = 0.05 |
| 348 |
b = 2.0 |
| 349 |
slope_limit = 0.5 |
| 350 |
result = special_pow(a, b, slope_limit) |
| 351 |
assert result == pytest.approx(max(math.pow(a, b), a / slope_limit)) |
| 352 |
|
| 353 |
|
| 354 |
@pytest.mark.parametrize( |
| 355 |
"a, b, expected", |
| 356 |
[ |
| 357 |
# sRGB forward (XYZ -> RGB, sRGB TRC) |
| 358 |
(0.002, 1.0 / -2.4, 0.002 * SRGB_P), |
| 359 |
(0.1, 1.0 / -2.4, 1.055 * math.pow(0.1, 1.0 / 2.4) - 0.055), |
| 360 |
# sRGB reverse (RGB -> XYZ, sRGB TRC) |
| 361 |
(0.002, -2.4, 0.002 / SRGB_P), |
| 362 |
(0.1, -2.4, math.pow((0.1 + 0.055) / 1.055, 2.4)), |
| 363 |
# L* forward (XYZ -> RGB, L* TRC) |
| 364 |
(0.005, 1.0 / -3.0, 0.01 * 0.005 * LSTAR_K), |
| 365 |
(0.1, 1.0 / -3.0, 1.16 * math.pow(0.1, 1.0 / 3.0) - 0.16), |
| 366 |
# L* reverse (RGB -> XYZ, L* TRC) |
| 367 |
(0.05, -3.0, 100.0 * 0.05 / LSTAR_K), |
| 368 |
(0.2, -3.0, math.pow((0.2 + 0.16) / 1.16, 3.0)), |
| 369 |
# Rec. 709 forward (XYZ -> RGB, Rec. 709 TRC) |
| 370 |
(0.01, 1.0 / -709, 0.01 * REC709_P), |
| 371 |
(0.2, 1.0 / -709, 1.099 * math.pow(0.2, 0.45) - 0.099), |
| 372 |
# Rec. 709 reverse (RGB -> XYZ, Rec. 709 TRC) |
| 373 |
(0.01, -709, 0.01 / REC709_P), |
| 374 |
(0.2, -709, math.pow((0.2 + 0.099) / 1.099, 1.0 / 0.45)), |
| 375 |
# SMPTE 240M forward (XYZ -> RGB, SMPTE 240M TRC) |
| 376 |
(0.01, 1.0 / -240, 0.01 * SMPTE240M_P), |
| 377 |
(0.2, 1.0 / -240, 1.1115 * math.pow(0.2, 0.45) - 0.1115), |
| 378 |
# SMPTE 240M reverse (RGB -> XYZ, SMPTE 240M TRC) |
| 379 |
(0.01, -240, 0.01 / SMPTE240M_P), |
| 380 |
(0.2, -240, math.pow((0.1115 + 0.2) / 1.1115, 1.0 / 0.45)), |
| 381 |
], |
| 382 |
) |
| 383 |
def test_special_pow_transfer_functions(a, b, expected): |
| 384 |
assert special_pow(a, b) == pytest.approx(expected) |
| 385 |
|
| 386 |
|
| 387 |
def test_special_pow_invalid_gamma(): |
| 388 |
with pytest.raises(ValueError): |
| 389 |
special_pow(1.0, -9999) |
| 390 |
|
| 391 |
|
| 392 |
def quadratic_func(fdata, xt): |
| 393 |
# Simple quadratic: f(x) = (x-3)^2, minimum at x=3 |
| 394 |
# xt is a dict or list, use first dimension |
| 395 |
x = xt[0] if isinstance(xt, (list, tuple)) else xt.get(0, 0) |
| 396 |
return (x - 3.0) ** 2 |
| 397 |
|
| 398 |
def linear_func(fdata, xt): |
| 399 |
# Linear function: f(x) = 2x + 1, minimum at -infinity |
| 400 |
x = xt[0] if isinstance(xt, (list, tuple)) else xt.get(0, 0) |
| 401 |
return 2 * x + 1 |
| 402 |
|
| 403 |
def multi_dim_quadratic(fdata, xt): |
| 404 |
# 2D quadratic: f(x, y) = (x-2)^2 + (y+1)^2, minimum at (2, -1) |
| 405 |
x = xt[0] if isinstance(xt, (list, tuple)) else xt.get(0, 0) |
| 406 |
y = xt[1] if isinstance(xt, (list, tuple)) else xt.get(1, 0) |
| 407 |
return (x - 2.0) ** 2 + (y + 1.0) ** 2 |
| 408 |
|
| 409 |
def test_linmin_quadratic_minimization(): |
| 410 |
cp = [0.0] |
| 411 |
xi = [1.0] |
| 412 |
di = 1 |
| 413 |
ftol = 1e-6 |
| 414 |
fdata = None |
| 415 |
result = linmin(cp, xi, di, ftol, quadratic_func, fdata) |
| 416 |
# Minimum should be at x=3, so cp[0] should be close to 3, result close to 0 |
| 417 |
assert cp[0] == pytest.approx(3.0, abs=1e-3) |
| 418 |
assert result == pytest.approx(0.0, abs=1e-6) |
| 419 |
|
| 420 |
def test_linmin_linear_function(): |
| 421 |
cp = [0.0] |
| 422 |
xi = [1.0] |
| 423 |
di = 1 |
| 424 |
ftol = 1e-6 |
| 425 |
fdata = None |
| 426 |
result = linmin(cp, xi, di, ftol, linear_func, fdata) |
| 427 |
# For a linear function, linmin should move cp in the negative direction |
| 428 |
# as much as possible, but since the bracket is limited, it should not diverge |
| 429 |
# Just check that the result is less than the starting value |
| 430 |
assert result < linear_func(None, {0: 0.0}) |
| 431 |
|
| 432 |
def test_linmin_multidimensional_quadratic(): |
| 433 |
cp = [0.0, 0.0] |
| 434 |
xi = [1.0, 1.0] |
| 435 |
di = 2 |
| 436 |
ftol = 1e-6 |
| 437 |
fdata = None |
| 438 |
result = linmin(cp, xi, di, ftol, multi_dim_quadratic, fdata) |
| 439 |
# The minimum along the direction [1,1] from [0,0] is at t where |
| 440 |
# (t-2)^2 + (t+1)^2 is minimized, i.e., t = 0.5 |
| 441 |
# So cp should be [0.5, 0.5] |
| 442 |
assert cp[0] == pytest.approx(0.5, abs=1e-3) |
| 443 |
assert cp[1] == pytest.approx(0.5, abs=1e-3) |
| 444 |
# The function value at this point |
| 445 |
expected = multi_dim_quadratic(None, [0.5, 0.5]) |
| 446 |
assert result == pytest.approx(expected, abs=1e-6) |
| 447 |
|
| 448 |
def test_linmin_with_zero_direction(): |
| 449 |
cp = [1.0] |
| 450 |
xi = [0.0] |
| 451 |
di = 1 |
| 452 |
ftol = 1e-6 |
| 453 |
fdata = None |
| 454 |
result = linmin(cp, xi, di, ftol, quadratic_func, fdata) |
| 455 |
# If direction is zero, cp should not change |
| 456 |
assert cp[0] == pytest.approx(1.0) |
| 457 |
# The result should be the function value at cp |
| 458 |
assert result == pytest.approx(quadratic_func(None, {0: 1.0})) |
| 459 |
|
| 460 |
def test_linmin_handles_dict_xt(): |
| 461 |
# linmin uses dict for xt if di <= 10, so test that path |
| 462 |
cp = [0.0] |
| 463 |
xi = [1.0] |
| 464 |
di = 1 |
| 465 |
ftol = 1e-6 |
| 466 |
fdata = None |
| 467 |
result = linmin(cp, xi, di, ftol, quadratic_func, fdata) |
| 468 |
assert cp[0] == pytest.approx(3.0, abs=1e-3) |
| 469 |
assert result == pytest.approx(0.0, abs=1e-6) |