-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
160 lines (137 loc) · 3.64 KB
/
index.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { WorkerResizeOptions } from './meta.js';
import type { InitInput as InitResizeInput } from './lib/resize/pkg/squoosh_resize.js';
import type { InitInput as InitHqxInput } from './lib/hqx/pkg/squooshhqx.js';
import { getContainOffsets } from './util.js';
import initResizeWasm, {
resize as wasmResize,
} from './lib/resize/pkg/squoosh_resize.js';
import initHqxWasm, { resize as wasmHqx } from './lib/hqx/pkg/squooshhqx.js';
import { defaultOptions } from './meta.js';
let resizeWasmReady: Promise<unknown>;
let hqxWasmReady: Promise<unknown>;
export function initResize(moduleOrPath?: InitResizeInput) {
if (!resizeWasmReady) {
resizeWasmReady = initResizeWasm(moduleOrPath);
}
return resizeWasmReady;
}
export function initHqx(moduleOrPath?: InitHqxInput) {
if (!hqxWasmReady) {
hqxWasmReady = initHqxWasm(moduleOrPath);
}
return hqxWasmReady;
}
interface HqxResizeOptions extends WorkerResizeOptions {
method: 'hqx';
}
function optsIsHqxOpts(opts: WorkerResizeOptions): opts is HqxResizeOptions {
return opts.method === 'hqx';
}
function crop(
data: ImageData,
sx: number,
sy: number,
sw: number,
sh: number,
): ImageData {
const inputPixels = new Uint32Array(data.data.buffer);
// Copy within the same buffer for speed and memory efficiency.
for (let y = 0; y < sh; y += 1) {
const start = (y + sy) * data.width + sx;
inputPixels.copyWithin(y * sw, start, start + sw);
}
return new ImageData(
new Uint8ClampedArray(inputPixels.buffer.slice(0, sw * sh * 4)),
sw,
sh,
);
}
interface ClampOpts {
min?: number;
max?: number;
}
function clamp(
num: number,
{ min = Number.MIN_VALUE, max = Number.MAX_VALUE }: ClampOpts,
): number {
return Math.min(Math.max(num, min), max);
}
/** Resize methods by index */
const resizeMethods: WorkerResizeOptions['method'][] = [
'triangle',
'catrom',
'mitchell',
'lanczos3',
];
async function hqx(
input: ImageData,
opts: HqxResizeOptions,
): Promise<ImageData> {
await initHqx();
const widthRatio = opts.width / input.width;
const heightRatio = opts.height / input.height;
const ratio = Math.max(widthRatio, heightRatio);
const factor = clamp(Math.ceil(ratio), { min: 1, max: 4 }) as 1 | 2 | 3 | 4;
if (factor === 1) return input;
const result = wasmHqx(
new Uint32Array(input.data.buffer),
input.width,
input.height,
factor,
);
return new ImageData(
new Uint8ClampedArray(result.buffer),
input.width * factor,
input.height * factor,
);
}
export default async function resize(
data: ImageData,
overrideOptions: Partial<WorkerResizeOptions> & {
width: number;
height: number;
},
): Promise<ImageData> {
let options: WorkerResizeOptions = {
...(defaultOptions as WorkerResizeOptions),
...overrideOptions,
};
let input = data;
initResize();
if (optsIsHqxOpts(options)) {
input = await hqx(input, options);
// Regular resize to make up the difference
options = { ...options, method: 'catrom' };
}
await resizeWasmReady;
if (options.fitMethod === 'contain') {
const { sx, sy, sw, sh } = getContainOffsets(
data.width,
data.height,
options.width,
options.height,
);
input = crop(
input,
Math.round(sx),
Math.round(sy),
Math.round(sw),
Math.round(sh),
);
}
const result = wasmResize(
new Uint8Array(input.data.buffer),
input.width,
input.height,
options.width,
options.height,
resizeMethods.indexOf(options.method),
options.premultiply,
options.linearRGB,
);
return new ImageData(
new Uint8ClampedArray(result.buffer),
options.width,
options.height,
);
}