From 4d84ca84cb30cc12db0b57573b86e87bdbeed6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Chucks=20=F0=9F=87=AC=F0=9F=87=AD?= <31349069+emmanuelchucks@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:40:10 +0000 Subject: [PATCH 1/2] fix(web): pasted 16-bit PNG photos upload at 8 bits per channel --- .../src/lib/imageCompression.browser.test.ts | 24 +++++++++++-- apps/web/src/lib/imageCompression.test.ts | 15 ++++++-- apps/web/src/lib/imageCompression.ts | 35 ++++++++++++------- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/apps/web/src/lib/imageCompression.browser.test.ts b/apps/web/src/lib/imageCompression.browser.test.ts index 2e7597eaa8bf8..6724962cc1e5b 100644 --- a/apps/web/src/lib/imageCompression.browser.test.ts +++ b/apps/web/src/lib/imageCompression.browser.test.ts @@ -12,12 +12,13 @@ async function makeImage( colorSpace: PredefinedColorSpace, fill: string, type: string, + { width = 2400, height = 1600, colorType = "unorm8" } = {}, ): Promise { - const canvas = new OffscreenCanvas(2400, 1600); - const context = canvas.getContext("2d", { colorSpace }); + const canvas = new OffscreenCanvas(width, height); + const context = canvas.getContext("2d", { colorSpace, colorType }); if (!context) throw new Error("no 2d context"); context.fillStyle = fill; - context.fillRect(0, 0, 2400, 1600); + context.fillRect(0, 0, width, height); const blob = await canvas.convertToBlob({ type, quality: 0.95 }); return new File([blob], `shot.${type.slice(6)}`, { type }); } @@ -111,6 +112,23 @@ describe("prepareImageForAttachment color", () => { await expectSameColors(result.file, source); }); + it("redraws a 16-bit Display P3 PNG at 8 bits per channel and keeps its colors", async () => { + const readBitDepth = async (png: Blob) => new Uint8Array(await png.arrayBuffer())[24]; + const source = await makeImage("display-p3", "color(display-p3 0 1 0)", "image/png", { + width: 300, + height: 400, + colorType: "float16", + }); + expect(await readBitDepth(source)).toBe(16); + + const result = await prepareImageForAttachment(source, 20 * 1024 * 1024); + if (!result.ok) throw new Error(result.reason); + expect(result.imageSize).toEqual({ width: 300, height: 400 }); + expect(result.file.type).toBe("image/png"); + expect(await readBitDepth(result.file)).toBe(8); + await expectSameColors(result.file, source); + }); + it("keeps Display P3 colors when the stash re-encodes a WebP", async () => { const source = await makeImage("display-p3", "color(display-p3 0 1 0)", "image/webp"); const result = await compressImageForStash( diff --git a/apps/web/src/lib/imageCompression.test.ts b/apps/web/src/lib/imageCompression.test.ts index e40c3968bc850..40a1ff1f58fae 100644 --- a/apps/web/src/lib/imageCompression.test.ts +++ b/apps/web/src/lib/imageCompression.test.ts @@ -374,7 +374,7 @@ describe("HEIC attachment preparation", () => { describe("attachment max edge", () => { const u32 = (n: number) => [(n >>> 24) & 0xff, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff]; const u16 = (n: number) => [(n >>> 8) & 0xff, n & 0xff]; - const pngHeader = (width: number, height: number) => [ + const pngHeader = (width: number, height: number, bitDepth = 8) => [ 0x89, 0x50, 0x4e, @@ -390,7 +390,7 @@ describe("attachment max edge", () => { 0x52, ...u32(width), ...u32(height), - 8, + bitDepth, 6, 0, 0, @@ -461,6 +461,17 @@ describe("attachment max edge", () => { expect(result.ok && result.imageSize).toEqual({ width: 1500, height: 2000 }); }); + it("keeps a 16-bit PNG whose 8-bit redraw comes out larger", async () => { + const { decode } = stubDecoder({ width: 1042, height: 1389 }); + const original = image(pngHeader(1042, 1389, 16), "image.png", "image/png"); + + const result = await prepareImageForAttachment(original, 10 * 1024 * 1024); + + expect(decode).toHaveBeenCalledOnce(); + expect(result.ok && result.file).toBe(original); + expect(result.ok && result.recompressed).toBe(false); + }); + it.each([ { label: "a 2000px PNG", header: pngHeader(2000, 1200), type: "image/png" }, { diff --git a/apps/web/src/lib/imageCompression.ts b/apps/web/src/lib/imageCompression.ts index 7e5485cabcee9..561252c6301b1 100644 --- a/apps/web/src/lib/imageCompression.ts +++ b/apps/web/src/lib/imageCompression.ts @@ -581,24 +581,37 @@ export async function compressImageForStash( } } -async function fitImageToMaxEdge(file: File): Promise<{ file: File; imageSize: ImageSize } | null> { +/** macOS pastes a photo copied from an iPhone as a 16-bit PNG, several times its 8-bit size. */ +function isSixteenBitPng(header: Uint8Array): boolean { + return ( + header.length > 24 && + new DataView(header.buffer, header.byteOffset).getUint32(0) === 0x89504e47 && + header[24] === 16 + ); +} + +async function fitImageForAttachment( + 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()), + const headerBytes = new Uint8Array( + await file.slice(0, IMAGE_DIMENSIONS_HEADER_BYTES).arrayBuffer(), ); - if (!header || !fitImageAttachmentSize(header)) return null; + const header = readImageDimensions(headerBytes); + if (!header || (!fitImageAttachmentSize(header) && !isSixteenBitPng(headerBytes))) return null; bitmap = await createImageBitmap(file, { imageOrientation: "from-image" }); - const imageSize = fitImageAttachmentSize(bitmap); - if (!imageSize) return null; + const scaledSize = fitImageAttachmentSize(bitmap); + const imageSize = scaledSize ?? { width: bitmap.width, height: bitmap.height }; const target = createCanvas(imageSize.width, imageSize.height, await readImageColorSpace(file)); 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; + // The canvas writes RGBA, so a 16-bit grayscale PNG can come out larger at 8 bits. + if (!blob || (!scaledSize && blob.size >= file.size)) return null; return { file: new File([blob], file.name, { type: mimeType, lastModified: file.lastModified }), imageSize, @@ -615,7 +628,7 @@ export async function compressImageToByteLimit( maxBytes: number, options?: { preferredMimeType?: "image/jpeg"; sourceSizeBytes?: number }, ): Promise { - const fitted = await fitImageToMaxEdge(file); + const fitted = await fitImageForAttachment(file); if (fitted && fitted.file.size <= maxBytes) { return { ok: true, file: fitted.file, recompressed: true, imageSize: fitted.imageSize }; } @@ -704,10 +717,8 @@ async function convertHeicImage( 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 scaledSize = fitImageAttachmentSize(bitmap); + const imageSize = scaledSize ?? { width: bitmap.width, height: bitmap.height }; // heic-to ignores the color profile and hands back the raw Display P3 values as sRGB. if (!decodedNatively && metadata.displayP3) { const source = bitmap; From c8a7527fd590a1c411526b8b70fa3046e954aedd Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 17:57:54 +0000 Subject: [PATCH 2/2] refactor(web): leave HEIC attachment sizing as it was Co-Authored-By: Claude Opus 5.5 --- apps/web/src/lib/imageCompression.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/web/src/lib/imageCompression.ts b/apps/web/src/lib/imageCompression.ts index 561252c6301b1..888044cd18daf 100644 --- a/apps/web/src/lib/imageCompression.ts +++ b/apps/web/src/lib/imageCompression.ts @@ -717,8 +717,10 @@ async function convertHeicImage( const { heicTo } = await import("heic-to/csp"); bitmap = await heicTo({ blob: file, type: "bitmap" }); } - const scaledSize = fitImageAttachmentSize(bitmap); - const imageSize = scaledSize ?? { width: bitmap.width, height: bitmap.height }; + const imageSize = fitImageAttachmentSize(bitmap) ?? { + width: bitmap.width, + height: bitmap.height, + }; // heic-to ignores the color profile and hands back the raw Display P3 values as sRGB. if (!decodedNatively && metadata.displayP3) { const source = bitmap;