UNPKG

portable-image

Version:

Image data structure that supports RGB/RGBA/indexed color and arbitrary channels as well as blitters and web's ImageData output.

1,472 lines (1,117 loc) 113 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.PortableImage = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; function Animation() { this.name = '' ; this.loop = false ; this.startFrame = 0 ; this.endFrame = 0 ; //this.frameIndexes = [] ; } module.exports = Animation ; },{}],2:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; /* Autoplay an animation. */ function Animator( sprite , params = {} ) { this.sprite = sprite ; this.startFrame = + params.startFrame || 0 ; this.endFrame = params.endFrame ?? sprite.frames.length ; this.useCache = !! params.useCache ; this.scaleX = params.scaleX ?? params.scale ?? 1 ; this.scaleY = params.scaleY ?? params.scale ?? 1 ; this.blitParams = { scaleX: this.scaleX , scaleY: this.scaleY } ; this.x = + params.x || 0 ; this.y = + params.y || 0 ; this.imageData = this.sprite.prepareImageData( this.blitParams ) ; this.ctx = params.ctx ; this.running = false ; this.frameIndex = 0 ; } module.exports = Animator ; Animator.prototype.start = function() { if ( this.running ) { return ; } this.running = true ; this.runLoop() ; } ; Animator.prototype.stop = function() { this.running = false ; } ; Animator.prototype.runLoop = function() { if ( ! this.running ) { return ; } //console.log( "******* about to render frame #" + this.frameIndex ) ; var imageData , frame = this.sprite.frames[ this.frameIndex ] ; if ( this.useCache ) { if ( ! frame.imageDataCache ) { frame.updateImageData( null , this.blitParams ) ; } imageData = frame.imageDataCache ; } else { if ( this.imageData ) { frame.updateImageData( this.imageData , this.blitParams ) ; } else { this.imageData = frame.createImageData( this.blitParams ) ; } imageData = this.imageData ; } this.ctx.putImageData( imageData , this.x , this.y ) ; this.frameIndex ++ ; if ( this.frameIndex >= this.endFrame ) { this.frameIndex = this.startFrame ; } setTimeout( () => this.runLoop() , frame.duration ) ; } ; },{}],3:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; /* A Sprite is a sort-of 2-dimensional array of images, with frames as the top-header and layers as the left-header. A Cell is an element of this 2-dimensional array. */ function Cell( params = {} ) { this.imageIndex = + params.imageIndex || 0 ; // The index of the image stored in Sprite.images this.x = + params.x || 0 ; this.y = + params.y || 0 ; } module.exports = Cell ; },{}],4:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; /* Params: channels: the channels, default to [ 'red' , 'green' , 'blue' , 'alpha' ] or PortableImage.RGBA indexed: (boolean) it uses a palette, up to 256 entries, each pixel is a 1-Byte index palette: (array of array of integers) force indexed a pass an array of array of channel value */ function ChannelDef( params = {} ) { this.channels = Array.isArray( params.channels ) ? params.channels : ChannelDef.RGBA ; this.indexed = params.indexed || Array.isArray( params.palette ) ; this.bytesPerPixel = this.indexed ? 1 : this.channels.length ; this.palette = this.indexed ? [] : null ; if ( Array.isArray( params.palette ) ) { this.setPalette( params.palette ) ; } this.channelIndex = {} ; for ( let i = 0 ; i < this.channels.length ; i ++ ) { this.channelIndex[ this.channels[ i ] ] = i ; } this.isRgbCompatible = this.channels.length >= 3 && this.channels[ 0 ] === 'red' && this.channels[ 1 ] === 'green' && this.channels[ 2 ] === 'blue' ; this.isRgbaCompatible = this.channels.length >= 4 && this.isRgbCompatible && this.channels[ 3 ] === 'alpha' ; this.isRgb = this.isRgbCompatible && this.channels.length === 3 ; this.isRgba = this.isRgbaCompatible && this.channels.length === 4 ; this.isGrayCompatible = this.channels.length >= 1 && this.channels[ 0 ] === 'gray' ; this.isGrayAlphaCompatible = this.channels.length >= 2 && this.isGrayCompatible && this.channels[ 1 ] === 'alpha' ; this.isGray = this.isGrayCompatible && this.channels.length === 1 ; this.isGrayAlpha = this.isGrayAlphaCompatible && this.channels.length === 2 ; } module.exports = ChannelDef ; const Mapping = ChannelDef.Mapping = require( './Mapping.js' ) ; ChannelDef.Mapping = Mapping ; ChannelDef.DirectChannelMapping = Mapping.DirectChannelMapping ; ChannelDef.DirectChannelMappingWithDefault = Mapping.DirectChannelMappingWithDefault ; ChannelDef.MatrixChannelMapping = Mapping.MatrixChannelMapping ; ChannelDef.compositing = require( './compositing.js' ) ; ChannelDef.RGB = [ 'red' , 'green' , 'blue' ] ; ChannelDef.RGBA = [ 'red' , 'green' , 'blue' , 'alpha' ] ; ChannelDef.GRAY = [ 'gray' ] ; ChannelDef.GRAY_ALPHA = [ 'gray' , 'alpha' ] ; ChannelDef.prototype.setPalette = function( palette ) { if ( ! this.indexed ) { throw new Error( "This is not an indexed image" ) ; } this.palette.length = 0 ; for ( let index = 0 ; index < palette.length ; index ++ ) { this.setPaletteEntry( index , palette[ index ] ) ; } } ; ChannelDef.prototype.setPaletteEntry = function( index , entry ) { if ( this.isRgb || this.isRgba ) { return this.setPaletteColor( index , entry ) ; } if ( ! this.indexed ) { throw new Error( "This is not an indexed image" ) ; } if ( ! entry ) { return ; } var currentEntry = this.palette[ index ] ; if ( ! currentEntry ) { currentEntry = this.palette[ index ] = [] ; } if ( Array.isArray( entry ) ) { for ( let i = 0 ; i < this.channels.length ; i ++ ) { currentEntry[ i ] = entry[ i ] ?? 0 ; } } else if ( typeof entry === 'object' ) { for ( let i = 0 ; i < this.channels.length ; i ++ ) { currentEntry[ i ] = entry[ this.channels[ i ] ] ?? 0 ; } } } ; const LESSER_BYTE_MASK = 0xff ; ChannelDef.prototype.setPaletteColor = function( index , color ) { if ( ! this.indexed ) { throw new Error( "This is not an indexed image" ) ; } if ( ! color ) { return ; } var currentColor = this.palette[ index ] ; if ( ! currentColor ) { currentColor = this.palette[ index ] = [] ; } if ( Array.isArray( color ) ) { currentColor[ 0 ] = color[ 0 ] ?? 0 ; currentColor[ 1 ] = color[ 1 ] ?? 0 ; currentColor[ 2 ] = color[ 2 ] ?? 0 ; if ( this.isRgba ) { currentColor[ 3 ] = color[ 3 ] ?? 255 ; } } else if ( typeof color === 'object' ) { currentColor[ 0 ] = color.R ?? color.r ?? 0 ; currentColor[ 1 ] = color.G ?? color.g ?? 0 ; currentColor[ 2 ] = color.B ?? color.b ?? 0 ; if ( this.isRgba ) { currentColor[ 3 ] = color.A ?? color.a ?? 255 ; } } else if ( typeof color === 'string' && color[ 0 ] === '#' ) { color = color.slice( 1 ) ; if ( color.length === 3 ) { color = color[ 0 ] + color[ 0 ] + color[ 1 ] + color[ 1 ] + color[ 2 ] + color[ 2 ] ; } let code = Number.parseInt( color , 16 ) ; if ( color.length === 6 ) { currentColor[ 0 ] = ( code >> 16 ) & LESSER_BYTE_MASK ; currentColor[ 1 ] = ( code >> 8 ) & LESSER_BYTE_MASK ; currentColor[ 2 ] = code & LESSER_BYTE_MASK ; if ( this.isRgba ) { currentColor[ 3 ] = 255 ; } } else if ( color.length === 8 ) { currentColor[ 0 ] = ( code >> 24 ) & LESSER_BYTE_MASK ; currentColor[ 1 ] = ( code >> 16 ) & LESSER_BYTE_MASK ; currentColor[ 2 ] = ( code >> 8 ) & LESSER_BYTE_MASK ; if ( this.isRgba ) { currentColor[ 3 ] = code & LESSER_BYTE_MASK ; } } } } ; ChannelDef.prototype.hasSamePalette = function( channelDef ) { if ( ! this.palette || ! channelDef.palette ) { return false ; } if ( this.palette.length !== channelDef.palette.length ) { return false ; } for ( let index = 0 ; index < this.palette.length ; index ++ ) { let values = this.palette[ index ] ; for ( let c = 0 ; c < values.length ; c ++ ) { if ( values[ c ] !== channelDef.palette[ index ][ c ] ) { return false ; } } } return true ; } ; ChannelDef.DEFAULT_CHANNEL_VALUES = { red: 0 , green: 0 , blue: 0 , alpha: 255 } ; ChannelDef.prototype.getAutoMappingToChannels = function( toChannels , defaultChannelValues = ChannelDef.DEFAULT_CHANNEL_VALUES ) { var matrix = new Array( toChannels.length * 2 ) ; for ( let index = 0 ; index < toChannels.length ; index ++ ) { let channel = toChannels[ index ] ; if ( Object.hasOwn( this.channelIndex , channel ) ) { matrix[ index * 2 ] = this.channelIndex[ channel ] ; matrix[ index * 2 + 1 ] = null ; } else { matrix[ index * 2 ] = null ; matrix[ index * 2 + 1 ] = defaultChannelValues[ channel ] ?? 0 ; } } return new ChannelDef.DirectChannelMappingWithDefault( matrix ) ; } ; ChannelDef.getAutoMapping = function( fromChannels , toChannels , defaultChannelValues = ChannelDef.DEFAULT_CHANNEL_VALUES ) { var matrix = new Array( toChannels.length * 2 ) ; for ( let index = 0 ; index < toChannels.length ; index ++ ) { let channel = toChannels[ index ] ; let indexOf = fromChannels.indexOf( channel ) ; if ( indexOf >= 0 ) { matrix[ index * 2 ] = indexOf ; matrix[ index * 2 + 1 ] = null ; } else { matrix[ index * 2 ] = null ; matrix[ index * 2 + 1 ] = defaultChannelValues[ channel ] ?? 0 ; } } return new ChannelDef.DirectChannelMappingWithDefault( matrix ) ; } ; // Simple color matcher ChannelDef.prototype.getClosestPaletteIndex = ( channelValues ) => { var cMax = Math.min( this.channels.length , channelValues.length ) , minDist = Infinity , minIndex = 0 ; for ( let index = 0 ; index < this.palette.length ; index ++ ) { let dist = 0 ; for ( let c = 0 ; c < cMax ; c ++ ) { let delta = this.palette[ index ][ c ] - channelValues[ c ] ; dist += delta * delta ; if ( dist < minDist ) { minDist = dist ; minIndex = index ; } } } return minIndex ; } ; },{"./Mapping.js":8,"./compositing.js":10}],5:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; function Frame( params = {} , sprite = null ) { Object.defineProperty( this , 'sprite' , { configurable: true , value: sprite } ) ; this.duration = params.duration ; // in ms this.cells = [] ; // Cells are ordered so that indexes are the than Sprite.layers indexes this.imageDataCache = null ; } module.exports = Frame ; const Sprite = require( './Sprite.js' ) ; const Layer = require( './Layer.js' ) ; const Image = require( './Image.js' ) ; Frame.prototype.setParent = function( sprite ) { Object.defineProperty( this , 'sprite' , { value: sprite } ) ; } ; Frame.prototype.clearCache = function() { this.imageDataCache = null ; } ; Frame.prototype.addCell = function( cell ) { this.cells.push( cell ) ; return this.cells.length - 1 ; } ; Frame.prototype.toImage = function( ImageClass = Image ) { var image = new Image( { width: this.sprite.width , height: this.sprite.height , channelDef: this.sprite.channelDef } ) ; for ( let cell of this.cells ) { let cellImage = this.sprite.images[ cell.imageIndex ] ; cellImage.copyTo( image , { compositing: Image.compositing.binaryOver , x: cell.x , y: cell.y } ) ; console.log( "Copy from/to:" , image , cellImage , " --- cell: " , cell ) ; } return image ; } ; // Just for consistency... Frame.prototype.prepareImageData = function( params = {} ) { return this.sprite.prepareImageData( params ) ; } ; Frame.prototype.createImageData = function( params = {} ) { var imageData = this.sprite.prepareImageData( params ) ; this.updateImageData( imageData , params , true ) ; return imageData ; } ; Frame.prototype.updateImageData = function( imageData , params , noClear = false ) { params = params ? Object.assign( {} , params ) : {} ; if ( ! imageData ) { if ( this.imageDataCache ) { imageData = this.imageDataCache ; if ( ! noClear ) { imageData.data.fill( 0 ) ; } } else { imageData = this.imageDataCache = this.sprite.prepareImageData( params ) ; } } else if ( ! noClear ) { imageData.data.fill( 0 ) ; } for ( let cell of this.cells ) { let cellImage = this.sprite.images[ cell.imageIndex ] ; // .innerX/Y are like .x/y except that it is multiplied by scaling, // it's important to use that for Cells instead of .x/y, or the Cells' positions would be wrong params.innerX = cell.x ; params.innerY = cell.y ; params.compositing = Image.compositing.binaryOver ; cellImage.updateImageData( imageData , params , true ) ; } } ; },{"./Image.js":6,"./Layer.js":7,"./Sprite.js":9}],6:[function(require,module,exports){ (function (Buffer){(function (){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; /* Params: width: image width in pixel height: image height in pixel channelDef: pass an already existing channel definition channels: the channels, default to [ 'red' , 'green' , 'blue' , 'alpha' ] or ChannelDef.RGBA indexed: (boolean) it uses a palette, up to 256 entries, each pixel is a 1-Byte index palette: (array of array of integers) force indexed a pass an array of array of channel value pixelBuffer: (Buffer or Uint8Array) the buffer containing all the pixel data */ function Image( params = {} ) { this.width = params.width ; this.height = params.height ; this.channelDef = params.channelDef ?? new ChannelDef( params ) ; this.pixelBuffer = null ; if ( params.pixelBuffer ) { if ( params.pixelBuffer instanceof Buffer ) { if ( params.pixelBuffer.length !== this.width * this.height * this.channelDef.bytesPerPixel ) { throw new Error( "Provided pixel Buffer mismatch the expected size (should be exactly width * height * bytesPerPixel)" ) ; } this.pixelBuffer = params.pixelBuffer ; } else if ( params.pixelBuffer instanceof Uint8Array ) { if ( params.pixelBuffer.length !== this.width * this.height * this.channelDef.bytesPerPixel ) { throw new Error( "Provided pixel Uint8Array buffer mismatch the expected size (should be exactly width * height * bytesPerPixel)" ) ; } this.pixelBuffer = Buffer.from( params.pixelBuffer ) ; } else { throw new Error( "Provided pixel buffer is not a Buffer or a Uint8Array" ) ; } } else { this.pixelBuffer = Buffer.allocUnsafe( this.width * this.height * this.channelDef.bytesPerPixel ) ; } } module.exports = Image ; const ChannelDef = Image.ChannelDef = Image.prototype.ChannelDef = require( './ChannelDef.js' ) ; Image.compositing = require( './compositing.js' ) ; Image.prototype.setPalette = function( palette ) { this.channelDef.setPalette( palette ) ; } ; Image.prototype.setPaletteEntry = function( index , entry ) { this.channelDef.setPaletteEntry( index , entry ) ; } ; Image.prototype.setPaletteColor = function( index , color ) { this.channelDef.setPaletteColor( index , color ) ; } ; Image.prototype.hasSamePalette = function( image ) { return this.channelDef.hasSamePalette( image.channelDef ) ; } ; // Create the mapping to another Image Image.prototype.getAutoMappingTo = function( toImage , defaultChannelValues = ChannelDef.DEFAULT_CHANNEL_VALUES ) { return this.channelDef.getAutoMappingToChannels( toImage.channelDef.channels , defaultChannelValues ) ; } ; /* Copy to another Image instance. */ Image.prototype.copyTo = function( image , params = {} ) { var mapping = params.mapping , scaleX = params.scaleX ?? params.scale ?? 1 , scaleY = params.scaleY ?? params.scale ?? 1 ; if ( ! mapping ) { mapping = this.getAutoMappingTo( image ) ; } let src = { buffer: this.pixelBuffer , width: this.width , height: this.height , bytesPerPixel: this.channelDef.bytesPerPixel , //x: params.x < 0 ? - params.x / scaleX : 0 , x: Math.max( 0 , - ( ( + params.x || 0 ) / scaleX + ( + params.innerX || 0 ) ) ) , //y: params.y < 0 ? - params.y / scaleY : 0 , y: Math.max( 0 , - ( ( + params.y || 0 ) / scaleY + ( + params.innerY || 0 ) ) ) , endX: this.width , endY: this.height , channels: this.channelDef.channels.length , scaleX , scaleY , mapping , compositing: params.compositing || null } ; let dst = { buffer: image.pixelBuffer , width: image.width , height: image.height , bytesPerPixel: image.channelDef.bytesPerPixel , //x: params.x > 0 ? params.x : 0 , x: Math.max( 0 , ( + params.x || 0 ) + ( + params.innerX || 0 ) * scaleX ) , //y: params.y > 0 ? params.y : 0 , y: Math.max( 0 , ( + params.y || 0 ) + ( + params.innerY || 0 ) * scaleY ) , endX: image.width , endY: image.height , channels: image.channelDef.channels.length } ; //console.log( "### Mapping: " , dst.mapping ) ; if ( src.compositing ) { if ( this.channelDef.indexed ) { if ( image.channelDef.indexed ) { if ( ! this.hasSamePalette( image ) ) { throw new Error( "Uncompatible palettes are not supported yet" ) ; } src.palette = this.channelDef.palette ; if ( src.compositing.id === 'binaryOver' && this.channelDef.channelIndex.alpha !== undefined ) { src.alphaChannel = this.channelDef.channelIndex.alpha ; Image.fullIndexedBlitWithTransparency( src , dst ) ; } else { throw new Error( "Copy indexed to indexed with compositing is not supported yet, except for 'binaryOver' mode" ) ; } } else { src.palette = this.channelDef.palette ; Image.compositingBlitFromIndexed( src , dst ) ; } } else { if ( image.channelDef.indexed ) { throw new Error( "Copy to indexed portable image is not supported yet" ) ; } Image.compositingBlit( src , dst ) ; } } else { if ( this.channelDef.indexed ) { if ( image.channelDef.indexed ) { if ( ! this.hasSamePalette( image ) ) { throw new Error( "Uncompatible palettes are not supported yet" ) ; } src.mapping = ChannelDef.Mapping.INDEXED_TO_INDEXED ; Image.blit( src , dst ) ; } else { src.palette = this.channelDef.palette ; Image.blitFromIndexed( src , dst ) ; } } else { if ( image.channelDef.indexed ) { throw new Error( "Copy to indexed portable image is not supported yet" ) ; } Image.blit( src , dst ) ; } } } ; // Prepare, but do not copy any pixels Image.prototype.prepareImageData = function( params = {} ) { var scaleX = params.scaleX ?? params.scale ?? 1 , scaleY = params.scaleY ?? params.scale ?? 1 ; return new ImageData( this.width * scaleX , this.height * scaleY ) ; } ; // Create an ImageData and copy pixels to it Image.prototype.createImageData = function( params = {} ) { var imageData = this.prepareImageData( params ) ; this.updateImageData( imageData , params , true ) ; return imageData ; } ; // Copy pixels to an existing ImageData Image.prototype.updateImageData = function( imageData , params = {} , noClear = false ) { var mapping = params.mapping , scaleX = params.scaleX ?? params.scale ?? 1 , scaleY = params.scaleY ?? params.scale ?? 1 ; if ( ! noClear ) { imageData.data.fill( 0 ) ; } //console.log( "Image.prototype.updateImageData() params:" , params ) ; if ( ! mapping ) { if ( imageData.width === this.width && imageData.height === this.height && ! params.x && ! params.y && scaleX === 1 && scaleY === 1 ) { if ( this.channelDef.indexed ) { if ( this.channelDef.isRgbaCompatible ) { return this.isoIndexedRgbaCompatibleToRgbaBlit( imageData.data ) ; } if ( this.channelDef.isRgbCompatible ) { return this.isoIndexedRgbCompatibleToRgbaBlit( imageData.data ) ; } } else { if ( this.channelDef.isRgbaCompatible ) { return this.isoRgbaCompatibleToRgbaBlit( imageData.data ) ; } if ( this.channelDef.isRgbCompatible ) { return this.isoRgbCompatibleToRgbaBlit( imageData.data ) ; } } } if ( this.channelDef.isRgbaCompatible ) { mapping = ChannelDef.Mapping.RGBA_COMPATIBLE_TO_RGBA ; } else if ( this.channelDef.isRgbCompatible ) { mapping = ChannelDef.Mapping.RGB_COMPATIBLE_TO_RGBA ; } else if ( this.channelDef.isGrayAlphaCompatible ) { mapping = ChannelDef.Mapping.GRAY_ALPHA_COMPATIBLE_TO_RGBA ; } else if ( this.channelDef.isGrayCompatible ) { mapping = ChannelDef.Mapping.GRAY_COMPATIBLE_TO_RGBA ; } else { throw new Error( "Mapping required for image that are not RGB/RGBA/Grayscale/Grayscale+Alpha compatible" ) ; } } //console.warn( "Mapping:" , mapping ) ; let src = { buffer: this.pixelBuffer , width: this.width , height: this.height , bytesPerPixel: this.channelDef.bytesPerPixel , //x: params.x < 0 ? - params.x / scaleX : 0 , x: Math.max( 0 , - ( ( + params.x || 0 ) / scaleX + ( + params.innerX || 0 ) ) ) , //y: params.y < 0 ? - params.y / scaleY : 0 , y: Math.max( 0 , - ( ( + params.y || 0 ) / scaleY + ( + params.innerY || 0 ) ) ) , endX: this.width , endY: this.height , channels: this.channelDef.channels.length , scaleX , scaleY , mapping , compositing: params.compositing || null } ; let dst = { buffer: imageData.data , width: imageData.width , height: imageData.height , bytesPerPixel: 4 , //x: params.x > 0 ? params.x : 0 , x: Math.max( 0 , ( + params.x || 0 ) + ( + params.innerX || 0 ) * scaleX ) , //y: params.y > 0 ? params.y : 0 , y: Math.max( 0 , ( + params.y || 0 ) + ( + params.innerY || 0 ) * scaleY ) , endX: imageData.width , endY: imageData.height , channels: 4 } ; if ( src.compositing ) { if ( this.channelDef.indexed ) { src.palette = this.channelDef.palette ; Image.compositingBlitFromIndexed( src , dst ) ; } else { Image.compositingBlit( src , dst ) ; } } else { if ( this.channelDef.indexed ) { src.palette = this.channelDef.palette ; Image.blitFromIndexed( src , dst ) ; } else { Image.blit( src , dst ) ; } } } ; /* Perform a regular blit, copying a rectangle are a the src to a rectangulare are of the dst. src, dst: * buffer: array-like * width,height: geometry stored in the array-like * bytesPerPixel * x,y: coordinate where to start copying (included) * endX,endY: coordinate where to stop copying (excluded) src only: * scaleX,scaleY: drawing scale (nearest) * mapping: an instance of Mapping, that maps the channels from src to dst */ Image.blit = function( src , dst ) { //console.warn( ".blit() used" , src , dst ) ; var blitWidth = Math.min( dst.endX - dst.x , ( src.endX - src.x ) * src.scaleX ) , blitHeight = Math.min( dst.endY - dst.y , ( src.endY - src.y ) * src.scaleY ) ; for ( let yOffset = 0 ; yOffset < blitHeight ; yOffset ++ ) { for ( let xOffset = 0 ; xOffset < blitWidth ; xOffset ++ ) { let iDst = ( ( dst.y + yOffset ) * dst.width + ( dst.x + xOffset ) ) * dst.bytesPerPixel ; let iSrc = ( Math.floor( src.y + yOffset / src.scaleY ) * src.width + Math.floor( src.x + xOffset / src.scaleX ) ) * src.bytesPerPixel ; src.mapping.map( src , dst , iSrc , iDst ) ; } } } ; /* Perform a blit, but the source pixel is an index, that will be substituted by the relevant source palette. Same arguments than .blit(), plus: src only: * palette: an array of array of values */ Image.blitFromIndexed = function( src , dst ) { //console.warn( ".blitFromIndexed() used" , src , dst ) ; var blitWidth = Math.min( dst.endX - dst.x , ( src.endX - src.x ) * src.scaleX ) , blitHeight = Math.min( dst.endY - dst.y , ( src.endY - src.y ) * src.scaleY ) ; for ( let yOffset = 0 ; yOffset < blitHeight ; yOffset ++ ) { for ( let xOffset = 0 ; xOffset < blitWidth ; xOffset ++ ) { let iDst = ( ( dst.y + yOffset ) * dst.width + ( dst.x + xOffset ) ) * dst.bytesPerPixel ; let iSrc = ( Math.floor( src.y + yOffset / src.scaleY ) * src.width + Math.floor( src.x + xOffset / src.scaleX ) ) * src.bytesPerPixel ; let channelValues = src.palette[ src.buffer[ iSrc ] ] ; src.mapping.map( src , dst , 0 , iDst , channelValues ) ; } } } ; /* Perform a blit, but with compositing (alpha-blending, etc). src only: * compositing: a compositing object, having a method "alpha" and "channel" */ Image.compositingBlit = function( src , dst ) { //console.warn( ".compositingBlit() used" , src , dst ) ; var blitWidth = Math.min( dst.endX - dst.x , ( src.endX - src.x ) * src.scaleX ) , blitHeight = Math.min( dst.endY - dst.y , ( src.endY - src.y ) * src.scaleY ) ; for ( let yOffset = 0 ; yOffset < blitHeight ; yOffset ++ ) { for ( let xOffset = 0 ; xOffset < blitWidth ; xOffset ++ ) { let iDst = ( ( dst.y + yOffset ) * dst.width + ( dst.x + xOffset ) ) * dst.bytesPerPixel ; let iSrc = ( Math.floor( src.y + yOffset / src.scaleY ) * src.width + Math.floor( src.x + xOffset / src.scaleX ) ) * src.bytesPerPixel ; src.mapping.compose( src , dst , iSrc , iDst , src.compositing ) ; } } } ; /* Perform a blit, but with compositing (alpha-blending, etc) + the source pixel is an index, that will be substituted by the relevant source palette. Same arguments than .blit(), plus: src only: * palette: an array of array of values * compositing: a compositing object, having a method "alpha" and "channel" */ Image.compositingBlitFromIndexed = function( src , dst ) { //console.warn( ".compositingBlitFromIndexed() used" , src , dst ) ; var blitWidth = Math.min( dst.endX - dst.x , ( src.endX - src.x ) * src.scaleX ) , blitHeight = Math.min( dst.endY - dst.y , ( src.endY - src.y ) * src.scaleY ) ; for ( let yOffset = 0 ; yOffset < blitHeight ; yOffset ++ ) { for ( let xOffset = 0 ; xOffset < blitWidth ; xOffset ++ ) { let iDst = ( ( dst.y + yOffset ) * dst.width + ( dst.x + xOffset ) ) * dst.bytesPerPixel ; let iSrc = ( Math.floor( src.y + yOffset / src.scaleY ) * src.width + Math.floor( src.x + xOffset / src.scaleX ) ) * src.bytesPerPixel ; let channelValues = src.palette[ src.buffer[ iSrc ] ] ; src.mapping.compose( src , dst , 0 , iDst , src.compositing , channelValues ) ; } } } ; /* Perform a blit, copying palette indexes, ignoring index-color association, except for source transparency. The transparency is binary, it is either fully transparent (alpha=0) or fully opaque (alpha>0). Same arguments than .blit(), plus: src only: * palette: an array of array of values * alphaChannel: the index of the alpha channel (3 for RGBA, 1 for grayscale+alpha) */ Image.fullIndexedBlitWithTransparency = function( src , dst ) { //console.warn( ".fullIndexedBlitWithTransparency() used" , src , dst ) ; var blitWidth = Math.min( dst.endX - dst.x , ( src.endX - src.x ) * src.scaleX ) , blitHeight = Math.min( dst.endY - dst.y , ( src.endY - src.y ) * src.scaleY ) ; for ( let yOffset = 0 ; yOffset < blitHeight ; yOffset ++ ) { for ( let xOffset = 0 ; xOffset < blitWidth ; xOffset ++ ) { let iDst = ( ( dst.y + yOffset ) * dst.width + ( dst.x + xOffset ) ) * dst.bytesPerPixel ; let iSrc = ( Math.floor( src.y + yOffset / src.scaleY ) * src.width + Math.floor( src.x + xOffset / src.scaleX ) ) * src.bytesPerPixel ; // If alpha > 0 ... if ( src.palette[ src.buffer[ iSrc ] ][ src.alphaChannel ] ) { dst.buffer[ iDst ] = src.buffer[ iSrc ] ; } } } } ; // Optimized Blit for RGB-compatible to RGBA Image.prototype.isoRgbCompatibleToRgbaBlit = function( dst ) { //console.warn( ".isoRgbCompatibleToRgbaBlit() used" , dst ) ; for ( let i = 0 , imax = this.width * this.height ; i < imax ; i ++ ) { let iSrc = i * this.channelDef.bytesPerPixel ; let iDst = i * 4 ; dst[ iDst ] = this.pixelBuffer[ iSrc ] ; // Red dst[ iDst + 1 ] = this.pixelBuffer[ iSrc + 1 ] ; // Green dst[ iDst + 2 ] = this.pixelBuffer[ iSrc + 2 ] ; // Blue dst[ iDst + 3 ] = 255 ; // Alpha } } ; // Optimized Blit for RGBA-compatible to RGBA Image.prototype.isoRgbaCompatibleToRgbaBlit = function( dst ) { //console.warn( ".isoRgbaCompatibleToRgbaBlit() used" , dst , this ) ; for ( let i = 0 , imax = this.width * this.height ; i < imax ; i ++ ) { let iSrc = i * this.channelDef.bytesPerPixel ; let iDst = i * 4 ; dst[ iDst ] = this.pixelBuffer[ iSrc ] ; // Red dst[ iDst + 1 ] = this.pixelBuffer[ iSrc + 1 ] ; // Green dst[ iDst + 2 ] = this.pixelBuffer[ iSrc + 2 ] ; // Blue dst[ iDst + 3 ] = this.pixelBuffer[ iSrc + 3 ] ; // Alpha } } ; // Optimized Blit for Indexed RGB-compatible to RGBA Image.prototype.isoIndexedRgbCompatibleToRgbaBlit = function( dst ) { //console.warn( ".isoIndexedRgbCompatibleToRgbaBlit() used" , dst ) ; for ( let i = 0 , imax = this.width * this.height ; i < imax ; i ++ ) { let iSrc = i * this.channelDef.bytesPerPixel ; let iDst = i * 4 ; let paletteEntry = this.channelDef.palette[ this.pixelBuffer[ iSrc ] ] ; dst[ iDst ] = paletteEntry[ 0 ] ; // Red dst[ iDst + 1 ] = paletteEntry[ 1 ] ; // Green dst[ iDst + 2 ] = paletteEntry[ 2 ] ; // Blue dst[ iDst + 3 ] = 255 ; // Alpha } } ; // Optimized Blit for Indexed RGBA-compatible to RGBA Image.prototype.isoIndexedRgbaCompatibleToRgbaBlit = function( dst ) { //console.warn( ".isoIndexedRgbaCompatibleToRgbaBlit() used" , dst ) ; for ( let i = 0 , imax = this.width * this.height ; i < imax ; i ++ ) { let iSrc = i * this.channelDef.bytesPerPixel ; let iDst = i * 4 ; let paletteEntry = this.channelDef.palette[ this.pixelBuffer[ iSrc ] ] ; dst[ iDst ] = paletteEntry[ 0 ] ; // Red dst[ iDst + 1 ] = paletteEntry[ 1 ] ; // Green dst[ iDst + 2 ] = paletteEntry[ 2 ] ; // Blue dst[ iDst + 3 ] = paletteEntry[ 3 ] ; // Alpha } } ; Image.prototype.updateFromImageData = function( imageData , mapping ) { throw new Error( "Not coded!" ) ; // /!\ TODO /!\ /* if ( ! mapping ) { if ( this.channelDef.isRgbaCompatible ) { mapping = ChannelDef.Mapping.RGBA_COMPATIBLE_TO_RGBA ; } else if ( this.channelDef.isRgbCompatible ) { mapping = ChannelDef.Mapping.RGB_COMPATIBLE_TO_RGBA ; } else { throw new Error( "ChannelDef.Mapping required for image that are not RGB/RGBA compatible" ) ; } } if ( imageData.width !== this.width || imageData.height !== this.height ) { throw new Error( ".updateFromImageData(): width and/or height mismatch" ) ; } for ( let i = 0 , imax = this.width * this.height ; i < imax ; i ++ ) { let iDst = i * this.channelDef.bytesPerPixel ; let iSrc = i * 4 ; if ( this.channelDef.indexed ) { let channelValues = [] ; channelValues[ iDst + mapping[ 0 ] ] = imageData[ iSrc ] ; channelValues[ iDst + mapping[ 1 ] ] = imageData[ iSrc + 1 ] ; channelValues[ iDst + mapping[ 2 ] ] = imageData[ iSrc + 2 ] ; channelValues[ iDst + mapping[ 3 ] ] = imageData[ iSrc + 3 ] ; this.pixelBuffer[ iDst ] = this.getClosestPaletteIndex( channelValues ) ; } this.pixelBuffer[ iDst + mapping[ 0 ] ] = imageData[ iSrc ] ; this.pixelBuffer[ iDst + mapping[ 1 ] ] = imageData[ iSrc + 1 ] ; this.pixelBuffer[ iDst + mapping[ 2 ] ] = imageData[ iSrc + 2 ] ; this.pixelBuffer[ iDst + mapping[ 3 ] ] = imageData[ iSrc + 3 ] ; } */ } ; }).call(this)}).call(this,require("buffer").Buffer) },{"./ChannelDef.js":4,"./compositing.js":10,"buffer":13}],7:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; function Layer( name , params = {} ) { this.name = name || '' ; this.visible = params.visible ?? true ; this.compositing = params.compositing ?? null ; // Compositing mode this.opacity = params.opacity ?? 1 ; this.order = params.order ?? 0 ; // The order of the layer, rendered from lower to greater } module.exports = Layer ; },{}],8:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; // Base class function Mapping( matrix , alphaChannelDst ) { this.matrix = matrix ; this.alphaChannelDst = alphaChannelDst ?? null ; this.composeChannelOrder = null ; if ( this.alphaChannelDst === null ) { this.compose = this.map ; } else { this.composeChannelOrder = [ this.alphaChannelDst ] ; for ( let i = 0 ; i < this.dstChannels ; i ++ ) { if ( i !== this.alphaChannelDst ) { this.composeChannelOrder.push( i ) ; } } } } Mapping.prototype.map = function() {} ; Mapping.prototype.compose = function() {} ; module.exports = Mapping ; function clampUint8( value ) { return Math.max( 0 , Math.min( 255 , Math.round( value ) ) ) ; } function normalizedToUint8( value ) { return Math.max( 0 , Math.min( 255 , Math.round( 255 * value ) ) ) ; } function uint8ToNormalized( value ) { return Math.max( 0 , Math.min( 1 , value / 255 ) ) ; } const NO_COMPOSITING = { alpha: ( alphaSrc /*, alphaDst */ ) => alphaSrc , channel: ( alphaSrc , alphaDst , channelSrc /*, channelDst */ ) => channelSrc } ; /* Direct mapping of dst to src, each dst channel is copied from a src channel. Each entry is a src channel index. */ function DirectChannelMapping( matrix , alphaChannelDst ) { this.dstChannels = matrix.length ; Mapping.call( this , matrix , alphaChannelDst ) ; } DirectChannelMapping.prototype = Object.create( Mapping.prototype ) ; DirectChannelMapping.prototype.constructor = DirectChannelMapping ; Mapping.DirectChannelMapping = DirectChannelMapping ; DirectChannelMapping.prototype.map = function( src , dst , iSrc , iDst , srcBuffer = src.buffer ) { for ( let cDst = 0 ; cDst < dst.channels ; cDst ++ ) { dst.buffer[ iDst + cDst ] = srcBuffer[ iSrc + this.matrix[ cDst ] ] ; } } ; DirectChannelMapping.prototype.compose = function( src , dst , iSrc , iDst , compositing = NO_COMPOSITING , srcBuffer = src.buffer ) { let alphaDst = dst.buffer[ iDst + this.alphaChannelDst ] / 255 ; let alphaSrc = 1 ; for ( let cDst of this.composeChannelOrder ) { if ( cDst === this.alphaChannelDst ) { alphaSrc = srcBuffer[ iSrc + this.matrix[ cDst ] ] / 255 ; dst.buffer[ iDst + cDst ] = normalizedToUint8( compositing.alpha( alphaSrc , alphaDst ) ) ; } else { dst.buffer[ iDst + cDst ] = normalizedToUint8( compositing.channel( alphaSrc , alphaDst , srcBuffer[ iSrc + this.matrix[ cDst ] ] / 255 , dst.buffer[ iDst + cDst ] / 255 ) ) ; } } } ; /* Direct mapping of dst to src, each dst channel is copied from a src channel OR have a default value. There are 2 entries per dst channel, the first one is a src channel index, the second one is a default value. The default value is used unless its value is null. */ function DirectChannelMappingWithDefault( matrix , alphaChannelDst ) { this.dstChannels = Math.floor( matrix.length / 2 ) ; Mapping.call( this , matrix , alphaChannelDst ) ; } DirectChannelMappingWithDefault.prototype = Object.create( Mapping.prototype ) ; DirectChannelMappingWithDefault.prototype.constructor = DirectChannelMappingWithDefault ; Mapping.DirectChannelMappingWithDefault = DirectChannelMappingWithDefault ; DirectChannelMappingWithDefault.prototype.map = function( src , dst , iSrc , iDst , srcBuffer = src.buffer ) { for ( let cDst = 0 ; cDst < dst.channels ; cDst ++ ) { dst.buffer[ iDst + cDst ] = this.matrix[ cDst * 2 + 1 ] ?? srcBuffer[ iSrc + this.matrix[ cDst * 2 ] ] ; } } ; DirectChannelMappingWithDefault.prototype.compose = function( src , dst , iSrc , iDst , compositing = NO_COMPOSITING , srcBuffer = src.buffer ) { let alphaDst = dst.buffer[ iDst + this.alphaChannelDst ] / 255 ; let alphaSrc = 1 ; for ( let cDst of this.composeChannelOrder ) { if ( cDst === this.alphaChannelDst ) { alphaSrc = ( this.matrix[ cDst * 2 + 1 ] ?? srcBuffer[ iSrc + this.matrix[ cDst * 2 ] ] ) / 255 ; dst.buffer[ iDst + cDst ] = normalizedToUint8( compositing.alpha( alphaSrc , alphaDst ) ) ; } else { dst.buffer[ iDst + cDst ] = normalizedToUint8( compositing.channel( alphaSrc , alphaDst , ( this.matrix[ cDst * 2 + 1 ] ?? srcBuffer[ iSrc + this.matrix[ cDst * 2 ] ] ) / 255 , dst.buffer[ iDst + cDst ] / 255 ) ) ; } } } ; /* Matrix mapping of the dst to src, each dst channel is composed by all src channels + one additional value. There are ( srcChannelsUsed + 1 ) entries per dst channel, the last one is the additionnal value. */ function MatrixChannelMapping( matrix , srcChannelsUsed , alphaChannelDst ) { this.dstChannels = Math.floor( matrix.length / ( srcChannelsUsed + 1 ) ) ; this.srcChannelsUsed = srcChannelsUsed ; Mapping.call( this , matrix , alphaChannelDst ) ; } MatrixChannelMapping.prototype = Object.create( Mapping.prototype ) ; MatrixChannelMapping.prototype.constructor = MatrixChannelMapping ; Mapping.MatrixChannelMapping = MatrixChannelMapping ; MatrixChannelMapping.prototype.map = function( src , dst , iSrc , iDst , srcBuffer = src.buffer ) { let matrixIndex = 0 ; for ( let cDst = 0 ; cDst < dst.channels ; cDst ++ ) { let value = 0 ; for ( let cSrc = 0 ; cSrc < this.srcChannelsUsed ; cSrc ++ ) { value += srcBuffer[ iSrc + cSrc ] * this.matrix[ matrixIndex ++ ] ; } value += this.matrix[ matrixIndex ++ ] ; // This is the additionnal value dst.buffer[ iDst + cDst ] = clampUint8( value ) ; } } ; MatrixChannelMapping.prototype.compose = function( src , dst , iSrc , iDst , compositing = NO_COMPOSITING , srcBuffer = src.buffer ) { let alphaDst = dst.buffer[ iDst + this.alphaChannelDst ] / 255 ; let alphaSrc = 1 ; for ( let cDst of this.composeChannelOrder ) { let matrixIndex = cDst * ( this.srcChannelsUsed + 1 ) ; let value = 0 ; for ( let cSrc = 0 ; cSrc < this.srcChannelsUsed ; cSrc ++ ) { value += srcBuffer[ iSrc + cSrc ] * this.matrix[ matrixIndex ++ ] ; } value += this.matrix[ matrixIndex ++ ] ; // This is the additionnal value value = uint8ToNormalized( value ) ; if ( cDst === this.alphaChannelDst ) { // Always executed at the first loop iteration dst.buffer[ iDst + cDst ] = normalizedToUint8( compositing.alpha( value , alphaDst ) ) ; } else { dst.buffer[ iDst + cDst ] = normalizedToUint8( compositing.channel( alphaSrc , alphaDst , value , dst.buffer[ iDst + cDst ] / 255 ) ) ; } } } ; /* Built-in channel mapping. Should come after prototype definition, because of *.prototype = Object.create(...) */ Mapping.INDEXED_TO_INDEXED = new DirectChannelMapping( [ 0 ] ) ; Mapping.RGBA_COMPATIBLE_TO_RGBA = new DirectChannelMapping( [ 0 , 1 , 2 , 3 ] , 3 ) ; Mapping.RGB_COMPATIBLE_TO_RGBA = new DirectChannelMappingWithDefault( [ 0 , null , 1 , null , 2 , null , null , 255 ] , 3 ) ; Mapping.GRAY_ALPHA_COMPATIBLE_TO_RGBA = new DirectChannelMapping( [ 0 , 0 , 0 , 1 ] , 3 ) ; Mapping.GRAY_COMPATIBLE_TO_RGBA = new DirectChannelMappingWithDefault( [ 0 , null , 0 , null , 0 , null , null , 255 ] , 3 ) ; Mapping.RGBA_COMPATIBLE_TO_GRAY_ALPHA = new MatrixChannelMapping( [ 1 / 3 , 1 / 3 , 1 / 3 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , 4 , 1 ) ; Mapping.RGB_COMPATIBLE_TO_GRAY_ALPHA = new MatrixChannelMapping( [ 1 / 3 , 1 / 3 , 1 / 3 , 0 , 0 , 0 , 0 , 255 ] , 3 , 1 ) ; },{}],9:[function(require,module,exports){ /* Portable Image Copyright (c) 2024 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; /* A “Sprite” is a multi-layered image, with multiple frames and animation referencing those frames. */ function Sprite( params = {} ) { // Clipping size this.width = params.width ; this.height = params.height ; this.channelDef = params.channelDef ?? new ChannelDef( params ) ; this.images = [] ; // Image instances this.layers = [] ; // Layer instances this.frames = [] ; // Frame instances this.animations = [] ; // Animations instances this.orderedLayers = [] ; // Store layer indexes this.animationByName = new Map() ; // Animation name to animation index } module.exports = Sprite ; const ChannelDef = Sprite.ChannelDef = Sprite.prototype.ChannelDef = require( './ChannelDef.js' ) ; const Image = Sprite.Image = Sprite.prototype.Image = require( './Image.js' ) ; const Frame = Sprite.Frame = Sprite.prototype.Frame = require( './Frame.js' ) ; const Layer = Sprite.Layer = Sprite.prototype.Layer = require( './Layer.js' ) ; const Cell = Sprite.Cell = Sprite.prototype.Cell = require( '.