Brighter in, brighter out
rawr, my iPhone camera: the RAW video the hardware wouldn't give me, film stages in physical order, and the one job a tone curve has.
rawr is the camera app for my iPhone that I started in March. On the first day I wrote down what I wanted: Bayer RAW stills, ProRes RAW video, and a processing pipeline you can see into and control, an "honest camera" that "tells you exactly what happened to your sensor data".
The video half depended on a pixel format and a codec that arrived with the iPhone 17 Pro. I have a 16 Pro, so that afternoon I checked whether either one shows up on it anyway. Neither did, even through a private format selector. The fallback was to build RAW video out of RAW stills, shot as fast as the phone allows and kept as keyframes beside an ordinary video track, and I'd decided that needed at least six stills a second. The 16 Pro gave me Apple ProRAW, which arrives already demosaiced, at about 2.24 a second, and at a quarter of the pixels I'd asked for. My roadmap had a line in it that read "If the answer is weak, everything else is theater." That evening I took the line out and put stills first.
Film, in the order it happens#
The other half of honest was the processing. I wanted film looks, built as a simulation of what film does, in the order it does it. Light spreads inside the emulsion, and some of it bounces back off the film base and exposes the emulsion a second time, which is halation, and that happens during the exposure itself. The silver halide crystals respond to the light, and the crystals are the grain. How much density a given exposure produces is the film's characteristic curve. Then comes development, then the print.
The day after the hardware probes I wrote that order into a spec, with the reason for the grain's place spelled out: "Real grain exists in exposure space, before the nonlinear curve bends it." Grain added before the curve gets compressed wherever the curve flattens, in the deep shadows and the brightest highlights, and comes through at full strength in the middle, which is where you see it on real film. The spec builds the curve on log exposure, the way film is measured. The code so far indexes it by linear exposure, so the grain goes in before the curve, but not yet in the space the spec describes.
The characteristic curve is Hurter and Driffield's. In 1890 they plotted the density of developed plates against the logarithm of the exposure that produced it, and the plot has a slow toe in the shadows, a long straight middle, and a shoulder where the highlights run out. Inside a camera, all that bending serves one job: brighter in, brighter out. The curve can flatten as much as it likes, and it can never turn back.
Every picture a negative#
On the third day I moved the film stages onto Metal, and for about five hours afterwards the film render came out as a negative. The curve turned an exposure into a density between the film's base fog and its maximum, normalized that to between 0 and 1, and then, under a comment calling the result "print-space luminance", subtracted it from 1. In illustrative form:
let normalized = (density - baseFog) / (maxDensity - baseFog)
return 1 - normalized
On a negative, dense means dark, so somewhere a picture made of densities has to be inverted to look right. Mine already had been. In my docs, "print space" meant the positive picture, and the day before I'd put it in as many words: "Our pipeline already works in positive space." The extra 1 - inverted it a second time, and both the Metal tone-curve texture and the Core Image fallback took their values from it.
The tests passed the whole time. They checked that the curve had 4096 samples, that every sample was between 0 and 1, and that the three channels differed, and an upside-down curve satisfies all three. With the fix I added a test for the curve's one job, in its weakest form: in each channel, the first sample has to be darker than the last.
Between the points#
Two hours later I refit the first three stocks, Portra 400, Ektar 100 and Tri-X 400, against Kodak's datasheets. Each channel's curve is eight points I entered by hand, with the datasheet noted beside each stock. The old points for every stock had ended in a plateau, four values in a row at exactly 1, and the new ones climb into a shoulder.
Eight points don't make a curve. Something has to draw it between them, and the obvious choice, a smooth cubic through every point, can go the wrong way between points that only ever go up. A cubic spline bends to stay smooth, and where a steep climb meets a flat stretch it overshoots and has to come back. As an illustration, take the five points my first version averaged out of Portra's curves and put them through an ordinary natural cubic spline. It rises to about 1.04 just past the top of the climb, then sinks to 0.99 before it levels off. The sinking is the part that breaks the rule. Brighter in, darker out.
What I built instead draws each piece between two points as a cubic Hermite segment, a cubic fixed by the values and slopes at its two ends, and it chooses the slopes so the curve can't turn back. Every slope starts as the average of the straight-line slopes on either side of its point, and a flat stretch gets zero slope at both ends. Then, segment by segment, it divides the two end slopes by the segment's own straight-line slope, calls the ratios α and β, and if the point (α, β) lies outside the circle of radius 3, scales both back onto it. Inside that circle the segment is guaranteed monotone. The construction is Fritsch and Carlson's, from Monotone Piecewise Cubic Interpolation (SIAM Journal on Numerical Analysis, 1980).
The tests I wrote alongside it check that the points rise, that the curve passes within 0.002 of every one, and that nothing jumps where the toe and the shoulder meet the straight part. The direction test from that evening still compares the first sample with the last. Between the points nothing tests the curve's one job, and the guarantee there is Fritsch and Carlson's.