Debian Codegraph @

test/common/pixelmatch/utils.py

raw
1 # Copyright (c) 2019, Mapbox, Wu Haotian
2 # Copyright (c) 2026, Freya Gustavsson
3 # SPDX-License-Identifier: ISC
4
5 from collections.abc import Collection
6
7 from .types import ImageArea, ImageSequence, MutableImageSequence
8
9
10 def antialiased(img: ImageSequence, x1: int, y1: int, width: int, height: int, img2: ImageSequence) -> bool:
11 """
12 check if a pixel is likely a part of anti-aliasing;
13 based on "Anti-aliased Pixel and Intensity Slope Detector" paper by V. Vysniauskas, 2009
14 """
15 x0 = max(x1 - 1, 0)
16 y0 = max(y1 - 1, 0)
17 x2 = min(x1 + 1, width - 1)
18 y2 = min(y1 + 1, height - 1)
19 pos = (y1 * width + x1) * 4
20 zeroes = int(x1 == x0 or x1 == x2 or y1 == y0 or y1 == y2)
21 min_delta = max_delta = 0.0
22 min_x = min_y = max_x = max_y = 0
23
24 # go through 8 adjacent pixels
25 for x in range(x0, x2 + 1):
26 for y in range(y0, y2 + 1):
27 if x == x1 and y == y1:
28 continue
29
30 # brightness delta between the center pixel and adjacent one
31 delta = color_delta(img, img, pos, (y * width + x) * 4, y_only=True)
32
33 # count the number of equal, darker and brighter adjacent pixels
34 if delta == 0:
35 zeroes += 1
36 # if found more than 2 equal siblings, it's definitely not anti-aliasing
37 if zeroes > 2:
38 return False
39
40 # remember the darkest pixel
41 elif delta < min_delta:
42 min_delta = delta
43 min_x = x
44 min_y = y
45
46 # remember the brightest pixel
47 elif delta > max_delta:
48 max_delta = delta
49 max_x = x
50 max_y = y
51
52 # if there are no both darker and brighter pixels among siblings, it's not anti-aliasing
53 if min_delta == 0 or max_delta == 0:
54 return False
55
56 # if either the darkest or the brightest pixel has 3+ equal siblings in both images
57 # (definitely not anti-aliased), this pixel is anti-aliased
58 return (
59 has_many_siblings(img, min_x, min_y, width, height) and has_many_siblings(img2, min_x, min_y, width, height)
60 ) or (
61 has_many_siblings(img, max_x, max_y, width, height) and has_many_siblings(img2, max_x, max_y, width, height)
62 )
63
64
65 def has_many_siblings(img: ImageSequence, x1: int, y1: int, width: int, height: int) -> bool:
66 """
67 check if a pixel has 3+ adjacent pixels of the same color.
68 """
69 x0 = max(x1 - 1, 0)
70 y0 = max(y1 - 1, 0)
71 x2 = min(x1 + 1, width - 1)
72 y2 = min(y1 + 1, height - 1)
73 pos = (y1 * width + x1) * 4
74 zeroes = int(x1 == x0 or x1 == x2 or y1 == y0 or y1 == y2)
75
76 # go through 8 adjacent pixels
77 for x in range(x0, x2 + 1):
78 for y in range(y0, y2 + 1):
79 if x == x1 and y == y1:
80 continue
81
82 pos2 = (y * width + x) * 4
83 if all(img[pos + offset] == img[pos2 + offset] for offset in range(4)):
84 zeroes += 1
85
86 if zeroes > 2:
87 return True
88
89 return False
90
91
92 def color_delta(img1: ImageSequence, img2: ImageSequence, k: int, m: int, y_only: bool = False) -> float:
93 """
94 calculate color difference according to the paper "Measuring perceived color difference
95 using YIQ NTSC transmission color space in mobile applications" by Y. Kotsarenko and F. Ramos
96 """
97 r1, g1, b1, a1 = [img1[k + offset] for offset in range(4)]
98 r2, g2, b2, a2 = [img2[m + offset] for offset in range(4)]
99
100 if a1 == a2 and r1 == r2 and g1 == g2 and b1 == b2:
101 return 0.0
102
103 if a1 < 255:
104 a1 /= 255
105 r1, b1, g1 = blendRGB(r1, b1, g1, a1)
106
107 if a2 < 255:
108 a2 /= 255
109 r2, b2, g2 = blendRGB(r2, b2, g2, a2)
110
111 y = rgb2y(r1, g1, b1) - rgb2y(r2, g2, b2)
112
113 if y_only:
114 # brightness difference only
115 return y
116
117 i = rgb2i(r1, g1, b1) - rgb2i(r2, g2, b2)
118 q = rgb2q(r1, g1, b1) - rgb2q(r2, g2, b2)
119
120 return 0.5053 * y * y + 0.299 * i * i + 0.1957 * q * q
121
122
123 def rgb2y(r: float, g: float, b: float) -> float:
124 return r * 0.29889531 + g * 0.58662247 + b * 0.11448223
125
126
127 def rgb2i(r: float, g: float, b: float) -> float:
128 return r * 0.59597799 - g * 0.27417610 - b * 0.32180189
129
130
131 def rgb2q(r: float, g: float, b: float) -> float:
132 return r * 0.21147017 - g * 0.52261711 + b * 0.31114694
133
134
135 def blendRGB(r: float, g: float, b: float, a: float):
136 """
137 Blend r, g, and b with a
138 :param r: red channel to blend with a
139 :param g: green channel to blend with a
140 :param b: blue channel to blend with a
141 :param a: alpha to blend with
142 :return: tuple of blended r, g, b
143 """
144 return blend(r, a), blend(g, a), blend(b, a)
145
146
147 def blend(c: float, a: float) -> float:
148 """blend semi-transparent color with white"""
149 return 255 + (c - 255) * a
150
151
152 def draw_pixel(output: MutableImageSequence, pos: int, r: float, g: float, b: float) -> None:
153 output[pos + 0] = int(r)
154 output[pos + 1] = int(g)
155 output[pos + 2] = int(b)
156 output[pos + 3] = 255
157
158
159 def draw_gray_pixel(img: ImageSequence, i: int, alpha: float, output: MutableImageSequence) -> None:
160 r = img[i + 0]
161 g = img[i + 1]
162 b = img[i + 2]
163 val = blend(rgb2y(r, g, b), alpha * img[i + 3] / 255)
164 draw_pixel(output, i, val, val, val)
165
166
167 def ignorable_coord(x: int, y: int, masked_areas: Collection[ImageArea]) -> bool:
168 for (x0, y0, x1, y1) in masked_areas:
169 if x >= x0 - 2 and x < x1 + 2 and y >= y0 - 2 and y < y1 + 2:
170 return True
171 return False