Lens correction in Photisan: how it's done
Distortion, vignetting and chromatic aberration can be measured and undone. This is how Photisan finds which lens took your photo, even when the camera only writes a number, and how it applies its correction without losing quality.
No lens is perfect. Designing one means trading sharpness, size, weight and price against each other, and something nearly always gives: straight lines bend near the edges, the corners get less light than the center, and red and blue don't form the image at exactly the same size as green. Many lenses today are designed counting on software to finish the job, because correcting a curve in software costs less than correcting it with glass.
Those three defects have something in common: they can be measured, and for the same lens, at the same focal length and aperture, they are always the same. Measured once, they can be undone on every photo. That's a profile correction, and doing it well has two halves: knowing which lens took the photo, and applying its correction without losing quality.
On the cover, a façade in Havana photographed with a Canon EOS Rebel XTi and the EF-S 18-55mm f/3.5-5.6 at 22 mm. On the left, as the lens drew it, with the lines a straight grid turns into; on the right, corrected with its profile, with the same grid straight. At that focal length the lens squeezes the edges: a point halfway along the long side lands about 80 pixels closer to the center than it should, and the balcony and the cornice bow like a barrel.
Three defects that can be measured
Distortion changes the magnification with the distance from the center. If the edges come out smaller, straight lines bow outward like a barrel, typical of a wide angle. If they come out larger, they bow inward like a pincushion, typical of a telephoto. And some lenses do both, barrel near the center and pincushion toward the corners: a wavy distortion called mustache, the hardest to correct by hand.

A straight grid as three lenses draw it, exaggerated to be seen: barrel, pincushion and wavy.

The cover's façade, larger. On top, as the lens drew it, with the grid bent the way the lens bends straight lines: look at the balcony and the cornice. Below, corrected with its profile. The correction removes the curve, not the perspective: the façade is still slightly tilted because the camera wasn't parallel to it, and that's fixed separately, with perspective correction.
Vignetting darkens the corners. Part of it is pure geometry: light reaching a point far from the center arrives at a slant, travels farther, falls tilted and sees the aperture narrower, and all of that together gives the cosine-fourth law, where θ is the angle to the lens's axis:
E(θ) = E₀ · cos⁴ θThe other part is mechanical: wide open, the lens barrel itself blocks part of the beam headed for the corners. That's why vignetting drops a lot when you stop down one or two stops.
Lateral chromatic aberration comes from glass bending each color of light a little differently. The result is that the lens forms the red, green and blue images at slightly different sizes: in the center they coincide, and toward the corners they separate, with a red fringe on one side of things and cyan on the other, or blue and yellow. Don't confuse it with the purple or green halos around very high-contrast edges, which come from each color's focus rather than its size; for those, Photisan has another tool, Color fringes.
The models
All three defects are symmetrical around the optical center, so it's enough to describe how they change with the radius r, the distance from the center. For distortion, the classic model is the polynomial Duane Brown published in the 1960s from Alexander Conrady's work: the point that should be at radius r appears at
r′ = r · (1 + k₁ r² + k₂ r⁴)With k₁ negative it's a barrel; positive, a pincushion; with k₁ and k₂ of opposite signs, the wavy one. The profile database Photisan uses, Lensfun, stores nearly all its lenses with a third-degree variant, Panorama Tools' ptlens model:
r_source = r · (a r³ + b r² + c r + d), d = 1 − a − b − cwhich says, for each point of the corrected photo, at what radius its light is in the photo as taken. The condition on d fixes the scale at a reference radius, and the correction keeps the center's. Lateral aberration is described the same way, as a change of scale of red and blue relative to green that can also vary with the radius:
r_red = r · (b r² + c r + v)with three more numbers for blue. And vignetting, as the fraction of light left at each radius, undone by dividing:
V(r) = 1 + k₁ r² + k₂ r⁴ + k₃ r⁶
I_corrected = I / V(r)One detail matters: what units r is measured in. In Lensfun's profiles, distortion takes as 1 half the shorter side of the sensor the lens was measured on, and vignetting, the corner. Photisan compares positions in millimeters on the sensor, so a profile measured on a camera with another sensor size applies correctly on yours: a full-frame lens on an APS-C body uses only the central part of its correction.
First, knowing which lens it was
It looks like the easy part and it's the delicate one. Correcting with the wrong profile is worse than not correcting: it straightens a curve that isn't there or brightens corners that didn't need it. And every maker writes the name its own way. Canon records the mount glued to the focal length, “EF24-105mm f/4L IS USM”, and the database writes “Canon EF 24-105mm f/4L IS USM”; Fujifilm writes “XF33mmF1.4 R LM WR”, with the aperture glued to the millimeters.
Photisan cuts names into their pieces (digits, letters and signs) without breaking what is one thing: “24-105” is a range and not two numbers, and a 28 mm isn't the 28-70 mm. Then it applies rules so as not to over-match:
- Every word in the name the camera saved has to be in the profile's. What the profile says on top can be what the maker leaves out, like “Zuiko Digital ED”, but never another version of the lens: an extra II, III, IS, VR or USM is another lens, with other optics.
- If the camera is known, only lenses that can be mounted on it count.
- If the file names the maker, only its lenses count.
To check it we ran the hardest test: take each lens out of the database and search for it by its own name, where the right answer is “no profile”. A naive search picked another lens in 471 of 1,577 cases; Photisan's, in 44, and nearly all of those are the same lens written twice or variants of the same optics.
Some cameras don't even write a name. Nikon F-mount bodies, Pentax and older Canon cameras save a number that identifies the lens. For those, Photisan uses a map from numbers to names: the tables the ExifTool community has gathered over the years, converted and published at photisan.com/lens-ids with their license. A number isn't always enough: several makers of compatible lenses reuse the camera brand's numbers, and one number can name several different lenses. Those are left out of the map, because guessing among them would mean risking the wrong profile again. With the map, a Nikon D7200 finds its AF-S DX VR Zoom-Nikkor 18-105mm, and a Pentax K10D its smc PENTAX-DA 70mm F2.4 Limited, with its profile.
And if none of that is enough, the panel says so (“No profile for…: choose the lens”) and you can search for it in the list.
Then, the correction for that photo
A profile doesn't have numbers for every possible focal length: it's measured at a few, and vignetting at several apertures too. For a photo taken between two measured focal lengths, each coefficient is interpolated:
t = (f − f₁) / (f₂ − f₁)
P(f) = (1 − t) · P(f₁) + t · P(f₂)For the aperture, the interpolation is done in stops, with log₂ N, not in the f-number: from f/4 to f/5.6 there's the same step of light as from f/8 to f/11, even though the numbers are twice as far apart.
The correction the camera saves
Mirrorless cameras changed the rules: many don't correct the sensor's data, but they save in each file how to correct it for that particular photo, with its focal length, aperture and focus distance. It's the correction the maker itself wants, the one it applies to its JPEG. Photisan reads it from Sony, Fujifilm, Nikon Z, Olympus and OM System, Panasonic and Leica, and from the DNGs that carry it, such as phones' and drones', with readers written by us from the public descriptions of where each brand stores its data and in what units.
Reading something doesn't prove it's read right. We checked each reader against the camera's JPEG of the same photo: we find the same points in both images, align them and measure how far apart they are along the radius. Some results, in pixels of an image at a quarter of the size:
- Nikon Z6 with the Z 24-70 f/4 S at 24 mm: 14 px uncorrected, 0.1 px with the camera's correction, 3 px with the database profile.
- Olympus E-M10 III with the 14-42 EZ at 14 mm: 10 px uncorrected, 0.06 px with the camera's and 18 px with the profile, worse than not correcting.
- Leica Q3: 17.5 px and black corners uncorrected, because its lens was designed to be corrected in software; 0.1 px with the camera's.
So when the camera saved its correction, Photisan uses it first, and the database is for when there's none. If the camera's doesn't include vignetting (Olympus and Panasonic don't save it), it's completed with the profile's. And on phones, which correct the lens's shading color by color with a grid of gains, that grid is applied to the sensor's mosaic, before the color is rebuilt, as the DNG specification asks.
Applying it without losing quality
Correcting distortion means moving pixels, and there's a right way to do it: backwards. Instead of carrying each pixel of the photo to its new place, which leaves gaps and pile-ups, for each pixel of the corrected photo you work out where its light comes from and read it there:
p_source = c + (p − c) · r_source(r) / rwhere c is the optical center and p the pixel. The optical center isn't always the center of the image: some cameras save where it is, sometimes several pixels off, and Photisan corrects around it. Each color is looked up at its own radius, so the same operation undoes distortion and lateral aberration. The aberration in the camera's data goes one step earlier, while the mosaic's color is rebuilt, because that's where it comes out exact (we told that story in the post on demosaicing).
All of this happens inside the development, on the RAW's linear light at full precision, before exposure and curves, not on a finished image. Vignetting is undone where it is a simple multiplication of light, and the image is fitted to fill the frame with no empty corners. And whatever you draw on the photo follows the photo: masks live on the photo as it was taken, so turning the correction on or off, or changing lenses, doesn't peel them off the object they followed.
What we keep to ourselves
How we read each brand's format, the full rules of the search, how we resample and how each step fits with the rest of the development are our recipe. The models above are public, and the profiles come from the Lensfun community; what's ours is finding the right one and applying it well.
Try it
Open a RAW file and find the Lens group of the development. With “Profile correction” on, the panel says what is being used: “Camera's correction · automatic” when the camera saved its own, or the lens name from the database. “Choose another lens…” opens the list with search, and under “Manual” you can fine-tune distortion and vignetting on top of what the profile corrects. Under “Chromatic aberration”, “Remove chromatic aberration” measures it on the photo when neither the camera nor the profile corrects it. Compare a photo with straight lines near the edges with the correction on and off. If you don't have Photisan yet, download it and try it for free.