Free open-source image editor for the browser
Add histogram feature
Rodrigo1a5762755357
diff --git a/src/app/editor.tsx b/src/app/editor.tsxindex 47f2579..8a8c454 100644--- a/src/app/editor.tsx+++ b/src/app/editor.tsx@@ -3,6 +3,7 @@ import { sampler } from "vgpu";import { Canvas, useCanvas, useFrameLoop, useGpu, useShader } from "vgpu-react";import ResizablePanel from "@/components/ui/resizable-panel";import Spinner from "@/components/ui/spinner";+import Histogram from "@/features/histogram/histogram";import usePanZoom, { type View } from "@/hooks/use-pan-zoom";import decode, { type Target } from "@/lib/decode";import shader from "./editor.wgsl";@@ -74,7 +75,7 @@ export default function Editor({ file }: EditorProps) {<Spinner />)}</div>- <ResizablePanel />+ <ResizablePanel>{image && <Histogram image={image} />}</ResizablePanel></main>);}diff --git a/src/app/editor.wgsl b/src/app/editor.wgslindex c6bac68..db35f9a 100644--- a/src/app/editor.wgsl+++ b/src/app/editor.wgsl@@ -1,4 +1,4 @@-import { linearToSrgb3 } from "@vgpu/wgsl-std/color";+import { display } from "../lib/color.wgsl";struct Params {size: vec2f,@@ -12,19 +12,11 @@ struct Params {const background = vec3f(0.09);-// Rec.2020, the working space, to linear sRGB primaries (column-major).-const rec2020ToSrgb = mat3x3f(- 1.6605, -0.1246, -0.0182,- -0.5876, 1.1329, -0.1006,- -0.0728, -0.0083, 1.1187,-);-@fragment fn fs_main(@location(0) uv: vec2f) -> @location(0) vec4f {// Zoom 1 fits the image (contain, capped at 1:1), scaled about the center, then shifted by pan.let scale = min(min(params.size.x / params.image.x, params.size.y / params.image.y), 1.0) * params.zoom;let p = ((uv - 0.5) * params.size - params.pan) / (params.image * scale) + 0.5;let inside = all(p >= vec2f(0.0)) && all(p <= vec2f(1.0));- let linear = rec2020ToSrgb * textureSampleLevel(source, sourceSampler, p, 0.0).rgb;- let color = linearToSrgb3(clamp(linear, vec3f(0.0), vec3f(1.0)));+ let color = display(textureSampleLevel(source, sourceSampler, p, 0.0).rgb);return vec4f(select(background, color, inside), 1.0);}diff --git a/src/features/histogram/bins.wgsl b/src/features/histogram/bins.wgslnew file mode 100644index 0000000..75b31fb--- /dev/null+++ b/src/features/histogram/bins.wgsl@@ -0,0 +1,25 @@+import { display } from "../../lib/color.wgsl";++@group(0) @binding(0) var source: texture_2d<f32>;+// 256 bins per channel, then the peak of the interior bins for scaling the chart.+@group(0) @binding(1) var<storage, read_write> bins: array<atomic<u32>, 769>;++const samples = vec2u(512, 320);++fn count(channel: u32, bin: u32) {+ let n = atomicAdd(&bins[channel * 256u + bin], 1u) + 1u;+ if (bin > 0u && bin < 255u) {+ atomicMax(&bins[768], n);+ }+}++@compute @workgroup_size(16, 16) fn main(@builtin(global_invocation_id) id: vec3u) {+ if (any(id.xy >= samples)) {+ return;+ }+ let texel = textureLoad(source, id.xy * textureDimensions(source) / samples, 0).rgb;+ let bin = vec3u(round(display(texel) * 255.0));+ count(0u, bin.r);+ count(1u, bin.g);+ count(2u, bin.b);+}diff --git a/src/features/histogram/histogram.tsx b/src/features/histogram/histogram.tsxnew file mode 100644index 0000000..12dbcb3--- /dev/null+++ b/src/features/histogram/histogram.tsx@@ -0,0 +1,80 @@+import { useMemo, useRef, useState } from "react";+import { compute, storage, type Target } from "vgpu";+import { useFrameLoop, useGpu } from "vgpu-react";+import shader from "./bins.wgsl";++const channels = ["#f25445", "#6bd175", "#5c8ffa"];+const empty = new Uint32Array(769);++/** Bins a 512×320 sampling of the displayed image on the GPU, as often as the readback allows. */+function useBins(image: Target) {+ const gpu = useGpu();+ const [bins, setBins] = useState(empty);+ const buffer = useMemo(() => storage(gpu, empty.byteLength), [gpu]);+ const counter = useMemo(+ () => compute(gpu, shader, { set: { bins: buffer } }),+ [gpu, buffer],+ );+ const reading = useRef(false);++ useFrameLoop(() => {+ if (reading.current) {+ return;+ }+ reading.current = true;+ buffer.write(empty);+ counter.set({ source: image.color }).dispatch(32, 20);+ buffer.read().then((data) => {+ reading.current = false;+ setBins(new Uint32Array(data));+ });+ });++ return bins;+}++/** One channel as "x,y" pairs on a 255×100 box: 5-tap smoothed, square-root scaled to the interior peak. */+function curve(bins: Uint32Array, channel: number) {+ const peak = Math.max(bins[768], 1);+ return Array.from({ length: 256 }, (_, bin) => {+ let sum = 0;+ for (let k = -2; k <= 2; k++) {+ const i = Math.min(255, Math.max(0, bin + k));+ sum += bins[channel * 256 + i] * (3 - Math.abs(k));+ }+ return `${bin},${(100 - 100 * Math.sqrt(sum / 9 / peak)).toFixed(1)}`;+ }).join(" ");+}++type HistogramProps = { image: Target };++export default function Histogram({ image }: HistogramProps) {+ const bins = useBins(image);+ return (+ <svg+ className="h-24 w-full"+ preserveAspectRatio="none"+ viewBox="0 0 255 100"+ >+ <title>Histogram</title>+ {channels.map((color, channel) => {+ const points = curve(bins, channel);+ return (+ <g key={color}>+ <polygon+ fill={color}+ fillOpacity={0.2}+ points={`0,100 ${points} 255,100`}+ />+ <polyline+ fill="none"+ points={points}+ stroke={color}+ vectorEffect="non-scaling-stroke"+ />+ </g>+ );+ })}+ </svg>+ );+}diff --git a/src/lib/color.wgsl b/src/lib/color.wgslnew file mode 100644index 0000000..8dd1edd--- /dev/null+++ b/src/lib/color.wgsl@@ -0,0 +1,19 @@+import { linearToSrgb3 } from "@vgpu/wgsl-std/color";++// The working space is linear Rec.2020; these convert its primaries to and from linear sRGB (column-major).+export const srgbToRec2020 = mat3x3f(+ 0.6274, 0.0691, 0.0164,+ 0.3293, 0.9195, 0.0880,+ 0.0433, 0.0114, 0.8956,+);++export const rec2020ToSrgb = mat3x3f(+ 1.6605, -0.1246, -0.0182,+ -0.5876, 1.1329, -0.1006,+ -0.0728, -0.0083, 1.1187,+);++/** Working space to what the screen shows: sRGB, clipped to its gamut, encoded. */+export fn display(working: vec3f) -> vec3f {+ return linearToSrgb3(clamp(rec2020ToSrgb * working, vec3f(0.0), vec3f(1.0)));+}diff --git a/src/lib/decode/linearize.wgsl b/src/lib/decode/linearize.wgslindex a84c812..6adac06 100644--- a/src/lib/decode/linearize.wgsl+++ b/src/lib/decode/linearize.wgsl@@ -1,11 +1,6 @@-@group(0) @binding(0) var source: texture_2d<f32>;+import { srgbToRec2020 } from "../color.wgsl";-// Linear sRGB primaries to Rec.2020, the working space (column-major).-const srgbToRec2020 = mat3x3f(- 0.6274, 0.0691, 0.0164,- 0.3293, 0.9195, 0.0880,- 0.0433, 0.0114, 0.8956,-);+@group(0) @binding(0) var source: texture_2d<f32>;@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {let texel = textureLoad(source, vec2i(position.xy), 0);