Skip to content

Commit 3158dfc

Browse files
committed
Unpremultiply raster output before putImageData
hb-raster outputs premultiplied BGRA, but ImageData expects non-premultiplied RGBA. Without unpremultiply, semi-transparent pixels appear too dark (grayish gradients instead of clean color fading to transparent). Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2603e21 commit 3158dfc

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

js/app.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -817,10 +817,17 @@ hb_blob_destroy (blob);`
817817
Module._web_free_string (dataPtr);
818818
Module._free (wPtr); Module._free (hPtr);
819819

820-
/* BGRA -> RGBA in place. */
820+
/* BGRA premultiplied -> RGBA non-premultiplied in place.
821+
* ImageData expects non-premultiplied RGBA. */
821822
for (let i = 0; i < bgra.length; i += 4) {
822-
const b = bgra[i], r = bgra[i + 2];
823-
bgra[i] = r; bgra[i + 2] = b;
823+
const b = bgra[i], g = bgra[i + 1], r = bgra[i + 2], a = bgra[i + 3];
824+
if (a > 0 && a < 255) {
825+
bgra[i] = Math.min (255, (r * 255 / a + 0.5) | 0);
826+
bgra[i + 1] = Math.min (255, (g * 255 / a + 0.5) | 0);
827+
bgra[i + 2] = Math.min (255, (b * 255 / a + 0.5) | 0);
828+
} else {
829+
bgra[i] = r; bgra[i + 2] = b;
830+
}
824831
}
825832
rasterCanvas.width = w;
826833
rasterCanvas.height = h;

0 commit comments

Comments
 (0)