1
0
mirror of https://github.com/kettek/png-js synced 2025-03-14 06:24:30 -07:00

Add clipping functionality to toImageData

This commit is contained in:
kts of kettek (Gliese) 2019-01-15 02:45:38 -08:00
parent cedcb60245
commit 919494ddbc

View File

@ -299,6 +299,31 @@ class IndexedPNG {
if (!this.decodedPixels) {
await this.decode()
}
if (options.clip) {
// Ensure some sane defaults
if (options.clip.x == undefined) options.clip.x = 0
if (options.clip.y == undefined) options.clip.y = 0
if (options.clip.w == undefined) options.clip.w = this.width - options.clip.x
if (options.clip.h == undefined) options.clip.h = this.height - options.clip.y
// Now check for user errors.
if (options.clip.x < 0 || options.clip.x >= this.width) throw new Error("clip.x is out of bounds")
if (options.clip.y < 0 || options.clip.y >= this.height) throw new Error("clip.y is out of bounds")
if (options.clip.w <= 0 || options.clip.w > this.width) throw new Error("clip.w is out of bounds")
if (options.clip.h <= 0 || options.clip.h > this.height) throw new Error("clip.h is out of bounds")
// Now we can get our clipped array.
const pixels = new Uint8ClampedArray(options.clip.w*options.clip.h * 4)
for (let x = 0; x < options.clip.w; x++) {
for (let y = 0; y < options.clip.h; y++) {
let i = (x + y * options.clip.w) * 4
let index = this.decodedPixels[(x + options.clip.x) + ( (y + options.clip.y) * this.width)] * 4
pixels[i++] = palette[index]
pixels[i++] = palette[index+1]
pixels[i++] = palette[index+2]
pixels[i++] = palette[index+3]
}
}
return new ImageData(pixels, options.clip.w)
} else {
// Allocate RGBA buffer
const pixels = new Uint8ClampedArray(this.decodedPixels.length * 4)
let j = 0
@ -309,9 +334,9 @@ class IndexedPNG {
pixels[j++] = palette[index+2] // B
pixels[j++] = palette[index+3] // A
}
console.log(pixels)
return new ImageData(pixels, this.width)
}
}
async decode() {
this.decodedPalette = this.decodePalette()