Wednesday, January 11, 2012

Lightroom's new lossy DNG compression

Adobe's Lightroom 4 beta is out, and among it's other enhancements, it features a new lossy compression scheme for DNG images. LR has always has lossless compression, which could usually reduce image size by up to half. However, it's not been used much, except when you used any of Adobe Camera Raw, Lightroom or the Adobe DNG converter to convert an existing raw image to DNG. The lossless compression scheme hasn't, so far as a I know, ever been used in-camera, presumably because it's quite CPU intensive.

Eric Chan was kind enough to give some details on the new lossy scheme, which I'll summarize here, together with my views. You can read the full exchange on the Adobe forums in this thread.

NOTE: This post is based on Eric's comments - the specification isn't yet available.

What is the new scheme?

The new scheme basically works as follows:
  1. It uses JPEG DCT compression - exactly the same as a normal JPEG image.
  2. The data is 8-bit, same as a normal JPEG.
  3. Data is demosaiced, also the same as a regular JPEG. 
So up till this point, a lossy DNG is pretty much the same as a regular JPEG. But there's more:

The clever bits

The new scheme has some clever bits that make a lossy DNG better than any normal JPEG. Those bits are:
  1. What Adobe call "1D perceptual mapping". What that actually is, is a tone curve, just like the sRGB tone curve in JPEG images, but a clever tone curve. 8-Bit pixel values range from 0 to 255. What Adobe are doing is to analyze the image data, and optimize the curve to the image data. So, for example, if your image doesn't have significant highlights, a "normal" JPEG image might only use values from 0 to 210, and not use 211 to 255. The new clever curve will spread the pixel values in the image over the full range of 0-255. Better still, this is done for each color channel individually. And because information on the new curve is in the image, that can by undone as soon as the image is loaded again.
  2. Dithering is used to prevent banding. With 8-bit values, there's the danger of banding, especially if a lot of post processing get done on the image. Dithering, basically adding noise (very carefully controlled and shaped noise!), helps to smooth out any banding. 
  3. The image stays in the camera's color space, rather than being compressed into the space defined by the JPEG primaries. Pretty much all modern cameras can represent colors that a standard JPEG image can't. Now there are some downsides to this - a wider color space being represented in 8-bit values can result in color banding, so we're going to have to see whether that turns into an issue or not. I'd say probably not, given the use of dithering, but we'll see over the next few months.
  4. The image remains scene referred. This is important for white balance. JPEG images have a fixed white balance of 6500k, so a white balance transform needs to be applied before they are created. That makes it difficult (not impossible, but less easy to do well) to adjust white balance later than if the data is in its "as shot" form.

Why have Adobe designed it this way?

Back when lossy compression was a just a rumor, I predicted that if lossy compression happened, it would be JPEG based. See this thread on LuLu. It's nice to be right, even if only occasionally(!)

The reason, in my opinion, is that a lot of camera chips have hardware acceleration for JPEG DCT built in to support JPEG out, so doing JPEG DCT lossy compression is "costless" in terms of CPU utilization for many modern cameras.


So, where are we on this:
  1. Firstly, to state the obvious, if you use the new lossy compression, your file is no longer raw. The new lossy compression works by first demosiacing the image, then compressing. And that's irreversible. So, if at some time in the future, a better demosaicing algorithm comes out, you won't be able to take advantage of it.
  2. Secondly, the compression scheme is, and I can't put this any other way, an engineering abomination if your starting point was Bayer matrix type raw data, which maybe 99% of raw image data is. Here's why: When the image is demosaiced from the raw Bayer data, basically what you're doing is adding two thirds more data that is actually redundant. What demosaicing does is to interpolate, via an algorithm, two thirds of the pixels in an image. But these interpolated pixels are redundant data - they can be recreated from the original raw data just by recalculating. What is then compressed is the original raw data, plus all of this redundant data. So to get, according to Eric's data in the thread above, about a 50% reduction is data size relative to lossless compression (70% relative to no compression), you're actually firstly multiplying the data size by three, then compressing. So, what you're doing is a factor of ten JPEG compression to get a factor of between two (relative to lossless compression) and three (relative to no compression) data size reduction. All because of the two thirds of data that your compressing is actually redundant. That's just horrible.
  3. The quality of Adobe lossy DNG images are going to be very implementation dependent. In other words, there will be good implementations and bad implementations. There is no single way to do JPEG compression; a good JPEG compression engine will look at the data, and chose the best compression parameters for the image. Similarly, there are good and bad ways to do dithering. Finally, a simple way to implement the "1D perceptual mapping" would just to have a single fixed tone curve. Add together a not so great JPEG compression engine, not so great dithering, and some cutting of corners on the "1D perceptual mapping", and you have a recipe for really bad image quality. I have no doubt that Adobe implementation will be good. But I also suspect that if lossy DNG compression makes it into cameras, there will be some pretty bad implementations.

Conclusion and recommendation

Bottom line, a lossy DNG is a JPEG that's been somewhat tweaked to allow easier image adjustment later on. Which is no bad thing, but let's be clear - lossy DNG is an alternative to JPEG, not an alternative to raw.

If, in some future camera, you get the choice of either a JPEG image, or a lossy DNG image, you should almost certainly choose the lossy DNG. The data compression and sample size are the same, and there are advantages to be had from the perceptual mapping, color space and the scene referred nature of the image, as discussed above. In addition, if you want to convert an existing JPEG into DNG, e.g., because your workflow is standardized around DNG, then lossy DNG may be a good idea. You won't lose much quality, and alternate ways of converting from JPEG to DNG results in far larger files than the original.

However, if you're converting an already existing raw image to DNG, don't use lossy compression! Chances are, you'll get similar compression from lossless compression, you won't risk JPEG compression artifacts, and you'll be able to benefit from better demosaicing algorithms in the future. Even if you didn't get the same compression, my view would be that given the low cost of storage today, the trade-off just wouldn't be worth it.

Updated 13 Jan: after I wrote this post, Eric Chan, who is certainly in a good position to know, responded to a question about using lossy DNG compression as follows: "To be honest, I'm not really comfortable with the idea of lossy compressed DNG for archival storage purposes". Which probably sums it up well.


Ezira_De said...

I'll see lossy-DNG in a more optimistic manner:
downsampled to 1/3 of the sensors Mega[Sub!!!]Pixel, you'll get a considerable smaller file without any spatial redundancy.

For archival, there is always to trade complexity vs. storage size.

Martin Podhola, DiS said...

Thanks for article, good for my thoughts.

I am commercial photographer, I need a really big storages (because I am also enthusiastic traveller and photographer on the journeys).

I really need to reduce my HUGE amounts of data (x2 in reason of back-up). I am going to convert my Canon RAWs 21Mpix from Canon EOS 5D MkII to lossless DNG to make them smaller. But on a few shots I think about lossy compression.

Just on photos, which I probably will archive just for remembrance (family celebrations, parties with friends etc) I have not touch them for a years.

On the other photos I am going to use lossless DNG.

And thinking about using sRAW on my camera on business which are not going to be large-printed