import * as structs from '../const/structs' ; import { NF_MASKED , PALETTE_SIZE , PALETTE_ALPHA_INDEX , RGB_SIZE , RGBA_SIZE } from '../const/constants' ; param buffer param texture returns export const buildTexture = ( buffer , texture ) : Uint8ClampedArray => { const textureArea = texture . width * texture . height ; const isTextureMasked = texture . flags & NF_MASKED ; const textureData = new Uint8Array ( buffer , texture . index , textureArea ) ; const palette = new Uint8Array ( buffer , texture . index + textureArea , PALETTE_SIZE ) ; const alphaColor = palette . slice ( PALETTE_ALPHA_INDEX , PALETTE_ALPHA_INDEX + RGB_SIZE ) ; const imageBuffer = new Uint8ClampedArray ( textureArea * RGBA_SIZE ) ; for ( let i = 0 ; i < textureData . length ; i ++ ) { const item = textureData [ i ] ; const paletteOffset = item * RGB_SIZE ; const pixelOffset = i * RGBA_SIZE ; const isAlphaColor = palette [ paletteOffset + 0 ] === alphaColor [ 0 ] && palette [ paletteOffset + 1 ] === alphaColor [ 1 ] && palette [ paletteOffset + 2 ] === alphaColor [ 2 ] ; if ( isTextureMasked && isAlphaColor ) { imageBuffer [ pixelOffset + 0 ] = 0 ; imageBuffer [ pixelOffset + 1 ] = 0 ; imageBuffer [ pixelOffset + 2 ] = 0 ; imageBuffer [ pixelOffset + 3 ] = 0 ; } else { imageBuffer [ pixelOffset + 0 ] = palette [ paletteOffset + 0 ] ; imageBuffer [ pixelOffset + 1 ] = palette [ paletteOffset + 1 ] ; imageBuffer [ pixelOffset + 2 ] = palette [ paletteOffset + 2 ] ; imageBuffer [ pixelOffset + 3 ] = 255 ; } } return imageBuffer ; } ;