Debian Codegraph @

pixelmatch/utils.py

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