{"version":3,"file":"Gamma.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":";;;;;AAKA,MAAM,QAAQ;AAQd,MAAa,qBAAoC,EAC/C,OAAO;CAAC;CAAG;CAAG;CAAE,EACjB;;;;;;;;;;AAWD,IAAa,QAAb,cAA2B,WAAwC;CAkBjE,oBAAoB;AAClB,SAAO;;CAGT,YAAY,UAAkC,EAAE,EAAE;AAChD,QAAM,QAAQ;AACd,OAAK,QACH,QAAQ,SAEN,KAAK,YACL,SAAS,MAAM,QAAQ;;;;;;;;CAS7B,UAAU,EAAE,WAAW,EAAE,UAA4B;EACnD,MAAM,QAAQ,KAAK,OACjB,OAAO,IAAI,MAAM,IACjB,OAAO,IAAI,MAAM,IACjB,OAAO,IAAI,MAAM;AAEnB,MAAI,CAAC,KAAK,UACR,MAAK,YAAY;GACf,GAAG,IAAI,WAAW,IAAI;GACtB,GAAG,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