Skip to content

Commit 062d5bb

Browse files
committed
inline rgb transformations
1 parent 336c346 commit 062d5bb

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

index.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function pixelmatch(img1, img2, output, width, height, options =
4949
const pos = (y * width + x) * 4;
5050

5151
// squared YUV distance between colors at this pixel position, negative if the img2 pixel is darker
52-
const delta = colorDelta(img1, img2, pos, pos);
52+
const delta = colorDelta(img1, img2, pos, pos, false);
5353

5454
// the color difference is above the threshold
5555
if (Math.abs(delta) > maxDelta) {
@@ -171,12 +171,12 @@ function hasManySiblings(img, x1, y1, width, height) {
171171
// using YIQ NTSC transmission color space in mobile applications" by Y. Kotsarenko and F. Ramos
172172

173173
function colorDelta(img1, img2, k, m, yOnly) {
174-
let r1 = img1[k + 0];
174+
let r1 = img1[k];
175175
let g1 = img1[k + 1];
176176
let b1 = img1[k + 2];
177177
let a1 = img1[k + 3];
178178

179-
let r2 = img2[m + 0];
179+
let r2 = img2[m];
180180
let g2 = img2[m + 1];
181181
let b2 = img2[m + 2];
182182
let a2 = img2[m + 3];
@@ -197,25 +197,23 @@ function colorDelta(img1, img2, k, m, yOnly) {
197197
b2 = blend(b2, a2);
198198
}
199199

200-
const y1 = rgb2y(r1, g1, b1);
201-
const y2 = rgb2y(r2, g2, b2);
202-
const y = y1 - y2;
200+
const dr = r1 - r2;
201+
const dg = g1 - g2;
202+
const db = b1 - b2;
203+
204+
const y = dr * 0.29889531 + dg * 0.58662247 + db * 0.11448223;
203205

204206
if (yOnly) return y; // brightness difference only
205207

206-
const i = rgb2i(r1, g1, b1) - rgb2i(r2, g2, b2);
207-
const q = rgb2q(r1, g1, b1) - rgb2q(r2, g2, b2);
208+
const i = dr * 0.59597799 - dg * 0.27417610 - db * 0.32180189;
209+
const q = dr * 0.21147017 - dg * 0.52261711 + db * 0.31114694;
208210

209211
const delta = 0.5053 * y * y + 0.299 * i * i + 0.1957 * q * q;
210212

211213
// encode whether the pixel lightens or darkens in the sign
212-
return y1 > y2 ? -delta : delta;
214+
return y > 0 ? -delta : delta;
213215
}
214216

215-
function rgb2y(r, g, b) { return r * 0.29889531 + g * 0.58662247 + b * 0.11448223; }
216-
function rgb2i(r, g, b) { return r * 0.59597799 - g * 0.27417610 - b * 0.32180189; }
217-
function rgb2q(r, g, b) { return r * 0.21147017 - g * 0.52261711 + b * 0.31114694; }
218-
219217
// blend semi-transparent color with white
220218
function blend(c, a) {
221219
return 255 + (c - 255) * a;
@@ -232,6 +230,6 @@ function drawGrayPixel(img, i, alpha, output) {
232230
const r = img[i + 0];
233231
const g = img[i + 1];
234232
const b = img[i + 2];
235-
const val = blend(rgb2y(r, g, b), alpha * img[i + 3] / 255);
233+
const val = blend(r * 0.29889531 + g * 0.58662247 + b * 0.11448223, alpha * img[i + 3] / 255);
236234
drawPixel(output, i, val, val, val);
237235
}

0 commit comments

Comments
 (0)