From e31688f697afe0abc7fa11b1b76baedcb4bfbd81 Mon Sep 17 00:00:00 2001 From: Boran Date: Thu, 12 Nov 2026 16:34:16 +0300 Subject: [PATCH] feat: add 3-way color wheels and selective retouch gradient masking --- src/pixelflow/api/static/index.html | 504 ++++++++++++++++++- src/pixelflow/operations/__init__.py | 12 + src/pixelflow/operations/color_wheels.py | 61 +++ src/pixelflow/operations/selective_adjust.py | 117 +++++ tests/test_color_wheels.py | 64 +++ tests/test_selective_adjust.py | 96 ++++ 6 files changed, 853 insertions(+), 1 deletion(-) create mode 100644 src/pixelflow/operations/color_wheels.py create mode 100644 src/pixelflow/operations/selective_adjust.py create mode 100644 tests/test_color_wheels.py create mode 100644 tests/test_selective_adjust.py diff --git a/src/pixelflow/api/static/index.html b/src/pixelflow/api/static/index.html index bd457cf..c6b2f2d 100644 --- a/src/pixelflow/api/static/index.html +++ b/src/pixelflow/api/static/index.html @@ -1853,6 +1853,13 @@ Çerçeve + + + + + + + +
+
Bölgesel İyileştirme Ayarları
+
+
+ Pozlama (Exposure) + +0.30 +
+ +
+
+
+ Kontrast + 1.10 +
+ +
+
+
+ Doygunluk + 1.20 +
+ +
+
+
+ Sıcaklık (Warmth) + 0.00 +
+ +
+
+
+ Kenar Yumuşatma + 0.25 +
+ +
+
+
+ Bölgesel Bulanıklık + 0 +
+ +
+
+ +
Hızlı Maske Şablonları
+
+ +
+ + + +
+
+ + + + + + + 3-Yollu Renk Çarkları (Lift/Gamma/Gain) + + + ▾ +
+
+
+ DaVinci Resolve CDL + +
+ +
+ +
+
GÖLGELER
+
Lift
+
+
+
+
+ +
+ Y0.00 +
+
+
+ +
+
ORTA TON
+
Gamma
+
+
+
+
+ +
+ Y1.00 +
+
+
+ +
+
AÇIK TON
+
Gain
+
+
+
+
+ +
+ Y1.00 +
+
+
+
+
+
+
@@ -2857,6 +2998,53 @@

{ + const title = currentLang === "en" ? m.nameEn : m.name; + const sub = currentLang === "en" ? m.descEn : m.desc; + return ` +
+
+ + + + +
+
+
${title}
+
${sub}
+
+
+ `; + }).join(""); + } + + function applyMaskPreset(presetId) { + const m = PRO_MASK_PRESETS.find(x => x.id === presetId); + if (!m) return; + let step = activeSteps.find(s => s.op === "selective_adjust"); + const params = JSON.parse(JSON.stringify(m.params)); + if (step) { + step.params = params; + selectedStepId = step.id; + } else { + const id = stepCounter++; + activeSteps.push({ + id, + op: "selective_adjust", + name: `katman_${id}_mask`, + params + }); + selectedStepId = id; + } + syncMaskUIFromStep(params); + pushHistory(); + renderStepsUI(); + scheduleRender(); + statusMessage.textContent = currentLang === "en" ? `✓ Mask preset applied: ${m.nameEn}` : `✓ Maske şablonu uygulandı: ${m.name}`; + } + + function syncMaskUIFromStep(p) { + if (!p) return; + ["radial", "linear", "box"].forEach(s => { + const b = document.getElementById("btnMask" + s.charAt(0).toUpperCase() + s.slice(1)); + if (b) b.classList.toggle("active", p.shape === s); + }); + const chk = document.getElementById("chkMaskInvert"); + if (chk) chk.checked = !!p.invert; + const sExp = document.getElementById("sliderMaskExposure"); + const vExp = document.getElementById("valMaskExposure"); + if (sExp && p.exposure !== undefined) { sExp.value = p.exposure; if (vExp) vExp.textContent = (p.exposure > 0 ? "+" : "") + p.exposure.toFixed(2); } + const sCt = document.getElementById("sliderMaskContrast"); + const vCt = document.getElementById("valMaskContrast"); + if (sCt && p.contrast !== undefined) { sCt.value = p.contrast; if (vCt) vCt.textContent = p.contrast.toFixed(2); } + const sSat = document.getElementById("sliderMaskSaturation"); + const vSat = document.getElementById("valMaskSaturation"); + if (sSat && p.saturation !== undefined) { sSat.value = p.saturation; if (vSat) vSat.textContent = p.saturation.toFixed(2); } + const sW = document.getElementById("sliderMaskWarmth"); + const vW = document.getElementById("valMaskWarmth"); + if (sW && p.warmth !== undefined) { sW.value = p.warmth; if (vW) vW.textContent = p.warmth.toFixed(2); } + const sF = document.getElementById("sliderMaskFeather"); + const vF = document.getElementById("valMaskFeather"); + if (sF && p.feather !== undefined) { sF.value = p.feather; if (vF) vF.textContent = p.feather.toFixed(2); } + const sBl = document.getElementById("sliderMaskBlur"); + const vBl = document.getElementById("valMaskBlur"); + if (sBl && p.blur_radius !== undefined) { sBl.value = p.blur_radius; if (vBl) vBl.textContent = p.blur_radius; } + } + + function ensureMaskStep() { + let step = activeSteps.find(s => s.op === "selective_adjust"); + if (!step) { + const id = stepCounter++; + step = { + id, + op: "selective_adjust", + name: `katman_${id}_mask`, + params: { ...EFFECT_DEFS.selective_adjust.defaults } + }; + activeSteps.push(step); + selectedStepId = id; + } + return step; + } + + function setMaskShape(shape) { + const step = ensureMaskStep(); + step.params.shape = shape; + ["radial", "linear", "box"].forEach(s => { + const b = document.getElementById("btnMask" + s.charAt(0).toUpperCase() + s.slice(1)); + if (b) b.classList.toggle("active", s === shape); + }); + pushHistory(); + renderStepsUI(); + scheduleRender(); + } + + function toggleMaskInvert(val) { + const step = ensureMaskStep(); + step.params.invert = val; + pushHistory(); + renderStepsUI(); + scheduleRender(); + } + + function updateMaskParam(paramKey, val, valSpanId) { + const span = document.getElementById(valSpanId); + if (span) { + span.textContent = typeof val === "number" && !Number.isInteger(val) ? (val > 0 && paramKey === "exposure" ? "+" : "") + val.toFixed(2) : val; + } + const step = ensureMaskStep(); + step.params[paramKey] = val; + scheduleRender(); + } + + const colorWheelsState = { + lift: [0.0, 0.0, 0.0], + gamma: [1.0, 1.0, 1.0], + gain: [1.0, 1.0, 1.0], + lift_master: 0.0, + gamma_master: 1.0, + gain_master: 1.0 + }; + + function ensureColorWheelsStep() { + let step = activeSteps.find(s => s.op === "color_wheels"); + if (!step) { + const id = stepCounter++; + step = { + id, + op: "color_wheels", + name: `katman_${id}_color_wheels`, + params: JSON.parse(JSON.stringify(colorWheelsState)) + }; + activeSteps.push(step); + selectedStepId = id; + } + return step; + } + + function resetAllColorWheels() { + colorWheelsState.lift = [0.0, 0.0, 0.0]; + colorWheelsState.gamma = [1.0, 1.0, 1.0]; + colorWheelsState.gain = [1.0, 1.0, 1.0]; + colorWheelsState.lift_master = 0.0; + colorWheelsState.gamma_master = 1.0; + colorWheelsState.gain_master = 1.0; + ["Lift", "Gamma", "Gain"].forEach(w => { + const puck = document.getElementById("wheel" + w + "Puck"); + if (puck) { puck.style.left = "50%"; puck.style.top = "50%"; } + }); + const sL = document.getElementById("sliderWheelLiftMaster"); if (sL) sL.value = "0.0"; + const sGm = document.getElementById("sliderWheelGammaMaster"); if (sGm) sGm.value = "1.0"; + const sGn = document.getElementById("sliderWheelGainMaster"); if (sGn) sGn.value = "1.0"; + const vL = document.getElementById("valWheelLiftMaster"); if (vL) vL.textContent = "0.00"; + const vGm = document.getElementById("valWheelGammaMaster"); if (vGm) vGm.textContent = "1.00"; + const vGn = document.getElementById("valWheelGainMaster"); if (vGn) vGn.textContent = "1.00"; + let step = activeSteps.find(s => s.op === "color_wheels"); + if (step) { + step.params = JSON.parse(JSON.stringify(colorWheelsState)); + pushHistory(); + renderStepsUI(); + scheduleRender(); + } + } + + function handleWheelMaster(wheelType, val) { + const step = ensureColorWheelsStep(); + if (wheelType === "lift") { + colorWheelsState.lift_master = val; + step.params.lift_master = val; + const v = document.getElementById("valWheelLiftMaster"); + if (v) v.textContent = (val > 0 ? "+" : "") + val.toFixed(2); + } else if (wheelType === "gamma") { + colorWheelsState.gamma_master = val; + step.params.gamma_master = val; + const v = document.getElementById("valWheelGammaMaster"); + if (v) v.textContent = val.toFixed(2); + } else if (wheelType === "gain") { + colorWheelsState.gain_master = val; + step.params.gain_master = val; + const v = document.getElementById("valWheelGainMaster"); + if (v) v.textContent = val.toFixed(2); + } + scheduleRender(); + } + + function setupColorWheels() { + const wheelConfigs = [ + { key: "lift", id: "wheelLiftWrap", puckId: "wheelLiftPuck", base: 0.0, scale: 0.35 }, + { key: "gamma", id: "wheelGammaWrap", puckId: "wheelGammaPuck", base: 1.0, scale: 0.5 }, + { key: "gain", id: "wheelGainWrap", puckId: "wheelGainPuck", base: 1.0, scale: 0.5 } + ]; + + wheelConfigs.forEach(cfg => { + const wrap = document.getElementById(cfg.id); + const puck = document.getElementById(cfg.puckId); + if (!wrap || !puck) return; + + let dragging = false; + function updatePuck(e) { + const rect = wrap.getBoundingClientRect(); + const cx = rect.left + rect.width / 2; + const cy = rect.top + rect.height / 2; + const radius = rect.width / 2; + const clientX = e.touches ? e.touches[0].clientX : e.clientX; + const clientY = e.touches ? e.touches[0].clientY : e.clientY; + let dx = (clientX - cx) / radius; + let dy = (clientY - cy) / radius; + const dist = Math.sqrt(dx * dx + dy * dy); + if (dist > 1.0) { dx /= dist; dy /= dist; } + + puck.style.left = (50 + dx * 45) + "%"; + puck.style.top = (50 + dy * 45) + "%"; + + const rOffset = dx * cfg.scale; + const bOffset = -dx * cfg.scale; + const gOffset = -dy * cfg.scale; + + const step = ensureColorWheelsStep(); + if (cfg.key === "lift") { + colorWheelsState.lift = [rOffset, gOffset, bOffset]; + step.params.lift = [rOffset, gOffset, bOffset]; + } else if (cfg.key === "gamma") { + colorWheelsState.gamma = [cfg.base + rOffset, cfg.base + gOffset, cfg.base + bOffset]; + step.params.gamma = [cfg.base + rOffset, cfg.base + gOffset, cfg.base + bOffset]; + } else if (cfg.key === "gain") { + colorWheelsState.gain = [cfg.base + rOffset, cfg.base + gOffset, cfg.base + bOffset]; + step.params.gain = [cfg.base + rOffset, cfg.base + gOffset, cfg.base + bOffset]; + } + scheduleRender(); + } + + wrap.addEventListener("mousedown", e => { + dragging = true; + updatePuck(e); + const onMove = ev => { if (dragging) updatePuck(ev); }; + const onUp = () => { + if (dragging) { + dragging = false; + pushHistory(); + renderStepsUI(); + } + window.removeEventListener("mousemove", onMove); + window.removeEventListener("mouseup", onUp); + }; + window.addEventListener("mousemove", onMove); + window.addEventListener("mouseup", onUp); + }); + + wrap.addEventListener("dblclick", () => { + puck.style.left = "50%"; + puck.style.top = "50%"; + const step = ensureColorWheelsStep(); + if (cfg.key === "lift") { + colorWheelsState.lift = [0.0, 0.0, 0.0]; + step.params.lift = [0.0, 0.0, 0.0]; + } else { + colorWheelsState[cfg.key] = [cfg.base, cfg.base, cfg.base]; + step.params[cfg.key] = [cfg.base, cfg.base, cfg.base]; + } + pushHistory(); + renderStepsUI(); + scheduleRender(); + }); + }); + } + function switchLeftTab(tabKey) { - const tabs = ["presets", "crop", "palette", "stamps", "artfx", "frames", "docs", "history"]; + const tabs = ["presets", "crop", "palette", "stamps", "artfx", "frames", "mask", "docs", "history"]; tabs.forEach(t => { const btn = document.getElementById("segTab" + t.charAt(0).toUpperCase() + t.slice(1)); const content = document.getElementById("tabContent" + t.charAt(0).toUpperCase() + t.slice(1)); @@ -6814,6 +7314,7 @@

diff --git a/src/pixelflow/operations/__init__.py b/src/pixelflow/operations/__init__.py index d80818b..d342465 100644 --- a/src/pixelflow/operations/__init__.py +++ b/src/pixelflow/operations/__init__.py @@ -22,6 +22,10 @@ ColorSplashOperation, ColorSplashParams, ) +from pixelflow.operations.color_wheels import ( + ColorWheelsOperation, + ColorWheelsParams, +) from pixelflow.operations.curves import ( CurvesOperation, CurvesParams, @@ -118,6 +122,10 @@ StampOperation, StampParams, ) +from pixelflow.operations.selective_adjust import ( + SelectiveAdjustOperation, + SelectiveAdjustParams, +) from pixelflow.operations.signature import ( SignatureExtractOperation, SignatureExtractParams, @@ -158,6 +166,8 @@ "CannyParams", "ColorSplashOperation", "ColorSplashParams", + "ColorWheelsOperation", + "ColorWheelsParams", "CropOperation", "CropParams", "CurvesOperation", @@ -208,6 +218,8 @@ "ResizeParams", "RotateOperation", "RotateParams", + "SelectiveAdjustOperation", + "SelectiveAdjustParams", "ShadowRemovalOperation", "ShadowRemovalParams", "SharpenOperation", diff --git a/src/pixelflow/operations/color_wheels.py b/src/pixelflow/operations/color_wheels.py new file mode 100644 index 0000000..0500473 --- /dev/null +++ b/src/pixelflow/operations/color_wheels.py @@ -0,0 +1,61 @@ +from __future__ import annotations + +from typing import cast + +import numpy as np + +from pixelflow.core.image_utils import ensure_rgb, merge_alpha, split_alpha, to_uint8 +from pixelflow.core.operation import ImageArray, ImageOperation, OperationParams +from pixelflow.core.registry import register_operation + + +class ColorWheelsParams(OperationParams): + lift: tuple[float, float, float] = (0.0, 0.0, 0.0) + gamma: tuple[float, float, float] = (1.0, 1.0, 1.0) + gain: tuple[float, float, float] = (1.0, 1.0, 1.0) + lift_master: float = 0.0 + gamma_master: float = 1.0 + gain_master: float = 1.0 + + +def _compute_channel_grading( + channel: np.ndarray, + lift: float, + gamma: float, + gain: float, +) -> np.ndarray: + scaled = np.clip(channel * gain + lift, 0.0, 1.0) + safe_gamma = max(0.01, gamma) + graded = np.power(scaled, 1.0 / safe_gamma) + return np.clip(graded, 0.0, 1.0) + + +def _apply_color_wheels_rgb( + rgb: np.ndarray, + params: ColorWheelsParams, +) -> np.ndarray: + norm = rgb.astype(np.float32) / 255.0 + out = np.empty_like(norm) + for c in range(3): + eff_lift = params.lift[c] + params.lift_master + eff_gain = max(0.0, params.gain[c] * params.gain_master) + eff_gamma = max(0.01, params.gamma[c] * params.gamma_master) + out[:, :, c] = _compute_channel_grading( + norm[:, :, c], eff_lift, eff_gamma, eff_gain + ) + return np.clip(out * 255.0, 0.0, 255.0).round().astype(np.uint8) + + +@register_operation("color_wheels") +class ColorWheelsOperation(ImageOperation[ColorWheelsParams]): + operation_name = "color_wheels" + params_model = ColorWheelsParams + + def _execute(self, image: ImageArray) -> ImageArray: + u8 = to_uint8(image) + base, alpha = split_alpha(u8) + rgb = ensure_rgb(base) + graded = _apply_color_wheels_rgb(rgb, self.params) + if alpha is not None: + return merge_alpha(graded, alpha) + return cast(ImageArray, graded) diff --git a/src/pixelflow/operations/selective_adjust.py b/src/pixelflow/operations/selective_adjust.py new file mode 100644 index 0000000..dcc6839 --- /dev/null +++ b/src/pixelflow/operations/selective_adjust.py @@ -0,0 +1,117 @@ +from __future__ import annotations + +from typing import Literal, cast + +import cv2 +import numpy as np +from pydantic import Field + +from pixelflow.core.image_utils import ensure_rgb, merge_alpha, split_alpha, to_uint8 +from pixelflow.core.operation import ImageArray, ImageOperation, OperationParams +from pixelflow.core.registry import register_operation + +MaskShape = Literal["radial", "linear", "box"] + + +class SelectiveAdjustParams(OperationParams): + shape: MaskShape = "radial" + center: tuple[float, float] = (0.5, 0.5) + size: tuple[float, float] = (0.3, 0.3) + feather: float = Field(default=0.2, ge=0.0, le=1.0) + angle: float = 0.0 + invert: bool = False + exposure: float = Field(default=0.0, ge=-2.0, le=2.0) + contrast: float = Field(default=1.0, ge=0.0, le=3.0) + saturation: float = Field(default=1.0, ge=0.0, le=3.0) + warmth: float = Field(default=0.0, ge=-1.0, le=1.0) + blur_radius: int = Field(default=0, ge=0, le=50) + + +def _build_radial_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray: + ys, xs = np.ogrid[:h, :w] + cx = params.center[0] * w + cy = params.center[1] * h + rx = max(1.0, params.size[0] * w) + ry = max(1.0, params.size[1] * h) + dist = np.sqrt(((xs - cx) / rx) ** 2 + ((ys - cy) / ry) ** 2) + feather = max(0.01, params.feather) + val = (1.0 - dist) / feather + return cast(np.ndarray, np.clip(val, 0.0, 1.0)) + + +def _build_linear_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray: + ys, xs = np.mgrid[:h, :w] + cx = params.center[0] * w + cy = params.center[1] * h + rad = np.radians(params.angle) + proj = (xs - cx) * np.sin(rad) + (ys - cy) * np.cos(rad) + span = max(1.0, params.size[1] * h) + feather = max(0.01, params.feather) * span + val = 0.5 - (proj / (span + feather)) + return cast(np.ndarray, np.clip(val, 0.0, 1.0)) + + +def _build_box_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray: + ys, xs = np.ogrid[:h, :w] + cx = params.center[0] * w + cy = params.center[1] * h + rx = max(1.0, params.size[0] * w * 0.5) + ry = max(1.0, params.size[1] * h * 0.5) + dx = np.maximum(0.0, np.abs(xs - cx) - rx) + dy = np.maximum(0.0, np.abs(ys - cy) - ry) + dist = np.sqrt(dx**2 + dy**2) + feather = max(1.0, params.feather * max(rx, ry)) + return cast(np.ndarray, np.clip(1.0 - (dist / feather), 0.0, 1.0)) + + +def _generate_mask(h: int, w: int, params: SelectiveAdjustParams) -> np.ndarray: + if params.shape == "radial": + mask = _build_radial_mask(h, w, params) + elif params.shape == "linear": + mask = _build_linear_mask(h, w, params) + else: + mask = _build_box_mask(h, w, params) + if params.invert: + mask = 1.0 - mask + return mask[:, :, np.newaxis] + + +def _apply_tone_adjustments( + rgb: np.ndarray, + params: SelectiveAdjustParams, +) -> np.ndarray: + norm = rgb.astype(np.float32) / 255.0 + if params.exposure != 0.0: + norm = norm * (2.0**params.exposure) + if params.contrast != 1.0: + norm = (norm - 0.5) * params.contrast + 0.5 + if params.saturation != 1.0: + gray = norm[:, :, 0] * 0.299 + norm[:, :, 1] * 0.587 + norm[:, :, 2] * 0.114 + norm = gray[:, :, np.newaxis] + (norm - gray[:, :, np.newaxis]) * params.saturation + if params.warmth != 0.0: + norm[:, :, 0] += params.warmth * 0.1 + norm[:, :, 2] -= params.warmth * 0.1 + clipped = np.clip(norm * 255.0, 0.0, 255.0).astype(np.uint8) + if params.blur_radius > 0: + k = params.blur_radius if params.blur_radius % 2 == 1 else params.blur_radius + 1 + blurred = cv2.GaussianBlur(clipped, (k, k), 0) + return cast(np.ndarray, blurred) + return cast(np.ndarray, clipped) + + +@register_operation("selective_adjust") +class SelectiveAdjustOperation(ImageOperation[SelectiveAdjustParams]): + operation_name = "selective_adjust" + params_model = SelectiveAdjustParams + + def _execute(self, image: ImageArray) -> ImageArray: + u8 = to_uint8(image) + base, alpha = split_alpha(u8) + rgb = ensure_rgb(base) + mask = _generate_mask(rgb.shape[0], rgb.shape[1], self.params) + adjusted = _apply_tone_adjustments(rgb, self.params).astype(np.float32) + blended = (rgb.astype(np.float32) * (1.0 - mask) + adjusted * mask) + out = np.clip(blended, 0.0, 255.0).round().astype(np.uint8) + if alpha is not None: + return merge_alpha(out, alpha) + return cast(ImageArray, out) diff --git a/tests/test_color_wheels.py b/tests/test_color_wheels.py new file mode 100644 index 0000000..a981fe7 --- /dev/null +++ b/tests/test_color_wheels.py @@ -0,0 +1,64 @@ +from __future__ import annotations + +import numpy as np + +from pixelflow.core.registry import default_registry +from pixelflow.operations.color_wheels import ColorWheelsOperation, ColorWheelsParams + + +def test_color_wheels_identity() -> None: + img = np.full((20, 20, 3), 128, dtype=np.uint8) + op = ColorWheelsOperation(ColorWheelsParams()) + result = op.apply(img) + assert np.allclose(result, 128, atol=2) + + +def test_color_wheels_lift_brightens_shadows() -> None: + img = np.zeros((20, 20, 3), dtype=np.uint8) + params = ColorWheelsParams(lift=(0.2, 0.1, 0.0)) + op = ColorWheelsOperation(params) + result = op.apply(img) + assert result[0, 0, 0] > 40 + assert result[0, 0, 1] > 20 + assert result[0, 0, 2] == 0 + + +def test_color_wheels_gain_affects_highlights() -> None: + img = np.full((10, 10, 3), 200, dtype=np.uint8) + params = ColorWheelsParams(gain=(1.2, 0.8, 1.0)) + op = ColorWheelsOperation(params) + result = op.apply(img) + assert result[0, 0, 0] > 200 + assert result[0, 0, 1] < 200 + + +def test_color_wheels_gamma_shifts_midtones() -> None: + img = np.full((10, 10, 3), 128, dtype=np.uint8) + params = ColorWheelsParams(gamma=(0.5, 2.0, 1.0)) + op = ColorWheelsOperation(params) + result = op.apply(img) + assert result[0, 0, 0] < 128 + assert result[0, 0, 1] > 128 + + +def test_color_wheels_master_controls() -> None: + img = np.full((10, 10, 3), 100, dtype=np.uint8) + params = ColorWheelsParams(lift_master=0.1, gain_master=1.1, gamma_master=0.9) + op = ColorWheelsOperation(params) + result = op.apply(img) + assert result[0, 0, 0] > 100 + + +def test_color_wheels_alpha_preserved() -> None: + rgba = np.full((10, 10, 4), 128, dtype=np.uint8) + rgba[:, :, 3] = 200 + params = ColorWheelsParams(lift=(0.1, 0.1, 0.1)) + op = ColorWheelsOperation(params) + result = op.apply(rgba) + assert result.shape == (10, 10, 4) + assert np.all(result[:, :, 3] == 200) + + +def test_color_wheels_registered_in_registry() -> None: + op_cls = default_registry.get("color_wheels") + assert op_cls is ColorWheelsOperation diff --git a/tests/test_selective_adjust.py b/tests/test_selective_adjust.py new file mode 100644 index 0000000..ddc6625 --- /dev/null +++ b/tests/test_selective_adjust.py @@ -0,0 +1,96 @@ +from __future__ import annotations + +import numpy as np + +from pixelflow.core.registry import default_registry +from pixelflow.operations.selective_adjust import ( + SelectiveAdjustOperation, + SelectiveAdjustParams, +) + + +def test_selective_adjust_radial_brightness() -> None: + img = np.full((50, 50, 3), 100, dtype=np.uint8) + params = SelectiveAdjustParams( + shape="radial", + center=(0.5, 0.5), + size=(0.2, 0.2), + exposure=1.0, + contrast=1.0, + ) + op = SelectiveAdjustOperation(params) + res = op.apply(img) + assert res[25, 25, 0] > 150 + assert res[0, 0, 0] == 100 + + +def test_selective_adjust_linear_gradient() -> None: + img = np.full((40, 40, 3), 120, dtype=np.uint8) + params = SelectiveAdjustParams( + shape="linear", + center=(0.5, 0.5), + angle=0.0, + exposure=0.5, + ) + op = SelectiveAdjustOperation(params) + res = op.apply(img) + assert res.shape == (40, 40, 3) + assert res[5, 20, 0] >= res[35, 20, 0] + + +def test_selective_adjust_box_mask() -> None: + img = np.full((30, 30, 3), 80, dtype=np.uint8) + params = SelectiveAdjustParams( + shape="box", + center=(0.5, 0.5), + size=(0.4, 0.4), + feather=0.1, + exposure=1.0, + ) + op = SelectiveAdjustOperation(params) + res = op.apply(img) + assert res[15, 15, 0] > 120 + assert res[0, 0, 0] == 80 + + +def test_selective_adjust_invert_and_warmth() -> None: + img = np.full((20, 20, 3), 120, dtype=np.uint8) + params = SelectiveAdjustParams( + shape="radial", + center=(0.5, 0.5), + size=(0.3, 0.3), + invert=True, + warmth=0.8, + saturation=1.5, + ) + op = SelectiveAdjustOperation(params) + res = op.apply(img) + assert res[0, 0, 0] > res[0, 0, 2] + + +def test_selective_adjust_blur_radius() -> None: + img = np.random.randint(0, 255, (30, 30, 3), dtype=np.uint8) + params = SelectiveAdjustParams( + shape="radial", + center=(0.5, 0.5), + size=(0.4, 0.4), + blur_radius=5, + ) + op = SelectiveAdjustOperation(params) + res = op.apply(img) + assert res.shape == (30, 30, 3) + + +def test_selective_adjust_alpha_preservation() -> None: + rgba = np.full((20, 20, 4), 100, dtype=np.uint8) + rgba[:, :, 3] = 220 + params = SelectiveAdjustParams(exposure=0.5) + op = SelectiveAdjustOperation(params) + res = op.apply(rgba) + assert res.shape == (20, 20, 4) + assert np.all(res[:, :, 3] == 220) + + +def test_selective_adjust_registered() -> None: + op_cls = default_registry.get("selective_adjust") + assert op_cls is SelectiveAdjustOperation