fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 5.05 kB
Source Map (JSON)
{"version":3,"file":"Gamma.min.mjs","sources":["../../../src/filters/Gamma.ts"],"sourcesContent":["import { BaseFilter } from './BaseFilter';\nimport { classRegistry } from '../ClassRegistry';\nimport { fragmentSource } from './shaders/gamma';\nimport type { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs';\n\nconst GAMMA = 'Gamma' as const;\n\nexport type GammaInput = [number, number, number];\n\nexport type GammaOwnProps = {\n gamma: GammaInput;\n};\n\nexport const gammaDefaultValues: GammaOwnProps = {\n gamma: [1, 1, 1],\n};\n\n/**\n * Gamma filter class\n * @example\n * const filter = new Gamma({\n * gamma: [1, 0.5, 2.1]\n * });\n * object.filters.push(filter);\n * object.applyFilters();\n */\nexport class Gamma extends BaseFilter<typeof GAMMA, GammaOwnProps> {\n /**\n * Gamma array value, from 0.01 to 2.2.\n * @param {Array} gamma\n * @default\n */\n declare gamma: GammaOwnProps['gamma'];\n declare rgbValues?: {\n r: Uint8Array;\n g: Uint8Array;\n b: Uint8Array;\n };\n\n static type = GAMMA;\n\n static defaults = gammaDefaultValues;\n\n static uniformLocations = ['uGamma'];\n\n getFragmentSource() {\n return fragmentSource;\n }\n\n constructor(options: { gamma?: GammaInput } = {}) {\n super(options);\n this.gamma =\n options.gamma ||\n ((\n this.constructor as typeof Gamma\n ).defaults.gamma.concat() as GammaInput);\n }\n\n /**\n * Apply the Gamma operation to a Uint8Array representing the pixels of an image.\n *\n * @param {Object} options\n * @param {ImageData} options.imageData The Uint8Array to be filtered.\n */\n applyTo2d({ imageData: { data } }: T2DPipelineState) {\n const gamma = this.gamma,\n rInv = 1 / gamma[0],\n gInv = 1 / gamma[1],\n bInv = 1 / gamma[2];\n\n if (!this.rgbValues) {\n this.rgbValues = {\n r: new Uint8Array(256),\n g: new Uint8Array(256),\n b: new Uint8Array(256),\n };\n }\n\n // This is an optimization - pre-compute a look-up table for each color channel\n // instead of performing these pow calls for each pixel in the image.\n const rgb = this.rgbValues;\n for (let i = 0; i < 256; i++) {\n rgb.r[i] = Math.pow(i / 255, rInv) * 255;\n rgb.g[i] = Math.pow(i / 255, gInv) * 255;\n rgb.b[i] = Math.pow(i / 255, bInv) * 255;\n }\n for (let i = 0; i < data.length; i += 4) {\n data[i] = rgb.r[data[i]];\n data[i + 1] = rgb.g[data[i + 1]];\n data[i + 2] = rgb.b[data[i + 2]];\n }\n }\n\n /**\n * Send data from this filter to its shader program's uniforms.\n *\n * @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader.\n * @param {Object} uniformLocations A map of string uniform names to WebGLUniformLocation objects\n */\n sendUniformData(\n gl: WebGLRenderingContext,\n uniformLocations: TWebGLUniformLocationMap,\n ) {\n gl.uniform3fv(uniformLocations.uGamma, this.gamma);\n }\n\n isNeutralState() {\n const { gamma } = this;\n return gamma[0] === 1 && gamma[1] === 1 && gamma[2] === 1;\n }\n\n toObject(): { type: typeof GAMMA; gamma: GammaInput } {\n return {\n type: GAMMA,\n gamma: this.gamma.concat() as GammaInput,\n };\n }\n}\n\nclassRegistry.setClass(Gamma);\n"],"names":["GAMMA","gammaDefaultValues","gamma","Gamma","BaseFilter","getFragmentSource","fragmentSource","constructor","options","arguments","length","undefined","super","this","defaults","concat","applyTo2d","_ref","imageData","data","rInv","gInv","bInv","rgbValues","r","Uint8Array","g","b","rgb","i","Math","pow","sendUniformData","gl","uniformLocations","uniform3fv","uGamma","isNeutralState","toObject","type","_defineProperty","classRegistry","setClass"],"mappings":"sPAKA,MAAMA,EAAQ,QAQDC,EAAoC,CAC/CC,MAAO,CAAC,EAAG,EAAG,IAYT,MAAMC,UAAcC,EAmBzBC,iBAAAA,GACE,OAAOC,CACT,CAEAC,WAAAA,GAAkD,IAAtCC,EAA+BC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAG,CAAA,EAC5CG,MAAMJ,GACNK,KAAKX,MACHM,EAAQN,OA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