Skip to content

Commit bf151d2

Browse files
committed
Add Base64 PNG loading to BitmapGRF
1 parent f2fd1f7 commit bf151d2

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/Util/BitmapGRF.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,27 @@ export class BitmapGRF {
366366
return this.fromCanvasImageData(data, imageConversionOptions);
367367
}
368368

369+
/** Convert a Base64 encoded PNG to a BitmapGRF. */
370+
public static async fromBase64PNG(
371+
png: string,
372+
imageConversionOptions?: ImageConversionOptions,
373+
): Promise<BitmapGRF> {
374+
// Use an Image object as a parser for the PNG data
375+
const img = new Image();
376+
img.src = png;
377+
await img.decode();
378+
379+
// And render it to an offscreen canvas to get its image data.
380+
const ctx = new OffscreenCanvas(img.width, img.height)
381+
.getContext('2d') as OffscreenCanvasRenderingContext2D;
382+
ctx.imageSmoothingEnabled = false;
383+
ctx.drawImage(img, 0, 0);
384+
385+
const data = ctx.getImageData(0, 0, img.width, img.height);
386+
387+
return this.fromCanvasImageData(data, imageConversionOptions);
388+
}
389+
369390
/** Converts monochrome data to GRF format. */
370391
private static monochromeToGRF(monochromeData: Uint8Array, width: number, height: number) {
371392
// Method (c) 2022 metafloor

0 commit comments

Comments
 (0)