File size: 3,732 Bytes
b87996d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Port of Pillow's 8-bit resampling (libImaging/Resample.c) for RGB images, as used by torchvision's Resize(BICUBIC) on a PIL image,
// so the browser feeds the vision tower the same pixels as the Python original. img: { w, h, data: Uint8Array RGB }.
const PRECISION_BITS = 22;
const ONE = 1 << PRECISION_BITS;

const FILTERS = {
  bicubic: { support: 2.0, f: (x) => {
    const a = -0.5;
    if (x < 0) x = -x;
    if (x < 1) return ((a + 2) * x - (a + 3)) * x * x + 1;
    if (x < 2) return (((x - 5) * x + 8) * x - 4) * a;
    return 0;
  } },
  lanczos: { support: 3.0, f: (x) => {
    const sinc = (v) => { if (v === 0) return 1; v *= Math.PI; return Math.sin(v) / v; };
    return (x >= -3 && x < 3) ? sinc(x) * sinc(x / 3) : 0;
  } },
};

function coeffs(inSize, outSize, filter) {
  const scale = inSize / outSize;
  const filterscale = scale < 1 ? 1 : scale;
  const support = filter.support * filterscale;
  const ksize = Math.ceil(support) * 2 + 1;
  const kk = new Int32Array(outSize * ksize), bounds = new Int32Array(outSize * 2);
  const pre = new Float64Array(ksize);
  for (let xx = 0; xx < outSize; xx++) {
    const center = (xx + 0.5) * scale, ss = 1 / filterscale;
    let xmin = Math.trunc(center - support + 0.5); if (xmin < 0) xmin = 0;
    let xmax = Math.trunc(center + support + 0.5); if (xmax > inSize) xmax = inSize;
    xmax -= xmin;
    let ww = 0;
    for (let x = 0; x < xmax; x++) { const w = filter.f((x + xmin - center + 0.5) * ss); pre[x] = w; ww += w; }
    for (let x = 0; x < xmax; x++) {
      const v = ww !== 0 ? pre[x] / ww : pre[x];
      kk[xx * ksize + x] = v < 0 ? Math.trunc(-0.5 + v * ONE) : Math.trunc(0.5 + v * ONE);
    }
    bounds[xx * 2] = xmin; bounds[xx * 2 + 1] = xmax;
  }
  return { kk, bounds, ksize };
}

const clip8 = (v) => (v >= (ONE * 256) ? 255 : v <= 0 ? 0 : Math.floor(v / ONE));

// img: { w, h, data: Uint8Array RGB } -> { w, h, data }
export function resize(img, outW, outH, kind) {
  if (outW === img.w && outH === img.h) return { w: img.w, h: img.h, data: img.data.slice() };
  const filter = FILTERS[kind];
  const H = coeffs(img.w, outW, filter), V = coeffs(img.h, outH, filter);
  const needH = outW !== img.w, needV = outH !== img.h;
  const yFirst = V.bounds[0], yLast = V.bounds[(outH - 1) * 2] + V.bounds[(outH - 1) * 2 + 1];
  let src = img;
  if (needH) {
    const rows = yLast - yFirst, out = new Uint8Array(outW * rows * 3), d = img.data, W = img.w;
    for (let yy = 0; yy < rows; yy++) {
      const rowOff = (yy + yFirst) * W * 3;
      for (let xx = 0; xx < outW; xx++) {
        const xmin = H.bounds[xx * 2], xmax = H.bounds[xx * 2 + 1], k = xx * H.ksize;
        let s0 = ONE / 2, s1 = ONE / 2, s2 = ONE / 2;
        for (let x = 0; x < xmax; x++) {
          const c = H.kk[k + x], p = rowOff + (x + xmin) * 3;
          s0 += d[p] * c; s1 += d[p + 1] * c; s2 += d[p + 2] * c;
        }
        const o = (yy * outW + xx) * 3;
        out[o] = clip8(s0); out[o + 1] = clip8(s1); out[o + 2] = clip8(s2);
      }
    }
    src = { w: outW, h: rows, data: out };
  }
  if (!needV) return src;
  const shift = needH ? yFirst : 0, W = src.w, d = src.data, out = new Uint8Array(W * outH * 3);
  for (let yy = 0; yy < outH; yy++) {
    const ymin = V.bounds[yy * 2] - shift, ymax = V.bounds[yy * 2 + 1], k = yy * V.ksize;
    for (let xx = 0; xx < W; xx++) {
      let s0 = ONE / 2, s1 = ONE / 2, s2 = ONE / 2;
      for (let y = 0; y < ymax; y++) {
        const c = V.kk[k + y], p = ((y + ymin) * W + xx) * 3;
        s0 += d[p] * c; s1 += d[p + 1] * c; s2 += d[p + 2] * c;
      }
      const o = (yy * W + xx) * 3;
      out[o] = clip8(s0); out[o + 1] = clip8(s1); out[o + 2] = clip8(s2);
    }
  }
  return { w: W, h: outH, data: out };
}