Skip to content

Commit

Permalink
Add TXI RGBA4444 encoding support and bump to 1.1.6 (#38)
Browse files Browse the repository at this point in the history
* Update PixelEncoder with texture format formulas.
* Add test image references and update unit tests to include RGBA4444.

Signed-off-by: Bogdan Purcareata <[email protected]>
  • Loading branch information
dodgerblue authored Mar 17, 2022
1 parent 3874ea9 commit 06cb674
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ TXI is an image format used on some Fitbit devices. This package is used to conv
```
{
RGB565 = 'RGB565',
RGBA4444 = 'RGBA4444',
RGBA6666 = 'RGBA6666',
RGBA8888 = 'RGBA8888',
A8 = 'A8',
Expand Down
6 changes: 6 additions & 0 deletions bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ suite
outputFormat: imageCodecTxi.TXIOutputFormat.RGBA8888,
});
})
.add('RGBA4444', () => {
imageCodecTxi.encode(inputImage, {
rle: 'auto',
outputFormat: imageCodecTxi.TXIOutputFormat.RGBA4444,
});
})
.add('RGBA6666', () => {
imageCodecTxi.encode(inputImage, {
rle: 'auto',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fitbit/image-codec-txi",
"version": "1.1.5",
"version": "1.1.6",
"description": "TXI image format encoder library",
"files": [
"lib",
Expand Down
Binary file added src/__test__/rgb_image.txi.rgba4444
Binary file not shown.
Binary file added src/__test__/rgb_image.txi.rgba4444.rle
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions src/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ for (const withRLE of [true, false]) {
checkImage('rgb_image', withRLE));
it('converts an RGB PNG to RGB565', () =>
checkImage('rgb_image', withRLE, TXIOutputFormat.RGB565));
it('converts an RGB PNG to RGBA4444', () =>
checkImage('rgb_image', withRLE, TXIOutputFormat.RGBA4444));
it('converts an RGB PNG to RGBA6666', () =>
checkImage('rgb_image', withRLE, TXIOutputFormat.RGBA6666));
it('converts a paletted PNG', () => checkImage('palette', withRLE));
Expand Down Expand Up @@ -101,6 +103,14 @@ describe('readPNG', () => {
const png = readPNG(loadTestResource('transparency-palette.png'));
expect(png.data[3]).toBe(0);
});

it('goes through the RGBA4444 corner cases', () => {
checkImage('transparency-palette', 'auto', TXIOutputFormat.RGBA4444);
});

it('goes through the RGBA6666 corner cases', () => {
checkImage('transparency-palette', 'auto', TXIOutputFormat.RGBA6666);
});
});
});

Expand Down
19 changes: 19 additions & 0 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum TextureFormat {
A8 = 0x00080008,
BGR565 = 0x01100565,
BGRA8888 = 0x01208888,
ABGR4444 = 0x02104444,
ABGR6666 = 0x02186666,
ABGR8888 = 0x02208888,
}
Expand All @@ -30,6 +31,7 @@ const textureBPP: { [format: number]: number } = {
[TextureFormat.ABGR8888]: 4,
[TextureFormat.BGR565]: 2,
[TextureFormat.ABGR6666]: 3,
[TextureFormat.ABGR4444]: 2,
};

type PixelEncoder = (data: Uint8Array, offset: number, output: Pixel) => void;
Expand Down Expand Up @@ -58,6 +60,21 @@ const pixelEncoders: { [format: number]: PixelEncoder } = {
output[0] = 0xff & ((g6 << 5) | b5); // gggbbbbb
output[1] = 0xff & ((g6 >> 3) | (r5 << 3)); // rrrrrggg
},
[TextureFormat.ABGR4444]: (data, offset, output) => {
if (data[offset + 3] === 0) {
output[0] = 0;
output[1] = 0;
return;
}

const r = rescaleColor(data[offset], 15);
const g = rescaleColor(data[offset + 1], 15);
const b = rescaleColor(data[offset + 2], 15);
const a = rescaleColor(data[offset + 3], 15);

output[0] = 0xff & ((b << 4) | a); // bbbbaaaa
output[1] = 0xff & ((r << 4) | g); // rrrrgggg
},
[TextureFormat.ABGR6666]: (data, offset, output) => {
if (data[offset + 3] === 0) {
output[0] = 0;
Expand Down Expand Up @@ -86,6 +103,8 @@ function findTextureFormat(outputFormat: TXIOutputFormat, rle: boolean) {
return TextureFormat.A8;
case TXIOutputFormat.RGB565:
return TextureFormat.BGR565;
case TXIOutputFormat.RGBA4444:
return TextureFormat.ABGR4444;
case TXIOutputFormat.RGBA6666:
return TextureFormat.ABGR6666;
}
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ImageData {

export enum TXIOutputFormat {
RGB565 = 'RGB565',
RGBA4444 = 'RGBA4444',
RGBA6666 = 'RGBA6666',
RGBA8888 = 'RGBA8888',
A8 = 'A8',
Expand All @@ -17,3 +18,8 @@ export interface TXIEncoderOptions {
rle?: boolean | 'auto';
outputFormat?: TXIOutputFormat;
}

export type RGBAOutputFormat =
| TXIOutputFormat.RGBA8888
| TXIOutputFormat.RGBA6666
| TXIOutputFormat.RGBA4444;

0 comments on commit 06cb674

Please sign in to comment.