Lossy RAW compression

Thomas Chamberlin asks an interesting question in the Flickr D3 user’s group:

“With the D3 and D300 we are now offered lossless compression, no compression, or compression with loss for RAW files. Nikon says there is no effect on image quality using lossless compression. The file is compressed 20-40%. Write times are faster than with no compression. How can they compress the file 40% and not affect the image quality? What is lost and then regained?

The great thing about getting a new camera is that I get another chance to put “on paper” words that lost to the ether.

My digital bodies
My digital bodies
North Beach, San Francisco, California

Panasonic DMC-LX1, Ultrapod
2 exposures at .8sec @ f/3.6, iso 80, 11.6mm (52mm)

From right to left. The Leica M8 shoots RAW into the Adobe DNG format (not sure of the compression), the Nikon D70 which introduced lossy NEF compression, the Nikon D200 which shoots lossy and uncompressed NEF RAWs, the Nikon D3 which shoots lossy, lossless, and uncompressed NEFs in both 12 and 14-bits. The photo was taken with a Panasonic LX-1 which shoots a Panasonic/Leica RAW file format that is uncompressed and 13x larger than the JPEG sidecar file! Ironically, I processed this image, by accident, from the sidecar files instead of the RAWs, even though it was shot at ISO 80.

[Weighing in on lossy RAW after the jump.]

The Nikon line has had this stuff for a while now. The D200 and D2X offered both lossy compression and uncompressed, and all mid-range Nikon camera RAWs since the D70 were lossy compressed (while the D50 had uncompressed RAWs only—go figure). The difference in filesize was about 50% which explained why the pre-firmware updated D70 and D200 would have the RAW image counter off by a factor of two!

Now back then they called it “visually lossless compression.” The cynic and engineer in me must say:

Any time they have to qualify an absolute with an adjective that isn’t a superlative, then it’s marketing-speak for the opposite.

In other words:

“lossless” =lossless =not lossy
visually lossless” =lossy

Go figure.

So I guess that I can thank Nikon for finally fessing up this time around and calling it what it is: lossy compression.

Lossy, Lossless, and Uncompressed

Actually the new D300 and D3 have three modes: Uncompressed, Lossless, and Lossy. Nikon lossy and lossless compression are both losslessly compressed using an LZW-style lossless compression.

So let’s get that out of the way first. LZW algorithms will typically save you 15-30% in the file size. This means that you’d pretty much be an idiot to use “uncompressed” as your RAW setting in the D3 or D300.

We must next ask “What is lost and regained?” we mean to compare lossy compression to lossless compression.

Aside: The math is strong with this one

Some of you may be wondering, if uncompressed NEFs are 12-bit (or 14-bit), how come they’re only twice as big as lossy-compressed JPEG-fines and much smaller than TIFF?

The reason there has to do with the fact that the NEF’s are un-demosaic’d, which means the NEF data is actually monochrome.

Trust me, the math works out. :-)

Onward.

Lossy vs. Lossless

Luckily I don’t have to delve into the math because there is already a great discussion of the mathematics of lossy RAWs written by Fazal Majid: the upshot of it is that the lossy RAWs are reduced from 12-bit to 9.4 bit before being losslessly compressed. This means:

Nikon lossy compressed 12-bit RAWs can be seen as 9.4 bit lossly-compressed RAWs.

(I don’t know what happens in the D3 when you use 14-bit RAWs and lossy compression. Does anyone know yet?)

But, while I largely agree with the article, I do differ in one key aspect: the speculation in the conclusion.

You could argue it does not [really matter], as most color spaces have gamma correction anyway, but highlights are precisely where digital sensors are weakest, and losing resolution there means less headroom for dynamic range compression in high-contrast scenes. Thom’s argument is that RAW mode may not be able to salvage clipped highlights, but truly lossless RAW could allow recovering detail from marginal highlights. I am not sure how practicable this would be as increasing contrast in the highlights will almost certainly yield noise and posterization. But then again, there are also emotional aspects to the lossless vs. lossy debate…

Whoa, there bessy! That isn’t true at all!

Highlights are where digital sensors are the strongest except when clipped which has nothing to do with lossy vs. lossless compression.

Clipping is not done, Noise isn’t introduced, posterization, however, is!

I’ll not disagree that there is an “emotional aspect” to the debate here, but let’s not dismiss the factual aspects so quickly. I’ll justify this below.

Understanding highlights

You can go read Jeffrey Friedl’s excellent article on a practical analysis of the lossy compression. It has the added benefit of being much better written than anything I can do as well as not requiring knowledge of math or physics.

The conclusion is firm, incontrovertible, and factual:

In the end, the only reason to use compression in the first place is for convenience (smaller files, faster write times, and more images on a card), and that convenience is paid for by irrevocable loss of image detail, albeit a very small amount of image detail.

In other words, if you want smaller files and faster write times (more images on your card and disk and less delay between bursts), then you should use lossy compressed RAW, but it will result in a factual loss of image detail in the highlights which is so small as to be invisible in the processed shot.

To me, the interesting part is to ask why. Why is the loss in the highlights? Why is it not visible? When is it going to be visible?

Understanding our collection devices

No, I don’t mean the camera. I mean our eyes and ears. You cannot study cell biology, neuroscience and neural computing and not appreciate the engineering marvel that is ourselves. And it really is a marvel.

Very telling in the Majid article is this comment he makes in passing:

[Requantizing to discard bits] is a fairly common technique—digital telephony encodes 12 bits’ worth of dynamic range in 8 bits using the so-called A-law and mu-law codecs.

Why do they discard bits in telephony? Why does Nikon discard bits in photography? And, in both cases, why are the bits in the high end?

Because that’s how our eyes and ears work.

When we hear we talk about decibels, when we take a photograph we talk in “stops of light” (eV). Both of these are logarithmic. Our ears and eyes are logarithmic data collection devices, but our digital recorders are linear ones.

As I mentioned before, the last stop of highlights in the image (visually, as interpreted by our eye) takes up half the data! Therefore, you can remove data by quantization in that area and still not lose a single thing visually.

That’s why the data is lost on the highlights and preserved in the shadows. And I’m surprised more people don’t mention this fact when they engage in the emotional appeals of “lossy” vs. “lossless.”

When and what do I lose?

It will not recover clipped highlights. As I mentioned before, that shit fell off the edge. Welcome to the world of digital. So stop talking about that.

When you say “dynamic range is preserved,” you must mean it. Stop being an indian-giver in your discussions.

It will not allow you to denoise better because, as I mentioned before, you are in the part of the poisson distribution where the noise relative to value is minimized.

When you say “expose to the right” digitally, you must mean it. Stop obsessing over highlights if you can’t understand the physics of perception.

FAIL.

When lossy does fail is when you are postprocessing heavily in the highlights. At that point you will definitely posterize with a lossy NEF before you posterize with a lossless NEF.

An practical example is if you are taking a bracketed shot for work in HDR or contrast blending. The most noise free data will be in the highlights of your shots and therefore those are the bits that will be used in procressing. It makes sense that throwing away the most useful data during collection in such a heavily processed image would lack foresight. Besides, if you are taking such a photograph. Do you really care about your buffer or about the file size? ;-)

Another nit pick

As I mentioned before, I love to nitpick Thom Hogan. Now this isn’t because he’s wrong more than he is right like Ken Rockwell is, but it is because he is right so often than an error stands out.

In all of his PDF guides, he has a discussion on NEF compression in the section under “Compressed NEFs” and that discussion is good. If you are interested in it, go buy one of his digital SLR ebooks. But I will pick two nits.

“That’s party becuse our eyes work in a non-linear fashion with brightness (sensors are linear—the NEF compression scheme mimics our eye’s non-linearity), but also because our eyes generally are thought to distinguish tonal changes only about equivalent to those produced by 8-bit RGB data.”
—Thom Hogan, Thom Hogan’s Complete Guide to the Nikon D200, p 150

That’s so close to being totally true. The two nitpicks here are tiny.

The first is the caveat that by using the argot of “tonal” he is specifically referring to the luminance, not the chroma: i.e. 8-bit per channel RGB or 24-bits total.

But more importantly is the missing qualifier: “at once.” See, our eyes have this thing called a pupil, other eye muscles, and shit like that. This means that when taking in a scene in the real world, our eyes can focus in on different parts of it, our pupils can dilate, and the total dynamic range of the scene we can perceive may be 14-bits of “tonality” or higher!

Just because our eye can only distinguish about 10 million colors doesn’t mean that our eye only has just shy of 24 bits of total dynamic range!

Perspective

It really is an amazing piece of engineering, our eye. And it is an amazing instrument to see, our camera.

Let us never forget in our discussion, what this camera is: an instrument that teaches another to see.

Compressed or uncompressed. RAW or JPEG. Teach me by taking a photo today. :-)

Corrections and addendum

About tychay

light writing, word loving, ❤ coding
This entry was posted in photography. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.