From d268b34e76b4f8589de84f2ad847c74189ab026e Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroich Date: Mon, 15 Nov 2021 16:41:01 -0600 Subject: [PATCH] Get masking working with tile map output again --- tools/png2iigs.js | 86 +++++++++++++++++++++++++++------------------ tools/tiled2iigs.js | 23 ++++++++++-- 2 files changed, 72 insertions(+), 37 deletions(-) diff --git a/tools/png2iigs.js b/tools/png2iigs.js index 43dc6a6..f4821dd 100644 --- a/tools/png2iigs.js +++ b/tools/png2iigs.js @@ -133,6 +133,50 @@ function getOptions(argv) { return options; } +function getPaletteMap(options, png) { + // Get the RGB triplets from the palette + const sourcePalette = png.palette; + const targetPalette = options.targetPalette || sourcePalette; + const paletteCSSTripplets = sourcePalette.map(c => paletteToHexString(c)); + + // Start with an identity map + const paletteMap = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + + // If there is a transparent color / color index, make sure it gets mapped to index 0 + if (options.transparentIndex > 0) { + paletteMap[options.transparentIndex] = 0; + } + if (options.transparentColor !== null) { + const index = paletteCSSTripplets.findIndex(p => p === options.transparentColor); + if (index !== -1) { + options.transparentIndex = index; + paletteMap[index] = 0; + } else { + console.warn(`; transparent color defined, ${options.transparentColor}, but not found in image`); + } + } + + // Match up the source palette with the target palette + const targetTriplets = targetPalette.map(c => paletteToHexString(c)); + paletteCSSTripplets.forEach((color, i) => { + if (i !== options.transparentIndex) { + const j = targetTriplets.findIndex(p => p === color); + if (j !== -1) { + console.warn(`Assigned color index ${i} (${color}) to the target palette index ${j}`); + paletteMap[i] = j; + } else { + console.warn(`Could not map color index ${i} (${color}) to the target palette`); + } + } + }); + + return { + paletteMap, + sourcePalette, + targetPalette + }; +} + async function main(argv) { // try { const png = await readPNG(argv[0]); @@ -156,41 +200,7 @@ async function main(argv) { } // Get the RGB triplets from the palette - const sourcePalette = png.palette; - const targetPalette = options.targetPalette || sourcePalette; - const paletteCSSTripplets = sourcePalette.map(c => paletteToHexString(c)); - - // Start with an identity map - const paletteMap = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; - - // If there is a transparent color / color index, make sure it gets mapped to index 0 - if (options.transparentIndex > 0) { - paletteMap[options.transparentIndex] = 0; - } - if (options.transparentColor !== null) { - const index = paletteCSSTripplets.findIndex(p => p === options.transparentColor); - if (index !== -1) { - options.transparentIndex = index; - paletteMap[index] = 0; - } else { - console.warn(`; transparent color defined, ${options.transparentColor}, but not found in image`); - } - } - - // Match up the source palette with the target palette - const targetTriplets = targetPalette.map(c => paletteToHexString(c)); - paletteCSSTripplets.forEach((color, i) => { - if (i !== options.transparentIndex) { - const j = targetTriplets.findIndex(p => p === color); - if (j !== -1) { - console.warn(`Assigned color index ${i} (${color}) to the target palette index ${j}`); - paletteMap[i] = j; - } else { - console.warn(`Could not map color index ${i} (${color}) to the target palette`); - } - } - }); - + const { targetPalette, paletteMap } = getPaletteMap(options, png); options.paletteMap = paletteMap; // Dump the palette in IIgs hex format @@ -298,6 +308,11 @@ function buildTile(options, buff, _mask, width, x, y) { if (mask.some(h => h != 0)) { tile.isSolid = false; } + + if (x === 120 && y === 8) { + console.warn(`isSolid: ${tile.isSolid}` ); + console.warn(data.map(d => d.toString(16)), mask); + } } for (dy = 0; dy < 8; dy += 1) { @@ -475,6 +490,7 @@ module.exports = { buildMerlinCodeForTiles, buildMerlinCodeForTile, findColorIndex, + getPaletteMap, paletteToIIgs, pngToIIgsBuff, readPNG, diff --git a/tools/tiled2iigs.js b/tools/tiled2iigs.js index d979099..a6e6133 100644 --- a/tools/tiled2iigs.js +++ b/tools/tiled2iigs.js @@ -28,7 +28,8 @@ function hexToRbg(hex) { async function readTileSet(workdir, tileset) { // Load up the PNG image - const pngfile = path.resolve(path.join(workdir, tileset.image.source)); + const imageSource = GLOBALS.options.tilesetImage || tileset.image.source; + const pngfile = path.resolve(path.join(workdir, imageSource)); console.log(`Reading PNG file from ${pngfile}`); const png = await png2iigs.readPNG(pngfile); @@ -38,7 +39,7 @@ async function readTileSet(workdir, tileset) { if (tileset.image.trans) { const color = hexToRbg(tileset.image.trans); console.log(`Found color ${color} as transparent marker`); - transparentIndex = png2iigs.findColorIndex(GLOBALS.options, png, color); + [transparentIndex] = png2iigs.findColorIndex(GLOBALS.options, png, color); if (typeof transparentIndex !== 'number') { console.log('Could not find color in palette'); console.log(png.palette); @@ -47,10 +48,15 @@ async function readTileSet(workdir, tileset) { console.log(`Transparent color palette index is ${transparentIndex}`); } } + GLOBALS.options.transparentIndex = transparentIndex console.log(`Converting PNG to IIgs bitmap format...`); const [buff, mask] = png2iigs.pngToIIgsBuff(GLOBALS.options, png); + console.log(`Mapping source and target palettes`); + const { paletteMap } = png2iigs.getPaletteMap(GLOBALS.options, png); + GLOBALS.options.paletteMap = paletteMap; + console.log(`Building tiles...`); const tiles = png2iigs.buildTiles(GLOBALS.options, buff, mask, png.width / 2, transparentIndex); @@ -225,6 +231,7 @@ async function main(argv) { const forceMasked = getArg(argv, '--force-masked', x => true, false); const noGenTiles = getArg(argv, '--no-gen-tiles', x => true, false); const emptyTile = getArg(argv, '--empty-tile', x => parseInt(x, 10), -1); + const tileSet = getArg(argv, '--tile-set', x => x, null); console.log(`Reading Tiled JSON file from ${fullpath}`); const raw = fs.readFileSync(fullpath); @@ -263,6 +270,10 @@ async function main(argv) { // Load up any/all tilesets const tileSets = await Promise.all(doc.tilesets.map(tileset => loadTileset(workdir, tileset))); + if (tileSets.length === 1 && tileSet !== null) { + tileSets[0].tileset.image.source = tileSet; + } + // Create a global reference object GLOBALS = { ...GLOBALS, @@ -449,6 +460,14 @@ function convertTileID(tileId, tileset) { } const mask_bit = (!tileset[tileIndex - 1].isSolid || tileIndex === GLOBALS.emptyTile) && ((GLOBALS.tileLayers.length !== 1) || GLOBALS.forceMasked); + if (tileIndex === 48) { + console.warn('isSolid: ', tileset[tileIndex - 1].isSolid); + console.warn('GLOBALS.emptyTile: ', GLOBALS.emptyTile); + console.warn('GLOBALS.tileLayers.length: ', GLOBALS.tileLayers.length); + console.warn('GLOBALS.forceMasked: ', GLOBALS.forceMasked); + console.warn('mask_bit: ', mask_bit); + } + // Build up a partial set of control bits let control_bits = (mask_bit ? GTE_MASK_BIT : 0) + (hflip ? GTE_HFLIP_BIT : 0) + (vflip ? GTE_VFLIP_BIT : 0);