OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
feat(image): add Phase 3 effects and filters
Augustus Otu5e397dc8477b
diff --git a/apps/image/src/effects/blend-modes.ts b/apps/image/src/effects/blend-modes.tsnew file mode 100644index 00000000..7ea012f3--- /dev/null+++ b/apps/image/src/effects/blend-modes.ts@@ -0,0 +1,533 @@+export type BlendMode =+ | 'normal'+ | 'dissolve'+ | 'darken'+ | 'multiply'+ | 'color-burn'+ | 'linear-burn'+ | 'darker-color'+ | 'lighten'+ | 'screen'+ | 'color-dodge'+ | 'linear-dodge'+ | 'lighter-color'+ | 'overlay'+ | 'soft-light'+ | 'hard-light'+ | 'vivid-light'+ | 'linear-light'+ | 'pin-light'+ | 'hard-mix'+ | 'difference'+ | 'exclusion'+ | 'subtract'+ | 'divide'+ | 'hue'+ | 'saturation'+ | 'color'+ | 'luminosity';++export interface BlendModeInfo {+ name: string;+ category: 'normal' | 'darken' | 'lighten' | 'contrast' | 'comparative' | 'component';+ description: string;+}++export const BLEND_MODE_INFO: Record<BlendMode, BlendModeInfo> = {+ normal: { name: 'Normal', category: 'normal', description: 'Edits or paints each pixel to make it the result color' },+ dissolve: { name: 'Dissolve', category: 'normal', description: 'Randomly replaces pixels with the blend color' },+ darken: { name: 'Darken', category: 'darken', description: 'Selects the darker of the base or blend color' },+ multiply: { name: 'Multiply', category: 'darken', description: 'Multiplies the base color by the blend color' },+ 'color-burn': { name: 'Color Burn', category: 'darken', description: 'Darkens to increase the contrast' },+ 'linear-burn': { name: 'Linear Burn', category: 'darken', description: 'Darkens by decreasing the brightness' },+ 'darker-color': { name: 'Darker Color', category: 'darken', description: 'Compares total channel values' },+ lighten: { name: 'Lighten', category: 'lighten', description: 'Selects the lighter of the base or blend color' },+ screen: { name: 'Screen', category: 'lighten', description: 'Multiplies the inverse of the colors' },+ 'color-dodge': { name: 'Color Dodge', category: 'lighten', description: 'Brightens to decrease the contrast' },+ 'linear-dodge': { name: 'Linear Dodge (Add)', category: 'lighten', description: 'Brightens by increasing the brightness' },+ 'lighter-color': { name: 'Lighter Color', category: 'lighten', description: 'Compares total channel values' },+ overlay: { name: 'Overlay', category: 'contrast', description: 'Multiplies or screens depending on the base color' },+ 'soft-light': { name: 'Soft Light', category: 'contrast', description: 'Darkens or lightens depending on the blend color' },+ 'hard-light': { name: 'Hard Light', category: 'contrast', description: 'Multiplies or screens depending on the blend color' },+ 'vivid-light': { name: 'Vivid Light', category: 'contrast', description: 'Burns or dodges by increasing or decreasing the contrast' },+ 'linear-light': { name: 'Linear Light', category: 'contrast', description: 'Burns or dodges by decreasing or increasing the brightness' },+ 'pin-light': { name: 'Pin Light', category: 'contrast', description: 'Replaces colors depending on the blend color' },+ 'hard-mix': { name: 'Hard Mix', category: 'contrast', description: 'Reduces colors to 8 colors' },+ difference: { name: 'Difference', category: 'comparative', description: 'Subtracts the darker color from the lighter color' },+ exclusion: { name: 'Exclusion', category: 'comparative', description: 'Similar to Difference but lower contrast' },+ subtract: { name: 'Subtract', category: 'comparative', description: 'Subtracts the blend color from the base color' },+ divide: { name: 'Divide', category: 'comparative', description: 'Divides the base color by the blend color' },+ hue: { name: 'Hue', category: 'component', description: 'Creates a result with the hue of the blend color' },+ saturation: { name: 'Saturation', category: 'component', description: 'Creates a result with the saturation of the blend color' },+ color: { name: 'Color', category: 'component', description: 'Creates a result with the hue and saturation of the blend color' },+ luminosity: { name: 'Luminosity', category: 'component', description: 'Creates a result with the luminosity of the blend color' },+};++function clamp(value: number): number {+ return Math.max(0, Math.min(255, value));+}++function blendNormal(_base: number, blend: number): number {+ return blend;+}++function blendDissolve(base: number, blend: number, opacity: number): number {+ return Math.random() < opacity ? blend : base;+}++function blendDarken(base: number, blend: number): number {+ return Math.min(base, blend);+}++function blendMultiply(base: number, blend: number): number {+ return (base * blend) / 255;+}++function blendColorBurn(base: number, blend: number): number {+ if (blend === 0) return 0;+ return clamp(255 - ((255 - base) * 255) / blend);+}++function blendLinearBurn(base: number, blend: number): number {+ return clamp(base + blend - 255);+}++function blendLighten(base: number, blend: number): number {+ return Math.max(base, blend);+}++function blendScreen(base: number, blend: number): number {+ return 255 - ((255 - base) * (255 - blend)) / 255;+}++function blendColorDodge(base: number, blend: number): number {+ if (blend === 255) return 255;+ return clamp((base * 255) / (255 - blend));+}++function blendLinearDodge(base: number, blend: number): number {+ return clamp(base + blend);+}++function blendOverlay(base: number, blend: number): number {+ if (base < 128) {+ return (2 * base * blend) / 255;+ }+ return 255 - (2 * (255 - base) * (255 - blend)) / 255;+}++function blendSoftLight(base: number, blend: number): number {+ if (blend < 128) {+ return base - ((255 - 2 * blend) * base * (255 - base)) / (255 * 255);+ }+ const d = base < 64 ? ((16 * base - 12 * 255) * base + 4 * 255) * base / (255 * 255) : Math.sqrt(base / 255) * 255;+ return base + ((2 * blend - 255) * (d - base)) / 255;+}++function blendHardLight(base: number, blend: number): number {+ if (blend < 128) {+ return (2 * base * blend) / 255;+ }+ return 255 - (2 * (255 - base) * (255 - blend)) / 255;+}++function blendVividLight(base: number, blend: number): number {+ if (blend < 128) {+ return blendColorBurn(base, 2 * blend);+ }+ return blendColorDodge(base, 2 * (blend - 128));+}++function blendLinearLight(base: number, blend: number): number {+ if (blend < 128) {+ return blendLinearBurn(base, 2 * blend);+ }+ return blendLinearDodge(base, 2 * (blend - 128));+}++function blendPinLight(base: number, blend: number): number {+ if (blend < 128) {+ return Math.min(base, 2 * blend);+ }+ return Math.max(base, 2 * (blend - 128));+}++function blendHardMix(base: number, blend: number): number {+ return blendVividLight(base, blend) < 128 ? 0 : 255;+}++function blendDifference(base: number, blend: number): number {+ return Math.abs(base - blend);+}++function blendExclusion(base: number, blend: number): number {+ return base + blend - (2 * base * blend) / 255;+}++function blendSubtract(base: number, blend: number): number {+ return clamp(base - blend);+}++function blendDivide(base: number, blend: number): number {+ if (blend === 0) return 255;+ return clamp((base * 256) / (blend + 1));+}++function rgbToHsl(r: number, g: number, b: number): [number, number, number] {+ r /= 255;+ g /= 255;+ b /= 255;++ const max = Math.max(r, g, b);+ const min = Math.min(r, g, b);+ const l = (max + min) / 2;++ if (max === min) {+ return [0, 0, l];+ }++ const d = max - min;+ const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);++ let h: number;+ switch (max) {+ case r:+ h = ((g - b) / d + (g < b ? 6 : 0)) / 6;+ break;+ case g:+ h = ((b - r) / d + 2) / 6;+ break;+ default:+ h = ((r - g) / d + 4) / 6;+ break;+ }++ return [h, s, l];+}++function hslToRgb(h: number, s: number, l: number): [number, number, number] {+ if (s === 0) {+ const gray = Math.round(l * 255);+ return [gray, gray, gray];+ }++ const hue2rgb = (p: number, q: number, t: number): number => {+ if (t < 0) t += 1;+ if (t > 1) t -= 1;+ if (t < 1 / 6) return p + (q - p) * 6 * t;+ if (t < 1 / 2) return q;+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;+ return p;+ };++ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;+ const p = 2 * l - q;++ return [+ Math.round(hue2rgb(p, q, h + 1 / 3) * 255),+ Math.round(hue2rgb(p, q, h) * 255),+ Math.round(hue2rgb(p, q, h - 1 / 3) * 255),+ ];+}++function blendHue(+ baseR: number, baseG: number, baseB: number,+ blendR: number, blendG: number, blendB: number+): [number, number, number] {+ const [h] = rgbToHsl(blendR, blendG, blendB);+ const [, s, l] = rgbToHsl(baseR, baseG, baseB);+ return hslToRgb(h, s, l);+}++function blendSaturation(+ baseR: number, baseG: number, baseB: number,+ blendR: number, blendG: number, blendB: number+): [number, number, number] {+ const [h, , l] = rgbToHsl(baseR, baseG, baseB);+ const [, s] = rgbToHsl(blendR, blendG, blendB);+ return hslToRgb(h, s, l);+}++function blendColor(+ baseR: number, baseG: number, baseB: number,+ blendR: number, blendG: number, blendB: number+): [number, number, number] {+ const [h, s] = rgbToHsl(blendR, blendG, blendB);+ const [, , l] = rgbToHsl(baseR, baseG, baseB);+ return hslToRgb(h, s, l);+}++function blendLuminosity(+ baseR: number, baseG: number, baseB: number,+ blendR: number, blendG: number, blendB: number+): [number, number, number] {+ const [h, s] = rgbToHsl(baseR, baseG, baseB);+ const [, , l] = rgbToHsl(blendR, blendG, blendB);+ return hslToRgb(h, s, l);+}++function getLuminance(r: number, g: number, b: number): number {+ return r * 0.299 + g * 0.587 + b * 0.114;+}++function blendDarkerColor(+ baseR: number, baseG: number, baseB: number,+ blendR: number, blendG: number, blendB: number+): [number, number, number] {+ const baseLum = getLuminance(baseR, baseG, baseB);+ const blendLum = getLuminance(blendR, blendG, blendB);+ return baseLum < blendLum ? [baseR, baseG, baseB] : [blendR, blendG, blendB];+}++function blendLighterColor(+ baseR: number, baseG: number, baseB: number,+ blendR: number, blendG: number, blendB: number+): [number, number, number] {+ const baseLum = getLuminance(baseR, baseG, baseB);+ const blendLum = getLuminance(blendR, blendG, blendB);+ return baseLum > blendLum ? [baseR, baseG, baseB] : [blendR, blendG, blendB];+}++export function blendPixel(+ baseR: number, baseG: number, baseB: number, baseA: number,+ blendR: number, blendG: number, blendB: number, blendA: number,+ mode: BlendMode,+ opacity: number = 1+): [number, number, number, number] {+ if (blendA === 0 || opacity === 0) {+ return [baseR, baseG, baseB, baseA];+ }++ const effectiveOpacity = (blendA / 255) * opacity;+ let resultR: number, resultG: number, resultB: number;++ switch (mode) {+ case 'normal':+ resultR = blendNormal(baseR, blendR);+ resultG = blendNormal(baseG, blendG);+ resultB = blendNormal(baseB, blendB);+ break;++ case 'dissolve':+ resultR = blendDissolve(baseR, blendR, effectiveOpacity);+ resultG = blendDissolve(baseG, blendG, effectiveOpacity);+ resultB = blendDissolve(baseB, blendB, effectiveOpacity);+ return [resultR, resultG, resultB, baseA];++ case 'darken':+ resultR = blendDarken(baseR, blendR);+ resultG = blendDarken(baseG, blendG);+ resultB = blendDarken(baseB, blendB);+ break;++ case 'multiply':+ resultR = blendMultiply(baseR, blendR);+ resultG = blendMultiply(baseG, blendG);+ resultB = blendMultiply(baseB, blendB);+ break;++ case 'color-burn':+ resultR = blendColorBurn(baseR, blendR);+ resultG = blendColorBurn(baseG, blendG);+ resultB = blendColorBurn(baseB, blendB);+ break;++ case 'linear-burn':+ resultR = blendLinearBurn(baseR, blendR);+ resultG = blendLinearBurn(baseG, blendG);+ resultB = blendLinearBurn(baseB, blendB);+ break;++ case 'darker-color':+ [resultR, resultG, resultB] = blendDarkerColor(baseR, baseG, baseB, blendR, blendG, blendB);+ break;++ case 'lighten':+ resultR = blendLighten(baseR, blendR);+ resultG = blendLighten(baseG, blendG);+ resultB = blendLighten(baseB, blendB);+ break;++ case 'screen':+ resultR = blendScreen(baseR, blendR);+ resultG = blendScreen(baseG, blendG);+ resultB = blendScreen(baseB, blendB);+ break;++ case 'color-dodge':+ resultR = blendColorDodge(baseR, blendR);+ resultG = blendColorDodge(baseG, blendG);+ resultB = blendColorDodge(baseB, blendB);+ break;++ case 'linear-dodge':+ resultR = blendLinearDodge(baseR, blendR);+ resultG = blendLinearDodge(baseG, blendG);+ resultB = blendLinearDodge(baseB, blendB);+ break;++ case 'lighter-color':+ [resultR, resultG, resultB] = blendLighterColor(baseR, baseG, baseB, blendR, blendG, blendB);+ break;++ case 'overlay':+ resultR = blendOverlay(baseR, blendR);+ resultG = blendOverlay(baseG, blendG);+ resultB = blendOverlay(baseB, blendB);+ break;++ case 'soft-light':+ resultR = blendSoftLight(baseR, blendR);+ resultG = blendSoftLight(baseG, blendG);+ resultB = blendSoftLight(baseB, blendB);+ break;++ case 'hard-light':+ resultR = blendHardLight(baseR, blendR);+ resultG = blendHardLight(baseG, blendG);+ resultB = blendHardLight(baseB, blendB);+ break;++ case 'vivid-light':+ resultR = blendVividLight(baseR, blendR);+ resultG = blendVividLight(baseG, blendG);+ resultB = blendVividLight(baseB, blendB);+ break;++ case 'linear-light':+ resultR = blendLinearLight(baseR, blendR);+ resultG = blendLinearLight(baseG, blendG);+ resultB = blendLinearLight(baseB, blendB);+ break;++ case 'pin-light':+ resultR = blendPinLight(baseR, blendR);+ resultG = blendPinLight(baseG, blendG);+ resultB = blendPinLight(baseB, blendB);+ break;++ case 'hard-mix':+ resultR = blendHardMix(baseR, blendR);+ resultG = blendHardMix(baseG, blendG);+ resultB = blendHardMix(baseB, blendB);+ break;++ case 'difference':+ resultR = blendDifference(baseR, blendR);+ resultG = blendDifference(baseG, blendG);+ resultB = blendDifference(baseB, blendB);+ break;++ case 'exclusion':+ resultR = blendExclusion(baseR, blendR);+ resultG = blendExclusion(baseG, blendG);+ resultB = blendExclusion(baseB, blendB);+ break;++ case 'subtract':+ resultR = blendSubtract(baseR, blendR);+ resultG = blendSubtract(baseG, blendG);+ resultB = blendSubtract(baseB, blendB);+ break;++ case 'divide':+ resultR = blendDivide(baseR, blendR);+ resultG = blendDivide(baseG, blendG);+ resultB = blendDivide(baseB, blendB);+ break;++ case 'hue':+ [resultR, resultG, resultB] = blendHue(baseR, baseG, baseB, blendR, blendG, blendB);+ break;++ case 'saturation':+ [resultR, resultG, resultB] = blendSaturation(baseR, baseG, baseB, blendR, blendG, blendB);+ break;++ case 'color':+ [resultR, resultG, resultB] = blendColor(baseR, baseG, baseB, blendR, blendG, blendB);+ break;++ case 'luminosity':+ [resultR, resultG, resultB] = blendLuminosity(baseR, baseG, baseB, blendR, blendG, blendB);+ break;++ default:+ resultR = blendR;+ resultG = blendG;+ resultB = blendB;+ }++ const finalR = clamp(baseR + (resultR - baseR) * effectiveOpacity);+ const finalG = clamp(baseG + (resultG - baseG) * effectiveOpacity);+ const finalB = clamp(baseB + (resultB - baseB) * effectiveOpacity);+ const finalA = Math.max(baseA, Math.round(blendA * opacity));++ return [finalR, finalG, finalB, finalA];+}++export function blendImageData(+ base: ImageData,+ blend: ImageData,+ mode: BlendMode,+ opacity: number = 1+): ImageData {+ if (base.width !== blend.width || base.height !== blend.height) {+ throw new Error('ImageData dimensions must match');+ }++ const result = new ImageData(base.width, base.height);+ const baseData = base.data;+ const blendData = blend.data;+ const resultData = result.data;++ for (let i = 0; i < baseData.length; i += 4) {+ const [r, g, b, a] = blendPixel(+ baseData[i], baseData[i + 1], baseData[i + 2], baseData[i + 3],+ blendData[i], blendData[i + 1], blendData[i + 2], blendData[i + 3],+ mode,+ opacity+ );+ resultData[i] = r;+ resultData[i + 1] = g;+ resultData[i + 2] = b;+ resultData[i + 3] = a;+ }++ return result;+}++export function getCompositeOperation(mode: BlendMode): GlobalCompositeOperation | null {+ const modeMap: Partial<Record<BlendMode, GlobalCompositeOperation>> = {+ normal: 'source-over',+ multiply: 'multiply',+ screen: 'screen',+ overlay: 'overlay',+ darken: 'darken',+ lighten: 'lighten',+ 'color-dodge': 'color-dodge',+ 'color-burn': 'color-burn',+ 'hard-light': 'hard-light',+ 'soft-light': 'soft-light',+ difference: 'difference',+ exclusion: 'exclusion',+ hue: 'hue',+ saturation: 'saturation',+ color: 'color',+ luminosity: 'luminosity',+ };+ return modeMap[mode] ?? null;+}++export function requiresManualBlending(mode: BlendMode): boolean {+ return getCompositeOperation(mode) === null;+}++export const BLEND_MODE_GROUPS = {+ normal: ['normal', 'dissolve'] as BlendMode[],+ darken: ['darken', 'multiply', 'color-burn', 'linear-burn', 'darker-color'] as BlendMode[],+ lighten: ['lighten', 'screen', 'color-dodge', 'linear-dodge', 'lighter-color'] as BlendMode[],+ contrast: ['overlay', 'soft-light', 'hard-light', 'vivid-light', 'linear-light', 'pin-light', 'hard-mix'] as BlendMode[],+ comparative: ['difference', 'exclusion', 'subtract', 'divide'] as BlendMode[],+ component: ['hue', 'saturation', 'color', 'luminosity'] as BlendMode[],+};diff --git a/apps/image/src/effects/layer-styles.ts b/apps/image/src/effects/layer-styles.tsnew file mode 100644index 00000000..0086be64--- /dev/null+++ b/apps/image/src/effects/layer-styles.ts@@ -0,0 +1,742 @@+import { BlendMode, blendPixel } from './blend-modes';++export interface ContourPoint {+ input: number;+ output: number;+}++export interface ContourCurve {+ points: ContourPoint[];+ cornerAtPoint: boolean[];+}++export const DEFAULT_CONTOUR: ContourCurve = {+ points: [+ { input: 0, output: 0 },+ { input: 255, output: 255 },+ ],+ cornerAtPoint: [false, false],+};++export interface GradientStop {+ position: number;+ color: string;+ opacity: number;+}++export interface GradientDef {+ stops: GradientStop[];+ type: 'linear' | 'radial';+ angle?: number;+ reverse?: boolean;+}++export interface PatternDef {+ id: string;+ name: string;+ data: ImageData;+ scale: number;+}++export interface BevelEmbossSettings {+ enabled: boolean;+ style: 'outer-bevel' | 'inner-bevel' | 'emboss' | 'pillow-emboss' | 'stroke-emboss';+ technique: 'smooth' | 'chisel-hard' | 'chisel-soft';+ depth: number;+ direction: 'up' | 'down';+ size: number;+ soften: number;+ angle: number;+ altitude: number;+ highlightMode: BlendMode;+ highlightColor: string;+ highlightOpacity: number;+ shadowMode: BlendMode;+ shadowColor: string;+ shadowOpacity: number;+ glossContour: ContourCurve;+ contour: ContourCurve;+ antiAlias: boolean;+}++export const DEFAULT_BEVEL_EMBOSS: BevelEmbossSettings = {+ enabled: false,+ style: 'inner-bevel',+ technique: 'smooth',+ depth: 100,+ direction: 'up',+ size: 5,+ soften: 0,+ angle: 120,+ altitude: 30,+ highlightMode: 'screen',+ highlightColor: '#ffffff',+ highlightOpacity: 75,+ shadowMode: 'multiply',+ shadowColor: '#000000',+ shadowOpacity: 75,+ glossContour: DEFAULT_CONTOUR,+ contour: DEFAULT_CONTOUR,+ antiAlias: true,+};++export interface InnerGlowSettings {+ enabled: boolean;+ blendMode: BlendMode;+ opacity: number;+ noise: number;+ color: string;+ gradient?: GradientDef;+ technique: 'softer' | 'precise';+ source: 'center' | 'edge';+ choke: number;+ size: number;+ contour: ContourCurve;+ antiAlias: boolean;+ range: number;+ jitter: number;+}++export const DEFAULT_INNER_GLOW: InnerGlowSettings = {+ enabled: false,+ blendMode: 'screen',+ opacity: 75,+ noise: 0,+ color: '#ffffbe',+ technique: 'softer',+ source: 'edge',+ choke: 0,+ size: 5,+ contour: DEFAULT_CONTOUR,+ antiAlias: false,+ range: 50,+ jitter: 0,+};++export interface ColorOverlaySettings {+ enabled: boolean;+ blendMode: BlendMode;+ color: string;+ opacity: number;+}++export const DEFAULT_COLOR_OVERLAY: ColorOverlaySettings = {+ enabled: false,+ blendMode: 'normal',+ color: '#ff0000',+ opacity: 100,+};++export interface GradientOverlaySettings {+ enabled: boolean;+ blendMode: BlendMode;+ opacity: number;+ gradient: GradientDef;+ style: 'linear' | 'radial' | 'angle' | 'reflected' | 'diamond';+ alignWithLayer: boolean;+ angle: number;+ scale: number;+ reverse: boolean;+ dither: boolean;+}++export const DEFAULT_GRADIENT_OVERLAY: GradientOverlaySettings = {+ enabled: false,+ blendMode: 'normal',+ opacity: 100,+ gradient: {+ stops: [+ { position: 0, color: '#000000', opacity: 100 },+ { position: 100, color: '#ffffff', opacity: 100 },+ ],+ type: 'linear',+ },+ style: 'linear',+ alignWithLayer: true,+ angle: 90,+ scale: 100,+ reverse: false,+ dither: false,+};++export interface PatternOverlaySettings {+ enabled: boolean;+ blendMode: BlendMode;+ opacity: number;+ pattern: PatternDef | null;+ scale: number;+ linkWithLayer: boolean;+}++export const DEFAULT_PATTERN_OVERLAY: PatternOverlaySettings = {+ enabled: false,+ blendMode: 'normal',+ opacity: 100,+ pattern: null,+ scale: 100,+ linkWithLayer: true,+};++export interface SatinSettings {+ enabled: boolean;+ blendMode: BlendMode;+ color: string;+ opacity: number;+ angle: number;+ distance: number;+ size: number;+ contour: ContourCurve;+ antiAlias: boolean;+ invert: boolean;+}++export const DEFAULT_SATIN: SatinSettings = {+ enabled: false,+ blendMode: 'multiply',+ color: '#000000',+ opacity: 50,+ angle: 19,+ distance: 11,+ size: 14,+ contour: DEFAULT_CONTOUR,+ antiAlias: true,+ invert: false,+};++export interface LayerStyles {+ bevelEmboss: BevelEmbossSettings;+ innerGlow: InnerGlowSettings;+ colorOverlay: ColorOverlaySettings;+ gradientOverlay: GradientOverlaySettings;+ patternOverlay: PatternOverlaySettings;+ satin: SatinSettings;+}++export const DEFAULT_LAYER_STYLES: LayerStyles = {+ bevelEmboss: DEFAULT_BEVEL_EMBOSS,+ innerGlow: DEFAULT_INNER_GLOW,+ colorOverlay: DEFAULT_COLOR_OVERLAY,+ gradientOverlay: DEFAULT_GRADIENT_OVERLAY,+ patternOverlay: DEFAULT_PATTERN_OVERLAY,+ satin: DEFAULT_SATIN,+};++function parseColor(color: string): { r: number; g: number; b: number } {+ const match = color.match(/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i);+ if (match) {+ return {+ r: parseInt(match[1], 16),+ g: parseInt(match[2], 16),+ b: parseInt(match[3], 16),+ };+ }+ return { r: 0, g: 0, b: 0 };+}++function evaluateContour(contour: ContourCurve, input: number): number {+ const { points } = contour;+ if (points.length === 0) return input;++ input = Math.max(0, Math.min(255, input));++ for (let i = 0; i < points.length - 1; i++) {+ const p1 = points[i];+ const p2 = points[i + 1];++ if (input >= p1.input && input <= p2.input) {+ const t = (input - p1.input) / (p2.input - p1.input || 1);+ return p1.output + (p2.output - p1.output) * t;+ }+ }++ return points[points.length - 1].output;+}++function getEdgeDistance(+ imageData: ImageData,+ x: number,+ y: number,+ maxDistance: number,+ fromEdge: boolean = true+): number {+ const { width, height, data } = imageData;+ const centerAlpha = data[(y * width + x) * 4 + 3];++ if (fromEdge) {+ if (centerAlpha === 0) return maxDistance;+ } else {+ if (centerAlpha === 255) return maxDistance;+ }++ let minDist = maxDistance;++ for (let dy = -maxDistance; dy <= maxDistance; dy++) {+ for (let dx = -maxDistance; dx <= maxDistance; dx++) {+ const nx = x + dx;+ const ny = y + dy;++ if (nx < 0 || nx >= width || ny < 0 || ny >= height) continue;++ const neighborAlpha = data[(ny * width + nx) * 4 + 3];++ if (fromEdge ? neighborAlpha === 0 : neighborAlpha > 0) {+ const dist = Math.sqrt(dx * dx + dy * dy);+ if (dist < minDist) {+ minDist = dist;+ }+ }+ }+ }++ return minDist;+}++export function applyBevelEmboss(+ ctx: OffscreenCanvasRenderingContext2D,+ settings: BevelEmbossSettings,+ layerBounds: { x: number; y: number; width: number; height: number }+): void {+ if (!settings.enabled) return;++ const { width, height } = layerBounds;+ const imageData = ctx.getImageData(layerBounds.x, layerBounds.y, width, height);+ const data = imageData.data;++ const angleRad = (settings.angle * Math.PI) / 180;+ const altitudeRad = (settings.altitude * Math.PI) / 180;+ const lightX = Math.cos(angleRad) * Math.cos(altitudeRad);+ const lightY = Math.sin(angleRad) * Math.cos(altitudeRad);++ const highlightColor = parseColor(settings.highlightColor);+ const shadowColor = parseColor(settings.shadowColor);+ const size = settings.size;+ const depth = settings.depth / 100;++ const resultData = new Uint8ClampedArray(data);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const idx = (y * width + x) * 4;+ const alpha = data[idx + 3];++ if (alpha === 0) continue;++ const edgeDist = getEdgeDistance(imageData, x, y, size, true);++ if (edgeDist >= size) continue;++ const bevelFactor = 1 - edgeDist / size;+ const smoothedFactor = settings.technique === 'smooth'+ ? Math.sin(bevelFactor * Math.PI / 2)+ : settings.technique === 'chisel-hard'+ ? bevelFactor > 0.5 ? 1 : 0+ : bevelFactor;++ const nx = x > 0 ? data[((y) * width + (x - 1)) * 4 + 3] - data[((y) * width + (x + 1)) * 4 + 3] : 0;+ const ny = y > 0 ? data[((y - 1) * width + x) * 4 + 3] - data[((y + 1) * width + x) * 4 + 3] : 0;++ const normalLen = Math.sqrt(nx * nx + ny * ny + 1);+ const normalX = nx / normalLen;+ const normalY = ny / normalLen;++ let lighting = normalX * lightX + normalY * lightY;+ lighting = settings.direction === 'down' ? -lighting : lighting;+ lighting *= smoothedFactor * depth;++ const contourValue = evaluateContour(settings.contour, Math.abs(lighting) * 255);+ lighting = (lighting >= 0 ? 1 : -1) * (contourValue / 255);++ if (lighting > 0) {+ const opacity = lighting * (settings.highlightOpacity / 100);+ const [r, g, b, a] = blendPixel(+ resultData[idx], resultData[idx + 1], resultData[idx + 2], resultData[idx + 3],+ highlightColor.r, highlightColor.g, highlightColor.b, Math.round(255 * opacity),+ settings.highlightMode+ );+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ } else {+ const opacity = -lighting * (settings.shadowOpacity / 100);+ const [r, g, b, a] = blendPixel(+ resultData[idx], resultData[idx + 1], resultData[idx + 2], resultData[idx + 3],+ shadowColor.r, shadowColor.g, shadowColor.b, Math.round(255 * opacity),+ settings.shadowMode+ );+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ }+ }+ }++ const resultImage = new ImageData(resultData, width, height);+ ctx.putImageData(resultImage, layerBounds.x, layerBounds.y);+}++export function applyInnerGlow(+ ctx: OffscreenCanvasRenderingContext2D,+ settings: InnerGlowSettings,+ layerBounds: { x: number; y: number; width: number; height: number }+): void {+ if (!settings.enabled) return;++ const { width, height } = layerBounds;+ const imageData = ctx.getImageData(layerBounds.x, layerBounds.y, width, height);+ const data = imageData.data;++ const glowColor = parseColor(settings.color);+ const size = settings.size;+ const choke = settings.choke / 100;++ const resultData = new Uint8ClampedArray(data);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const idx = (y * width + x) * 4;+ const alpha = data[idx + 3];++ if (alpha === 0) continue;++ const fromEdge = settings.source === 'edge';+ const edgeDist = fromEdge+ ? getEdgeDistance(imageData, x, y, size, true)+ : Math.min(+ x, y,+ width - x - 1, height - y - 1,+ size+ );++ const effectiveSize = size * (1 - choke);+ if (edgeDist >= effectiveSize) continue;++ let intensity = 1 - edgeDist / effectiveSize;++ if (settings.technique === 'softer') {+ intensity = Math.sin(intensity * Math.PI / 2);+ }++ intensity = evaluateContour(settings.contour, intensity * 255) / 255;++ if (settings.noise > 0) {+ intensity *= 1 - (Math.random() * settings.noise / 100);+ }++ const opacity = intensity * (settings.opacity / 100);++ const [r, g, b, a] = blendPixel(+ resultData[idx], resultData[idx + 1], resultData[idx + 2], resultData[idx + 3],+ glowColor.r, glowColor.g, glowColor.b, Math.round(255 * opacity),+ settings.blendMode+ );++ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ }+ }++ const resultImage = new ImageData(resultData, width, height);+ ctx.putImageData(resultImage, layerBounds.x, layerBounds.y);+}++export function applyColorOverlay(+ ctx: OffscreenCanvasRenderingContext2D,+ settings: ColorOverlaySettings,+ layerBounds: { x: number; y: number; width: number; height: number }+): void {+ if (!settings.enabled) return;++ const { width, height } = layerBounds;+ const imageData = ctx.getImageData(layerBounds.x, layerBounds.y, width, height);+ const data = imageData.data;++ const overlayColor = parseColor(settings.color);+ const opacity = settings.opacity / 100;++ for (let i = 0; i < data.length; i += 4) {+ if (data[i + 3] === 0) continue;++ const [r, g, b, a] = blendPixel(+ data[i], data[i + 1], data[i + 2], data[i + 3],+ overlayColor.r, overlayColor.g, overlayColor.b, Math.round(255 * opacity),+ settings.blendMode+ );++ data[i] = r;+ data[i + 1] = g;+ data[i + 2] = b;+ data[i + 3] = a;+ }++ ctx.putImageData(imageData, layerBounds.x, layerBounds.y);+}++export function applyGradientOverlay(+ ctx: OffscreenCanvasRenderingContext2D,+ settings: GradientOverlaySettings,+ layerBounds: { x: number; y: number; width: number; height: number }+): void {+ if (!settings.enabled) return;++ const { width, height } = layerBounds;+ const imageData = ctx.getImageData(layerBounds.x, layerBounds.y, width, height);+ const data = imageData.data;++ const angleRad = (settings.angle * Math.PI) / 180;+ const centerX = width / 2;+ const centerY = height / 2;+ const diagonal = Math.sqrt(width * width + height * height);+ const scale = settings.scale / 100;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const idx = (y * width + x) * 4;+ if (data[idx + 3] === 0) continue;++ let gradientPos: number;++ switch (settings.style) {+ case 'linear': {+ const dx = x - centerX;+ const dy = y - centerY;+ const projected = dx * Math.cos(angleRad) + dy * Math.sin(angleRad);+ gradientPos = (projected / (diagonal * scale) + 0.5);+ break;+ }+ case 'radial': {+ const dx = (x - centerX) / scale;+ const dy = (y - centerY) / scale;+ gradientPos = Math.sqrt(dx * dx + dy * dy) / (diagonal / 2);+ break;+ }+ case 'angle': {+ const dx = x - centerX;+ const dy = y - centerY;+ gradientPos = (Math.atan2(dy, dx) + Math.PI) / (2 * Math.PI);+ break;+ }+ case 'reflected': {+ const dx = x - centerX;+ const dy = y - centerY;+ const projected = dx * Math.cos(angleRad) + dy * Math.sin(angleRad);+ gradientPos = Math.abs(projected / (diagonal * scale / 2));+ break;+ }+ case 'diamond': {+ const dx = Math.abs(x - centerX) / scale;+ const dy = Math.abs(y - centerY) / scale;+ gradientPos = (dx + dy) / diagonal;+ break;+ }+ default:+ gradientPos = 0;+ }++ if (settings.reverse) {+ gradientPos = 1 - gradientPos;+ }++ gradientPos = Math.max(0, Math.min(1, gradientPos));++ const { r: gradR, g: gradG, b: gradB, a: gradA } = interpolateGradient(+ settings.gradient,+ gradientPos+ );++ const opacity = (settings.opacity / 100) * (gradA / 255);+ const [r, g, b, a] = blendPixel(+ data[idx], data[idx + 1], data[idx + 2], data[idx + 3],+ gradR, gradG, gradB, Math.round(255 * opacity),+ settings.blendMode+ );++ data[idx] = r;+ data[idx + 1] = g;+ data[idx + 2] = b;+ data[idx + 3] = a;+ }+ }++ ctx.putImageData(imageData, layerBounds.x, layerBounds.y);+}++function interpolateGradient(+ gradient: GradientDef,+ position: number+): { r: number; g: number; b: number; a: number } {+ const { stops } = gradient;+ if (stops.length === 0) return { r: 0, g: 0, b: 0, a: 255 };++ const pos = position * 100;++ let stop1 = stops[0];+ let stop2 = stops[stops.length - 1];++ for (let i = 0; i < stops.length - 1; i++) {+ if (pos >= stops[i].position && pos <= stops[i + 1].position) {+ stop1 = stops[i];+ stop2 = stops[i + 1];+ break;+ }+ }++ const t = stop1.position === stop2.position+ ? 0+ : (pos - stop1.position) / (stop2.position - stop1.position);++ const c1 = parseColor(stop1.color);+ const c2 = parseColor(stop2.color);++ return {+ r: Math.round(c1.r + (c2.r - c1.r) * t),+ g: Math.round(c1.g + (c2.g - c1.g) * t),+ b: Math.round(c1.b + (c2.b - c1.b) * t),+ a: Math.round((stop1.opacity + (stop2.opacity - stop1.opacity) * t) * 2.55),+ };+}++export function applyPatternOverlay(+ ctx: OffscreenCanvasRenderingContext2D,+ settings: PatternOverlaySettings,+ layerBounds: { x: number; y: number; width: number; height: number }+): void {+ if (!settings.enabled || !settings.pattern) return;++ const { width, height } = layerBounds;+ const imageData = ctx.getImageData(layerBounds.x, layerBounds.y, width, height);+ const data = imageData.data;++ const pattern = settings.pattern;+ const patternData = pattern.data.data;+ const patternWidth = pattern.data.width;+ const patternHeight = pattern.data.height;+ const scale = (settings.scale / 100) * pattern.scale;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const idx = (y * width + x) * 4;+ if (data[idx + 3] === 0) continue;++ const px = Math.floor((x / scale) % patternWidth);+ const py = Math.floor((y / scale) % patternHeight);+ const pIdx = (py * patternWidth + px) * 4;++ const opacity = (settings.opacity / 100) * (patternData[pIdx + 3] / 255);+ const [r, g, b, a] = blendPixel(+ data[idx], data[idx + 1], data[idx + 2], data[idx + 3],+ patternData[pIdx], patternData[pIdx + 1], patternData[pIdx + 2], Math.round(255 * opacity),+ settings.blendMode+ );++ data[idx] = r;+ data[idx + 1] = g;+ data[idx + 2] = b;+ data[idx + 3] = a;+ }+ }++ ctx.putImageData(imageData, layerBounds.x, layerBounds.y);+}++export function applySatin(+ ctx: OffscreenCanvasRenderingContext2D,+ settings: SatinSettings,+ layerBounds: { x: number; y: number; width: number; height: number }+): void {+ if (!settings.enabled) return;++ const { width, height } = layerBounds;+ const imageData = ctx.getImageData(layerBounds.x, layerBounds.y, width, height);+ const data = imageData.data;++ const satinColor = parseColor(settings.color);+ const angleRad = (settings.angle * Math.PI) / 180;+ const offsetX = Math.cos(angleRad) * settings.distance;+ const offsetY = Math.sin(angleRad) * settings.distance;++ const resultData = new Uint8ClampedArray(data);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const idx = (y * width + x) * 4;+ if (data[idx + 3] === 0) continue;++ const x1 = Math.round(x + offsetX);+ const y1 = Math.round(y + offsetY);+ const x2 = Math.round(x - offsetX);+ const y2 = Math.round(y - offsetY);++ let alpha1 = 0;+ let alpha2 = 0;++ if (x1 >= 0 && x1 < width && y1 >= 0 && y1 < height) {+ alpha1 = data[(y1 * width + x1) * 4 + 3];+ }+ if (x2 >= 0 && x2 < width && y2 >= 0 && y2 < height) {+ alpha2 = data[(y2 * width + x2) * 4 + 3];+ }++ let satinIntensity = Math.abs(alpha1 - alpha2) / 255;++ const dist1 = getEdgeDistance(imageData, x, y, settings.size, true);+ const distFactor = 1 - Math.min(dist1, settings.size) / settings.size;+ satinIntensity *= distFactor;++ satinIntensity = evaluateContour(settings.contour, satinIntensity * 255) / 255;++ if (settings.invert) {+ satinIntensity = 1 - satinIntensity;+ }++ const opacity = satinIntensity * (settings.opacity / 100);++ const [r, g, b, a] = blendPixel(+ resultData[idx], resultData[idx + 1], resultData[idx + 2], resultData[idx + 3],+ satinColor.r, satinColor.g, satinColor.b, Math.round(255 * opacity),+ settings.blendMode+ );++ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ }+ }++ const resultImage = new ImageData(resultData, width, height);+ ctx.putImageData(resultImage, layerBounds.x, layerBounds.y);+}++export function applyLayerStyles(+ ctx: OffscreenCanvasRenderingContext2D,+ styles: Partial<LayerStyles>,+ layerBounds: { x: number; y: number; width: number; height: number }+): void {+ if (styles.patternOverlay) {+ applyPatternOverlay(ctx, { ...DEFAULT_PATTERN_OVERLAY, ...styles.patternOverlay }, layerBounds);+ }++ if (styles.gradientOverlay) {+ applyGradientOverlay(ctx, { ...DEFAULT_GRADIENT_OVERLAY, ...styles.gradientOverlay }, layerBounds);+ }++ if (styles.colorOverlay) {+ applyColorOverlay(ctx, { ...DEFAULT_COLOR_OVERLAY, ...styles.colorOverlay }, layerBounds);+ }++ if (styles.satin) {+ applySatin(ctx, { ...DEFAULT_SATIN, ...styles.satin }, layerBounds);+ }++ if (styles.innerGlow) {+ applyInnerGlow(ctx, { ...DEFAULT_INNER_GLOW, ...styles.innerGlow }, layerBounds);+ }++ if (styles.bevelEmboss) {+ applyBevelEmboss(ctx, { ...DEFAULT_BEVEL_EMBOSS, ...styles.bevelEmboss }, layerBounds);+ }+}diff --git a/apps/image/src/filters/blur/blur-filters.ts b/apps/image/src/filters/blur/blur-filters.tsnew file mode 100644index 00000000..7df0c29f--- /dev/null+++ b/apps/image/src/filters/blur/blur-filters.ts@@ -0,0 +1,458 @@+export interface GaussianBlurSettings {+ radius: number;+}++export interface MotionBlurSettings {+ angle: number;+ distance: number;+}++export interface RadialBlurSettings {+ amount: number;+ method: 'spin' | 'zoom';+ quality: 'draft' | 'better' | 'best';+ centerX: number;+ centerY: number;+}++export interface LensBlurSettings {+ radius: number;+ irisShape: number;+ irisRotation: number;+ irisCurvature: number;+ highlightBrightness: number;+ highlightThreshold: number;+}++export interface SurfaceBlurSettings {+ radius: number;+ threshold: number;+}++export interface TiltShiftSettings {+ blur: number;+ focusY: number;+ focusHeight: number;+ transitionSize: number;+ angle: number;+}++export const DEFAULT_GAUSSIAN_BLUR: GaussianBlurSettings = {+ radius: 5,+};++export const DEFAULT_MOTION_BLUR: MotionBlurSettings = {+ angle: 0,+ distance: 10,+};++export const DEFAULT_RADIAL_BLUR: RadialBlurSettings = {+ amount: 10,+ method: 'spin',+ quality: 'better',+ centerX: 0.5,+ centerY: 0.5,+};++export const DEFAULT_LENS_BLUR: LensBlurSettings = {+ radius: 15,+ irisShape: 6,+ irisRotation: 0,+ irisCurvature: 0,+ highlightBrightness: 0,+ highlightThreshold: 255,+};++export const DEFAULT_SURFACE_BLUR: SurfaceBlurSettings = {+ radius: 5,+ threshold: 15,+};++export const DEFAULT_TILT_SHIFT: TiltShiftSettings = {+ blur: 15,+ focusY: 0.5,+ focusHeight: 0.2,+ transitionSize: 0.1,+ angle: 0,+};++function createGaussianKernel(radius: number): number[] {+ const size = radius * 2 + 1;+ const kernel = new Array(size);+ const sigma = radius / 3;+ const twoSigmaSquare = 2 * sigma * sigma;+ let sum = 0;++ for (let i = 0; i < size; i++) {+ const x = i - radius;+ kernel[i] = Math.exp(-(x * x) / twoSigmaSquare);+ sum += kernel[i];+ }++ for (let i = 0; i < size; i++) {+ kernel[i] /= sum;+ }++ return kernel;+}++export function applyGaussianBlur(imageData: ImageData, settings: GaussianBlurSettings): ImageData {+ const { width, height, data } = imageData;+ const radius = Math.max(1, Math.round(settings.radius));+ const kernel = createGaussianKernel(radius);++ const tempData = new Uint8ClampedArray(data);+ const resultData = new Uint8ClampedArray(data.length);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0, a = 0;++ for (let k = -radius; k <= radius; k++) {+ const sx = Math.min(Math.max(x + k, 0), width - 1);+ const idx = (y * width + sx) * 4;+ const weight = kernel[k + radius];++ r += data[idx] * weight;+ g += data[idx + 1] * weight;+ b += data[idx + 2] * weight;+ a += data[idx + 3] * weight;+ }++ const idx = (y * width + x) * 4;+ tempData[idx] = r;+ tempData[idx + 1] = g;+ tempData[idx + 2] = b;+ tempData[idx + 3] = a;+ }+ }++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0, a = 0;++ for (let k = -radius; k <= radius; k++) {+ const sy = Math.min(Math.max(y + k, 0), height - 1);+ const idx = (sy * width + x) * 4;+ const weight = kernel[k + radius];++ r += tempData[idx] * weight;+ g += tempData[idx + 1] * weight;+ b += tempData[idx + 2] * weight;+ a += tempData[idx + 3] * weight;+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyMotionBlur(imageData: ImageData, settings: MotionBlurSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const angleRad = (settings.angle * Math.PI) / 180;+ const dx = Math.cos(angleRad);+ const dy = Math.sin(angleRad);+ const samples = Math.max(1, Math.round(settings.distance));++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0, a = 0;+ let count = 0;++ for (let i = -samples; i <= samples; i++) {+ const sx = Math.round(x + dx * i);+ const sy = Math.round(y + dy * i);++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const idx = (sy * width + sx) * 4;+ r += data[idx];+ g += data[idx + 1];+ b += data[idx + 2];+ a += data[idx + 3];+ count++;+ }+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = r / count;+ resultData[idx + 1] = g / count;+ resultData[idx + 2] = b / count;+ resultData[idx + 3] = a / count;+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyRadialBlur(imageData: ImageData, settings: RadialBlurSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const centerX = width * settings.centerX;+ const centerY = height * settings.centerY;++ const qualitySamples = settings.quality === 'draft' ? 8 : settings.quality === 'better' ? 16 : 32;+ const amount = settings.amount / 100;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0, a = 0;++ const dx = x - centerX;+ const dy = y - centerY;+ const dist = Math.sqrt(dx * dx + dy * dy);+ const angle = Math.atan2(dy, dx);++ for (let i = 0; i < qualitySamples; i++) {+ const t = (i / qualitySamples - 0.5) * amount;+ let sx: number, sy: number;++ if (settings.method === 'spin') {+ const newAngle = angle + t;+ sx = Math.round(centerX + Math.cos(newAngle) * dist);+ sy = Math.round(centerY + Math.sin(newAngle) * dist);+ } else {+ const scale = 1 + t;+ sx = Math.round(centerX + dx * scale);+ sy = Math.round(centerY + dy * scale);+ }++ sx = Math.min(Math.max(sx, 0), width - 1);+ sy = Math.min(Math.max(sy, 0), height - 1);++ const idx = (sy * width + sx) * 4;+ r += data[idx];+ g += data[idx + 1];+ b += data[idx + 2];+ a += data[idx + 3];+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = r / qualitySamples;+ resultData[idx + 1] = g / qualitySamples;+ resultData[idx + 2] = b / qualitySamples;+ resultData[idx + 3] = a / qualitySamples;+ }+ }++ return new ImageData(resultData, width, height);+}++function createBokehKernel(radius: number, shape: number, rotation: number): Array<{ x: number; y: number; weight: number }> {+ const kernel: Array<{ x: number; y: number; weight: number }> = [];+ const rotRad = (rotation * Math.PI) / 180;++ for (let y = -radius; y <= radius; y++) {+ for (let x = -radius; x <= radius; x++) {+ const rx = x * Math.cos(rotRad) - y * Math.sin(rotRad);+ const ry = x * Math.sin(rotRad) + y * Math.cos(rotRad);++ const angle = Math.atan2(ry, rx);+ const angleStep = (2 * Math.PI) / shape;+ const polygonRadius = radius / Math.cos((angle % angleStep) - angleStep / 2);++ const dist = Math.sqrt(rx * rx + ry * ry);+ if (dist <= Math.abs(polygonRadius)) {+ kernel.push({ x, y, weight: 1 });+ }+ }+ }++ const totalWeight = kernel.length;+ kernel.forEach(k => k.weight /= totalWeight);++ return kernel;+}++export function applyLensBlur(imageData: ImageData, settings: LensBlurSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const radius = Math.max(1, Math.round(settings.radius));+ const kernel = createBokehKernel(radius, settings.irisShape, settings.irisRotation);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0, a = 0;+ let totalWeight = 0;++ for (const k of kernel) {+ const sx = Math.min(Math.max(x + k.x, 0), width - 1);+ const sy = Math.min(Math.max(y + k.y, 0), height - 1);+ const idx = (sy * width + sx) * 4;++ const luminance = (data[idx] * 0.299 + data[idx + 1] * 0.587 + data[idx + 2] * 0.114);+ let weight = k.weight;++ if (luminance > settings.highlightThreshold) {+ weight *= 1 + (settings.highlightBrightness / 100) * ((luminance - settings.highlightThreshold) / (255 - settings.highlightThreshold));+ }++ r += data[idx] * weight;+ g += data[idx + 1] * weight;+ b += data[idx + 2] * weight;+ a += data[idx + 3] * weight;+ totalWeight += weight;+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = Math.min(255, r / totalWeight);+ resultData[idx + 1] = Math.min(255, g / totalWeight);+ resultData[idx + 2] = Math.min(255, b / totalWeight);+ resultData[idx + 3] = a / totalWeight;+ }+ }++ return new ImageData(resultData, width, height);+}++export function applySurfaceBlur(imageData: ImageData, settings: SurfaceBlurSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const radius = Math.max(1, Math.round(settings.radius));+ const threshold = settings.threshold;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const centerIdx = (y * width + x) * 4;+ const centerR = data[centerIdx];+ const centerG = data[centerIdx + 1];+ const centerB = data[centerIdx + 2];++ let r = 0, g = 0, b = 0, a = 0;+ let totalWeight = 0;++ for (let ky = -radius; ky <= radius; ky++) {+ for (let kx = -radius; kx <= radius; kx++) {+ const sx = Math.min(Math.max(x + kx, 0), width - 1);+ const sy = Math.min(Math.max(y + ky, 0), height - 1);+ const idx = (sy * width + sx) * 4;++ const diff = Math.abs(data[idx] - centerR) ++ Math.abs(data[idx + 1] - centerG) ++ Math.abs(data[idx + 2] - centerB);++ const colorWeight = Math.max(0, 1 - diff / (threshold * 3));+ const spatialWeight = 1 / (1 + Math.sqrt(kx * kx + ky * ky));+ const weight = colorWeight * spatialWeight;++ r += data[idx] * weight;+ g += data[idx + 1] * weight;+ b += data[idx + 2] * weight;+ a += data[idx + 3] * weight;+ totalWeight += weight;+ }+ }++ resultData[centerIdx] = r / totalWeight;+ resultData[centerIdx + 1] = g / totalWeight;+ resultData[centerIdx + 2] = b / totalWeight;+ resultData[centerIdx + 3] = a / totalWeight;+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyTiltShift(imageData: ImageData, settings: TiltShiftSettings): ImageData {+ const { width, height, data } = imageData;++ const blurredData = applyGaussianBlur(imageData, { radius: settings.blur });+ const blurData = blurredData.data;+ const resultData = new Uint8ClampedArray(data.length);++ const focusCenter = height * settings.focusY;+ const focusHalfHeight = (height * settings.focusHeight) / 2;+ const transitionSize = height * settings.transitionSize;++ const angleRad = (settings.angle * Math.PI) / 180;+ const centerX = width / 2;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const dx = x - centerX;+ const adjustedY = y + Math.tan(angleRad) * dx;++ const distFromCenter = Math.abs(adjustedY - focusCenter);+ let blurAmount: number;++ if (distFromCenter < focusHalfHeight) {+ blurAmount = 0;+ } else if (distFromCenter < focusHalfHeight + transitionSize) {+ blurAmount = (distFromCenter - focusHalfHeight) / transitionSize;+ blurAmount = blurAmount * blurAmount;+ } else {+ blurAmount = 1;+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = data[idx] * (1 - blurAmount) + blurData[idx] * blurAmount;+ resultData[idx + 1] = data[idx + 1] * (1 - blurAmount) + blurData[idx + 1] * blurAmount;+ resultData[idx + 2] = data[idx + 2] * (1 - blurAmount) + blurData[idx + 2] * blurAmount;+ resultData[idx + 3] = data[idx + 3];+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyBoxBlur(imageData: ImageData, radius: number): ImageData {+ const { width, height, data } = imageData;+ const size = radius * 2 + 1;++ const tempData = new Uint8ClampedArray(data);+ const resultData = new Uint8ClampedArray(data.length);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0, a = 0;++ for (let k = -radius; k <= radius; k++) {+ const sx = Math.min(Math.max(x + k, 0), width - 1);+ const idx = (y * width + sx) * 4;+ r += data[idx];+ g += data[idx + 1];+ b += data[idx + 2];+ a += data[idx + 3];+ }++ const idx = (y * width + x) * 4;+ tempData[idx] = r / size;+ tempData[idx + 1] = g / size;+ tempData[idx + 2] = b / size;+ tempData[idx + 3] = a / size;+ }+ }++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0, a = 0;++ for (let k = -radius; k <= radius; k++) {+ const sy = Math.min(Math.max(y + k, 0), height - 1);+ const idx = (sy * width + x) * 4;+ r += tempData[idx];+ g += tempData[idx + 1];+ b += tempData[idx + 2];+ a += tempData[idx + 3];+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = r / size;+ resultData[idx + 1] = g / size;+ resultData[idx + 2] = b / size;+ resultData[idx + 3] = a / size;+ }+ }++ return new ImageData(resultData, width, height);+}diff --git a/apps/image/src/filters/distort/distort-filters.ts b/apps/image/src/filters/distort/distort-filters.tsnew file mode 100644index 00000000..c8fc2ffe--- /dev/null+++ b/apps/image/src/filters/distort/distort-filters.ts@@ -0,0 +1,483 @@+export interface SpherizeSettings {+ amount: number;+ mode: 'normal' | 'horizontal' | 'vertical';+ centerX: number;+ centerY: number;+}++export interface PinchSettings {+ amount: number;+ centerX: number;+ centerY: number;+ radius: number;+}++export interface TwirlSettings {+ angle: number;+ centerX: number;+ centerY: number;+ radius: number;+}++export interface WaveSettings {+ generators: number;+ wavelengthMin: number;+ wavelengthMax: number;+ amplitudeMin: number;+ amplitudeMax: number;+ scaleX: number;+ scaleY: number;+ type: 'sine' | 'triangle' | 'square';+ wrapAround: boolean;+}++export interface RippleSettings {+ amount: number;+ size: 'small' | 'medium' | 'large';+}++export interface ZigZagSettings {+ amount: number;+ ridges: number;+ style: 'around-center' | 'out-from-center' | 'pond-ripples';+ centerX: number;+ centerY: number;+}++export interface PolarCoordinatesSettings {+ mode: 'rectangular-to-polar' | 'polar-to-rectangular';+}++export const DEFAULT_SPHERIZE: SpherizeSettings = {+ amount: 100,+ mode: 'normal',+ centerX: 0.5,+ centerY: 0.5,+};++export const DEFAULT_PINCH: PinchSettings = {+ amount: 50,+ centerX: 0.5,+ centerY: 0.5,+ radius: 0.5,+};++export const DEFAULT_TWIRL: TwirlSettings = {+ angle: 50,+ centerX: 0.5,+ centerY: 0.5,+ radius: 0.5,+};++export const DEFAULT_WAVE: WaveSettings = {+ generators: 5,+ wavelengthMin: 10,+ wavelengthMax: 120,+ amplitudeMin: 5,+ amplitudeMax: 35,+ scaleX: 100,+ scaleY: 100,+ type: 'sine',+ wrapAround: true,+};++export const DEFAULT_RIPPLE: RippleSettings = {+ amount: 100,+ size: 'medium',+};++export const DEFAULT_ZIGZAG: ZigZagSettings = {+ amount: 100,+ ridges: 5,+ style: 'pond-ripples',+ centerX: 0.5,+ centerY: 0.5,+};++export const DEFAULT_POLAR_COORDINATES: PolarCoordinatesSettings = {+ mode: 'rectangular-to-polar',+};++function bilinearSample(+ data: Uint8ClampedArray,+ width: number,+ height: number,+ x: number,+ y: number+): [number, number, number, number] {+ const x0 = Math.floor(x);+ const y0 = Math.floor(y);+ const x1 = Math.min(x0 + 1, width - 1);+ const y1 = Math.min(y0 + 1, height - 1);++ const fx = x - x0;+ const fy = y - y0;++ const idx00 = (y0 * width + x0) * 4;+ const idx10 = (y0 * width + x1) * 4;+ const idx01 = (y1 * width + x0) * 4;+ const idx11 = (y1 * width + x1) * 4;++ const result: [number, number, number, number] = [0, 0, 0, 0];++ for (let c = 0; c < 4; c++) {+ const v00 = data[idx00 + c];+ const v10 = data[idx10 + c];+ const v01 = data[idx01 + c];+ const v11 = data[idx11 + c];++ result[c] = (1 - fx) * (1 - fy) * v00 ++ fx * (1 - fy) * v10 ++ (1 - fx) * fy * v01 ++ fx * fy * v11;+ }++ return result;+}++export function applySpherize(imageData: ImageData, settings: SpherizeSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const centerX = width * settings.centerX;+ const centerY = height * settings.centerY;+ const radius = Math.min(width, height) / 2;+ const amount = settings.amount / 100;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let dx = (x - centerX) / radius;+ let dy = (y - centerY) / radius;++ const dist = Math.sqrt(dx * dx + dy * dy);++ if (dist < 1) {+ const sphereDist = Math.sqrt(1 - dist * dist);+ const factor = (1 - sphereDist) * amount + (1 - amount);++ if (settings.mode === 'normal' || settings.mode === 'horizontal') {+ dx *= factor;+ }+ if (settings.mode === 'normal' || settings.mode === 'vertical') {+ dy *= factor;+ }+ }++ const sx = centerX + dx * radius;+ const sy = centerY + dy * radius;++ const idx = (y * width + x) * 4;++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const [r, g, b, a] = bilinearSample(data, width, height, sx, sy);+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ } else {+ resultData[idx] = 0;+ resultData[idx + 1] = 0;+ resultData[idx + 2] = 0;+ resultData[idx + 3] = 0;+ }+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyPinch(imageData: ImageData, settings: PinchSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const centerX = width * settings.centerX;+ const centerY = height * settings.centerY;+ const radius = Math.min(width, height) * settings.radius;+ const amount = settings.amount / 100;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const dx = x - centerX;+ const dy = y - centerY;+ const dist = Math.sqrt(dx * dx + dy * dy);++ let sx = x, sy = y;++ if (dist < radius) {+ const normalizedDist = dist / radius;+ const pinchFactor = Math.pow(Math.sin(normalizedDist * Math.PI / 2), -amount);++ sx = centerX + dx * pinchFactor;+ sy = centerY + dy * pinchFactor;+ }++ const idx = (y * width + x) * 4;++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const [r, g, b, a] = bilinearSample(data, width, height, sx, sy);+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ } else {+ resultData[idx] = 0;+ resultData[idx + 1] = 0;+ resultData[idx + 2] = 0;+ resultData[idx + 3] = 0;+ }+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyTwirl(imageData: ImageData, settings: TwirlSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const centerX = width * settings.centerX;+ const centerY = height * settings.centerY;+ const radius = Math.min(width, height) * settings.radius;+ const angleRad = (settings.angle * Math.PI) / 180;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const dx = x - centerX;+ const dy = y - centerY;+ const dist = Math.sqrt(dx * dx + dy * dy);++ let sx = x, sy = y;++ if (dist < radius) {+ const angle = Math.atan2(dy, dx);+ const normalizedDist = dist / radius;+ const twirlAngle = angle + angleRad * (1 - normalizedDist);++ sx = centerX + Math.cos(twirlAngle) * dist;+ sy = centerY + Math.sin(twirlAngle) * dist;+ }++ const idx = (y * width + x) * 4;++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const [r, g, b, a] = bilinearSample(data, width, height, sx, sy);+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ } else {+ resultData[idx] = 0;+ resultData[idx + 1] = 0;+ resultData[idx + 2] = 0;+ resultData[idx + 3] = 0;+ }+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyWave(imageData: ImageData, settings: WaveSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const generators: Array<{+ wavelength: number;+ amplitude: number;+ phase: number;+ }> = [];++ for (let i = 0; i < settings.generators; i++) {+ const t = settings.generators > 1 ? i / (settings.generators - 1) : 0;+ generators.push({+ wavelength: settings.wavelengthMin + (settings.wavelengthMax - settings.wavelengthMin) * t,+ amplitude: settings.amplitudeMin + (settings.amplitudeMax - settings.amplitudeMin) * t,+ phase: Math.random() * Math.PI * 2,+ });+ }++ const waveFunc = (value: number): number => {+ switch (settings.type) {+ case 'triangle':+ return 2 * Math.abs(2 * (value / (Math.PI * 2) - Math.floor(value / (Math.PI * 2) + 0.5))) - 1;+ case 'square':+ return Math.sin(value) >= 0 ? 1 : -1;+ case 'sine':+ default:+ return Math.sin(value);+ }+ };++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let offsetX = 0;+ let offsetY = 0;++ for (const gen of generators) {+ offsetX += waveFunc(y / gen.wavelength * Math.PI * 2 + gen.phase) * gen.amplitude;+ offsetY += waveFunc(x / gen.wavelength * Math.PI * 2 + gen.phase) * gen.amplitude;+ }++ offsetX *= settings.scaleX / 100;+ offsetY *= settings.scaleY / 100;++ let sx = x + offsetX;+ let sy = y + offsetY;++ if (settings.wrapAround) {+ sx = ((sx % width) + width) % width;+ sy = ((sy % height) + height) % height;+ }++ const idx = (y * width + x) * 4;++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const [r, g, b, a] = bilinearSample(data, width, height, sx, sy);+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ } else {+ resultData[idx] = 0;+ resultData[idx + 1] = 0;+ resultData[idx + 2] = 0;+ resultData[idx + 3] = 0;+ }+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyRipple(imageData: ImageData, settings: RippleSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const sizeMap = { small: 10, medium: 25, large: 50 };+ const wavelength = sizeMap[settings.size];+ const amplitude = settings.amount / 100 * 10;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const offsetX = Math.sin(y / wavelength * Math.PI * 2) * amplitude;+ const offsetY = Math.sin(x / wavelength * Math.PI * 2) * amplitude;++ const sx = Math.min(Math.max(x + offsetX, 0), width - 1);+ const sy = Math.min(Math.max(y + offsetY, 0), height - 1);++ const idx = (y * width + x) * 4;+ const [r, g, b, a] = bilinearSample(data, width, height, sx, sy);+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyZigZag(imageData: ImageData, settings: ZigZagSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const centerX = width * settings.centerX;+ const centerY = height * settings.centerY;+ const maxRadius = Math.sqrt(centerX * centerX + centerY * centerY);+ const amount = settings.amount / 100 * 20;+ const ridges = settings.ridges;++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ const dx = x - centerX;+ const dy = y - centerY;+ const dist = Math.sqrt(dx * dx + dy * dy);+ const angle = Math.atan2(dy, dx);++ let offset = 0;++ switch (settings.style) {+ case 'around-center':+ offset = Math.sin(angle * ridges) * amount * (dist / maxRadius);+ break;+ case 'out-from-center':+ offset = Math.sin(dist / maxRadius * ridges * Math.PI) * amount;+ break;+ case 'pond-ripples':+ offset = Math.sin(dist / maxRadius * ridges * Math.PI * 2) * amount * (1 - dist / maxRadius);+ break;+ }++ const sx = centerX + (dx + Math.cos(angle) * offset);+ const sy = centerY + (dy + Math.sin(angle) * offset);++ const idx = (y * width + x) * 4;++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const [r, g, b, a] = bilinearSample(data, width, height, sx, sy);+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ } else {+ resultData[idx] = 0;+ resultData[idx + 1] = 0;+ resultData[idx + 2] = 0;+ resultData[idx + 3] = 0;+ }+ }+ }++ return new ImageData(resultData, width, height);+}++export function applyPolarCoordinates(imageData: ImageData, settings: PolarCoordinatesSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const centerX = width / 2;+ const centerY = height / 2;+ const maxRadius = Math.sqrt(centerX * centerX + centerY * centerY);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let sx: number, sy: number;++ if (settings.mode === 'rectangular-to-polar') {+ const normalizedX = x / width;+ const normalizedY = y / height;++ const angle = normalizedX * Math.PI * 2;+ const radius = normalizedY * maxRadius;++ sx = centerX + Math.cos(angle) * radius;+ sy = centerY + Math.sin(angle) * radius;+ } else {+ const dx = x - centerX;+ const dy = y - centerY;+ const radius = Math.sqrt(dx * dx + dy * dy);+ const angle = Math.atan2(dy, dx);++ sx = ((angle + Math.PI) / (Math.PI * 2)) * width;+ sy = (radius / maxRadius) * height;+ }++ const idx = (y * width + x) * 4;++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const [r, g, b, a] = bilinearSample(data, width, height, sx, sy);+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = a;+ } else {+ resultData[idx] = 0;+ resultData[idx + 1] = 0;+ resultData[idx + 2] = 0;+ resultData[idx + 3] = 0;+ }+ }+ }++ return new ImageData(resultData, width, height);+}diff --git a/apps/image/src/filters/sharpen/sharpen-filters.ts b/apps/image/src/filters/sharpen/sharpen-filters.tsnew file mode 100644index 00000000..22cd88c7--- /dev/null+++ b/apps/image/src/filters/sharpen/sharpen-filters.ts@@ -0,0 +1,285 @@+export interface UnsharpMaskSettings {+ amount: number;+ radius: number;+ threshold: number;+}++export interface SmartSharpenSettings {+ amount: number;+ radius: number;+ removeBlur: 'gaussian' | 'lens' | 'motion';+ motionAngle?: number;+ noiseReduction: number;+}++export interface HighPassSettings {+ radius: number;+}++export const DEFAULT_UNSHARP_MASK: UnsharpMaskSettings = {+ amount: 50,+ radius: 1,+ threshold: 0,+};++export const DEFAULT_SMART_SHARPEN: SmartSharpenSettings = {+ amount: 100,+ radius: 1,+ removeBlur: 'gaussian',+ noiseReduction: 0,+};++export const DEFAULT_HIGH_PASS: HighPassSettings = {+ radius: 10,+};++function createGaussianKernel(radius: number): number[] {+ const size = radius * 2 + 1;+ const kernel = new Array(size);+ const sigma = radius / 3;+ const twoSigmaSquare = 2 * sigma * sigma;+ let sum = 0;++ for (let i = 0; i < size; i++) {+ const x = i - radius;+ kernel[i] = Math.exp(-(x * x) / twoSigmaSquare);+ sum += kernel[i];+ }++ for (let i = 0; i < size; i++) {+ kernel[i] /= sum;+ }++ return kernel;+}++function gaussianBlur(data: Uint8ClampedArray, width: number, height: number, radius: number): Uint8ClampedArray {+ const kernel = createGaussianKernel(radius);+ const tempData = new Uint8ClampedArray(data);+ const resultData = new Uint8ClampedArray(data.length);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0;++ for (let k = -radius; k <= radius; k++) {+ const sx = Math.min(Math.max(x + k, 0), width - 1);+ const idx = (y * width + sx) * 4;+ const weight = kernel[k + radius];++ r += data[idx] * weight;+ g += data[idx + 1] * weight;+ b += data[idx + 2] * weight;+ }++ const idx = (y * width + x) * 4;+ tempData[idx] = r;+ tempData[idx + 1] = g;+ tempData[idx + 2] = b;+ tempData[idx + 3] = data[idx + 3];+ }+ }++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0;++ for (let k = -radius; k <= radius; k++) {+ const sy = Math.min(Math.max(y + k, 0), height - 1);+ const idx = (sy * width + x) * 4;+ const weight = kernel[k + radius];++ r += tempData[idx] * weight;+ g += tempData[idx + 1] * weight;+ b += tempData[idx + 2] * weight;+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = r;+ resultData[idx + 1] = g;+ resultData[idx + 2] = b;+ resultData[idx + 3] = tempData[idx + 3];+ }+ }++ return resultData;+}++export function applyUnsharpMask(imageData: ImageData, settings: UnsharpMaskSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const radius = Math.max(1, Math.round(settings.radius));+ const amount = settings.amount / 100;+ const threshold = settings.threshold;++ const blurredData = gaussianBlur(data, width, height, radius);++ for (let i = 0; i < data.length; i += 4) {+ for (let c = 0; c < 3; c++) {+ const original = data[i + c];+ const blurred = blurredData[i + c];+ const diff = original - blurred;++ if (Math.abs(diff) >= threshold) {+ resultData[i + c] = Math.max(0, Math.min(255, original + diff * amount));+ } else {+ resultData[i + c] = original;+ }+ }+ resultData[i + 3] = data[i + 3];+ }++ return new ImageData(resultData, width, height);+}++function motionBlur(data: Uint8ClampedArray, width: number, height: number, radius: number, angle: number): Uint8ClampedArray {+ const resultData = new Uint8ClampedArray(data.length);+ const angleRad = (angle * Math.PI) / 180;+ const dx = Math.cos(angleRad);+ const dy = Math.sin(angleRad);++ for (let y = 0; y < height; y++) {+ for (let x = 0; x < width; x++) {+ let r = 0, g = 0, b = 0;+ let count = 0;++ for (let k = -radius; k <= radius; k++) {+ const sx = Math.round(x + dx * k);+ const sy = Math.round(y + dy * k);++ if (sx >= 0 && sx < width && sy >= 0 && sy < height) {+ const idx = (sy * width + sx) * 4;+ r += data[idx];+ g += data[idx + 1];+ b += data[idx + 2];+ count++;+ }+ }++ const idx = (y * width + x) * 4;+ resultData[idx] = r / count;+ resultData[idx + 1] = g / count;+ resultData[idx + 2] = b / count;+ resultData[idx + 3] = data[idx + 3];+ }+ }++ return resultData;+}++export function applySmartSharpen(imageData: ImageData, settings: SmartSharpenSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const radius = Math.max(1, Math.round(settings.radius));+ const amount = settings.amount / 100;+ const noiseReduction = settings.noiseReduction / 100;++ let blurredData: Uint8ClampedArray;++ switch (settings.removeBlur) {+ case 'motion':+ blurredData = motionBlur(data, width, height, radius, settings.motionAngle ?? 0);+ break;+ case 'lens':+ blurredData = gaussianBlur(data, width, height, radius);+ break;+ case 'gaussian':+ default:+ blurredData = gaussianBlur(data, width, height, radius);+ break;+ }++ for (let i = 0; i < data.length; i += 4) {+ for (let c = 0; c < 3; c++) {+ const original = data[i + c];+ const blurred = blurredData[i + c];+ let diff = original - blurred;++ if (noiseReduction > 0) {+ const absDiff = Math.abs(diff);+ if (absDiff < 10 * noiseReduction) {+ diff *= 1 - noiseReduction;+ }+ }++ resultData[i + c] = Math.max(0, Math.min(255, original + diff * amount));+ }+ resultData[i + 3] = data[i + 3];+ }++ return new ImageData(resultData, width, height);+}++export function applyHighPass(imageData: ImageData, settings: HighPassSettings): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const radius = Math.max(1, Math.round(settings.radius));+ const blurredData = gaussianBlur(data, width, height, radius);++ for (let i = 0; i < data.length; i += 4) {+ for (let c = 0; c < 3; c++) {+ const original = data[i + c];+ const blurred = blurredData[i + c];+ resultData[i + c] = Math.max(0, Math.min(255, 128 + (original - blurred)));+ }+ resultData[i + 3] = data[i + 3];+ }++ return new ImageData(resultData, width, height);+}++export function applySharpen(imageData: ImageData, amount: number = 50): ImageData {+ const { width, height, data } = imageData;+ const resultData = new Uint8ClampedArray(data.length);++ const factor = amount / 100;++ const kernel = [+ 0, -1, 0,+ -1, 5, -1,+ 0, -1, 0,+ ];++ for (let y = 1; y < height - 1; y++) {+ for (let x = 1; x < width - 1; x++) {+ for (let c = 0; c < 3; c++) {+ let sum = 0;+ let ki = 0;++ for (let ky = -1; ky <= 1; ky++) {+ for (let kx = -1; kx <= 1; kx++) {+ const idx = ((y + ky) * width + (x + kx)) * 4;+ sum += data[idx + c] * kernel[ki];+ ki++;+ }+ }++ const idx = (y * width + x) * 4;+ const original = data[idx + c];+ resultData[idx + c] = Math.max(0, Math.min(255, original + (sum - original) * factor));+ }++ const idx = (y * width + x) * 4;+ resultData[idx + 3] = data[idx + 3];+ }+ }++ for (let x = 0; x < width; x++) {+ for (let c = 0; c < 4; c++) {+ resultData[x * 4 + c] = data[x * 4 + c];+ resultData[((height - 1) * width + x) * 4 + c] = data[((height - 1) * width + x) * 4 + c];+ }+ }++ for (let y = 0; y < height; y++) {+ for (let c = 0; c < 4; c++) {+ resultData[(y * width) * 4 + c] = data[(y * width) * 4 + c];+ resultData[(y * width + width - 1) * 4 + c] = data[(y * width + width - 1) * 4 + c];+ }+ }++ return new ImageData(resultData, width, height);+}