Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

png2src: add ability to handle any width #713

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions cli/lib/png2src.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,7 @@ The first occurrence of another color is at (${x}, ${y}) and has the value of (R
odinFlags = "{ .USE_2BPP }"
}

const factor = 8 / bpp;
if (png.width % factor != 0) {
throw new Error(`${bpp}BPP sprites must have a width divisible by ${factor}`);
}

const bytes = new Uint8Array(png.width * png.height * bpp / 8);
const bytes = new Uint8Array((png.width * png.height * bpp + 7 ) / 8);

// Read a color (palette index) from the source png
function readColor(x, y) {
Expand All @@ -260,25 +255,15 @@ The first occurrence of another color is at (${x}, ${y}) and has the value of (R

// Write a color (palette index) to the output buffer
function writeColor(color, x, y) {
let idx, shift, mask;
switch (bpp) {
case 1:
idx = (y * png.width + x) >> 3;
shift = 7 - (x & 0x07);
mask = 0x1 << shift;
break;

case 2:
idx = (y * png.width + x) >> 2;
shift = 6 - ((x & 0x3) << 1);
mask = 0x3 << shift;
break;

default:
throw new Error("assert");
if(bpp != 1 && bpp != 2){
throw new Error("unexpexted bpp");
aduros marked this conversation as resolved.
Show resolved Hide resolved
}

bytes[idx] = (color << shift) | (bytes[idx] & (~mask));
const color_idx = (y * png.width + x);
const idx = Math.floor(color_idx / ( 8 / bpp ));
const shift = ((8 / bpp - 1)-color_idx % ( 8 / bpp )) * bpp;

bytes[idx] = (color << shift) | (bytes[idx]);
}

for (let y = 0; y < png.height; ++y) {
Expand Down