Skip to content

Commit

Permalink
confirmTilesAre16ColorsOrLess
Browse files Browse the repository at this point in the history
  • Loading branch information
city41 committed Mar 15, 2024
1 parent d0130fc commit b90e0dd
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 25 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
"handlebars": "^4.7.8",
"io-ts": "^2.2.20",
"io-ts-human-reporter": "^0.1.2",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"mkdirp": "^3.0.1"
},
"devDependencies": {
"@types/jest": "^29.5.5",
"@types/lodash": "^4.14.199",
"@types/mkdirp": "^2.0.0",
"@types/node": "^20.8.2",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
Expand Down
42 changes: 42 additions & 0 deletions src/orchestrators/common/confirmTilesAre16ColorsOrLess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
import * as mkdirp from 'mkdirp';
import { get24BitPalette } from '../../api/palette/get24BitPalette';
import { BaseTile } from '../../types';

/**
* Verifies all tiles are 16 colors or less. This takes into account index 0/transparency,
* as get24BitPalette will ensure the palette has transparency at zero
* @param tiles
*/
function confirmTilesAre16ColorsOrLess<TTile extends BaseTile>(tiles: TTile[]) {
const badTiles = tiles.filter((t) => {
return get24BitPalette(t.canvasSource).length > 16;
});

if (badTiles.length > 0) {
const tmpDir = path.resolve(
os.tmpdir(),
`sromcromBadTiles-${Date.now().toString()}`
);

mkdirp.sync(tmpDir);

for (let i = 0; i < badTiles.length; ++i) {
const tile = badTiles[i];
const paletteLength = get24BitPalette(tile.canvasSource).length;
const buffer = tile.canvasSource.toBuffer();

const tilePath = path.resolve(tmpDir, `tile-${i}-${paletteLength}.png`);

fs.writeFileSync(tilePath, buffer);
}

throw new Error(
`Some tiles have more than 16 colors, bad tiles written to: ${tmpDir}`
);
}
}

export { confirmTilesAre16ColorsOrLess };
3 changes: 3 additions & 0 deletions src/orchestrators/cromOrchestrator/cromOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { tilesets } from '../../generators/tilesets';
import { cromImages } from '../../generators/cromImages';
import { cromAnimations } from '../../generators/cromAnimations';
import { cromFFBlankGenerator } from './cromFFBlankGenerator';
import { confirmTilesAre16ColorsOrLess } from '../common/confirmTilesAre16ColorsOrLess';

// create crom tile generators based on what is in the json file
const generators: Record<string, ICROMGenerator> = {
Expand Down Expand Up @@ -66,6 +67,8 @@ function orchestrate(
throw new Error('the same tile ref was added more than once');
}

confirmTilesAre16ColorsOrLess(allTiles);

const finalPalettes = determinePalettesToEmit(
allTiles,
palettesStartingIndex
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type FileToWrite = {

export type BaseTile = {
/**
* the modern png/aseprite source for this tile,
* the modern png source for this tile,
*/
canvasSource: Canvas;

Expand Down
18 changes: 18 additions & 0 deletions testData/kof94TeResources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"romPathRoot": "./out/202-",
"padCROMFilesTo": 1048576,
"cromImages": [
{
"name": "bg_oceans",
"imageFile": "./kof94te_bg_oceans.png"
},
{
"name": "logo_countries",
"imageFile": "./kof94te_logo_countries.png"
},
{
"name": "characterGrid",
"imageFile": "./kof94te_characterGrid.png"
}
]
}
Binary file added testData/kof94te_bg_oceans.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testData/kof94te_characterGrid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testData/kof94te_logo_countries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testData/out/202-c1.c1
Binary file not shown.
Binary file added testData/out/202-c2.c2
Binary file not shown.
Binary file added testData/out/202-s1.s1
Binary file not shown.
Binary file modified testData/out/test-c1.c1
Binary file not shown.
Binary file modified testData/out/test-c2.c2
Binary file not shown.
Binary file modified testData/out/test-s1.s1
Binary file not shown.
Binary file added testData/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 6 additions & 23 deletions testData/testResources.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
{
"romPathRoot": "./out/test-",
"padCROMFilesTo": 1048576,
"eyecatcher": {
"mainLogoImageFile": "./eyecatcher_mainLogo.png",
"max330MegaImageFile": "./eyecatcher_max330.png",
"proGearSpecImageFile": "./eyecatcher_progear.png",
"snkLogoImageFile": "./eyecatcher_companyLogo.png",
"copyrightCharacterImageFile": "./eyecatcher_copyright.png"
},
"sromImages": {
"inputs": [
{
"name": "font",
"imageFile": "./fixFont.png"
}
]
},
"cromImages": {
"inputs": [
{
"name": "fire",
"imageFile": "./campFire.png"
}
]
}
"cromImages": [
{
"name": "test",
"imageFile": "./test.png"
}
]
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,13 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.199.tgz#c3edb5650149d847a277a8961a7ad360c474e9bf"
integrity sha512-Vrjz5N5Ia4SEzWWgIVwnHNEnb1UE1XMkvY5DGXrAeOGE9imk0hgTHh5GyDjLDJi9OTCn9oo9dXH1uToK1VRfrg==

"@types/mkdirp@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-2.0.0.tgz#d14f087a889c3744e7cc41e82ad762b047ebc636"
integrity sha512-c/iUqMymAlxLAyIK3u5SzrwkrkyOdv1XDc91T+b5FsY7Jr6ERhUD19jJHOhPW4GD6tmN6mFEorfSdks525pwdQ==
dependencies:
mkdirp "*"

"@types/node@*":
version "17.0.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.0.tgz#62797cee3b8b497f6547503b2312254d4fe3c2bb"
Expand Down Expand Up @@ -2645,6 +2652,11 @@ minizlib@^2.1.1:
minipass "^3.0.0"
yallist "^4.0.0"

mkdirp@*, mkdirp@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-3.0.1.tgz#e44e4c5607fb279c168241713cc6e0fea9adcb50"
integrity sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg==

mkdirp@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
Expand Down

0 comments on commit b90e0dd

Please sign in to comment.