{"version":3,"file":"Blur.mjs","names":[],"sources":["../../../src/filters/Blur.ts"],"sourcesContent":["import { BaseFilter } from './BaseFilter';\nimport type {\n  TWebGLPipelineState,\n  T2DPipelineState,\n  TWebGLUniformLocationMap,\n} from './typedefs';\nimport { isWebGLPipelineState } from './utils';\nimport { classRegistry } from '../ClassRegistry';\nimport { fragmentSource } from './shaders/blur';\n\ntype BlurOwnProps = {\n  blur: number;\n};\n\nexport const blurDefaultValues: BlurOwnProps = {\n  blur: 0,\n};\n\n/**\n * Blur filter class\n * @example\n * const filter = new Blur({\n *   blur: 0.5\n * });\n * object.filters.push(filter);\n * object.applyFilters();\n * canvas.renderAll();\n */\nexport class Blur extends BaseFilter<'Blur', BlurOwnProps> {\n  /**\n   * blur value, in percentage of image dimensions.\n   * specific to keep the image blur constant at different resolutions\n   * range between 0 and 1.\n   * @type Number\n   */\n  declare blur: BlurOwnProps['blur'];\n\n  declare horizontal: boolean;\n  declare aspectRatio: number;\n\n  static type = 'Blur';\n\n  static defaults = blurDefaultValues;\n\n  static uniformLocations = ['uDelta'];\n\n  getFragmentSource(): string {\n    return fragmentSource;\n  }\n\n  applyTo(options: TWebGLPipelineState | T2DPipelineState) {\n    if (isWebGLPipelineState(options)) {\n      // this aspectRatio is used to give the same blur to vertical and horizontal\n      this.aspectRatio = options.sourceWidth / options.sourceHeight;\n      options.passes++;\n      this._setupFrameBuffer(options);\n      this.horizontal = true;\n      this.applyToWebGL(options);\n      this._swapTextures(options);\n      this._setupFrameBuffer(options);\n      this.horizontal = false;\n      this.applyToWebGL(options);\n      this._swapTextures(options);\n    } else {\n      this.applyTo2d(options);\n    }\n  }\n\n  applyTo2d({ imageData: { data, width, height } }: T2DPipelineState) {\n    // this code mimic the shader for output consistency\n    // it samples 31 pixels across the image over a distance that depends from the blur value.\n    this.aspectRatio = width / height;\n    this.horizontal = true;\n    let blurValue = this.getBlurValue() * width;\n    const imageData = new Uint8ClampedArray(data);\n    const samples = 15;\n    const bytesInRow = 4 * width;\n    for (let i = 0; i < data.length; i += 4) {\n      let r = 0.0,\n        g = 0.0,\n        b = 0.0,\n        a = 0.0,\n        totalA = 0;\n      const minIRow = i - (i % bytesInRow);\n      const maxIRow = minIRow + bytesInRow;\n      // for now let's keep noise out of the way\n      // let pixelOffset = 0;\n      // const offset = Math.random() * 3;\n      // if (offset > 2) {\n      //   pixelOffset = 4;\n      // } else if (offset < 1) {\n      //   pixelOffset = -4;\n      // }\n      for (let j = -samples + 1; j < samples; j++) {\n        const percent = j / samples;\n        const distance = Math.floor(blurValue * percent) * 4;\n        const weight = 1 - Math.abs(percent);\n        let sampledPixel = i + distance; // + pixelOffset;\n        // try to implement edge mirroring\n        if (sampledPixel < minIRow) {\n          sampledPixel = minIRow;\n        } else if (sampledPixel > maxIRow) {\n          sampledPixel = maxIRow;\n        }\n        const localAlpha = data[sampledPixel + 3] * weight;\n        r += data[sampledPixel] * localAlpha;\n        g += data[sampledPixel + 1] * localAlpha;\n        b += data[sampledPixel + 2] * localAlpha;\n        a += localAlpha;\n        totalA += weight;\n      }\n      imageData[i] = r / a;\n      imageData[i + 1] = g / a;\n      imageData[i + 2] = b / a;\n      imageData[i + 3] = a / totalA;\n    }\n    this.horizontal = false;\n    blurValue = this.getBlurValue() * height;\n    for (let i = 0; i < imageData.length; i += 4) {\n      let r = 0.0,\n        g = 0.0,\n        b = 0.0,\n        a = 0.0,\n        totalA = 0;\n      const minIRow = i % bytesInRow;\n      const maxIRow = imageData.length - bytesInRow + minIRow;\n      // for now let's keep noise out of the way\n      // let pixelOffset = 0;\n      // const offset = Math.random() * 3;\n      // if (offset > 2) {\n      //   pixelOffset = bytesInRow;\n      // } else if (offset < 1) {\n      //   pixelOffset = -bytesInRow;\n      // }\n      for (let j = -samples + 1; j < samples; j++) {\n        const percent = j / samples;\n        const distance = Math.floor(blurValue * percent) * bytesInRow;\n        const weight = 1 - Math.abs(percent);\n        let sampledPixel = i + distance; // + pixelOffset;\n        // try to implement edge mirroring\n        if (sampledPixel < minIRow) {\n          sampledPixel = minIRow;\n        } else if (sampledPixel > maxIRow) {\n          sampledPixel = maxIRow;\n        }\n        const localAlpha = imageData[sampledPixel + 3] * weight;\n        r += imageData[sampledPixel] * localAlpha;\n        g += imageData[sampledPixel + 1] * localAlpha;\n        b += imageData[sampledPixel + 2] * localAlpha;\n        a += localAlpha;\n        totalA += weight;\n      }\n      data[i] = r / a;\n      data[i + 1] = g / a;\n      data[i + 2] = b / a;\n      data[i + 3] = a / totalA;\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    const delta = this.chooseRightDelta();\n    gl.uniform2fv(uniformLocations.uDelta, delta);\n  }\n\n  isNeutralState() {\n    return this.blur === 0;\n  }\n\n  getBlurValue(): number {\n    let blurScale = 1;\n    const { horizontal, aspectRatio } = this;\n    if (horizontal) {\n      if (aspectRatio > 1) {\n        // image is wide, i want to shrink radius horizontal\n        blurScale = 1 / aspectRatio;\n      }\n    } else {\n      if (aspectRatio < 1) {\n        // image is tall, i want to shrink radius vertical\n        blurScale = aspectRatio;\n      }\n    }\n    return blurScale * this.blur * 0.12;\n  }\n\n  /**\n   * choose right value of image percentage to blur with\n   * @returns {Array} a numeric array with delta values\n   */\n  chooseRightDelta() {\n    const blur = this.getBlurValue();\n    return this.horizontal ? [blur, 0] : [0, blur];\n  }\n}\n\nclassRegistry.setClass(Blur);\n"],"mappings":";;;;;;AAcA,MAAa,oBAAkC,EAC7C,MAAM,GACP;;;;;;;;;;;AAYD,IAAa,OAAb,cAA0B,WAAiC;CAkBzD,oBAA4B;AAC1B,SAAO;;CAGT,QAAQ,SAAiD;AACvD,MAAI,qBAAqB,QAAQ,EAAE;AAEjC,QAAK,cAAc,QAAQ,cAAc,QAA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