{"version":3,"file":"Pixelate.min.mjs","names":[],"sources":["../../../src/filters/Pixelate.ts"],"sourcesContent":["import { BaseFilter } from './BaseFilter';\nimport type { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { fragmentSource } from './shaders/pixelate';\n\nexport type PixelateOwnProps = {\n  blocksize: number;\n};\n\nexport const pixelateDefaultValues: PixelateOwnProps = {\n  blocksize: 4,\n};\n\n/**\n * Pixelate filter class\n * @example\n * const filter = new Pixelate({\n *   blocksize: 8\n * });\n * object.filters.push(filter);\n * object.applyFilters();\n */\nexport class Pixelate extends BaseFilter<'Pixelate', PixelateOwnProps> {\n  declare blocksize: PixelateOwnProps['blocksize'];\n\n  static type = 'Pixelate';\n\n  static defaults = pixelateDefaultValues;\n\n  static uniformLocations = ['uBlocksize'];\n\n  /**\n   * Apply the Pixelate operation to a Uint8ClampedArray representing the pixels of an image.\n   *\n   * @param {Object} options\n   * @param {ImageData} options.imageData The Uint8ClampedArray to be filtered.\n   */\n  applyTo2d({ imageData: { data, width, height } }: T2DPipelineState) {\n    for (let i = 0; i < height; i += this.blocksize) {\n      for (let j = 0; j < width; j += this.blocksize) {\n        const index = i * 4 * width + j * 4;\n        const r = data[index];\n        const g = data[index + 1];\n        const b = data[index + 2];\n        const a = data[index + 3];\n\n        for (let _i = i; _i < Math.min(i + this.blocksize, height); _i++) {\n          for (let _j = j; _j < Math.min(j + this.blocksize, width); _j++) {\n            const index = _i * 4 * width + _j * 4;\n            data[index] = r;\n            data[index + 1] = g;\n            data[index + 2] = b;\n            data[index + 3] = a;\n          }\n        }\n      }\n    }\n  }\n\n  /**\n   * Indicate when the filter is not gonna apply changes to the image\n   **/\n  isNeutralState() {\n    return this.blocksize === 1;\n  }\n\n  protected getFragmentSource(): string {\n    return fragmentSource;\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.uniform1f(uniformLocations.uBlocksize, this.blocksize);\n  }\n}\n\nclassRegistry.setClass(Pixelate);\n"],"mappings":"qRAsBA,IAAa,EAAb,cAA8B,CAAA,CAe5B,UAAA,CAAY,UAAA,CAAW,KAAE,EAAA,MAAM,EAAA,OAAO,IAAA,CACpC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAQ,GAAK,KAAK,UACpC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAO,GAAK,KAAK,UAAW,CAC9C,IAAM,EAAY,EAAJ,EAAQ,EAAY,EAAJ,EACxB,EAAI,EAAK,GACT,EAAI,EAAK,EAAQ,GACjB,EAAI,EAAK,EAAQ,GACjB,EAAI,EAAK,EAAQ,GAEvB,IAAK,IAAI,EAAK,EAAG,EAAK,KAAK,IAAI,EAAI,KAAK,UAAW,EAAA,CAAS,IAC1D,IAAK,IAAI,EAAK,EAAG,EAAK,KAAK,IAAI,EAAI,KAAK,UAAW,EAAA,CAAQ,IAAM,CAC/D,IAAM,EAAa,EAAL,EAAS,EAAa,EAAL,EAC/B,EAAK,GAAS,EACd,EAAK,EAAQ,GAAK,EAClB,EAAK,EAAQ,GAAK,EAClB,EAAK,EAAQ,GAAK,IAU5B,gBAAA,CACE,OAAO,KAAK,YAAc,EAG5B,mBAAA,CACE,OAAO,EAST,gBACE,EACA,EAAA,CAEA,EAAG,UAAU,EAAiB,WAAY,KAAK,UAAA,GAAA,EAAA,EAvD1C,OAAO,WAAA,CAAA,EAAA,EAEP,WAlB8C,CACrD,UAAW,EAAA,CAAA,CAAA,EAAA,EAmBJ,mBAAmB,CAAC,aAAA,CAAA,CAuD7B,EAAc,SAAS,EAAA,CAAA,OAAA,KAAA"}