gb-image-decoder
Advanced tools
+40
-54
@@ -61,48 +61,11 @@ 'use strict'; | ||
| const TILES_PER_LINE = 20; | ||
| const TILES_PER_COLUMN = 18; | ||
| const FRAME_SIZE = 2; | ||
| const FRAME_TILES = FRAME_SIZE * 2; | ||
| const DEFAULT_FULL_PIXEL_HEIGHT = TILES_PER_COLUMN * TILE_PIXEL_HEIGHT; | ||
| const DEFAULT_FULL_PIXEL_WIDTH = TILES_PER_LINE * TILE_PIXEL_WIDTH; | ||
| const BW_PALETTE = [16777215, 11184810, 5592405, 0]; | ||
| const BW_PALETTE_HEX = ["#ffffff", "#aaaaaa", "#555555", "#000000"]; | ||
| const BLACK_LINE = [ | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK | ||
| ]; | ||
| const WHITE_LINE = [ | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE | ||
| ]; | ||
| const BLACK_LINE = Array(TILES_PER_LINE).fill(BLACK); | ||
| const WHITE_LINE = Array(TILES_PER_LINE).fill(WHITE); | ||
| const defaultPalette = { | ||
@@ -171,2 +134,14 @@ r: [0, 84, 172, 255], | ||
| const calculateImageStartLine = (handleExportFrame, imageStartLine) => { | ||
| switch (handleExportFrame) { | ||
| case ExportFrameMode.FRAMEMODE_CROP: | ||
| return 0; | ||
| case ExportFrameMode.FRAMEMODE_SQUARE_WHITE: | ||
| case ExportFrameMode.FRAMEMODE_SQUARE_BLACK: | ||
| return 2; | ||
| case ExportFrameMode.FRAMEMODE_KEEP: | ||
| default: | ||
| return imageStartLine; | ||
| } | ||
| }; | ||
| const getRGBValue = ({ | ||
@@ -181,3 +156,4 @@ pixels, | ||
| }) => { | ||
| const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData; | ||
| const calculatedImageStartLine = calculateImageStartLine(handleExportFrame, imageStartLine); | ||
| const palette = tileIndexIsPartOfFrame(tileIndex, calculatedImageStartLine, handleExportFrame) ? frameColorData : colorData; | ||
| const value = palette[pixels[index]]; | ||
@@ -291,3 +267,3 @@ return { | ||
| let handleFrameMode = handleExportFrame; | ||
| if ((this.tiles.length !== 360 || this.tilesPerLine !== TILES_PER_LINE) && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) { | ||
| if (this.tilesPerLine !== TILES_PER_LINE && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) { | ||
| handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP; | ||
@@ -313,3 +289,2 @@ } | ||
| getScaledCanvasSize(handleExportFrame) { | ||
| const FRAME_TILES = 4; | ||
| const width = this.getWidth(); | ||
@@ -326,5 +301,5 @@ const height = this.getHeight(); | ||
| return { | ||
| initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES, | ||
| initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES, | ||
| tilesPerLine: this.tilesPerLine - FRAME_TILES | ||
| initialHeight: DEFAULT_FULL_PIXEL_HEIGHT - TILE_PIXEL_HEIGHT * FRAME_TILES, | ||
| initialWidth: DEFAULT_FULL_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES, | ||
| tilesPerLine: TILES_PER_LINE - FRAME_TILES | ||
| }; | ||
@@ -334,3 +309,3 @@ case ExportFrameMode.FRAMEMODE_SQUARE_BLACK: | ||
| return { | ||
| initialHeight: height + 2 * TILE_PIXEL_HEIGHT, | ||
| initialHeight: width, | ||
| initialWidth: width, | ||
@@ -351,7 +326,7 @@ tilesPerLine: this.tilesPerLine | ||
| case ExportFrameMode.FRAMEMODE_CROP: | ||
| return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []); | ||
| return this.getCroppedTiles(); | ||
| case ExportFrameMode.FRAMEMODE_SQUARE_BLACK: | ||
| return [ | ||
| ...BLACK_LINE, | ||
| ...this.tiles, | ||
| ...this.getDefaultImageRange(), | ||
| ...BLACK_LINE | ||
@@ -362,3 +337,3 @@ ]; | ||
| ...WHITE_LINE, | ||
| ...this.tiles, | ||
| ...this.getDefaultImageRange(), | ||
| ...WHITE_LINE | ||
@@ -370,2 +345,13 @@ ]; | ||
| } | ||
| getCroppedTiles() { | ||
| return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []); | ||
| } | ||
| // for wild frame image, this returns the part of theimage | ||
| // which has the image data in the default position by cropping | ||
| // away part of the wild frame which does not fit into 160x144 | ||
| getDefaultImageRange() { | ||
| const wholeImageStartLine = this.imageStartLine - FRAME_SIZE; | ||
| const startIndex = wholeImageStartLine * TILES_PER_LINE; | ||
| return this.tiles.slice(startIndex, startIndex + TILES_PER_LINE * TILES_PER_COLUMN); | ||
| } | ||
| setCanvas(canvas) { | ||
@@ -372,0 +358,0 @@ if (this.canvas === canvas) { |
+2
-0
@@ -105,2 +105,4 @@ declare enum BlendMode { | ||
| private getExportTiles; | ||
| private getCroppedTiles; | ||
| private getDefaultImageRange; | ||
| private setCanvas; | ||
@@ -107,0 +109,0 @@ private setPalettes; |
+2
-0
@@ -105,2 +105,4 @@ declare enum BlendMode { | ||
| private getExportTiles; | ||
| private getCroppedTiles; | ||
| private getDefaultImageRange; | ||
| private setCanvas; | ||
@@ -107,0 +109,0 @@ private setPalettes; |
+2
-0
@@ -105,2 +105,4 @@ declare enum BlendMode { | ||
| private getExportTiles; | ||
| private getCroppedTiles; | ||
| private getDefaultImageRange; | ||
| private setCanvas; | ||
@@ -107,0 +109,0 @@ private setPalettes; |
+40
-54
@@ -59,48 +59,11 @@ var BlendMode = /* @__PURE__ */ ((BlendMode2) => { | ||
| const TILES_PER_LINE = 20; | ||
| const TILES_PER_COLUMN = 18; | ||
| const FRAME_SIZE = 2; | ||
| const FRAME_TILES = FRAME_SIZE * 2; | ||
| const DEFAULT_FULL_PIXEL_HEIGHT = TILES_PER_COLUMN * TILE_PIXEL_HEIGHT; | ||
| const DEFAULT_FULL_PIXEL_WIDTH = TILES_PER_LINE * TILE_PIXEL_WIDTH; | ||
| const BW_PALETTE = [16777215, 11184810, 5592405, 0]; | ||
| const BW_PALETTE_HEX = ["#ffffff", "#aaaaaa", "#555555", "#000000"]; | ||
| const BLACK_LINE = [ | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK, | ||
| BLACK | ||
| ]; | ||
| const WHITE_LINE = [ | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE, | ||
| WHITE | ||
| ]; | ||
| const BLACK_LINE = Array(TILES_PER_LINE).fill(BLACK); | ||
| const WHITE_LINE = Array(TILES_PER_LINE).fill(WHITE); | ||
| const defaultPalette = { | ||
@@ -169,2 +132,14 @@ r: [0, 84, 172, 255], | ||
| const calculateImageStartLine = (handleExportFrame, imageStartLine) => { | ||
| switch (handleExportFrame) { | ||
| case ExportFrameMode.FRAMEMODE_CROP: | ||
| return 0; | ||
| case ExportFrameMode.FRAMEMODE_SQUARE_WHITE: | ||
| case ExportFrameMode.FRAMEMODE_SQUARE_BLACK: | ||
| return 2; | ||
| case ExportFrameMode.FRAMEMODE_KEEP: | ||
| default: | ||
| return imageStartLine; | ||
| } | ||
| }; | ||
| const getRGBValue = ({ | ||
@@ -179,3 +154,4 @@ pixels, | ||
| }) => { | ||
| const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData; | ||
| const calculatedImageStartLine = calculateImageStartLine(handleExportFrame, imageStartLine); | ||
| const palette = tileIndexIsPartOfFrame(tileIndex, calculatedImageStartLine, handleExportFrame) ? frameColorData : colorData; | ||
| const value = palette[pixels[index]]; | ||
@@ -289,3 +265,3 @@ return { | ||
| let handleFrameMode = handleExportFrame; | ||
| if ((this.tiles.length !== 360 || this.tilesPerLine !== TILES_PER_LINE) && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) { | ||
| if (this.tilesPerLine !== TILES_PER_LINE && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) { | ||
| handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP; | ||
@@ -311,3 +287,2 @@ } | ||
| getScaledCanvasSize(handleExportFrame) { | ||
| const FRAME_TILES = 4; | ||
| const width = this.getWidth(); | ||
@@ -324,5 +299,5 @@ const height = this.getHeight(); | ||
| return { | ||
| initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES, | ||
| initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES, | ||
| tilesPerLine: this.tilesPerLine - FRAME_TILES | ||
| initialHeight: DEFAULT_FULL_PIXEL_HEIGHT - TILE_PIXEL_HEIGHT * FRAME_TILES, | ||
| initialWidth: DEFAULT_FULL_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES, | ||
| tilesPerLine: TILES_PER_LINE - FRAME_TILES | ||
| }; | ||
@@ -332,3 +307,3 @@ case ExportFrameMode.FRAMEMODE_SQUARE_BLACK: | ||
| return { | ||
| initialHeight: height + 2 * TILE_PIXEL_HEIGHT, | ||
| initialHeight: width, | ||
| initialWidth: width, | ||
@@ -349,7 +324,7 @@ tilesPerLine: this.tilesPerLine | ||
| case ExportFrameMode.FRAMEMODE_CROP: | ||
| return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []); | ||
| return this.getCroppedTiles(); | ||
| case ExportFrameMode.FRAMEMODE_SQUARE_BLACK: | ||
| return [ | ||
| ...BLACK_LINE, | ||
| ...this.tiles, | ||
| ...this.getDefaultImageRange(), | ||
| ...BLACK_LINE | ||
@@ -360,3 +335,3 @@ ]; | ||
| ...WHITE_LINE, | ||
| ...this.tiles, | ||
| ...this.getDefaultImageRange(), | ||
| ...WHITE_LINE | ||
@@ -368,2 +343,13 @@ ]; | ||
| } | ||
| getCroppedTiles() { | ||
| return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []); | ||
| } | ||
| // for wild frame image, this returns the part of theimage | ||
| // which has the image data in the default position by cropping | ||
| // away part of the wild frame which does not fit into 160x144 | ||
| getDefaultImageRange() { | ||
| const wholeImageStartLine = this.imageStartLine - FRAME_SIZE; | ||
| const startIndex = wholeImageStartLine * TILES_PER_LINE; | ||
| return this.tiles.slice(startIndex, startIndex + TILES_PER_LINE * TILES_PER_COLUMN); | ||
| } | ||
| setCanvas(canvas) { | ||
@@ -370,0 +356,0 @@ if (this.canvas === canvas) { |
+4
-3
| { | ||
| "name": "gb-image-decoder", | ||
| "version": "1.3.0", | ||
| "version": "1.3.1", | ||
| "description": "Decoder classes for GameBoy-encoded images", | ||
@@ -25,4 +25,5 @@ "repository": "", | ||
| "dev": "vitest dev", | ||
| "test": "rimraf images && vitest run", | ||
| "test:u": "rimraf images && vitest run -u", | ||
| "test:clean": "rimraf images/*", | ||
| "test": "npm run test:clean && vitest run", | ||
| "test:u": "npm run test:clean && vitest run -u", | ||
| "eslint": "eslint -c .eslintrc --ext .js,.ts ." | ||
@@ -29,0 +30,0 @@ }, |
66939
3.09%1448
-1.76%