Principle · How it works

How an ISP turns RAW into an image

Follow the image signal processor as it estimates missing colours in a RAW mosaic and prepares the result for an output.

From RAW samples to output
RAW mosaicOne code per photosite
Subtract black · reconstruct RGB · calibrate colour
Working RGBCalibrated values for editing
Choose tone and fit colours to output
Display-linear RGBLight values for the display
Apply transfer curve
Encoded RGBCodes for a photo file or video signal

Why do black-and-white stripes come out in colour?

In the capture article, the Bayer sensor recorded one code per position. The image signal processor (ISP) has to produce three colour components at that position. On the one-pixel stripes of our Colour test chart, the samples used to fill in those missing components can come from opposite sides of an edge.

Put the RAW codes on a light scale

The ISP needs to know which filter sits at each position, which code represents darkness and where the usable signal saturates. In DNG, for example, an optional linearisation table converts the stored codes to a scale proportional to light before black subtraction. For a single black level, subtract it and divide by the difference between the saturation and black levels. On that sensor scale, 0 means darkness and 1 means sensor or readout saturation. In the stripe example, 864 is the code for chart white, which we use as the reference for the preview.

Reconstruct one pixel

Start by subtracting the black reference of 64 from the RAW codes. A photosite on chart white now reads 800 and one on chart black reads zero. The marked photosite in row 2, column 2 has a blue filter. In the original alignment it sits on black. Its blue component is measured, so we keep zero.

To estimate the missing components, use bilinear demosaicing. Average the four green neighbours above, below, left and right. Two sit on white and two on black, so the estimate is 400. Average the four red neighbours on the diagonals. All four sit on white, so red comes out as 800.

Choose Trace channel to see the exact samples in the average. Select Blue to highlight the measured value at the outlined site.

Reconstruct the outlined blue site
Signal above black · RAW code minus 64
RowColumn 1, WhiteColumn 2, BlackColumn 3, WhiteColumn 4, Black
1R 800G 0, contributes to GR 800G 0
2G 800, contributes to GB 0, photosite used in the reconstructionG 800, contributes to GB 0
3R 800G 0, contributes to GR 800G 0
4G 800B 0G 800B 0
Chart pixel: black
Reconstructed pixel: amber
G averages 4 marked neighbours: (0 + 800 + 800 + 0) / 4 = 400.Linear RGB above black: (800, 400, 0)

The outlined pixel on the black stripe is reconstructed as amber. Before shifting the stripes by one photosite, predict what happens to each component. After the shift, the outlined site measures 800 in blue. The red estimate falls to zero and green stays at 400, giving sky blue. The preview divides the signals by 800 and encodes these ideal RGB values for sRGB display.

A defective photosite can also contaminate several reconstructed pixels through the same averaging. Correcting its sample in the mosaic keeps the error out of neighbouring estimates.

From one pixel to a false-colour pattern

Start with Bayer and 1 photosite. White and black repeat every two columns, just like the red and blue sampling positions. Every red site sees white and every blue site sees black. This is spatial aliasing. From the red measurements alone, the stripes could also be a uniform white field. Shift the pattern and the two channels swap their readings.

When does a neutral pattern acquire colour?
Original stripes
Measured samplesCell colour marks the red, green or blue filter. Filled cells sample white. Outlined cells sample black.Samples on white: R 36/36 · G 36/72 · B 0/36
Reconstructed RGB
144 of 144 reconstructed pixels have unequal RGB channels. Top-left pixel: R filter on white in the target, linear RGB (1, 0.5, 0) after reconstruction.

With Bayer still selected, set Stripe width to 8 photosites. All three estimates agree inside each stripe. False colour clusters near the edges, where the reconstruction mixes measurements from opposite sides. An edge-aware estimate can use samples from the same side where they exist.

Return to 1 photosite and switch Colour-filter mosaic. Quad Bayer and X-Trans place red and blue sites on both kinds of stripe. Compare the counts below Measured samples, then look for the different patterns of false colour in Reconstructed RGB.

All three layouts keep the measured value at each site. For each missing channel, they average the samples of that colour tied for closest. The comparison uses sharp stripes and ideal filters, and reads Quad Bayer at full resolution. In a camera, optical blur or a low-pass filter can reduce stripe contrast before sampling.

From camera RGB to an image you can edit

Imagine photographing a neutral grey card and a red patch beside a bright window. After demosaicing, every pixel already has an R, G and B value. Those numbers still describe the responses of this camera’s filters. To make a working image, we first account for the light in the room and then express the colours in a known RGB space.

White balance sets the neutral reference. Under warm light the grey card might give camera RGB (0.40, 0.32, 0.20). Multiplying the channels by (0.80, 1.00, 1.60) gives (0.32, 0.32, 0.32). Apply those same gains to the whole image, including the red patch.

Colour calibration

A grey card helps set white balance. A colour chart shows how the camera reads other colours under the same light. With both gains at 1×, the grey patches in Camera preview are neutral, but the red patch looks duller than the reference. The numbers from the camera’s filters mean something different from the same numbers in linear sRGB. The colour matrix uses all nine patches to translate between the two.

Select Mid grey on any chart and lower Red gain. The middle patch takes on a cyan cast as its red reading falls. The right-hand patch stays grey because the example fits a new matrix to the changed readings. Watch the weight on camera R below the charts.

One colour chart, before and after calibration
Simulate a white-balance error
Both gains at 1× show the original camera readings after white balance.Selected patch: Red.
Reference
RGB (0.636, 0.168, 0.152)
Camera preview
RGB (0.560, 0.200, 0.160)
After calibration
RGB (0.636, 0.168, 0.152)
Working red: weight on camera R 1.20 · weight on camera B −0.10. See the full matrix.All three charts use the same sRGB display curve. The middle chart shows what happens when camera RGB is displayed directly as linear sRGB.

How we fit the colour correction matrix

The matrix has nine weights. Each row tells the processor how much of the camera’s R, G and B readings to add or subtract to get one working RGB channel.

Match each camera patch to the reference in the same position. To get the first row, fit three input weights that minimise the sum of squared errors between the calculated and reference red values across all nine patches. Repeat for green and blue to get the other two rows.

Weights fitted to all nine patches
OutputR inputG inputB input
R1.20−0.10−0.10
G−0.101.20−0.10
B0.00−0.201.20

For the selected red patch, the first row gives working red. 1.20 × 0.560 + (−0.10) × 0.200 + (−0.10) × 0.160 ≈ 0.636. The calculation uses the full precision of the fitted weights.

At 0.50× red gain, the camera’s red readings are half their values at 1.00×. The weights in the R input column double, so their contributions stay the same. The weight for working red goes from 1.20 to 2.40. The corrected chart matches the reference exactly here because the simulated error and the gain change can both be undone by a matrix, and the readings never clip.

Build a profile, then use it on photographs

To build a profile, photograph a known colour chart under even, controlled lighting. Use linear RAW-derived readings and keep the patches below clipping. Pair each reading with a reference colour for that light, from the chart’s published measurements or from a measurement of the patch. A single matrix usually matches real patches only approximately. To learn how well it works on other colours, check patches that were left out of the fit.

The sandbox repeats that fit as you move the sliders because it has the chart and its reference values. For a regular photograph, the ISP or RAW editor applies a profile built for that camera and appropriate to the lighting. The colour transform acts on every pixel. The table below uses the sandbox’s starting gains (1×), where the neutral card stays grey and the red patch gets new values in linear sRGB.

The same two patches after demosaicing, white balance and colour calibration.
PatchCamera RGBAfter white balanceWorking RGB
Neutral cardR 0.400G 0.320B 0.200R 0.320G 0.320B 0.320R 0.320G 0.320B 0.320
Red patchR 0.700G 0.200B 0.100R 0.560G 0.200B 0.160R 0.636G 0.168B 0.152

Edit in linear light

The Working RGB column shows values from the scene-linear working image. Those values are proportional to the captured light within the linear range. An extra stop of exposure in the editor doubles the grey card’s (0.32, 0.32, 0.32) to (0.64, 0.64, 0.64). The same edit takes two neutral areas of the window from 0.60 and 0.80 to 1.20 and 1.60. The working image keeps both values. This multiplication happens after capture, so the edit leaves the original RAW codes intact. The colour models article compares this arithmetic with editing encoded values.

From the working image to a photograph you can view

Return to those two areas of the window. Values 1.2 and 1.6 belong to the edited working image. On the separate display-linear scale, 1 means the brightest white. Clipping caps every value above that limit at 1. Both window areas become the same white, so the output loses the detail that their difference carried.

Rendering chooses how the photograph will look. In this example, tone mapping fits the brightness range to the display. The highlight curve maps the window values to 0.792 and 0.844, so the two areas remain distinguishable. For neutral tones it keeps values through 0.5 and applies T(x) = 1 − 1/(4x) above 0.5. Rendering also fits colours to the range the output can represent, called its gamut.

Transfer encoding then turns those display-linear values into numbers to store. For an 8-bit sRGB photo the two window values become codes 230 and 237. The viewer interprets these codes using the image’s colour space and sends the result to the display. The sRGB encoding curve below maps each display-linear component L to a signal E.

Encode light for an sRGB file
● sRGB encoding · □ Linear code
00.5100.51Normalised codeDisplay-linear inputsRGB signal 0.735If written directly as code 128, the file decodes to 0.216 light.
sRGB encoded · code 188
Written directly · code 128

The final code is rounded to a finite set of levels. On a smooth ramp, several successive inputs can round to the same code and then jump together to the next one. That produces banding when the steps become visible. The slope of the encoding curve controls how widely those steps are spaced in linear light. Clipping flattens an entire range above a ceiling. Quantisation creates many small plateaus across the range.

The same camera capture can give you a RAW file to develop or a ready-to-view JPEG. A RAW editor reconstructs a working image and renders a preview as you adjust it. On export, it renders and encodes the edited result for the chosen output. A camera that saves a JPEG makes those rendering choices in-camera. If both areas of the window were clipped to the same white in the JPEG, darkening that JPEG would still leave them identical. Starting from the RAW data can preserve their difference if the sensor captured it.

Video workflows can also hand off at these points:

References