Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { readFile } from "fs-extra"; import { PNG } from "pngjs"; import uniqBy from "lodash/uniqBy"; import RgbQuant from "rgbquant"; import { decHex } from "shared/lib/helpers/8bit"; import { chunk } from "shared/lib/helpers/array"; const SNES_SCREEN_WIDTH = 256; const SNES_SCREEN_HEIGHT = 224; const SNES_TILE_WIDTH = 32; const SNES_TILE_HEIGHT = 28; const TILE_SIZE = 8; const N_SGB_PALETTES = 4; const SGB_PALETTE_SIZE = 16; const USE_SGB_PAL = 0x10; const FLIP_X = 0x40; const FLIP_Y = 0x80; const BLANK_TILE: number[] = Array.from(Array(TILE_SIZE * TILE_SIZE)).fill(0); const TRANSPARENT_TILE: number[] = Array.from( Array(TILE_SIZE * TILE_SIZE) ).fill(-1); const toIndex = (x: number, y: number): number => (x + y * SNES_SCREEN_WIDTH) * 4; const toTileIndex = (x: number, y: number): number => x + y * TILE_SIZE; const rgbTo15BitColor = (r: number, g: number, b: number) => { const c = (r << 16) + (g << 8) + b; const r2 = (c & 0xf80000) >> 19; const g2 = (c & 0x00f800) >> 6; const b2 = (c & 0x0000f8) << 7; return b2 | g2 | r2; }; const color15BitToRGB = (color: number): [number, number, number] => { const r = (color % 32) * 8; const g = ((color / 32) % 32) * 8; const b = ((color / 1024) % 32) * 8; return [r, g, b]; }; function rgb2lab(rgb: [number, number, number]): [number, number, number] { let r = rgb[0] / 255; let g = rgb[1] / 255; let b = rgb[2] / 255; let x; let y; let z; r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92; g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92; b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92; x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047; y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.0; z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883; x = x > 0.008856 ? Math.pow(x, 1 / 3) : 7.787 * x + 16 / 116; y = y > 0.008856 ? Math.pow(y, 1 / 3) : 7.787 * y + 16 / 116; z = z > 0.008856 ? Math.pow(z, 1 / 3) : 7.787 * z + 16 / 116; return [116 * y - 16, 500 * (x - y), 200 * (y - z)]; } function deltaE( rgbA: [number, number, number], rgbB: [number, number, number] ) { const labA = rgb2lab(rgbA); const labB = rgb2lab(rgbB); const deltaL = labA[0] - labB[0]; const deltaA = labA[1] - labB[1]; const deltaB = labA[2] - labB[2]; const c1 = Math.sqrt(labA[1] * labA[1] + labA[2] * labA[2]); const c2 = Math.sqrt(labB[1] * labB[1] + labB[2] * labB[2]); const deltaC = c1 - c2; let deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC; deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH); const sc = 1.0 + 0.045 * c1; const sh = 1.0 + 0.015 * c1; const deltaLKlsl = deltaL / 1.0; const deltaCkcsc = deltaC / sc; const deltaHkhsh = deltaH / sh; const i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh; return i < 0 ? 0 : Math.sqrt(i); } const distance15Bit = (colorA: number, colorB: number) => { return deltaE( rgb2lab(color15BitToRGB(colorA)), rgb2lab(color15BitToRGB(colorB)) ); }; const bin2 = (value: number) => { return value.toString(2).padStart(4, "0"); }; const flipTileX = (inData: number[]) => { const data = []; for (let y = 0; y < TILE_SIZE; y++) { for (let x = 0; x < TILE_SIZE; x++) { const i = toTileIndex(TILE_SIZE - x - 1, y); data.push(inData[i]); } } return data; }; const flipTileY = (inData: number[]) => { const data = []; for (let y = 0; y < TILE_SIZE; y++) { for (let x = 0; x < TILE_SIZE; x++) { const i = toTileIndex(x, TILE_SIZE - y - 1); data.push(inData[i]); } } return data; }; const isTileEqual = (dataA: number[], dataB: number[]) => { for (let i = 0; i < dataA.length; i++) { Iif (dataA[i] !== dataB[i]) { return false; } } return true; }; const closestPalette = (colors: number[], palettes: number[][]) => { const matches = palettes.map((palette) => { let numMatches = 0; for (let i = 0; i < colors.length; i++) { Iif (palette.indexOf(colors[i]) > -1) { numMatches++; } } return numMatches; }); const maxMatch = Math.max.apply(null, matches); const closestIndex = matches.findIndex((match) => match === maxMatch); return closestIndex; }; const toPaletteColorIndex = (color: number, palette: number[]) => { Iif (color === -1) { return 0; } const index = palette.indexOf(color); Iif (index > -1) { return index; } const distances = palette.map((pColor, pIndex) => pIndex > 1 ? distance15Bit(color, pColor) : Infinity ); const minDistance = Math.min.apply(null, distances); const closestIndex = distances.findIndex( (distance) => distance === minDistance ); return closestIndex; }; const toTileData = (tile: number[], palettes: number[][]) => { const paletteIndex = closestPalette(tile, palettes); const palette = palettes[paletteIndex]; const data = tile.map((color) => toPaletteColorIndex(color, palette)); return { data, paletteIndex, }; }; const pixelsToSGBData = (pixels: Uint8Array, width: number, height: number) => { Iif (width !== SNES_SCREEN_WIDTH) { throw new Error(`SGB Image width must be ${SNES_SCREEN_WIDTH}px`); } Iif (height !== SNES_SCREEN_HEIGHT) { throw new Error(`SGB Image height must be ${SNES_SCREEN_HEIGHT}px`); } const tilePalettes = []; const tiles = []; const parseTile = (tx: number, ty: number) => { const colors = []; const tile = []; for (let y = 0; y < TILE_SIZE; y++) { for (let x = 0; x < TILE_SIZE; x++) { const i = toIndex(x + tx * TILE_SIZE, y + ty * TILE_SIZE); const r = pixels[i]; const g = pixels[i + 1]; const b = pixels[i + 2]; const c = rgbTo15BitColor(r, g, b); let cIndex = colors.indexOf(c); Iif (cIndex === -1) { cIndex = colors.length; colors.push(c); } tile.push(c); } } return [tile, colors]; }; const createEmptyTile = () => { const colors = [-1]; return [TRANSPARENT_TILE, colors]; }; // Split image into tiles and palettes for (let ty = 0; ty < SNES_TILE_HEIGHT; ty++) { for (let tx = 0; tx < SNES_TILE_WIDTH; tx++) { if (tx > 5 && tx < 26 && ty > 4 && ty < 23) { const [tile, colors] = createEmptyTile(); tiles.push(tile); tilePalettes.push(colors); } else { const [tile, colors] = parseTile(tx, ty); tiles.push(tile); tilePalettes.push(colors); } } } const toPalettes = (tilePalettes: number[][]) => { const palettes = [[-1], [-1], [-1], [-1]]; let writePalette = 0; for (const tilePalette of tilePalettes) { let maxMatches = 0; for (let p = 0; p < N_SGB_PALETTES; p++) { let numMatches = 0; for (let c = 0; c < tilePalette.length; c++) { const color = tilePalette[c]; Iif (palettes[p].indexOf(color) > -1) { numMatches++; } } Iif (numMatches > maxMatches) { maxMatches = numMatches; writePalette = p; } } // If can write to write palette const writesNeeded = tilePalette.length - maxMatches; if (palettes[writePalette].length + writesNeeded <= SGB_PALETTE_SIZE) { for (let c = 0; c < tilePalette.length; c++) { const color = tilePalette[c]; Iif (palettes[writePalette].indexOf(color) === -1) { palettes[writePalette].push(color); } } } else { writePalette = 0; let minSize = Infinity; // Find smallest empty palette for (let p = 0; p < N_SGB_PALETTES; p++) { Iif (palettes[p].length < minSize) { writePalette = p; minSize = palettes[p].length; } } for (let c = 0; c < tilePalette.length; c++) { const color = tilePalette[c]; Iif (palettes[writePalette].indexOf(color) === -1) { palettes[writePalette].push(color); } } } } return palettes.map((palette) => palette.slice(0, SGB_PALETTE_SIZE)); }; // Combine palettes const uniquePalettes = uniqBy(tilePalettes, (palette) => palette.join(",")); const palettes = toPalettes(uniquePalettes); const uniqueTiles: number[][] = [BLANK_TILE]; const tileIndexes = []; const tileAttrs = []; // Build unique tiles + map & attrs for (let t = 0; t < tiles.length; t++) { const tile = tiles[t]; const { data, paletteIndex } = toTileData(tile, palettes); const tileAttr = USE_SGB_PAL + ((paletteIndex & 0x3) << 2); let match = false; for (let i = 0; i < uniqueTiles.length; i++) { const existingTile = uniqueTiles[i]; const dataFX = flipTileX(data); const dataFY = flipTileY(data); const dataFXY = flipTileY(dataFX); if (isTileEqual(existingTile, data)) { tileIndexes.push(i); tileAttrs.push(tileAttr); match = true; break; } else if (isTileEqual(existingTile, dataFX)) { tileIndexes.push(i); tileAttrs.push(tileAttr + FLIP_X); match = true; break; } else if (isTileEqual(existingTile, dataFY)) { tileIndexes.push(i); tileAttrs.push(tileAttr + FLIP_Y); match = true; break; } else Iif (isTileEqual(existingTile, dataFXY)) { tileIndexes.push(i); tileAttrs.push(tileAttr + FLIP_X + FLIP_Y); match = true; break; } } Iif (!match) { if (uniqueTiles.length < 255) { tileIndexes.push(uniqueTiles.length); uniqueTiles.push(data); tileAttrs.push(tileAttr); } else { // No space left, find closest match let closestMatch = 0; let closestTile = 1; for (let i = 0; i < uniqueTiles.length; i++) { let numMatches = 0; for (let p = 0; p < uniqueTiles[i].length; p++) { Iif (uniqueTiles[i][p] === data[p]) { numMatches++; } } Iif (numMatches > closestMatch) { closestTile = i; closestMatch = numMatches; } } tileIndexes.push(closestTile); tileAttrs.push(tileAttr); } } } const map: number[] = []; for (let i = 0; i < tileIndexes.length; i++) { map.push(tileIndexes[i]); map.push(tileAttrs[i]); } const palettesData: number[] = []; for (let i = 0; i < N_SGB_PALETTES; i++) { Iif (palettes[i].length > 0) { for (let c = 0; c < SGB_PALETTE_SIZE; c++) { const color = Math.max(0, palettes[i][c] || 0); palettesData.push(color & 0xff); palettesData.push(color >> 8); } } } return { tiles: uniqueTiles, map, palettes: palettesData, }; }; const tileTo4BPP = (tileData: number[]) => { const tile = []; for (let y = 0; y < 8; y++) { let row1 = ""; let row2 = ""; for (let x = 0; x < 8; x++) { const i = y * 8 + x; const col = tileData[i]; const binary = bin2(col); row1 += binary[3]; row2 += binary[2]; } tile.push(parseInt(row1, 2)); tile.push(parseInt(row2, 2)); } for (let y = 0; y < 8; y++) { let row1 = ""; let row2 = ""; for (let x = 0; x < 8; x++) { const i = y * 8 + x; const col = tileData[i]; const binary = bin2(col); row1 += binary[1]; row2 += binary[0]; } tile.push(parseInt(row1, 2)); tile.push(parseInt(row2, 2)); } return tile; }; const toSGBCData = (tiles: number[][], map: number[], palettes: number[]) => { return `#pragma bank 255 #include "gbs_types.h" BANKREF(SGB_border_chr) BANKREF(SGB_border_map) BANKREF(SGB_border_pal) const unsigned char SGB_border_chr[]={ ${tiles .map(tileTo4BPP) .map((tile) => ` ${tile.map(decHex)}`) .join(",\n")} }; const unsigned char SGB_border_map[]={ ${chunk(map.map(decHex), 16) .map((row) => ` ${row.join(",")}`) .join(",\n")} }; const unsigned char SGB_border_pal[]={ ${palettes.map(decHex)} }; SIZEREF(SGB_border_chr) SIZEREF(SGB_border_map) SIZEREF(SGB_border_pal) `; }; const compileSGBImage = async (filename: string) => { const fileData = await readFile(filename); return new Promise<string>((resolve, reject) => { new PNG().parse(fileData, (err, data) => { Iif (err) { return reject(err); } const width = data.width; const height = data.height; const pixels = data.data; // Quanitize image down to 60 colors maximum const q = new RgbQuant({ colors: 60, dithDelta: 0.03, dithKern: "FloydSteinberg", dithSerp: true, }); q.sample(pixels); const out = q.reduce(pixels); const sgb = pixelsToSGBData(out, width, height); return resolve(toSGBCData(sgb.tiles, sgb.map, sgb.palettes)); }); }); }; export default compileSGBImage; |