|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +export type ImageData = { width: number, height: number, data: Buffer }; |
| 18 | + |
| 19 | +export function padImageToSize(image: ImageData, size: { width: number, height: number }): ImageData { |
| 20 | + if (image.width === size.width && image.height === size.height) |
| 21 | + return image; |
| 22 | + const buffer = new Uint8Array(size.width * size.height * 4); |
| 23 | + for (let y = 0; y < size.height; y++) { |
| 24 | + for (let x = 0; x < size.width; x++) { |
| 25 | + const to = (y * size.width + x) * 4; |
| 26 | + if (y < image.height && x < image.width) { |
| 27 | + const from = (y * image.width + x) * 4; |
| 28 | + buffer[to] = image.data[from]; |
| 29 | + buffer[to + 1] = image.data[from + 1]; |
| 30 | + buffer[to + 2] = image.data[from + 2]; |
| 31 | + buffer[to + 3] = image.data[from + 3]; |
| 32 | + } else { |
| 33 | + buffer[to] = 0; |
| 34 | + buffer[to + 1] = 0; |
| 35 | + buffer[to + 2] = 0; |
| 36 | + buffer[to + 3] = 0; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + return { data: Buffer.from(buffer), width: size.width, height: size.height }; |
| 41 | +} |
| 42 | + |
| 43 | +export function scaleImageToSize(image: ImageData, size: { width: number; height: number }): ImageData { |
| 44 | + const { data: src, width: w1, height: h1 } = image; |
| 45 | + const w2 = size.width | 0, h2 = size.height | 0; |
| 46 | + if (w1 === w2 && h1 === h2) |
| 47 | + return image; |
| 48 | + |
| 49 | + const clamp = (v: number, lo: number, hi: number) => (v < lo ? lo : v > hi ? hi : v); |
| 50 | + |
| 51 | + // Catmull–Rom weights |
| 52 | + const weights = (t: number, o: Float32Array) => { |
| 53 | + const t2 = t * t, t3 = t2 * t; |
| 54 | + o[0] = -0.5 * t + 1.0 * t2 - 0.5 * t3; |
| 55 | + o[1] = 1.0 - 2.5 * t2 + 1.5 * t3; |
| 56 | + o[2] = 0.5 * t + 2.0 * t2 - 1.5 * t3; |
| 57 | + o[3] = -0.5 * t2 + 0.5 * t3; |
| 58 | + }; |
| 59 | + |
| 60 | + const srcRowStride = w1 * 4; |
| 61 | + const dstRowStride = w2 * 4; |
| 62 | + |
| 63 | + // Precompute X: indices, weights, and byte offsets (idx*4) |
| 64 | + const xIdx = new Int32Array(w2 * 4); |
| 65 | + const xOff = new Int32Array(w2 * 4); // byte offsets = xIdx*4 |
| 66 | + const xW = new Float32Array(w2 * 4); |
| 67 | + const wx = new Float32Array(4); |
| 68 | + const xScale = w1 / w2; |
| 69 | + for (let x = 0; x < w2; x++) { |
| 70 | + const sx = (x + 0.5) * xScale - 0.5; |
| 71 | + const sxi = Math.floor(sx); |
| 72 | + const t = sx - sxi; |
| 73 | + weights(t, wx); |
| 74 | + const b = x * 4; |
| 75 | + const i0 = clamp(sxi - 1, 0, w1 - 1); |
| 76 | + const i1 = clamp(sxi + 0, 0, w1 - 1); |
| 77 | + const i2 = clamp(sxi + 1, 0, w1 - 1); |
| 78 | + const i3 = clamp(sxi + 2, 0, w1 - 1); |
| 79 | + xIdx[b + 0] = i0; xIdx[b + 1] = i1; xIdx[b + 2] = i2; xIdx[b + 3] = i3; |
| 80 | + xOff[b + 0] = i0 << 2; xOff[b + 1] = i1 << 2; xOff[b + 2] = i2 << 2; xOff[b + 3] = i3 << 2; |
| 81 | + xW[b + 0] = wx[0]; xW[b + 1] = wx[1]; xW[b + 2] = wx[2]; xW[b + 3] = wx[3]; |
| 82 | + } |
| 83 | + |
| 84 | + // Precompute Y: indices, weights, and row-base byte offsets (y*rowStride) |
| 85 | + const yIdx = new Int32Array(h2 * 4); |
| 86 | + const yRow = new Int32Array(h2 * 4); // row base in bytes |
| 87 | + const yW = new Float32Array(h2 * 4); |
| 88 | + const wy = new Float32Array(4); |
| 89 | + const yScale = h1 / h2; |
| 90 | + for (let y = 0; y < h2; y++) { |
| 91 | + const sy = (y + 0.5) * yScale - 0.5; |
| 92 | + const syi = Math.floor(sy); |
| 93 | + const t = sy - syi; |
| 94 | + weights(t, wy); |
| 95 | + const b = y * 4; |
| 96 | + const j0 = clamp(syi - 1, 0, h1 - 1); |
| 97 | + const j1 = clamp(syi + 0, 0, h1 - 1); |
| 98 | + const j2 = clamp(syi + 1, 0, h1 - 1); |
| 99 | + const j3 = clamp(syi + 2, 0, h1 - 1); |
| 100 | + yIdx[b + 0] = j0; yIdx[b + 1] = j1; yIdx[b + 2] = j2; yIdx[b + 3] = j3; |
| 101 | + yRow[b + 0] = j0 * srcRowStride; |
| 102 | + yRow[b + 1] = j1 * srcRowStride; |
| 103 | + yRow[b + 2] = j2 * srcRowStride; |
| 104 | + yRow[b + 3] = j3 * srcRowStride; |
| 105 | + yW[b + 0] = wy[0]; yW[b + 1] = wy[1]; yW[b + 2] = wy[2]; yW[b + 3] = wy[3]; |
| 106 | + } |
| 107 | + |
| 108 | + const dst = new Uint8Array(w2 * h2 * 4); |
| 109 | + |
| 110 | + for (let y = 0; y < h2; y++) { |
| 111 | + const yb = y * 4; |
| 112 | + const rb0 = yRow[yb + 0], rb1 = yRow[yb + 1], rb2 = yRow[yb + 2], rb3 = yRow[yb + 3]; |
| 113 | + const wy0 = yW[yb + 0], wy1 = yW[yb + 1], wy2 = yW[yb + 2], wy3 = yW[yb + 3]; |
| 114 | + const dstBase = y * dstRowStride; |
| 115 | + |
| 116 | + for (let x = 0; x < w2; x++) { |
| 117 | + const xb = x * 4; |
| 118 | + const xo0 = xOff[xb + 0], xo1 = xOff[xb + 1], xo2 = xOff[xb + 2], xo3 = xOff[xb + 3]; |
| 119 | + const wx0 = xW[xb + 0], wx1 = xW[xb + 1], wx2 = xW[xb + 2], wx3 = xW[xb + 3]; |
| 120 | + const di = dstBase + (x << 2); |
| 121 | + |
| 122 | + // unrolled RGBA |
| 123 | + for (let c = 0; c < 4; c++) { |
| 124 | + const r0 = src[rb0 + xo0 + c] * wx0 + src[rb0 + xo1 + c] * wx1 + src[rb0 + xo2 + c] * wx2 + src[rb0 + xo3 + c] * wx3; |
| 125 | + const r1 = src[rb1 + xo0 + c] * wx0 + src[rb1 + xo1 + c] * wx1 + src[rb1 + xo2 + c] * wx2 + src[rb1 + xo3 + c] * wx3; |
| 126 | + const r2 = src[rb2 + xo0 + c] * wx0 + src[rb2 + xo1 + c] * wx1 + src[rb2 + xo2 + c] * wx2 + src[rb2 + xo3 + c] * wx3; |
| 127 | + const r3 = src[rb3 + xo0 + c] * wx0 + src[rb3 + xo1 + c] * wx1 + src[rb3 + xo2 + c] * wx2 + src[rb3 + xo3 + c] * wx3; |
| 128 | + const v = r0 * wy0 + r1 * wy1 + r2 * wy2 + r3 * wy3; |
| 129 | + dst[di + c] = v < 0 ? 0 : v > 255 ? 255 : v | 0; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return { data: Buffer.from(dst), width: w2, height: h2 }; |
| 135 | +} |
0 commit comments