Repository navigation
Expand file tree
/
Copy pathimageCompression.ts
More file actions
555 lines (511 loc) · 18.1 KB
/
Copy pathimageCompression.ts
File metadata and controls
555 lines (511 loc) · 18.1 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import {
downscalableImageMimeType,
fitImageAttachmentSize,
IMAGE_ATTACHMENT_MAX_EDGE,
} from "@t3tools/client-runtime/image-attachment-size";
import {
IMAGE_DIMENSIONS_HEADER_BYTES,
readImageDimensions,
} from "@t3tools/shared/imageDimensions";
const MAX_DIMENSION = IMAGE_ATTACHMENT_MAX_EDGE;
export const MAX_STASH_IMAGE_DATA_URL_CHARS = 1_300_000;
/** Bound source decoding: enormous ImageBitmaps can OOM the tab before resizing. */
export const MAX_COMPRESSIBLE_SOURCE_BYTES = 50 * 1024 * 1024;
const MAX_HEIC_DECODE_PIXELS = 64_000_000;
const MAX_HEIC_METADATA_BYTES = 1024 * 1024;
const QUALITY_STEPS = [0.92, 0.85, 0.78, 0.68] as const;
const FALLBACK_SCALE_STEPS = [0.75, 0.55] as const;
const HEIC_IMAGE_MIME_TYPE = /^image\/hei(?:c|f)$/i;
const HEIC_IMAGE_EXTENSION = /\.(?:heic|heif)$/i;
type ImageSize = { width: number; height: number };
export interface CompressedStashImage {
imageSize?: ImageSize;
dataUrl: string;
mimeType: string;
sizeBytes: number;
recompressed: boolean;
}
export type ImageCompressionFailureReason = "too-large" | "unreadable";
export type CompressStashImageResult =
| { ok: true; image: CompressedStashImage }
| { ok: false; reason: ImageCompressionFailureReason };
export type CompressImageFileResult =
| { ok: true; file: File; recompressed: boolean; imageSize?: ImageSize }
| { ok: false; reason: ImageCompressionFailureReason };
/** Finder and some browsers omit the MIME type when dragging HEIC photos. */
export function isHeicImageFile(file: Pick<File, "name" | "type">): boolean {
if (HEIC_IMAGE_MIME_TYPE.test(file.type)) {
return true;
}
return (
(file.type === "" || file.type.toLowerCase() === "application/octet-stream") &&
HEIC_IMAGE_EXTENSION.test(file.name)
);
}
interface HeicMetadataBox {
payloadOffset: number;
endOffset: number;
}
function findHeicMetadataBox(
view: DataView,
startOffset: number,
endOffset: number,
type: number,
): HeicMetadataBox | null {
let offset = startOffset;
while (offset + 8 <= endOffset) {
let size = view.getUint32(offset);
let headerSize = 8;
if (size === 1) {
if (offset + 16 > endOffset) return null;
const extendedSize = view.getBigUint64(offset + 8);
if (extendedSize > BigInt(Number.MAX_SAFE_INTEGER)) return null;
size = Number(extendedSize);
headerSize = 16;
} else if (size === 0) {
size = endOffset - offset;
}
if (size < headerSize || size > endOffset - offset) return null;
const nextOffset = offset + size;
if (view.getUint32(offset + 4) === type) {
return { payloadOffset: offset + headerSize, endOffset: nextOffset };
}
offset = nextOffset;
}
return null;
}
async function validateHeicImageDimensions(
file: File,
): Promise<ImageCompressionFailureReason | null> {
const metadata = await file.slice(0, MAX_HEIC_METADATA_BYTES).arrayBuffer();
const view = new DataView(metadata);
const meta = findHeicMetadataBox(view, 0, view.byteLength, 0x6d657461);
if (!meta || meta.payloadOffset + 4 > meta.endOffset) return "unreadable";
const properties = findHeicMetadataBox(view, meta.payloadOffset + 4, meta.endOffset, 0x69707270);
if (!properties) return "unreadable";
const containers = findHeicMetadataBox(
view,
properties.payloadOffset,
properties.endOffset,
0x6970636f,
);
if (!containers) return "unreadable";
let offset = containers.payloadOffset;
let foundImageDimensions = false;
while (offset < containers.endOffset) {
const image = findHeicMetadataBox(view, offset, containers.endOffset, 0x69737065);
if (!image) break;
if (image.payloadOffset + 12 > image.endOffset) return "unreadable";
const width = view.getUint32(image.payloadOffset + 4);
const height = view.getUint32(image.payloadOffset + 8);
if (width === 0 || height === 0) return "unreadable";
if (width > MAX_HEIC_DECODE_PIXELS / height) return "too-large";
foundImageDimensions = true;
offset = image.endOffset;
}
return foundImageDimensions ? null : "unreadable";
}
export function readFileAsDataUrl(file: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.addEventListener("load", () => {
if (typeof reader.result === "string") {
resolve(reader.result);
return;
}
reject(new Error("Could not read image data."));
});
reader.addEventListener("error", () => {
reject(reader.error ?? new Error("Failed to read image."));
});
reader.readAsDataURL(file);
});
}
function dataUrlLength(blob: Blob): number {
return (
`data:${blob.type || "application/octet-stream"};base64,`.length + 4 * Math.ceil(blob.size / 3)
);
}
export function dataUrlToFile(dataUrl: string, name: string, mimeType: string): File {
const payload = dataUrl.slice(dataUrl.indexOf(",") + 1);
const binary = atob(payload);
const bytes = new Uint8Array(binary.length);
for (let index = 0; index < binary.length; index += 1) {
bytes[index] = binary.charCodeAt(index);
}
return new File([bytes], name, { type: mimeType });
}
function fileNameForMimeType(name: string, mimeType: string): string {
const extension = mimeType === "image/webp" ? ".webp" : ".jpg";
const dotIndex = name.lastIndexOf(".");
const base = dotIndex > 0 ? name.slice(0, dotIndex) : name;
return `${base}${extension}`;
}
function canRecompress(): boolean {
return (
typeof createImageBitmap === "function" &&
(typeof OffscreenCanvas === "function" || typeof document !== "undefined")
);
}
interface Canvas2D {
canvas: OffscreenCanvas | HTMLCanvasElement;
context: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D;
}
function createCanvas(width: number, height: number): Canvas2D | null {
if (typeof OffscreenCanvas === "function") {
const canvas = new OffscreenCanvas(width, height);
const context = canvas.getContext("2d");
if (!context) return null;
return { canvas, context };
}
if (typeof document === "undefined") return null;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
if (!context) return null;
return { canvas, context };
}
async function encodeCanvas(
canvas: OffscreenCanvas | HTMLCanvasElement,
quality: number,
mimeType: string,
): Promise<Blob | null> {
let blob: Blob | null;
if (typeof HTMLCanvasElement !== "undefined" && canvas instanceof HTMLCanvasElement) {
blob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, mimeType, quality));
} else {
blob = await (canvas as OffscreenCanvas).convertToBlob({ type: mimeType, quality });
}
return blob?.type === mimeType ? blob : null;
}
const composerThumbnails = new WeakMap<File, Promise<string | null>>();
const settledComposerThumbnails = new WeakMap<File, string | null>();
function cacheComposerImageThumbnail(
file: File,
thumbnail: Promise<string | null>,
): Promise<string | null> {
composerThumbnails.set(file, thumbnail);
void thumbnail.then((src) => settledComposerThumbnails.set(file, src));
return thumbnail;
}
/** `undefined` until the thumbnail settles, so a tile can paint a known thumbnail immediately. */
export function readComposerImageThumbnail(file: File): string | null | undefined {
return settledComposerThumbnails.get(file);
}
export function createComposerImageThumbnail(file: File): Promise<string | null> {
return (
composerThumbnails.get(file) ??
cacheComposerImageThumbnail(file, renderComposerImageThumbnail(file))
);
}
/**
* Pasted images are re-encoded after their placeholder already shows the source's thumbnail.
* Reusing it keeps the tile from flashing back to its fallback while the new file is decoded.
*/
export function inheritComposerImageThumbnail(source: File, target: File): void {
const sourceThumbnail = composerThumbnails.get(source);
if (source === target || !sourceThumbnail || composerThumbnails.has(target)) return;
const settled = settledComposerThumbnails.get(source);
if (settled) settledComposerThumbnails.set(target, settled);
cacheComposerImageThumbnail(
target,
sourceThumbnail.then((src) => src ?? renderComposerImageThumbnail(target)),
);
}
async function renderComposerImageThumbnail(file: File): Promise<string | null> {
if (isHeicImageFile(file)) return prepareHeicImage(file).thumbnail;
// Placeholders thumbnail the uncompressed source, which can be big enough to OOM the decode.
if (!canRecompress() || file.size > MAX_COMPRESSIBLE_SOURCE_BYTES) return null;
let bitmap: ImageBitmap | undefined;
try {
bitmap = await createImageBitmap(file);
return await renderComposerImageThumbnailFromBitmap(bitmap);
} catch {
return null;
} finally {
bitmap?.close();
}
}
async function renderComposerImageThumbnailFromBitmap(bitmap: ImageBitmap): Promise<string | null> {
try {
const side = Math.min(bitmap.width, bitmap.height);
if (side <= 0) return null;
const dimension = Math.min(256, side);
const surface = createCanvas(dimension, dimension);
if (!surface) return null;
surface.context.drawImage(
bitmap,
(bitmap.width - side) / 2,
(bitmap.height - side) / 2,
side,
side,
0,
0,
dimension,
dimension,
);
const blob = await encodeCanvas(surface.canvas, 1, "image/png");
return blob ? await readFileAsDataUrl(blob) : null;
} catch {
return null;
}
}
async function encodeWithinBudget(
bitmap: ImageBitmap,
maxDimension: number,
budgetChars: number,
preferredMimeType?: "image/jpeg",
): Promise<{ blob: Blob; mimeType: string; imageSize: ImageSize } | null> {
const scale = Math.min(1, maxDimension / Math.max(bitmap.width, bitmap.height));
const width = Math.max(1, Math.round(bitmap.width * scale));
const height = Math.max(1, Math.round(bitmap.height * scale));
const target = createCanvas(width, height);
if (!target) return null;
// Probe the codec before drawing; JPEG needs a white matte because it has no alpha.
const mimeType =
preferredMimeType ??
((await encodeCanvas(target.canvas, QUALITY_STEPS[0], "image/webp"))
? "image/webp"
: "image/jpeg");
if (mimeType === "image/jpeg") {
target.context.fillStyle = "#ffffff";
target.context.fillRect(0, 0, width, height);
}
target.context.drawImage(bitmap, 0, 0, width, height);
for (const quality of QUALITY_STEPS) {
const encoded = await encodeCanvas(target.canvas, quality, mimeType);
if (!encoded) break;
if (dataUrlLength(encoded) <= budgetChars) {
return { blob: encoded, mimeType, imageSize: { width, height } };
}
}
return null;
}
type ReencodeResult =
| { ok: true; blob: Blob; mimeType: string; imageSize: ImageSize }
| { ok: false; reason: ImageCompressionFailureReason };
async function reencodeWithinBudget(
file: File,
budgetChars: number,
preferredMimeType?: "image/jpeg",
): Promise<ReencodeResult> {
if (!canRecompress()) {
return { ok: false, reason: "too-large" };
}
let bitmap: ImageBitmap;
try {
bitmap = await createImageBitmap(file);
} catch {
return { ok: false, reason: "unreadable" };
}
try {
// Shrink the previous target; using MAX_DIMENSION leaves smaller images unchanged.
const baseDimension = Math.min(MAX_DIMENSION, Math.max(bitmap.width, bitmap.height));
let encodeFailed = false;
for (const dimensionScale of [1, ...FALLBACK_SCALE_STEPS]) {
const targetDimension = Math.max(1, Math.round(baseDimension * dimensionScale));
let encoded: Awaited<ReturnType<typeof encodeWithinBudget>>;
try {
encoded = await encodeWithinBudget(bitmap, targetDimension, budgetChars, preferredMimeType);
} catch {
encodeFailed = true;
continue;
}
encodeFailed = false;
if (encoded) {
return {
ok: true,
blob: encoded.blob,
mimeType: encoded.mimeType,
imageSize: encoded.imageSize,
};
}
}
return { ok: false, reason: encodeFailed ? "unreadable" : "too-large" };
} finally {
bitmap.close();
}
}
export async function compressImageForStash(
file: File,
budgetChars: number = MAX_STASH_IMAGE_DATA_URL_CHARS,
): Promise<CompressStashImageResult> {
try {
if (dataUrlLength(file) <= budgetChars) {
return {
ok: true,
image: {
dataUrl: await readFileAsDataUrl(file),
mimeType: file.type,
sizeBytes: file.size,
recompressed: false,
},
};
}
const reencoded = await reencodeWithinBudget(file, budgetChars);
if (!reencoded.ok) {
return reencoded;
}
return {
ok: true,
image: {
dataUrl: await readFileAsDataUrl(reencoded.blob),
mimeType: reencoded.mimeType,
sizeBytes: reencoded.blob.size,
recompressed: true,
imageSize: reencoded.imageSize,
},
};
} catch {
return { ok: false, reason: "unreadable" };
}
}
async function fitImageToMaxEdge(file: File): Promise<{ file: File; imageSize: ImageSize } | null> {
const mimeType = downscalableImageMimeType(file.type);
if (!mimeType || !canRecompress() || file.size > MAX_COMPRESSIBLE_SOURCE_BYTES) return null;
let bitmap: ImageBitmap | undefined;
try {
const header = readImageDimensions(
new Uint8Array(await file.slice(0, IMAGE_DIMENSIONS_HEADER_BYTES).arrayBuffer()),
);
if (!header || !fitImageAttachmentSize(header)) return null;
bitmap = await createImageBitmap(file, { imageOrientation: "from-image" });
const imageSize = fitImageAttachmentSize(bitmap);
if (!imageSize) return null;
const target = createCanvas(imageSize.width, imageSize.height);
if (!target) return null;
target.context.imageSmoothingQuality = "high";
target.context.drawImage(bitmap, 0, 0, imageSize.width, imageSize.height);
const blob = await encodeCanvas(target.canvas, QUALITY_STEPS[0], mimeType);
if (!blob) return null;
return {
file: new File([blob], file.name, { type: mimeType, lastModified: file.lastModified }),
imageSize,
};
} catch {
return null;
} finally {
bitmap?.close();
}
}
export async function compressImageToByteLimit(
file: File,
maxBytes: number,
options?: { preferredMimeType?: "image/jpeg"; sourceSizeBytes?: number },
): Promise<CompressImageFileResult> {
const fitted = await fitImageToMaxEdge(file);
if (fitted && fitted.file.size <= maxBytes) {
return { ok: true, file: fitted.file, recompressed: true, imageSize: fitted.imageSize };
}
if (!fitted && file.size <= maxBytes) {
return { ok: true, file, recompressed: false };
}
if ((options?.sourceSizeBytes ?? file.size) > MAX_COMPRESSIBLE_SOURCE_BYTES) {
return { ok: false, reason: "too-large" };
}
const budgetChars = Math.floor(maxBytes / 3) * 4;
const reencoded = await reencodeWithinBudget(
fitted?.file ?? file,
budgetChars,
options?.preferredMimeType,
);
if (!reencoded.ok) {
return reencoded;
}
return {
ok: true,
file: new File(
[reencoded.blob],
fileNameForMimeType(file.name || "image", reencoded.mimeType),
{ type: reencoded.mimeType },
),
recompressed: true,
imageSize: reencoded.imageSize,
};
}
const heicPreparations = new WeakMap<
File,
{ image: Promise<CompressImageFileResult>; thumbnail: Promise<string | null> }
>();
// Preparing tiles share full-size decodes, so keep only one HEIC bitmap alive at a time.
let heicPreparationQueue = Promise.resolve();
function prepareHeicImage(file: File) {
const cached = heicPreparations.get(file);
if (cached) return cached;
let resolveThumbnail!: (src: string | null) => void;
const thumbnail = new Promise<string | null>((resolve) => {
resolveThumbnail = resolve;
});
const image = heicPreparationQueue
.then(() => convertHeicImage(file, resolveThumbnail))
.finally(() => resolveThumbnail(null));
heicPreparationQueue = image.then(
() => undefined,
() => undefined,
);
const preparation = { image, thumbnail };
heicPreparations.set(file, preparation);
return preparation;
}
async function convertHeicImage(
file: File,
onThumbnail: (src: string | null) => void,
): Promise<CompressImageFileResult> {
if (file.size > MAX_COMPRESSIBLE_SOURCE_BYTES) {
return { ok: false, reason: "too-large" };
}
let bitmap: ImageBitmap | undefined;
try {
const dimensionError = await validateHeicImageDimensions(file);
if (dimensionError) {
return { ok: false, reason: dimensionError };
}
bitmap =
typeof createImageBitmap === "function"
? await createImageBitmap(file).catch(() => undefined)
: undefined;
if (!bitmap) {
const { heicTo } = await import("heic-to/csp");
bitmap = await heicTo({ blob: file, type: "bitmap" });
}
const imageSize = fitImageAttachmentSize(bitmap) ?? {
width: bitmap.width,
height: bitmap.height,
};
const surface = createCanvas(imageSize.width, imageSize.height);
if (!surface) return { ok: false, reason: "unreadable" };
surface.context.drawImage(bitmap, 0, 0, imageSize.width, imageSize.height);
const [converted] = await Promise.all([
encodeCanvas(surface.canvas, QUALITY_STEPS[0], "image/jpeg"),
renderComposerImageThumbnailFromBitmap(bitmap).then(onThumbnail),
]);
if (!converted) return { ok: false, reason: "unreadable" };
return {
ok: true,
file: new File([converted], fileNameForMimeType(file.name || "image", "image/jpeg"), {
type: "image/jpeg",
lastModified: file.lastModified,
}),
recompressed: true,
imageSize,
};
} catch {
return { ok: false, reason: "unreadable" };
} finally {
bitmap?.close();
}
}
export async function prepareImageForAttachment(
file: File,
maxBytes: number,
): Promise<CompressImageFileResult> {
if (!isHeicImageFile(file)) return compressImageToByteLimit(file, maxBytes);
const prepared = await prepareHeicImage(file).image;
if (!prepared.ok || prepared.file.size <= maxBytes) return prepared;
const result = await compressImageToByteLimit(prepared.file, maxBytes, {
preferredMimeType: "image/jpeg",
sourceSizeBytes: file.size,
});
return result.ok ? { ...result, recompressed: true } : result;
}