Skip to content

Commit

Permalink
chore(doc): add blp example to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Dec 12, 2023
1 parent 93bac76 commit 9c1882b
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,50 @@ To install Wowser Format:
npm install @wowserhq/format
```

Wowser Format has a single dependency: it uses `@wowserhq/io` to handle file IO (reads and writes).

### BLP

Wowser Format supports loading and converting image data contained in BLP files. BLP files encoded
in palette, DXT, and raw form are all supported. BLP files encoded in JPEG form are not currently
supported.

#### Rendering a BLP in a `<canvas>` element

```js
import { Blp, BLP_IMAGE_FORMAT } from '@wowserhq/format';

const loadBlp = async (url) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch BLP: Error ${response.status}`);
}

const data = await response.arrayBuffer();

const blp = new Blp();
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
// little-endian systems. As a result, IMAGE_ABGR8888 is the appropriate image format for use
// with ImageData and <canvas> elements.
const image = blp.getImage(0, BLP_IMAGE_FORMAT.IMAGE_ABGR8888);
const imageData = new ImageData(new Uint8ClampedArray(image.data), image.width, image.height);

return imageData;
};

const imageData = await loadBlp('url-of-blp-here');

const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;

const context = canvas.getContext('2d');
context.putImageData(imageData, 0, 0);
```

## License

Wowser Format is copyright © Wowser Contributors. It is licensed under the **MIT** license. See
Expand Down

0 comments on commit 9c1882b

Please sign in to comment.