aboutsummaryrefslogtreecommitdiff
path: root/build/js/imaging/blur.js
blob: 61bec8679af9630b0a7173e53b59714be54b3959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Fast gaussian blur approximation (three successive box blurs), operating on
// premultiplied alpha so transparent pixels don't bleed dark fringes.

function boxesForGauss(sigma, n = 3) {
  const wIdeal = Math.sqrt((12 * sigma * sigma) / n + 1);
  let wl = Math.floor(wIdeal);
  if (wl % 2 === 0) wl--;
  const wu = wl + 2;
  const mIdeal = (12 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / (-4 * wl - 4);
  const m = Math.round(mIdeal);
  const sizes = [];
  for (let i = 0; i < n; i++) sizes.push(i < m ? wl : wu);
  return sizes.map((s) => Math.max(0, (s - 1) / 2));
}

/** Blur one Float32 channel plane in place (tmp is scratch of the same size). */
function boxBlurPlane(plane, tmp, w, h, r) {
  if (r < 1) return;
  const iarr = 1 / (r + r + 1);
  // horizontal: plane -> tmp
  for (let y = 0; y < h; y++) {
    const row = y * w;
    const first = plane[row], last = plane[row + w - 1];
    let acc = (r + 1) * first;
    for (let j = 0; j < r; j++) acc += plane[row + Math.min(j, w - 1)];
    for (let x = 0; x < w; x++) {
      const addIdx = x + r;
      const subIdx = x - r - 1;
      acc += (addIdx < w ? plane[row + addIdx] : last) - (subIdx >= 0 ? plane[row + subIdx] : first);
      tmp[row + x] = acc * iarr;
    }
  }
  // vertical: tmp -> plane
  for (let x = 0; x < w; x++) {
    const first = tmp[x], last = tmp[x + (h - 1) * w];
    let acc = (r + 1) * first;
    for (let j = 0; j < r; j++) acc += tmp[x + Math.min(j, h - 1) * w];
    for (let y = 0; y < h; y++) {
      const addIdx = y + r;
      const subIdx = y - r - 1;
      acc += (addIdx < h ? tmp[x + addIdx * w] : last) - (subIdx >= 0 ? tmp[x + subIdx * w] : first);
      plane[x + y * w] = acc * iarr;
    }
  }
}

function gaussPlane(plane, tmp, w, h, radius) {
  const sigma = radius / 2;
  for (const r of boxesForGauss(sigma)) boxBlurPlane(plane, tmp, w, h, Math.round(r));
}

/** Gaussian blur an ImageData in place (edges clamp). */
export function blurImageData(id, radius) {
  if (radius <= 0) return id;
  const { width: w, height: h, data: d } = id;
  const n = w * h;
  const planes = [new Float32Array(n), new Float32Array(n), new Float32Array(n), new Float32Array(n)];
  const tmp = new Float32Array(n);
  for (let i = 0, p = 0; i < n; i++, p += 4) {
    const a = d[p + 3] / 255;
    planes[0][i] = d[p] * a;
    planes[1][i] = d[p + 1] * a;
    planes[2][i] = d[p + 2] * a;
    planes[3][i] = d[p + 3];
  }
  for (const pl of planes) gaussPlane(pl, tmp, w, h, radius);
  for (let i = 0, p = 0; i < n; i++, p += 4) {
    const a = planes[3][i];
    d[p + 3] = a;
    if (a > 0.0001) {
      const inv = 255 / a;
      d[p] = planes[0][i] * inv;
      d[p + 1] = planes[1][i] * inv;
      d[p + 2] = planes[2][i] * inv;
    }
  }
  return id;
}

/** Blur only the alpha channel of a canvas in place (used for selection feathering). */
export function blurAlpha(canvas, radius) {
  if (radius <= 0) return;
  const ctx = canvas.getContext('2d', { willReadFrequently: true });
  const { width: w, height: h } = canvas;
  const id = ctx.getImageData(0, 0, w, h);
  const d = id.data;
  const n = w * h;
  const plane = new Float32Array(n);
  const tmp = new Float32Array(n);
  for (let i = 0; i < n; i++) plane[i] = d[i * 4 + 3];
  // Pad conceptually with zeros by blurring and letting the clamp handle edges;
  // selections touching the border stay selected there, which is what users expect.
  gaussPlane(plane, tmp, w, h, radius);
  for (let i = 0; i < n; i++) d[i * 4 + 3] = plane[i];
  ctx.putImageData(id, 0, 0);
}