Whatevers
This commit is contained in:
104
scripts/gb2png/index.js
Normal file
104
scripts/gb2png/index.js
Normal file
@@ -0,0 +1,104 @@
|
||||
// let DATA = [
|
||||
// 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
// 0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,
|
||||
// 0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,
|
||||
// 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
|
||||
// ];
|
||||
|
||||
const TILEMAP = [
|
||||
];
|
||||
|
||||
const TILEMAP_WIDTH = 8;
|
||||
|
||||
const convertData = (DATA, fileOut) => {
|
||||
// Begin
|
||||
const PNG = require('pngjs').PNG;
|
||||
const fs = require('fs');
|
||||
|
||||
const PIXELS = DATA.length / 2 * 8;
|
||||
const TILE_WIDTH = 8;
|
||||
const TILE_HEIGHT = 8;
|
||||
const DATA_WIDTH = TILE_WIDTH;
|
||||
const DATA_HEIGHT = PIXELS / DATA_WIDTH;
|
||||
const TILEMAP_HEIGHT = TILEMAP.length / TILEMAP_WIDTH;
|
||||
const TILEMAP_PIXEL_WIDTH = TILEMAP_WIDTH * TILE_WIDTH;
|
||||
const TILEMAP_PIXEL_HEIGHT = TILEMAP_HEIGHT * TILE_HEIGHT;
|
||||
|
||||
const colorPixel = (id) => {
|
||||
if(id === undefined) id = 3;
|
||||
if(id === 3) return { r: 8, g: 24, b: 32 };
|
||||
if(id === 2) return { r: 52, g: 104, b: 86 };
|
||||
if(id === 1) return { r: 136, g: 192, b: 112 };
|
||||
if(id === 0) return { r: 224, g: 248, b: 208 };
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
// Create output image
|
||||
const imageData = new PNG({
|
||||
width: DATA_WIDTH,
|
||||
height: DATA_HEIGHT
|
||||
});
|
||||
|
||||
const tileData = new PNG({
|
||||
width: TILEMAP_PIXEL_WIDTH,
|
||||
height: TILEMAP_PIXEL_HEIGHT
|
||||
});
|
||||
|
||||
// Convert data into pixels
|
||||
const pixelsOut = [];
|
||||
for(let i = 0; i < DATA.length; i += 2) {
|
||||
const low = DATA[i];
|
||||
const high = DATA[i+1];
|
||||
|
||||
for(let j = 0; j < 8; j++) {
|
||||
const mask = 0x80 >> j;
|
||||
const pixel = (low & mask ? 1 : 0) + (high & mask ? 2 : 0);
|
||||
pixelsOut.push(pixel);
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer data output
|
||||
for(let y = 0; y < DATA_HEIGHT; y++) {
|
||||
for(let x = 0; x < DATA_WIDTH; x++) {
|
||||
const id = (DATA_WIDTH * y + x);
|
||||
const color = colorPixel(pixelsOut[id]);
|
||||
const idx = id << 2;
|
||||
|
||||
imageData.data[idx] = color.r;
|
||||
imageData.data[idx+1] = color.g;
|
||||
imageData.data[idx+2] = color.b;
|
||||
imageData.data[idx+3] = 0xFF;
|
||||
}
|
||||
}
|
||||
const buffer = PNG.sync.write(imageData, { });
|
||||
fs.writeFileSync(fileOut, buffer);
|
||||
}
|
||||
|
||||
// Now work out tile data
|
||||
if(TILEMAP.length) {
|
||||
for(let i = 0; i < TILEMAP.length; i++) {
|
||||
const tileX = i % TILEMAP_WIDTH;
|
||||
const tileY = Math.floor(i / TILEMAP_WIDTH);
|
||||
const tile = TILEMAP[i];
|
||||
|
||||
for(let j = 0; j < TILE_WIDTH*TILE_HEIGHT; j++) {
|
||||
const outI = (
|
||||
(tileX * TILE_WIDTH) + (tileY * TILE_HEIGHT * TILEMAP_PIXEL_WIDTH) +
|
||||
((j % TILE_WIDTH) + (Math.floor(j / TILE_WIDTH) * TILEMAP_PIXEL_WIDTH))
|
||||
);
|
||||
const idx = outI << 2;
|
||||
const pixelI = (tile * TILE_WIDTH * TILE_HEIGHT) + j;
|
||||
const color = colorPixel(pixelsOut[pixelI]);
|
||||
|
||||
tileData.data[idx] = color.r;
|
||||
tileData.data[idx+1] = color.g;
|
||||
tileData.data[idx+2] = color.b;
|
||||
tileData.data[idx+3] = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
const buffer2 = PNG.sync.write(tileData, { });
|
||||
fs.writeFileSync('out.png', buffer2);
|
||||
}
|
||||
|
||||
module.exports = convertData;
|
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