Skip to content

Commit

Permalink
fix(blp): use little-endian naming convention in BLP_IMAGE_FORMAT
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Dec 6, 2023
1 parent a20f149 commit 0dce033
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
22 changes: 11 additions & 11 deletions src/lib/blp/Blp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
BLP_PIXEL_FORMAT,
MAX_MIPS,
} from './const.js';
import { dxt1ToRgba8888, dxt3ToRgba8888, dxt5ToRgba8888 } from './dxt.js';
import { palToRgba8888 } from './pal.js';
import { dxt1ToAbgr8888, dxt3ToAbgr8888, dxt5ToAbgr8888 } from './dxt.js';
import { palToAbgr8888 } from './pal.js';
import * as blpIo from './io.js';
import { getSizeAtMipLevel } from './util.js';
import BlpImage from './BlpImage.js';
Expand Down Expand Up @@ -152,7 +152,7 @@ class Blp {
switch (this.#colorFormat) {
case BLP_COLOR_FORMAT.COLOR_PAL:
// Images using palettes are only useful when decoded
return this.#getPalImage(level, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
return this.#getPalImage(level, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

case BLP_COLOR_FORMAT.COLOR_DXT:
switch (this.#preferredFormat) {
Expand Down Expand Up @@ -204,8 +204,8 @@ class Blp {
case BLP_IMAGE_FORMAT.IMAGE_DXT1:
return new BlpImage(width, height, data, outputFormat);

case BLP_IMAGE_FORMAT.IMAGE_RGBA8888:
return new BlpImage(width, height, dxt1ToRgba8888(width, height, data), outputFormat);
case BLP_IMAGE_FORMAT.IMAGE_ABGR8888:
return new BlpImage(width, height, dxt1ToAbgr8888(width, height, data), outputFormat);

default:
throw new Error(`Unsupported output format: ${outputFormat}`);
Expand All @@ -221,8 +221,8 @@ class Blp {
case BLP_IMAGE_FORMAT.IMAGE_DXT3:
return new BlpImage(width, height, data, outputFormat);

case BLP_IMAGE_FORMAT.IMAGE_RGBA8888:
return new BlpImage(width, height, dxt3ToRgba8888(width, height, data), outputFormat);
case BLP_IMAGE_FORMAT.IMAGE_ABGR8888:
return new BlpImage(width, height, dxt3ToAbgr8888(width, height, data), outputFormat);

default:
throw new Error(`Unsupported output format: ${outputFormat}`);
Expand All @@ -238,8 +238,8 @@ class Blp {
case BLP_IMAGE_FORMAT.IMAGE_DXT5:
return new BlpImage(width, height, data, outputFormat);

case BLP_IMAGE_FORMAT.IMAGE_RGBA8888:
return new BlpImage(width, height, dxt5ToRgba8888(width, height, data), outputFormat);
case BLP_IMAGE_FORMAT.IMAGE_ABGR8888:
return new BlpImage(width, height, dxt5ToAbgr8888(width, height, data), outputFormat);

default:
throw new Error(`Unsupported output format: ${outputFormat}`);
Expand All @@ -252,11 +252,11 @@ class Blp {
const data = this.#images[level];

switch (outputFormat) {
case BLP_IMAGE_FORMAT.IMAGE_RGBA8888:
case BLP_IMAGE_FORMAT.IMAGE_ABGR8888:
return new BlpImage(
width,
height,
palToRgba8888(width, height, data, this.#palette, this.#alphaSize),
palToAbgr8888(width, height, data, this.#palette, this.#alphaSize),
outputFormat,
);
default:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/blp/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum BLP_PIXEL_FORMAT {
enum BLP_IMAGE_FORMAT {
IMAGE_UNSPECIFIED = 0,
IMAGE_JPEG,
IMAGE_RGBA8888,
IMAGE_ABGR8888,
IMAGE_DXT1,
IMAGE_DXT3,
IMAGE_DXT5,
Expand Down
16 changes: 8 additions & 8 deletions src/lib/blp/dxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const dxt5DecompressBlock = (block: Uint8Array) => {
DECOMPRESSED[35] = ALPHA_LOOKUP[ (block[5] & 0b00000111) >> 0];
};

const dxtToRgba8888 = (
const dxtToAbgr8888 = (
width: number,
height: number,
input: Uint8Array,
Expand Down Expand Up @@ -195,13 +195,13 @@ const dxtToRgba8888 = (
return output;
};

const dxt1ToRgba8888 = (width: number, height: number, input: Uint8Array) =>
dxtToRgba8888(width, height, input, DXT1_BLOCK_SIZE, dxt1DecompressBlock);
const dxt1ToAbgr8888 = (width: number, height: number, input: Uint8Array) =>
dxtToAbgr8888(width, height, input, DXT1_BLOCK_SIZE, dxt1DecompressBlock);

const dxt3ToRgba8888 = (width: number, height: number, input: Uint8Array) =>
dxtToRgba8888(width, height, input, DXT3_BLOCK_SIZE, dxt3DecompressBlock);
const dxt3ToAbgr8888 = (width: number, height: number, input: Uint8Array) =>
dxtToAbgr8888(width, height, input, DXT3_BLOCK_SIZE, dxt3DecompressBlock);

const dxt5ToRgba8888 = (width: number, height: number, input: Uint8Array) =>
dxtToRgba8888(width, height, input, DXT5_BLOCK_SIZE, dxt5DecompressBlock);
const dxt5ToAbgr8888 = (width: number, height: number, input: Uint8Array) =>
dxtToAbgr8888(width, height, input, DXT5_BLOCK_SIZE, dxt5DecompressBlock);

export { dxt1ToRgba8888, dxt3ToRgba8888, dxt5ToRgba8888 };
export { dxt1ToAbgr8888, dxt3ToAbgr8888, dxt5ToAbgr8888 };
20 changes: 10 additions & 10 deletions src/lib/blp/pal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pala0toRgba8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const pala0toAbgr8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const output = new Uint8Array(width * height * 4);

for (let i = 0; i < width * height; i++) {
Expand All @@ -17,7 +17,7 @@ const pala0toRgba8888 = (width: number, height: number, input: Uint8Array, palet
return output;
};

const pala1toRgba8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const pala1toAbgr8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const output = new Uint8Array(width * height * 4);
const alphaOfs = width * height;

Expand All @@ -38,7 +38,7 @@ const pala1toRgba8888 = (width: number, height: number, input: Uint8Array, palet
return output;
};

const pala4toRgba8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const pala4toAbgr8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const output = new Uint8Array(width * height * 4);
const alphaOfs = width * height;

Expand All @@ -60,7 +60,7 @@ const pala4toRgba8888 = (width: number, height: number, input: Uint8Array, palet
return output;
};

const pala8toRgba8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const pala8toAbgr8888 = (width: number, height: number, input: Uint8Array, palette: Uint8Array) => {
const output = new Uint8Array(width * height * 4);
const alphaOfs = width * height;

Expand All @@ -80,7 +80,7 @@ const pala8toRgba8888 = (width: number, height: number, input: Uint8Array, palet
return output;
};

const palToRgba8888 = (
const palToAbgr8888 = (
width: number,
height: number,
input: Uint8Array,
Expand All @@ -89,16 +89,16 @@ const palToRgba8888 = (
) => {
switch (alphaSize) {
case 0:
return pala0toRgba8888(width, height, input, palette);
return pala0toAbgr8888(width, height, input, palette);
case 1:
return pala1toRgba8888(width, height, input, palette);
return pala1toAbgr8888(width, height, input, palette);
case 4:
return pala4toRgba8888(width, height, input, palette);
return pala4toAbgr8888(width, height, input, palette);
case 8:
return pala8toRgba8888(width, height, input, palette);
return pala8toAbgr8888(width, height, input, palette);
default:
throw new Error(`Unsupported alpha size: ${alphaSize}`);
}
};

export { palToRgba8888 };
export { palToAbgr8888 };
36 changes: 18 additions & 18 deletions src/spec/blp/Blp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ describe('Blp', () => {
expect(image.width).toBe(512);
expect(image.height).toBe(512);
expect(image.data.byteLength).toBe(512 * 512 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});

test('should return image for level 1 and RGBA8888 format', () => {
const blp = new Blp();
blp.load('./fixture/pala0.blp');

const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

expect(image.width).toBe(256);
expect(image.height).toBe(256);
expect(image.data.byteLength).toBe(256 * 256 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});
});

Expand All @@ -146,19 +146,19 @@ describe('Blp', () => {
expect(image.width).toBe(32);
expect(image.height).toBe(32);
expect(image.data.byteLength).toBe(32 * 32 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});

test('should return image for level 1 and RGBA8888 format', () => {
const blp = new Blp();
blp.load('./fixture/pala1.blp');

const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

expect(image.width).toBe(16);
expect(image.height).toBe(16);
expect(image.data.byteLength).toBe(16 * 16 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});
});

Expand All @@ -172,19 +172,19 @@ describe('Blp', () => {
expect(image.width).toBe(128);
expect(image.height).toBe(128);
expect(image.data.byteLength).toBe(128 * 128 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});

test('should return image for level 1 and RGBA8888 format', () => {
const blp = new Blp();
blp.load('./fixture/pala4.blp');

const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

expect(image.width).toBe(64);
expect(image.height).toBe(64);
expect(image.data.byteLength).toBe(64 * 64 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});
});

Expand All @@ -198,19 +198,19 @@ describe('Blp', () => {
expect(image.width).toBe(128);
expect(image.height).toBe(64);
expect(image.data.byteLength).toBe(128 * 64 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});

test('should return image for level 1 and RGBA8888 format', () => {
const blp = new Blp();
blp.load('./fixture/pala8.blp');

const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

expect(image.width).toBe(64);
expect(image.height).toBe(32);
expect(image.data.byteLength).toBe(64 * 32 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});
});

Expand All @@ -231,12 +231,12 @@ describe('Blp', () => {
const blp = new Blp();
blp.load('./fixture/dxt1a.blp');

const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

expect(image.width).toBe(8);
expect(image.height).toBe(8);
expect(image.data.byteLength).toBe(8 * 8 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});
});

Expand All @@ -257,12 +257,12 @@ describe('Blp', () => {
const blp = new Blp();
blp.load('./fixture/dxt3.blp');

const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

expect(image.width).toBe(128);
expect(image.height).toBe(128);
expect(image.data.byteLength).toBe(128 * 128 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});
});

Expand All @@ -283,12 +283,12 @@ describe('Blp', () => {
const blp = new Blp();
blp.load('./fixture/dxt5.blp');

const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const image = blp.getImage(1, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

expect(image.width).toBe(256);
expect(image.height).toBe(256);
expect(image.data.byteLength).toBe(256 * 256 * 4);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
expect(image.format).toBe(BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
});
});
});
Expand Down

0 comments on commit 0dce033

Please sign in to comment.