What demosaicing is and how Photisan does it
Your camera's sensor doesn't see color: two thirds of the color in a RAW file are rebuilt. This is how that works, why averaging isn't enough, and how Photisan rebuilds the color without losing detail or inventing colors.
Your camera's sensor doesn't see color. Each of its millions of photosites counts the light that reaches it and nothing more: it's a light meter, blind to color. For a photo to have color, in front of each photosite there's a tiny filter, red, green or blue, and each records only the light its own filter lets through. A 24-megapixel camera saves 24 million numbers, not 72: at every point of the photo, two of the three colors are left unmeasured.
Filling in those missing colors is called demosaicing. It's the first step in developing a RAW file, before exposure, white balance or any adjustment, and almost nobody notices it. But two thirds of your photo's color come from it, and how well they are guessed decides how sharp the fine detail is and whether colors appear that were never in the scene.
On the cover, the eye from a portrait taken with a Sony A7 IV, enlarged so much that each photosite is a square, twice. On the left, the mosaic as the sensor recorded it: each photosite in its filter's color, as bright as the light it measured. On the right, the same photosites after demosaicing.
A mosaic of filters
What comes off the sensor can be written in one line. If Ic(x, y) is the light of color c at the point (x, y), and Mc is 1 where the filter is that color and 0 where it isn't, the sensor saves:
m(x, y) = M_R·I_R + M_G·I_G + M_B·I_BA single number per point, the sum of three terms of which only one isn't zero. Demosaicing tries the way back: from m, recover the three complete images. With three unknowns for every value, there's no single solution. Something has to be assumed about what photos are like, and that assumption is the whole difference between one method and another.
The most common pattern was patented by Bryce Bayer, at Kodak, in 1976: in each 2 × 2 block, two green photosites on a diagonal, one red and one blue. There are twice as many greens because our eyes get nearly all their sense of sharpness from brightness, and brightness depends mostly on green. Since 2012 Fujifilm has used another pattern, X-Trans, 6 × 6: 20 greens, 8 reds and 8 blues, laid out so that every row and every column has all three colors. It's less regular on purpose, so that the weave of a fabric or the bars of a fence don't line up with the sensor's grid and make moiré.

The two patterns as read from the files themselves: on the left, the Bayer pattern of a Sony A7 IV; on the right, the X-Trans of a Fujifilm X-T4.
What the sensor measures, in Photisan
Photisan lets you see the sensor before demosaicing, photosite by photosite, in two ways. In gray, what each one measured and nothing more: the sensor knows no colors, only how much light came through its filter. And in its filter's color, which is how to read that gray: a bright photosite under a red filter says there was red there. The view doesn't change the photo, and its brightness control only lightens what you see.

A lock of hair from the portrait, in the sensor's two views. The filter's pattern shows in gray even without color: brown hair lets more light through some filters than through others.
Why averaging isn't enough
The simplest idea is to fill in each color with the average of its nearest neighbors of that color. On a red photosite of a Bayer sensor, the missing green would be:
G = ¼ · (G_north + G_south + G_east + G_west)That's bilinear interpolation, and it works well in smooth areas. At edges, it doesn't: it averages across the edge, mixes light with dark and leaves it soft, often with a zipper-like jagging. And since red, green and blue are averaged from different points, each sees the edge in a slightly different place, and colors that didn't exist appear along it.
There's a deeper way to see it. If the Bayer pattern is laid out with the greens at the points where x + y is even, the mosaic can be written exactly like this:
m(x, y) = L + C₁·(−1)^(x+y) + C₂·[(−1)^x − (−1)^y]
L = (R + 2G + B) / 4
C₁ = (−R + 2G − B) / 4
C₂ = (B − R) / 4In other words: the mosaic is the brightness L as it is, plus two color signals multiplied by patterns that flip sign from one photosite to the next. In the image's spectrum (its frequencies, the analysis Alleysson, Süsstrunk and Hérault published in 2005), brightness sits in the center and color is shifted out to the corners and edges, at the highest frequency the sensor can record. As long as the scene's detail is coarse, each stays in its place. When the detail is as fine as the photosites (a fabric, hair, distant leaves, small lettering), brightness reaches where color is and the two get mixed up. Demosaicing has to decide, point by point, whether a fast change is detail or color. Wrong one way, it erases the detail; wrong the other, it invents colors, like the rainbow that shows up on a finely woven shirt.
The two ideas that changed everything
Nearly every good method published in the last thirty years rests on two ideas.
The first is direction. An edge has a direction, and along it the image barely changes. If at each point you measure which way the detail runs and fill in along the edge rather than across it, the edge survives. James Hamilton and John Adams, also at Kodak, patented this for green in 1997: they compute two estimates, one horizontal and one vertical, each corrected by the curvature of the color that was measured, and keep the one in the direction where the image changes least:
G_h = (G_w + G_e)/2 + (2C − C_ww − C_ee)/4
G_v = (G_n + G_s)/2 + (2C − C_nn − C_ss)/4
Δ_h = |G_w − G_e| + |2C − C_ww − C_ee|
Δ_v = |G_n − G_s| + |2C − C_nn − C_ss|Here C is the red or blue the photosite measured, and Cww, Cee, Cnn and Css are those of the same color two photosites away. If Δh is smaller, the edge runs horizontally and Gh is used; if Δv is, Gv.
The second idea is that colors change together. On almost any object, the difference between red and green (or their ratio) varies much more slowly than each color on its own: a shadow darkens all three alike. So instead of interpolating red, you interpolate the difference, which is smooth, and add it to green, which already has all the detail:
R = G + interpolate(R − G)With that, red and blue inherit the edges of green, the color with the most samples, and all three colors see the edge in the same place. Better and better methods were built on these two ideas, such as Keigo Hirakawa and Thomas Parks's AHD (2005), which picks the direction by which one leaves the color more even, or Frank Markesteijn's for X-Trans.

The same mosaic from the Sony A7 IV, the portrait's earring, rebuilt two textbook ways (this isn't Photisan's algorithm). On the left, the bilinear average: the hoop's edge in steps, with blue and yellow fringes that don't exist. On the right, Hamilton and Adams with red and blue as differences to green: the edge follows the hoop with almost no false color, though the sensor's grain shows more, because a method that keeps fine things keeps all the fine things. Handling that is the other half of the work.
How Photisan does it
Photisan has its own demosaicing for Bayer and for X-Trans, written by us. It starts from the two ideas above, but the decisions that matter are others:
- At each photosite it measures which way the detail runs with statistics of its neighbors of the same color, the only ones that can be compared without mixing colors, and refines that decision with what the ones around it decide, so an edge doesn't change its mind halfway along.
- It rebuilds green first, the color with the most samples, along the detail, making sure each neighbor it uses is corrected for how the light changes between where it is and where it's needed.
- Red and blue come from their relation to green, in the direction that suits each kind of photosite.
- On X-Trans, where neighbors of the same color aren't at the same distance or in the same directions, it looks in four directions (the two straight ones and the two diagonals) and prefers the one that leaves the colors most even.
What doesn't change is the color. Demosaicing decides how well the unmeasured values are guessed, not what color the photo is. We checked it by developing the same photos with the previous method and with ours: over 8 × 8 pixel blocks, the average color difference is one to three tenths of a ΔE00, below what the eye can tell.
How to measure a demosaicing method
To know whether a method is better you have to compare it with the truth, and in an ordinary photo the truth is exactly what wasn't measured. The trick is to make it: a RAW file at half size is already a complete image, because each 2 × 2 block of photosites has its own red, green and blue, measured, not interpolated. That image is turned back into a mosaic (Bayer or X-Trans), rebuilt with each method and compared with the original. The usual measure is the peak signal-to-noise ratio:
PSNR = 10 · log₁₀( 1 / MSE ) [dB]where MSE is the mean squared error against the truth, with values between 0 and 1. Each extra decibel is 21% less error. Measured on real photos from several cameras, our Bayer demosaicing comes out 1 dB above the AHD we used before, 1.1 dB in the quarter of the image with the most detail, with less false color and at the same speed. On X-Trans it beats three-pass Markesteijn and takes a third less time. We also checked it by eye, on RAW files from an X-T4: the 6 × 6 pattern in its place, no artifacts and less color mottling.
Chromatic aberration, while the color is rebuilt
There's a detail that ties demosaicing to the lens. Nearly every lens forms the red image a little larger or smaller than the green one, and the blue as well; toward the corners, that leaves red fringes on one side of things and cyan on the other. Many cameras save in the file how much, for each photo.
Measuring red and blue against green on RAW files from several brands, we found something we didn't expect: after demosaicing, only a half to a quarter of the sensor's aberration remains. The reason is the second idea above. If red is rebuilt as green plus an interpolated difference, and the red edge isn't where the green one is, that difference has a step that the interpolation smooths, and red moves toward green. Correcting afterwards all the aberration the camera reports overcorrected it, up to three times on one lens.
So Photisan corrects it while rebuilding the color: each sample is compared with the green from where the lens moved its light, not from where it landed. The difference no longer has that step, and each color ends up where it belongs. What's left ranges from a few tenths to about two ten-thousandths of the image's radius, where before, corrected afterwards, it reached almost six. Since the camera's data don't change, it's applied whenever the photo opens, with no extra switch.
What we keep to ourselves
Which statistics we use to decide the direction, how large the windows are, how the neighbors are weighted and how the estimates are combined are our recipe. The formulas above have been public for decades; what's ours is everything around them that makes them work on real photos.
Try it
Open a RAW file and, in the command palette (⌘K), look for “View the sensor (no demosaicing)”. Zoom in to 400% or more to see each photosite; as you move the pointer, Photisan tells you which filter it has and how much light it measured. “Sensor in gray” shows what each photosite measured and nothing more. Then go back to the photo and compare, at 100 or 200%, an area of fine detail: a fabric, hair, leaves or small lettering. If you don't have Photisan yet, download it and try it for free.