File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments