ADJUSTMENT ENGINE / LEVELS
Photoshot development 03: aligning levels, handles and histograms
A correct pixel transform is only part of the tool. The handles and the graph must explain the same operation. We checked the relationship with a pinned source replay and an editable-project round trip.
Abstract
Input black, gamma and input white belong to one levels transform. This entry separates the September 18 engine work from the September 22 histogram controls, then replays 18 gamma/range cases and nine archived gray outputs. The new browser check used the deployed files served locally. The historical full-chart Photoshop comparison was not rerun today.
1. Goal: one meaning across pixels and controls
Photoshot is an independent browser image editor. The goal here was to make numerical levels, draggable handles and the histogram describe the same input. A useful control must communicate its transformation as well as execute it.
Input black and white delimit the source interval; gamma changes its midtones. Output black and white delimit the resulting range. A combined RGB transform precedes the individual channel transform. Alpha, masks and layer compositing remain the responsibility of the existing adjustment-layer pipeline.
2. The initial implementation and its missing explanation
The archived September 18 record describes the adjustment engine and numeric panels first. The September 22 work adds the image-derived histogram and black, middle and white handles. These were separate development steps, not one retrospectively complete implementation.
Numeric fields alone do not show where a photograph contains shadows and highlights. Adding a graph raises a second problem: a weighted brightness histogram and a distribution of RGB channel values describe different quantities. The graph has to preserve that distinction.
3. Making failure conditions explicit
A middle handle fixed halfway between black and white is correct only at gamma 1. With black 0, white 255 and gamma 2, input 127.5 maps to approximately 0.707. A handle representing output 0.5 belongs at input 63.75. This is a diagnostic calculation of an incorrect alternative, not a reconstruction of an undocumented historical failure screen.
A second counterexample uses one opaque pure-red pixel. Its weighted brightness bin is 54. Its combined RGB distribution instead places 2/3 at bin 0 and 1/3 at bin 255. A fully transparent second pixel contributes nothing to either distribution. Treating these graphs as interchangeable would erase the meaning of the channel choice.
4. Pinned GitHub sources and original integration
The official GIMP Levels manual was a conceptual reference for the input range, output range and midtone control. This is not a port of GIMP C code. The listings below come from Photoshot revision 19f45ed. Its COPYRIGHT.md makes the source publicly visible without granting an open-source license for the original Photoshot code.
The levels transform is compiled into the existing channel lookup tables. The integration written and revised with AI connects a gamma number to a handle, turns a drag position back into a number, and accumulates image distributions with alpha weights. Conceptual reference material and code implemented inside Photoshot are separate contributions.
if (t === 'levels')
for (const c of ['rgb', channel])
x =
(v[c + 'OutBlack'] +
(v[c + 'OutWhite'] - v[c + 'OutBlack']) *
clamp((x * 255 - v[c + 'Black']) / (v[c + 'White'] - v[c + 'Black'])) **
(1 / v[c + 'Gamma'])) /
255;5. Application: the inverse function and histogram definitions
For normalized input t, output is t^(1/gamma). Setting output to 0.5 gives t = 0.5^gamma, so the handle position is black + (white − black) × 0.5^gamma. The inverse uses log(t) / log(0.5). The implementation clamps gamma to 0.1–10 and rounds the displayed value to two decimal places.
Histogram analysis uses alpha/255 as its weight. Fully transparent pixels are skipped and partial alpha contributes proportionally. Weighted brightness bins and R/G/B bins are accumulated separately; the combined RGB distribution averages the three channel bins. Logarithmic display height reveals small peaks without changing the adjustment transform.
// The middle handle marks the input that becomes 50% after inverse gamma.
export function gammaPosition(black: number, white: number, gamma: number) {
return black + (white - black) * Math.pow(0.5, clamp(gamma, 0.1, 10));
}export function gammaAtPosition(black: number, white: number, position: number) {
const t = clamp(
(position - black) / Math.max(1, white - black),
Math.pow(0.5, 10),
Math.pow(0.5, 0.1),
);
return Math.round(clamp(Math.log(t) / Math.log(0.5), 0.1, 10) * 100) / 100;
} for (let i = 0; i < data.length; i += 4) {
const weight = data[i + 3] / 255;
if (!weight) continue;
bins[Math.round(0.2126 * data[i] + 0.7152 * data[i + 1] + 0.0722 * data[i + 2])] += weight;
channelBins.r[data[i]] += weight;
channelBins.g[data[i + 1]] += weight;
channelBins.b[data[i + 2]] += weight;
count += weight;
}
// RGB shows the distribution of channel values, not the luminance histogram.
for (let i = 0; i < 256; i++)
channelBins.rgb[i] = (channelBins.r[i] + channelBins.g[i] + channelBins.b[i]) / 3;6. Verification, archived measurements and limits
The pinned source passed 18 cases: six gamma values across three input ranges, including a narrow one-step range. Each position-to-gamma round trip recovered the input gamma and each marker mapped to output 0.5. The existing tests also passed alpha weighting, channel versus brightness, empty distributions and the rightmost bin. With input 18/1.12/235 and output 0/255, all nine gray outputs matched the archived Photoshot values.
In a real browser we opened a new 1536×1024 portrait and applied those levels. Undo changed input white from 235 to 255; redo restored 235. After saving, closing and reopening the editable project, two layers and the 18/1.12/235 settings returned. External connection failures required serving the deployment files locally. This does not establish that every part of the live service was tested.
An archived September 25 Photoshop chart comparison reports mean absolute channel difference 0.229 and maximum 1. No new full chart or Photoshop reference was produced for this revision, so those remain historical measurements. These small deterministic tests do not establish rendering speed, universal color-profile support or compatibility for every image.
