Created
June 30, 2024 17:19
-
-
Save mattdesl/372b71865853e72854ad8e3d6bfd8882 to your computer and use it in GitHub Desktop.
Draw Uint8Array Display-P3 RGBA data to canvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ChunkType, | |
ColorType, | |
colorTypeToChannels, | |
encode, | |
encode_iCCP, | |
} from "png-tools"; | |
import { deflate } from "pako"; | |
... | |
const outProfileBuffer = await (await fetch(DISPLAY_P3_ICC_URL)).arrayBuffer(); | |
const outProfileCompressed = deflate(outProfileBuffer); | |
const iCCP = { | |
type: ChunkType.iCCP, | |
data: encode_iCCP({ name: "Display P3", data: outProfileCompressed }), | |
}; | |
const png = encode( | |
{ | |
data: rgba, | |
depth: 8, | |
colorType: ColorType.RGBA, | |
width, | |
height, | |
ancillary: [iCCP], | |
}, | |
deflate, | |
{ level: 3 } | |
); | |
const bitmap = await createImageBitmap( | |
new Blob([png], { type: "image/png" }) | |
); | |
// draw to canvas | |
context.drawImage(bitmap, 0, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment