{"version":3,"file":"Gamma.min.mjs","names":[],"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   */\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"],"mappings":"kRAKA,MAAM,EAAQ,QAqBd,IAAa,EAAb,cAA2B,CAAA,CAkBzB,mBAAA,CACE,OAAO,EAGT,YAAY,EAAkC,EAAA,CAAA,CAC5C,MAAM,EAAA,CACN,KAAK,MACH,EAAQ,OAEN,KAAK,YACL,SAAS,MAAM,QAAA,CASrB,UAAA,CAAY,UAAA,CAAW,KAAE,IAAA,CACvB,IAAM,EAAQ,KAAK,MACjB,EAAO,EAAI,EAAM,GACjB,EAAO,EAAI,EAAM,GACjB,EAAO,EAAI,EAAM,GAEd,KAAK,YACR,KAAK,UAAY,CACf,EAAG,IAAI,WAAW,IAAA,CAClB,EAAG,IAAI,WAAW,IAAA,CAClB,EAAG,IAAI,WAAW,IAAA,CAAA,EAMtB,IAAM,EAAM,KAAK,UACjB,IAAK,IAAI,E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