dcraw C Code, Outlined and Annotated, except the outline and annotations have been moved to a separate, non-indexed page to accommodate a bit of Google insanity
Understanding what the dcraw C code does will give you a better understanding of raw processing, and might even open a door to making better pictures.
Written around 2010. Minor Update in March 2015.
Why on earth would you want to read an article on dcraw C code?
Maybe you want to make your own modifications to the dcraw source code. If you've never looked at the code, an outline gives you a head start.
But what if you are code- and command-line-phobic ? I'm no coding guru. If the things I learned modifying the dcraw C code weren't incredibly interesting (at least to me, maybe to you, too), I would have given up long before I ever made my first successful code compilation.
If you are completely code-phobic, then skip over the C code and just read the annotations. You spend a lot of time in your digital darkroom rendering raw files — wouldn't you like to know what's going on under the hood?
Some highlights to entice you to dive in:
- Ever been told "a camera profile is only good under the exact lighting conditions under which it was made"? The truth is, processing a raw file pretty much requires a camera profile. If you are using dcraw, raw therapee, ufraw, digikam raw, and many other raw softwares, then unless you specify a custom camera profile, there is a profile for your camera right inside the dcraw C code that gets used instead. There's just one profile and so perforce it was made under just one lighting condition, probably using a wide spectrum lighting source that was somewhere near D65.
- There's an sRGB matrix at the top of the dcraw C code — are you losing any colors that your camera actually captures? (Not at all, but there's history behind that sRGB matrix, and some mighty convoluted matrix math . . .)
- Curious about the dcraw "-k darkness" and "-S Saturation" values? Check the annotations. Want to know more about noise removal, fixing bad pixels, and dark frame subtraction? The annotations have links to excellent resources.
- Ever get tired of typing the same command line options, over and over again? Changing them to suit your workflow is easy, even for a coding newbie.
Other dcraw resources
This article assumes that you already know what dcraw is and how to use it. If not, the very best explanation of how to use dcraw to develop your raw images is Guillermo Luijk's DCRAW TUTORIAL.
An excellent set of articles on the whys and wherefores of dcraw can be found on Dave Coffin's dcraw website, Decoding raw digital photos in Linux. And see the dcraw manpage for how to use the dcraw command line options.
The single best explanation of the inner workings of dcraw code that I could find is lclevy's "Understanding What is stored in a Canon RAW .CR2 file, How and Why" (much of what the article discusses is relevant to all camera raw files, not just Canon raw files). I've noted in the documentation below places where I drew heavily upon lclevy's hints about how dcraw works.
This article provides an annotated outline of the overall flow and functions of the dcraw C code sections. I don't explain things like dcraw's extensive use of arrays and pointers or C code bit math. Nor do I explain how to set up a development environment for compiling C code into an executable.
Annotated dcraw C Code A word about Google, or why the rest of this page has been moved to a non-indexed part of my website
Google is the tail that wags the dog, the "dog" being the internet, of course.
When Google first made its appearance as a boolean search engine, I loved it. Over time, Google has monetized itself. No surprise — if it's free, you are the product.
Google used to serve up user search histories as a product to be sold to advertisers for money. This was expected and more or less acceptable, at least to me.
Today Google is intent on training its users to be "better products", siloing user information and search results for the benefit of paying advertisers, with no regard for user privacy. I don't want to be trained to be a better product and I do value privacy, so I opted out and closed all my Google accounts.
Before closing my accounts I used Webmaster Tools to check on what Google had to say about my Nine Degrees Below website. To my horror, I found that Google thought my website was about "col" and "row", which are two dcraw c code variables. Apparently Google counts the number of times unique words appear on a website (so much for Google's purportedly intelligent algorithms that figure out what a website is really about) and I had allowed Google to index some pages that included dcraw's c code.
I can't figure out any way to convince Google that my website is about color management rather than about "col" and "row", other than removing the dcraw c code to pages that aren't indexed. So I did. The remainder of this article has been moved to dcraw C Code, Outlined and Annotated.