mirror of
https://github.com/kettek/png-js
synced 2025-03-14 06:24:30 -07:00
Allow extraction of pixel buffer via toPNGData()
This commit is contained in:
parent
d05c43904f
commit
7b43cf73ac
13
README.md
13
README.md
|
@ -57,7 +57,10 @@ async decode()
|
||||||
: Decodes the palette and the pixel data of the PNG data passed into the constructor. Calls `decodePixels()` and `decodePalette()`.
|
: Decodes the palette and the pixel data of the PNG data passed into the constructor. Calls `decodePixels()` and `decodePalette()`.
|
||||||
|
|
||||||
[ImageData](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) async toImageData([ImageDataOptions](#imagedataoptions))
|
[ImageData](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) async toImageData([ImageDataOptions](#imagedataoptions))
|
||||||
: Returns ImageData. Calls `decode()` automatically if PNG has not been decoded.
|
: Returns ImageData. Calls `toPNGData(options)`.
|
||||||
|
|
||||||
|
[PNGData](#pngdata) async toPNGData([ImageDataOptions](#imagedataoptions))
|
||||||
|
: Returns PNGData. Calls `decode()` automatically if PNG has not been decoded.
|
||||||
|
|
||||||
### ImageDataOptions
|
### ImageDataOptions
|
||||||
Object containing options to apply during `toImageData()`.
|
Object containing options to apply during `toImageData()`.
|
||||||
|
@ -97,3 +100,11 @@ let clip = {
|
||||||
h: 32,
|
h: 32,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### PNGData
|
||||||
|
Object containing the pixel data and row width.
|
||||||
|
|
||||||
|
| Property | Type
|
||||||
|
|----------|------------
|
||||||
|
| pixels | [Uint8ClampedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray) | RGBA pixel data.
|
||||||
|
| width | Number | Width of each row of pixel data.
|
||||||
|
|
12
src/index.js
12
src/index.js
|
@ -294,7 +294,7 @@ class IndexedPNG {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
async toImageData(options) {
|
async toPNGData(options) {
|
||||||
const palette = options.palette || this.decodedPalette
|
const palette = options.palette || this.decodedPalette
|
||||||
if (!this.decodedPixels) {
|
if (!this.decodedPixels) {
|
||||||
await this.decode()
|
await this.decode()
|
||||||
|
@ -322,7 +322,7 @@ class IndexedPNG {
|
||||||
pixels[i++] = palette[index+3]
|
pixels[i++] = palette[index+3]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new ImageData(pixels, options.clip.w)
|
return { pixels: pixels, width: options.clip.w }
|
||||||
} else {
|
} else {
|
||||||
// Allocate RGBA buffer
|
// Allocate RGBA buffer
|
||||||
const pixels = new Uint8ClampedArray(this.decodedPixels.length * 4)
|
const pixels = new Uint8ClampedArray(this.decodedPixels.length * 4)
|
||||||
|
@ -334,8 +334,14 @@ class IndexedPNG {
|
||||||
pixels[j++] = palette[index+2] // B
|
pixels[j++] = palette[index+2] // B
|
||||||
pixels[j++] = palette[index+3] // A
|
pixels[j++] = palette[index+3] // A
|
||||||
}
|
}
|
||||||
return new ImageData(pixels, this.width)
|
return { pixels: pixels, width: this.width }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async toImageData(options) {
|
||||||
|
let data = await this.toPNGData(options)
|
||||||
|
return new ImageData(data.pixels, data.width)
|
||||||
}
|
}
|
||||||
|
|
||||||
async decode() {
|
async decode() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user