{"version":3,"file":"Gamma.min.mjs","sources":["../../../src/filters/Gamma.ts"],"sourcesContent":["import type { TClassProperties } from '../typedefs';\nimport { BaseFilter } from './BaseFilter';\nimport type { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { fragmentSource } from './shaders/gamma';\nexport type GammaInput = [number, number, number];\n\nexport const gammaDefaultValues: Partial<TClassProperties<Gamma>> = {\n  mainParameter: 'gamma',\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 {\n  /**\n   * Gamma array value, from 0.01 to 2.2.\n   * @param {Array} gamma\n   * @default\n   */\n  declare gamma: GammaInput;\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  getFragmentSource() {\n    return fragmentSource;\n  }\n\n  constructor({ gamma = [1, 1, 1], ...options }: { gamma?: GammaInput } = {}) {\n    super(options);\n    this.gamma = gamma;\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   * Return WebGL uniform locations for this filter's shader.\n   *\n   * @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader.\n   * @param {WebGLShaderProgram} program This filter's compiled shader program.\n   */\n  getUniformLocations(\n    gl: WebGLRenderingContext,\n    program: WebGLProgram\n  ): TWebGLUniformLocationMap {\n    return {\n      uGamma: gl.getUniformLocation(program, 'uGamma'),\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\nclassRegistry.setClass(Gamma);\n"],"names":["gammaDefaultValues","mainParameter","gamma","Gamma","BaseFilter","getFragmentSource","fragmentSource","constructor","_ref","arguments","length","undefined","super","_objectWithoutProperties","_excluded","this","applyTo2d","_ref2","imageData","data","rInv","gInv","bInv","rgbValues","r","Uint8Array","g","b","rgb","i","Math","pow","getUniformLocations","gl","program","uGamma","getUniformLocation","sendUniformData","uniformLocations","uniform3fv","_defineProperty","classRegistry","setClass"],"mappings":"qSAOaA,EAAuD,CAClEC,cAAe,QACfC,MAAO,CAAC,EAAG,EAAG,IAYT,MAAMC,UAAcC,EAiBzBC,iBAAAA,GACE,OAAOC,CACT,CAEAC,WAAAA,GAA4E,IAAAC,EAAAC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAJ,CAAE,GAA9DP,MAAEA,EAAQ,CAAC,EAAG,EAAG,IAAwCM,EACnEI,MADyCC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