diff --git a/plugins/index.json b/plugins/index.json index 928d069..585d17d 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -13,9 +13,9 @@ }, { "id": "image-annotate", - "file": "plugin-image-annotate.a8227863bec8.js", - "version": "1.20.2", - "integrity": "sha256-qCJ4Y77InZpHP4YowJce4TiQ8/ybBQhBZHwznAYgVHE=" + "file": "plugin-image-annotate.443423515c0e.js", + "version": "1.20.3", + "integrity": "sha256-RDQjUVwOogb+baL6NDlfRFNJSznTM7q3lMraGe8jal4=" }, { "id": "image-vectorize", diff --git a/plugins/runtime/plugin-image-annotate.a8227863bec8.js b/plugins/runtime/plugin-image-annotate.443423515c0e.js similarity index 99% rename from plugins/runtime/plugin-image-annotate.a8227863bec8.js rename to plugins/runtime/plugin-image-annotate.443423515c0e.js index 4bee409..b049a86 100644 --- a/plugins/runtime/plugin-image-annotate.a8227863bec8.js +++ b/plugins/runtime/plugin-image-annotate.443423515c0e.js @@ -9,7 +9,7 @@ var __IWB_ANNOTATE_MANIFEST__ = { id: 'image-annotate', name: '标注 / 修图', - version: '1.20.2', + version: '1.20.3', apiVersion: 1, capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'], settings: { @@ -3899,7 +3899,26 @@ var __IWB_ANNOTATE_MANIFEST__ = { step: 1, def: () => 100, render: (canvas, v) => { - if (v > 0) filterCanvas(canvas, "invert(" + v / 100 + ")"); + if (v <= 0) return; + const k = v / 100; + if (!FILTER_SUPPORTED) { + const bright = 1 + 0.4 * k, contrast = 1 + 0.25 * k, sat = 1 + 0.2 * k; + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b); + const out = [r, g, b].map((c) => { + let v2 = y + (c - y) * sat; + v2 = (v2 - 128) * contrast + 128; + return clamp255(v2 * bright); + }); + return out; + }); + return; + } + if (FILTER_SUPPORTED) { + filterCanvas(canvas, "invert(" + k + ")"); + return; + } + mapPixels(canvas, (r, g, b) => [clamp255(255 - (255 - r) * k), clamp255(255 - (255 - g) * k), clamp255(255 - (255 - b) * k)]); } }, gray: { @@ -3912,7 +3931,16 @@ var __IWB_ANNOTATE_MANIFEST__ = { step: 1, def: () => 100, render: (canvas, v) => { - if (v > 0) filterCanvas(canvas, "grayscale(" + v / 100 + ")"); + if (v <= 0) return; + const k = v / 100; + if (FILTER_SUPPORTED) { + filterCanvas(canvas, "grayscale(" + k + ")"); + return; + } + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b); + return [clamp255(r + (y - r) * k), clamp255(g + (y - g) * k), clamp255(b + (y - b) * k)]; + }); } }, boost: { @@ -3942,7 +3970,17 @@ var __IWB_ANNOTATE_MANIFEST__ = { render: (canvas, v) => { if (v <= 0) return; const k = v / 100; - filterCanvas(canvas, "grayscale(" + k.toFixed(3) + ") contrast(" + (1 + 1.6 * k).toFixed(3) + ")"); + if (FILTER_SUPPORTED) { + filterCanvas(canvas, "grayscale(" + k.toFixed(3) + ") contrast(" + (1 + 1.6 * k).toFixed(3) + ")"); + return; + } + const c = 1 + 1.6 * k; + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b); + const gray = y * k + (r + g + b) / 3 * (1 - k); + const v2 = (gray - 128) * c + 128; + return [clamp255(v2), clamp255(v2), clamp255(v2)]; + }); } }, soft: { @@ -3955,7 +3993,9 @@ var __IWB_ANNOTATE_MANIFEST__ = { step: 0.5, def: (canvas) => softRadius(canvas), render: (canvas, v) => { - if (v > 0) filterCanvas(canvas, "blur(" + v + "px)"); + if (v <= 0) return; + if (FILTER_SUPPORTED) filterCanvas(canvas, "blur(" + v + "px)"); + else blurCanvas(canvas, v); } }, mosaic: { @@ -3995,6 +4035,60 @@ var __IWB_ANNOTATE_MANIFEST__ = { ctx.drawImage(tmp, 0, 0); ctx.restore(); } + var FILTER_SUPPORTED = function() { + try { + const c = document.createElement("canvas"); + const ctx = c.getContext("2d"); + if (!ctx) return false; + ctx.filter = "invert(1)"; + return ctx.filter === "invert(1)"; + } catch (e) { + return false; + } + }(); + function mapPixels(canvas, fn) { + const w = canvas.width, h = canvas.height; + if (!w || !h) return; + const ctx = canvas.getContext("2d", { willReadFrequently: true }); + const img = ctx.getImageData(0, 0, w, h); + const d = img.data; + for (let i = 0; i < d.length; i += 4) { + if (d[i + 3] === 0) continue; + const out = fn(d[i], d[i + 1], d[i + 2]); + d[i] = out[0]; + d[i + 1] = out[1]; + d[i + 2] = out[2]; + } + ctx.setTransform(1, 0, 0, 1, 0, 0); + ctx.putImageData(img, 0, 0); + } + function luma(r, g, b) { + return 0.2126 * r + 0.7152 * g + 0.0722 * b; + } + function clamp255(v) { + return v < 0 ? 0 : v > 255 ? 255 : v; + } + function blurCanvas(canvas, radius) { + const w = canvas.width, h = canvas.height; + if (!w || !h || radius <= 0) return; + const n = Math.max(2, Math.min(16, Math.round(radius * 1.6))); + const sw = Math.max(1, Math.round(w / n)); + const sh = Math.max(1, Math.round(h / n)); + const tmp = document.createElement("canvas"); + tmp.width = sw; + tmp.height = sh; + const tctx = tmp.getContext("2d"); + tctx.imageSmoothingEnabled = true; + tctx.drawImage(canvas, 0, 0, sw, sh); + const ctx = canvas.getContext("2d"); + ctx.save(); + ctx.setTransform(1, 0, 0, 1, 0, 0); + ctx.globalAlpha = 1; + ctx.globalCompositeOperation = "copy"; + ctx.imageSmoothingEnabled = true; + ctx.drawImage(tmp, 0, 0, w, h); + ctx.restore(); + } function pixelateCanvas(canvas, block) { const w = canvas.width, h = canvas.height; if (!w || !h) return;