diff --git a/README.md b/README.md index d968bf1..5c6a571 100644 --- a/README.md +++ b/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()`. [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 Object containing options to apply during `toImageData()`. @@ -97,3 +100,11 @@ let clip = { 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. diff --git a/src/index.js b/src/index.js index 5b0ade2..241ed91 100644 --- a/src/index.js +++ b/src/index.js @@ -294,7 +294,7 @@ class IndexedPNG { return ret; } - async toImageData(options) { + async toPNGData(options) { const palette = options.palette || this.decodedPalette if (!this.decodedPixels) { await this.decode() @@ -322,7 +322,7 @@ class IndexedPNG { pixels[i++] = palette[index+3] } } - return new ImageData(pixels, options.clip.w) + return { pixels: pixels, width: options.clip.w } } else { // Allocate RGBA buffer const pixels = new Uint8ClampedArray(this.decodedPixels.length * 4) @@ -334,8 +334,14 @@ class IndexedPNG { pixels[j++] = palette[index+2] // B 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() {