@vitavision/stage2d 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitavision/stage2d",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "The vitavision 2D image stage: view transform, zoom/pan, layered overlays, measurement.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "repository": {
@@ -46,7 +46,7 @@
46
46
  "react-dom": "^19.2.0"
47
47
  },
48
48
  "dependencies": {
49
- "@vitavision/ui": "^0.14.0",
49
+ "@vitavision/ui": "^0.14.1",
50
50
  "lucide-react": "^1.48.0"
51
51
  },
52
52
  "devDependencies": {
@@ -15,9 +15,11 @@
15
15
  * - **Pixelated past a zoom.** Above about 4× the browser's smoothing paints detail the
16
16
  * sensor never recorded; on a metrology bench the sensor's own samples are what is worth
17
17
  * looking at.
18
+ * - **A display tone curve.** A dark or low-contrast frame is read with a gain and a gamma
19
+ * applied on screen only (`gain`, `gamma`): the pixels the app measures are untouched.
18
20
  */
19
21
 
20
- import { useEffect, useRef, useState } from "react";
22
+ import { useEffect, useId, useRef, useState } from "react";
21
23
 
22
24
  import { cn } from "@vitavision/ui";
23
25
  import { useStage } from "./ImageStage";
@@ -47,6 +49,20 @@ export interface ImageLayerBaseProps {
47
49
  onLoad?: (() => void) | undefined;
48
50
  /** Called when the full image fails to load. */
49
51
  onError?: (() => void) | undefined;
52
+ /**
53
+ * Display gain: each channel is multiplied by it and clipped at white, after `gamma`.
54
+ * Defaults to 1. A negative value counts as 0, one that is not finite as 1.
55
+ *
56
+ * Like `gamma`, it changes what is drawn, never the image data: a way to read a dark or
57
+ * low-contrast frame, not a correction to measure on.
58
+ */
59
+ gain?: number | undefined;
60
+ /**
61
+ * Display gamma, as an image editor's levels control: each channel `v` (0 to 1, as encoded in
62
+ * the file) is drawn as `gain × v^(1 / gamma)`. Above 1 it lifts the shadows and mid-tones,
63
+ * below 1 it darkens them. Defaults to 1; a value that is not positive counts as 1.
64
+ */
65
+ gamma?: number | undefined;
50
66
  /** Merged onto both images with `cn`. */
51
67
  className?: string | undefined;
52
68
  }
@@ -79,12 +95,29 @@ export type ImageLayerProps = ImageLayerBaseProps &
79
95
  * With a `preview`, the full image is wanted once the stage would magnify the preview, and
80
96
  * `onFullNeeded` says so; `src` can be supplied then rather than up front.
81
97
  *
98
+ * With a `gain` or `gamma` other than 1, both images are drawn through one SVG tone-curve filter
99
+ * (sRGB values, per channel) and carry `data-adjusted`.
100
+ *
82
101
  * Each image carries `data-tier` (`preview` or `full`); the full image carries `data-loaded`
83
102
  * once it has loaded, the preview carries `data-wants-full` from the moment the full image is
84
103
  * wanted until it has loaded, and both carry `data-pixelated` while pixelated.
85
104
  */
86
- export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded, onLoad, onError, className }: ImageLayerProps) {
105
+ export function ImageLayer({
106
+ src,
107
+ preview,
108
+ alt,
109
+ pixelatedAbove = 4,
110
+ onFullNeeded,
111
+ onLoad,
112
+ onError,
113
+ gain,
114
+ gamma,
115
+ className,
116
+ }: ImageLayerProps) {
87
117
  const stage = useStage();
118
+ const curve = toneCurve(gain, gamma);
119
+ // `useId` may contain characters that are not valid in a CSS `url(#…)` reference.
120
+ const filterId = `stage-image-tone-${useId().replace(/[^A-Za-z0-9_-]/g, "")}`;
88
121
  const scale = stage.view.scale;
89
122
  // The full image is wanted once the preview would be magnified on screen: its own pixels
90
123
  // per image pixel are `preview.width / image.width`. Not before the viewport is measured:
@@ -112,9 +145,22 @@ export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded
112
145
  const pixelated = scale >= pixelatedAbove;
113
146
  const shared = cn("pointer-events-none absolute inset-0 h-full w-full select-none", className);
114
147
  const rendering = pixelated ? ("pixelated" as const) : undefined;
148
+ const style = { imageRendering: rendering, filter: curve ? `url(#${filterId})` : undefined };
149
+ const adjusted = curve ? "" : undefined;
115
150
 
116
151
  return (
117
152
  <>
153
+ {curve && (
154
+ <svg aria-hidden="true" width="0" height="0" className="pointer-events-none absolute" focusable="false">
155
+ <filter id={filterId} colorInterpolationFilters="sRGB" data-tone-curve="">
156
+ <feComponentTransfer>
157
+ <feFuncR type="gamma" amplitude={curve.amplitude} exponent={curve.exponent} offset={0} />
158
+ <feFuncG type="gamma" amplitude={curve.amplitude} exponent={curve.exponent} offset={0} />
159
+ <feFuncB type="gamma" amplitude={curve.amplitude} exponent={curve.exponent} offset={0} />
160
+ </feComponentTransfer>
161
+ </filter>
162
+ </svg>
163
+ )}
118
164
  {preview && !fullReady && (
119
165
  <img
120
166
  src={preview.src}
@@ -124,8 +170,9 @@ export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded
124
170
  data-tier="preview"
125
171
  data-wants-full={wanted ? "" : undefined}
126
172
  data-pixelated={pixelated ? "" : undefined}
173
+ data-adjusted={adjusted}
127
174
  className={shared}
128
- style={{ imageRendering: rendering }}
175
+ style={style}
129
176
  />
130
177
  )}
131
178
  {showFull && (
@@ -136,15 +183,27 @@ export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded
136
183
  data-tier="full"
137
184
  data-loaded={fullReady ? "" : undefined}
138
185
  data-pixelated={pixelated ? "" : undefined}
186
+ data-adjusted={adjusted}
139
187
  onLoad={() => {
140
188
  setLoaded(src);
141
189
  onLoad?.();
142
190
  }}
143
191
  onError={() => onError?.()}
144
192
  className={cn(shared, preview && !fullReady && "opacity-0")}
145
- style={{ imageRendering: rendering }}
193
+ style={style}
146
194
  />
147
195
  )}
148
196
  </>
149
197
  );
150
198
  }
199
+
200
+ /**
201
+ * The `feFuncX type="gamma"` parameters (`amplitude × v^exponent`) for a gain and a gamma, or
202
+ * `null` when the curve is the identity and no filter is needed.
203
+ */
204
+ function toneCurve(gain: number | undefined, gamma: number | undefined): { amplitude: number; exponent: number } | null {
205
+ const amplitude = gain === undefined || !Number.isFinite(gain) ? 1 : Math.max(0, gain);
206
+ const g = gamma === undefined || !(gamma > 0) || !Number.isFinite(gamma) ? 1 : gamma;
207
+ if (amplitude === 1 && g === 1) return null;
208
+ return { amplitude, exponent: 1 / g };
209
+ }