Whatevers
This commit is contained in:
89
scripts/png2gb/index.js
Normal file
89
scripts/png2gb/index.js
Normal file
@@ -0,0 +1,89 @@
|
||||
const PNG = require('pngjs').PNG;
|
||||
const fs = require('fs');
|
||||
|
||||
const convert = (fileIn, fileOut) => {
|
||||
const TILE_WIDTH = 8;
|
||||
const TILE_HEIGHT = 8;
|
||||
|
||||
const data = fs.readFileSync(fileIn);
|
||||
const png = PNG.sync.read(data);
|
||||
|
||||
const getPixelValue = (pixel) => {
|
||||
if(pixel.r === 15) return 3;
|
||||
if(pixel.r === 48) return 2;
|
||||
if(pixel.r === 0) return 1;
|
||||
if(pixel.r === 155) return 0;
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
// Convert PNG pixels into 0x00-0x03
|
||||
const pixels = [];
|
||||
for(let x = 0; x < png.width; x++) {
|
||||
for(let y = 0; y < png.height; y++) {
|
||||
const id = x + (y * png.width);
|
||||
const idx = id << 2;
|
||||
const r = png.data[idx];
|
||||
const g = png.data[idx+1];
|
||||
const b = png.data[idx+2];
|
||||
const value = getPixelValue({ r, g, b });
|
||||
pixels.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
// Now take these raw pixels and extract the tiles themselves
|
||||
let rearranged = [];
|
||||
const columns = (png.width / TILE_WIDTH);
|
||||
const rows = (png.height / TILE_HEIGHT);
|
||||
let n = 0;
|
||||
for(let i = 0; i < columns * rows; i++) {
|
||||
const tileX = i % columns;
|
||||
const tileY = Math.floor(i / columns) % rows;
|
||||
|
||||
for(let x = 0; x < TILE_WIDTH; x++) {
|
||||
for(let y = 0; y < TILE_HEIGHT; y++) {
|
||||
const px = (tileY * TILE_WIDTH) + x;// NO idea why I need to flipX/Y.
|
||||
const py = (tileX * TILE_HEIGHT) + y;// and too lazy to figure out why.
|
||||
const pi = px + (py * png.width);
|
||||
rearranged[n++] = pixels[pi];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now turn into a tileset
|
||||
const bits = [];
|
||||
for(let i = 0; i < rearranged.length; i += TILE_WIDTH) {
|
||||
let lowBits = 0x00;
|
||||
let highBits = 0x00;
|
||||
for(let j = 0; j < TILE_WIDTH; j++) {
|
||||
const pixel = rearranged[i + j];
|
||||
lowBits = lowBits | ((pixel & 0x01) << (7-j));
|
||||
highBits = highBits | ((pixel & 0x02) >> 1 << (7-j));
|
||||
}
|
||||
bits.push(lowBits, highBits);
|
||||
}
|
||||
|
||||
const b = bits.map(n => {
|
||||
return '0x' + (n.toString(16).padStart(2, '0').toUpperCase());
|
||||
});
|
||||
let str = '';
|
||||
for(let i = 0; i < b.length; i += 16) {
|
||||
str += ' ';
|
||||
for(let x = i; x < Math.min(i+16, b.length); x++) {
|
||||
str += b[x];
|
||||
str += ',';
|
||||
}
|
||||
str += '\n';
|
||||
}
|
||||
|
||||
let out = '';
|
||||
out += `#include "../libs.h"\n\n`
|
||||
out += `#define IMAGE_WIDTH ${png.width}\n`;
|
||||
out += `#define IMAGE_HEIGHT ${png.height}\n`;
|
||||
out += `#define IMAGE_TILES ${columns * rows}\n`;
|
||||
out += `\nconst uint8_t IMAGE[] = {\n${str}};`;
|
||||
|
||||
fs.writeFileSync(fileOut, out);
|
||||
require('./../gb2png/')(bits, `${fileOut}.png`);
|
||||
}
|
||||
|
||||
convert('font.png', 'out.c');
|
Reference in New Issue
Block a user