Add isIndexed

main
kts of kettek 2024-02-18 13:54:44 -08:00
parent ffeab51e36
commit 309aebdc8e
4 changed files with 10 additions and 6 deletions

View File

@ -66,17 +66,17 @@
canvas = new Canvas(png.width, png.height) canvas = new Canvas(png.width, png.height)
if (png.pixelBitlength === 32) { if (png.colorType === 6 || png.colorType === 2) { // RGBA / RGB
for (let i = 0; i < png.decodedPixels.length; i += 4) { for (let i = 0; i < png.decodedPixels.length; i += 4) {
let y = Math.floor(i / (png.width * 4)) let y = Math.floor(i / (png.width * 4))
let x = (i / 4) % png.width let x = (i / 4) % png.width
canvas.setPixelRGBA(x, y, png.decodedPixels[i], png.decodedPixels[i+1], png.decodedPixels[i+2], png.decodedPixels[i+3]) canvas.setPixelRGBA(x, y, png.decodedPixels[i], png.decodedPixels[i+1], png.decodedPixels[i+2], png.decodedPixels[i+3])
} }
} else if (png.pixelBitlength === 24) { canvas.isIndexed = false
// RGB } else if (png.colorType === 3) { // indexed
} else if (png.pixelBitlength === 8) {
canvas.setPaletteFromUint8Array(png.decodedPalette) canvas.setPaletteFromUint8Array(png.decodedPalette)
canvas.setPixelsFromUint8Array(png.decodedPixels) canvas.setPixelsFromUint8Array(png.decodedPixels)
canvas.isIndexed = true
} else { } else {
error = "pixel format" error = "pixel format"
error2 = "unsupported pixel format" error2 = "unsupported pixel format"

View File

@ -5,6 +5,7 @@ export class Canvas {
height: number height: number
palette: Uint32Array // 32-bit RGBA palette palette: Uint32Array // 32-bit RGBA palette
pixels: Uint8Array // 8-bit indices into the palette pixels: Uint8Array // 8-bit indices into the palette
public isIndexed: boolean = false
canvas: HTMLCanvasElement canvas: HTMLCanvasElement
imageData: ImageData imageData: ImageData
@ -18,8 +19,9 @@ export class Canvas {
} }
// fromData creates a new Canvas instance from the provided data. // fromData creates a new Canvas instance from the provided data.
static fromData({width, height, palette, pixels}: {width: number, height: number, palette: Uint32Array, pixels: Uint8Array}): Canvas { static fromData({width, height, isIndexed, palette, pixels}: {width: number, height: number, isIndexed: boolean, palette: Uint32Array, pixels: Uint8Array}): Canvas {
let canvas = new Canvas(width, height) let canvas = new Canvas(width, height)
canvas.isIndexed = isIndexed
canvas.palette = palette canvas.palette = palette
canvas.pixels = pixels canvas.pixels = pixels
canvas.imageData = new ImageData(width, height) canvas.imageData = new ImageData(width, height)

View File

@ -45,6 +45,7 @@ export class CopyPaste {
currentCanvas = Canvas.fromData({ currentCanvas = Canvas.fromData({
width: canvas.width, width: canvas.width,
height: canvas.height, height: canvas.height,
isIndexed: canvas.isIndexed,
palette: canvas.palette, palette: canvas.palette,
pixels: canvas.pixels pixels: canvas.pixels
}) })
@ -67,6 +68,7 @@ export class CopyPaste {
let canvas = Canvas.fromData({ let canvas = Canvas.fromData({
width: currentCanvas.width, width: currentCanvas.width,
height: currentCanvas.height, height: currentCanvas.height,
isIndexed: currentCanvas.isIndexed,
palette: currentCanvas.palette, palette: currentCanvas.palette,
pixels: currentCanvas.pixels pixels: currentCanvas.pixels
}) })

View File

@ -24,7 +24,7 @@ export class IndexedPNG {
public width: number public width: number
public height: number public height: number
private bits: number private bits: number
private colorType: number public colorType: number
private compressionMethod: number private compressionMethod: number
private filterMethod: number private filterMethod: number
private interlaceMethod: number private interlaceMethod: number