gb-image-decoder
Advanced tools
+39
-22
@@ -185,28 +185,33 @@ 'use strict'; | ||
| const toObjectUrl = async (canvas) => new Promise((resolve, reject) => { | ||
| canvas.toBlob((blob) => { | ||
| if (!blob) { | ||
| reject(new Error("Could not generate Blob from canvas")); | ||
| return; | ||
| } | ||
| try { | ||
| resolve(URL.createObjectURL(blob)); | ||
| } catch (error) { | ||
| reject(error); | ||
| } | ||
| }); | ||
| }); | ||
| const dataUrlFromRawOutput = async ({ | ||
| const blobFromRawOutput = async ({ | ||
| data, | ||
| dimensions: { width, height } | ||
| }, scaleFactor, hash, canvasCreator) => { | ||
| }, scaleFactor, canvasCreator, type = "image/png") => { | ||
| const canvas = canvasCreator(); | ||
| canvas.width = width * scaleFactor; | ||
| canvas.height = height * scaleFactor; | ||
| const context = canvas.getContext("2d"); | ||
| const imageData = new ImageData(data, canvas.width, canvas.height); | ||
| context?.putImageData(imageData, 0, 0); | ||
| return new Promise((resolve, reject) => { | ||
| canvas.toBlob((blob) => { | ||
| if (!blob) { | ||
| reject(new Error("Could not generate Blob from canvas")); | ||
| return; | ||
| } | ||
| try { | ||
| resolve(blob); | ||
| } catch (error) { | ||
| reject(error); | ||
| } | ||
| }, type, 1); | ||
| }); | ||
| }; | ||
| const dataUrlFromRawOutput = async (rawOutput, scaleFactor, hash, canvasCreator) => { | ||
| const urlCache = new UrlCache(); | ||
| urlCache.setUrl(hash, new Promise((resolve) => { | ||
| const canvas = canvasCreator(); | ||
| canvas.width = width * scaleFactor; | ||
| canvas.height = height * scaleFactor; | ||
| const context = canvas.getContext("2d"); | ||
| const imageData = new ImageData(data, canvas.width, canvas.height); | ||
| context?.putImageData(imageData, 0, 0); | ||
| resolve(toObjectUrl(canvas)); | ||
| blobFromRawOutput(rawOutput, scaleFactor, canvasCreator).then((blob) => { | ||
| resolve(URL.createObjectURL(blob)); | ||
| }); | ||
| })); | ||
@@ -483,2 +488,7 @@ const url = await urlCache.getUrl(hash); | ||
| }; | ||
| const getMonochromeImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => { | ||
| const fullParams = getFullParams$1(params); | ||
| const rawOutput = getRawMonochromeImageData(fullParams); | ||
| return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType); | ||
| }; | ||
@@ -604,2 +614,7 @@ const getFullParams = (params) => ({ | ||
| }; | ||
| const getRGBNImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => { | ||
| const fullParams = getFullParams(params); | ||
| const rawOutput = getRawRGBNImageData(fullParams, canvasCreator); | ||
| return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType); | ||
| }; | ||
@@ -631,3 +646,5 @@ const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0)); | ||
| exports.getDimensions = getDimensions$1; | ||
| exports.getMonochromeImageBlob = getMonochromeImageBlob; | ||
| exports.getMonochromeImageUrl = getMonochromeImageUrl; | ||
| exports.getRGBNImageBlob = getRGBNImageBlob; | ||
| exports.getRGBNImageUrl = getRGBNImageUrl; | ||
@@ -634,0 +651,0 @@ exports.getRGBValue = getRGBValue; |
+3
-1
@@ -100,5 +100,7 @@ declare enum BlendMode { | ||
| declare const getMonochromeImageUrl: (params: MonochromeImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>; | ||
| declare const getMonochromeImageBlob: (params: MonochromeImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>; | ||
| declare const getRawRGBNImageData: (params: FullRGBNImageCreationParams, canvasCreator: CanvasCreator) => RawOutput; | ||
| declare const getRGBNImageUrl: (params: RGBNImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>; | ||
| declare const getRGBNImageBlob: (params: RGBNImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>; | ||
@@ -140,2 +142,2 @@ declare class UrlCache { | ||
| export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; | ||
| export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; |
+3
-1
@@ -100,5 +100,7 @@ declare enum BlendMode { | ||
| declare const getMonochromeImageUrl: (params: MonochromeImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>; | ||
| declare const getMonochromeImageBlob: (params: MonochromeImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>; | ||
| declare const getRawRGBNImageData: (params: FullRGBNImageCreationParams, canvasCreator: CanvasCreator) => RawOutput; | ||
| declare const getRGBNImageUrl: (params: RGBNImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>; | ||
| declare const getRGBNImageBlob: (params: RGBNImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>; | ||
@@ -140,2 +142,2 @@ declare class UrlCache { | ||
| export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; | ||
| export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; |
+3
-1
@@ -100,5 +100,7 @@ declare enum BlendMode { | ||
| declare const getMonochromeImageUrl: (params: MonochromeImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>; | ||
| declare const getMonochromeImageBlob: (params: MonochromeImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>; | ||
| declare const getRawRGBNImageData: (params: FullRGBNImageCreationParams, canvasCreator: CanvasCreator) => RawOutput; | ||
| declare const getRGBNImageUrl: (params: RGBNImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>; | ||
| declare const getRGBNImageBlob: (params: RGBNImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>; | ||
@@ -140,2 +142,2 @@ declare class UrlCache { | ||
| export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; | ||
| export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; |
+38
-23
@@ -183,28 +183,33 @@ import { hash } from 'ohash'; | ||
| const toObjectUrl = async (canvas) => new Promise((resolve, reject) => { | ||
| canvas.toBlob((blob) => { | ||
| if (!blob) { | ||
| reject(new Error("Could not generate Blob from canvas")); | ||
| return; | ||
| } | ||
| try { | ||
| resolve(URL.createObjectURL(blob)); | ||
| } catch (error) { | ||
| reject(error); | ||
| } | ||
| }); | ||
| }); | ||
| const dataUrlFromRawOutput = async ({ | ||
| const blobFromRawOutput = async ({ | ||
| data, | ||
| dimensions: { width, height } | ||
| }, scaleFactor, hash, canvasCreator) => { | ||
| }, scaleFactor, canvasCreator, type = "image/png") => { | ||
| const canvas = canvasCreator(); | ||
| canvas.width = width * scaleFactor; | ||
| canvas.height = height * scaleFactor; | ||
| const context = canvas.getContext("2d"); | ||
| const imageData = new ImageData(data, canvas.width, canvas.height); | ||
| context?.putImageData(imageData, 0, 0); | ||
| return new Promise((resolve, reject) => { | ||
| canvas.toBlob((blob) => { | ||
| if (!blob) { | ||
| reject(new Error("Could not generate Blob from canvas")); | ||
| return; | ||
| } | ||
| try { | ||
| resolve(blob); | ||
| } catch (error) { | ||
| reject(error); | ||
| } | ||
| }, type, 1); | ||
| }); | ||
| }; | ||
| const dataUrlFromRawOutput = async (rawOutput, scaleFactor, hash, canvasCreator) => { | ||
| const urlCache = new UrlCache(); | ||
| urlCache.setUrl(hash, new Promise((resolve) => { | ||
| const canvas = canvasCreator(); | ||
| canvas.width = width * scaleFactor; | ||
| canvas.height = height * scaleFactor; | ||
| const context = canvas.getContext("2d"); | ||
| const imageData = new ImageData(data, canvas.width, canvas.height); | ||
| context?.putImageData(imageData, 0, 0); | ||
| resolve(toObjectUrl(canvas)); | ||
| blobFromRawOutput(rawOutput, scaleFactor, canvasCreator).then((blob) => { | ||
| resolve(URL.createObjectURL(blob)); | ||
| }); | ||
| })); | ||
@@ -481,2 +486,7 @@ const url = await urlCache.getUrl(hash); | ||
| }; | ||
| const getMonochromeImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => { | ||
| const fullParams = getFullParams$1(params); | ||
| const rawOutput = getRawMonochromeImageData(fullParams); | ||
| return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType); | ||
| }; | ||
@@ -602,5 +612,10 @@ const getFullParams = (params) => ({ | ||
| }; | ||
| const getRGBNImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => { | ||
| const fullParams = getFullParams(params); | ||
| const rawOutput = getRawRGBNImageData(fullParams, canvasCreator); | ||
| return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType); | ||
| }; | ||
| const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0)); | ||
| export { BLACK, BLACK_LINE, BW_PALETTE, BW_PALETTE_HEX, BlendMode, ChannelKey, ExportFrameMode, FRAME_WIDTH, RGBN_SHADES, Rotation, SKIP_LINE, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions$1 as getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; | ||
| export { BLACK, BLACK_LINE, BW_PALETTE, BW_PALETTE_HEX, BlendMode, ChannelKey, ExportFrameMode, FRAME_WIDTH, RGBN_SHADES, Rotation, SKIP_LINE, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions$1 as getDimensions, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame }; |
+1
-1
| { | ||
| "name": "gb-image-decoder", | ||
| "version": "2.0.2", | ||
| "version": "2.0.3", | ||
| "description": "Decoder classes for GameBoy-encoded images", | ||
@@ -5,0 +5,0 @@ "repository": "", |
67265
4.04%1370
2.39%