Skip to content

Commit deab94e

Browse files
committed
Fix pixel color handling in useTests and pack1bitAlpha functions. Updated test cases to correctly alternate between black and white pixels, ensuring accurate byte packing for horizontal and vertical orientations. Adjusted packing logic to iterate bits in reverse order for proper alpha channel processing.
1 parent 06cbee7 commit deab94e

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/hooks/useTests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function useTests() {
2727
{
2828
const tw = 8, th = 1;
2929
const px = [];
30-
for (let i = 0; i < 8; i++) px.push((i % 2 === 0) ? black() : white()); // 1,0,1,0,... (black=1)
30+
for (let i = 0; i < 8; i++) px.push((i % 2 === 0) ? white() : black()); // 1,0,1,0,... (white=1)
3131
const bytes = pack1bit(px, tw, th, 'horizontal');
3232
const expect = [0xAA];
3333
results.push({ name: "1BIT Horizontal 8px 10101010", pass: JSON.stringify(bytes) === JSON.stringify(expect), got: bytes, expect });
@@ -36,7 +36,7 @@ export function useTests() {
3636
// Test 2: 1-bit horizontal width 10, row of all ones → [0xFF, 0xC0]
3737
{
3838
const tw = 10, th = 1;
39-
const px = Array.from({ length: tw * th }, () => black());
39+
const px = Array.from({ length: tw * th }, () => white());
4040
const bytes = pack1bit(px, tw, th, 'horizontal');
4141
const expect = [0xFF, 0xC0];
4242
results.push({ name: "1BIT Horizontal 10px row all 1s", pass: JSON.stringify(bytes) === JSON.stringify(expect), got: bytes, expect });
@@ -46,7 +46,7 @@ export function useTests() {
4646
{
4747
const tw = 1, th = 8;
4848
const px = [];
49-
for (let i = 0; i < 8; i++) px.push((i % 2 === 0) ? black() : white()); // 1,0,1,0,... (black=1)
49+
for (let i = 0; i < 8; i++) px.push((i % 2 === 0) ? white() : black()); // 1,0,1,0,... (white=1)
5050
const bytes = pack1bit(px, tw, th, 'vertical');
5151
const expect = [0xAA]; // Same pattern but vertical
5252
results.push({ name: "1BIT Vertical 1x8 10101010", pass: JSON.stringify(bytes) === JSON.stringify(expect), got: bytes, expect });

src/lib/packers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ export function pack1bitAlpha(pixels, width, height, orientation = 'horizontal')
8787
for (let x = 0; x < width; x++) {
8888
for (let yPage = 0; yPage < Math.ceil(height / 8); yPage++) {
8989
let cur = 0;
90-
for (let bit = 0; bit < 8; bit++) {
91-
const y = yPage * 8 + bit;
90+
for (let bit = 7; bit >= 0; bit--) {
91+
const y = yPage * 8 + (7 - bit);
9292
if (y < height) {
9393
const p = pixels[I(x, y)];
9494
const hasAlpha = p.a > 127; // alpha threshold (opaque = on)

0 commit comments

Comments
 (0)