Skip to content

Commit

Permalink
feat(blp): permit chaining load calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Dec 16, 2023
1 parent 7898ddd commit 6becae8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 65 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ const loadBlp = async (url) => {

const data = await response.arrayBuffer();

const blp = new Blp();
blp.load(new Uint8Array(data));
const blp = new Blp().load(new Uint8Array(data));

// <canvas> elements use image data stored in [r, g, b, a] byte order. Per standard naming
// conventions, the BLP_IMAGE_FORMAT enum references formats in highest-to-lowest order on
Expand Down
4 changes: 3 additions & 1 deletion src/lib/blp/Blp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Blp {
return this.#images.length;
}

load(source: IoSource) {
load(source: IoSource): Blp {
const stream = openStream(source);
const header = blpIo.header.read(stream);

Expand Down Expand Up @@ -104,6 +104,8 @@ class Blp {
}

stream.close();

return this;
}

save(source?: IoSource) {
Expand Down
93 changes: 31 additions & 62 deletions src/spec/blp/Blp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ describe('Blp', () => {
describe('load', () => {
describe('pala0', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/pala0.blp');
const blp = new Blp().load('./fixture/pala0.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -20,8 +19,7 @@ describe('Blp', () => {

describe('pala1', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/pala1.blp');
const blp = new Blp().load('./fixture/pala1.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -34,8 +32,7 @@ describe('Blp', () => {

describe('pala4', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/pala4.blp');
const blp = new Blp().load('./fixture/pala4.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -48,8 +45,7 @@ describe('Blp', () => {

describe('pala8', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/pala8.blp');
const blp = new Blp().load('./fixture/pala8.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -62,8 +58,7 @@ describe('Blp', () => {

describe('dxt1a', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/dxt1a.blp');
const blp = new Blp().load('./fixture/dxt1a.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -76,8 +71,7 @@ describe('Blp', () => {

describe('dxt3', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/dxt3.blp');
const blp = new Blp().load('./fixture/dxt3.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -90,8 +84,7 @@ describe('Blp', () => {

describe('dxt5', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/dxt5.blp');
const blp = new Blp().load('./fixture/dxt5.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -104,8 +97,7 @@ describe('Blp', () => {

describe('raw', () => {
test('should load blp from valid file', () => {
const blp = new Blp();
blp.load('./fixture/raw.blp');
const blp = new Blp().load('./fixture/raw.blp');

expect(blp.magic).toBe('BLP2');
expect(blp.formatVersion).toBe(1);
Expand All @@ -120,8 +112,7 @@ describe('Blp', () => {
describe('getImage', () => {
describe('pala0', () => {
test('should return image for default level and format', () => {
const blp = new Blp();
blp.load('./fixture/pala0.blp');
const blp = new Blp().load('./fixture/pala0.blp');

const image = blp.getImage();

Expand All @@ -132,8 +123,7 @@ describe('Blp', () => {
});

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

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

Expand All @@ -155,8 +145,7 @@ describe('Blp', () => {

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

const image = blp.getImage();

Expand All @@ -167,8 +156,7 @@ describe('Blp', () => {
});

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

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

Expand All @@ -190,8 +178,7 @@ describe('Blp', () => {

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

const image = blp.getImage();

Expand All @@ -202,8 +189,7 @@ describe('Blp', () => {
});

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

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

Expand All @@ -225,8 +211,7 @@ describe('Blp', () => {

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

const image = blp.getImage();

Expand All @@ -237,8 +222,7 @@ describe('Blp', () => {
});

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

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

Expand All @@ -260,8 +244,7 @@ describe('Blp', () => {

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

const image = blp.getImage();

Expand All @@ -272,8 +255,7 @@ describe('Blp', () => {
});

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

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

Expand All @@ -295,8 +277,7 @@ describe('Blp', () => {

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

const image = blp.getImage();

Expand All @@ -307,8 +288,7 @@ describe('Blp', () => {
});

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

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

Expand All @@ -330,8 +310,7 @@ describe('Blp', () => {

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

const image = blp.getImage();

Expand All @@ -342,8 +321,7 @@ describe('Blp', () => {
});

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

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

Expand All @@ -365,8 +343,7 @@ describe('Blp', () => {

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

const image = blp.getImage();

Expand All @@ -377,8 +354,7 @@ describe('Blp', () => {
});

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

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

Expand Down Expand Up @@ -416,8 +392,7 @@ describe('Blp', () => {
});

test('should save raw blp', () => {
const blp = new Blp();
blp.load('./fixture/raw.blp');
const blp = new Blp().load('./fixture/raw.blp');
const savedData = blp.save();

const originalBuffer = fs.readFileSync('./fixture/raw.blp');
Expand All @@ -431,8 +406,7 @@ describe('Blp', () => {
});

test('should save dxt blp', () => {
const blp = new Blp();
blp.load('./fixture/dxt3.blp');
const blp = new Blp().load('./fixture/dxt3.blp');
const savedData = blp.save();

const originalBuffer = fs.readFileSync('./fixture/dxt3.blp');
Expand All @@ -446,8 +420,7 @@ describe('Blp', () => {
});

test('should save pal blp', () => {
const blp = new Blp();
blp.load('./fixture/pala4.blp');
const blp = new Blp().load('./fixture/pala4.blp');
const savedData = blp.save();

const originalBuffer = fs.readFileSync('./fixture/pala4.blp');
Expand All @@ -464,8 +437,7 @@ describe('Blp', () => {
describe('setImage', () => {
describe('raw', () => {
test('should set image with given ARGB8888 source and mipmap generation disabled', () => {
const srcBlp = new Blp();
srcBlp.load('./fixture/raw.blp');
const srcBlp = new Blp().load('./fixture/raw.blp');
const srcImage = srcBlp.getImage(0, BLP_IMAGE_FORMAT.IMAGE_ARGB8888);

const dstBlp = new Blp();
Expand All @@ -483,8 +455,7 @@ describe('Blp', () => {
});

test('should set image with given ARGB8888 source and mipmap generation enabled', () => {
const srcBlp = new Blp();
srcBlp.load('./fixture/raw.blp');
const srcBlp = new Blp().load('./fixture/raw.blp');
const srcImage = srcBlp.getImage(0, BLP_IMAGE_FORMAT.IMAGE_ARGB8888);

const dstBlp = new Blp();
Expand All @@ -502,8 +473,7 @@ describe('Blp', () => {
});

test('should set image with given ABGR8888 source and mipmap generation disabled', () => {
const srcBlp = new Blp();
srcBlp.load('./fixture/raw.blp');
const srcBlp = new Blp().load('./fixture/raw.blp');
const srcImage = srcBlp.getImage(0, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

const dstBlp = new Blp();
Expand All @@ -521,8 +491,7 @@ describe('Blp', () => {
});

test('should set image with given ABGR8888 source and mipmap generation enabled', () => {
const srcBlp = new Blp();
srcBlp.load('./fixture/raw.blp');
const srcBlp = new Blp().load('./fixture/raw.blp');
const srcImage = srcBlp.getImage(0, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);

const dstBlp = new Blp();
Expand Down

0 comments on commit 6becae8

Please sign in to comment.