RAW files

The unprocessed digital output of an image sensor is called a RAW file. In this document we sometimes refer to it as Camera RAW to distinguish it from Bayer RAW files, which are standard image files that contain undemosaiced (Bayer) data.

New in Imatest 3.6:  Generalized Raw read capability had been added.  You can specify a file extension and read properties for files with this extension. The file will be interpreted as a Bayer RAW file.

New in Imatest 3.5:  the dcraw front-end interface has been updated to include an auto white level checkbox (should be unchecked for sensitivity measurements) and a gamma setting (the old defaults, 0.45 for bit depth = 8 and 1.0 or bit depth = 16 no longer apply.)


The pixels in most digital image sensors are covered with a Bayer Color Filter Array (CFA), which makes each pixel sensitive to a single primary color, Red, Green, or Blue. The order of the colors is RGRGRG..., GBGBGB..., etc. There are twice as many greens as reds or blues because the eye is most sensitive to green. The four possible pixel arrangements (Red locations) are shown in the diagram below, where pixel locations start with Row 1 Column 1 (R1C1) in the upper left corner. There is no universal standard for mapping colors (R, G, or B) to pixel position (RmCn). The arrangement can be determined by examining a Bayer RAW image file with the Rawview utility, which can be called from a button on the right side of the Imatest main window.

R1C1
 
R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C3
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 
Red in R1C1
R1C1
 
R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C3
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 
Red in R1C2
R1C1
 
R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C3
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 
Red in R2C1
R1C1
 
R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C3
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 
Red in R2C2

Most camera RAW formats are proprietary to the camera manufacturer (e.g., CR2 for Canon and NEF for Nikon) and must be converted into standard file formats (JPEG, TIFF, PNG, etc.) for most applications. This usually involves demosaicing, which is a form of interpolation that fills in the two missing colors in each pixel. Demosaicing is the primary function of RAW converter programs (which may be either built into the camera or installed on a separate computer). In demosaicing, detail in the missing colors is inferred from detail in other, neighboring colors. Algorithms can be very mathematically sophisticated. Demosaicing (RAW conversion) programs usually incorporate a number of secondary image processing steps, including

  1. applying a gamma curve, typically with a tonal response curve often applied on top of it,
  2. white balance and color adjustment,
  3. sharpening, usually in the neighborhood of contrasty features, and
  4. software noise reduction, typically in the absence of contrasty features.

All these steps can affect Imatest results. To minimize their effects, Imatest uses dcraw, a freely available program for converting RAW images to useful file formats. Dcraw applies simple gamma curves (with no additional tonal response curves) and does not perform sharpening or noise reduction.

RAW files in Imatest

Imatest can use RAW files in three ways. The first two use dcraw (which only works for RAW files from commercially-available cameras).

  1. It can demosaic RAW files, producing standard 3-color RGB image files in TIFF, PPM, PNG, or JPEG format, with TIFF as the default. The advantage of using dcraw is that it applies no sharpening, no noise reduction, and a straight gamma curve. Several choices are available for gamma, including 1/2.2 (for typical displays) and 1.0 (true linear). If RAW files are available, cameras and lenses from different manufacturers can be compared on a consistent, fair basis.

    Dcraw should be used with caution in Colorcheck and Stepchart because is performs autoexposure by default. Starting with Imatest 3.5, the Auto white level button should be unchecked to turn off autoexposure (and get the correct levels). for 8/24-bit output. According to the dcraw FAQ, "No matter how dark an image is, dcraw's auto-exposure stretches it so that one percent of its pixels appear white. The "-W" option avoids this behavior."
  2. It can convert camera RAW files into Bayer RAW files stored in standard monochrome image file formats (TIFF or PGM). Most Imatest Master modules can analyze Bayer RAW files. Bayer RAW is particularly valuable for measuring lateral chromatic aberration, which can be strongly distorted by the demosaicing process. Caution: dcraw may apply some tonal processing, even though the documentation indicates it shouldn't, i.e., gamma measured by Stepchart and other modules may not be close to 1, as it should be for an unprocessed Bayer RAW file.
  3. Generalized Raw Read can be used (introduced in Imatest 3.6-Beta). This is particularly useful for development systems and noncommercial applications not supported by dcraw. One or more file extension must be selected, and decoding parameters must be set up and saved, as described here.

Dcraw can either (A) demosaic camera RAW files with minimal processing— no sharpening, no noise reduction, and a simple gamma curve, or (B) convert them to standard 8 or 16-bit monochrome file formats (TIFF or PGM) that contain Bayer RAW (undemosaiced) data, which can be analyzed by several Imatest Master modules. This conversion has the following choices:

Demosaicing Normal RAW conversion (demosaiced)
RAW 8-bit (Bayer RAW; no demosaicing)
RAW 16-bit (Bayer RAW; no demosaicing)

If the second or third option above (RAW 8-bit or RAW 16-bit) is selected, most the Demosaicing area (above) is grayed out and the output of dcraw is a monochrome TIFF or PGM file that contains undemosaiced (Bayer RAW) data from the image sensor. Note: the pixel levels and gammas of undemosaiced (Bayer RAW) files do not appear to be reliable as of July 2009. Levels are much too dark if Auto white level is unchecked, and the gamma is incorrect. Exposure and gamma are reliable with normal RAW conversion (with demosaicing).

Three separate dcraw programs are available.

Program dcraw The original; may not work in Vista
LibRaw dcraw emulator (works in Vista; no Bayer RAW)
dcrawMS Works in Vista

DCrawMS.exe is a special build of dcraw designed to work with Vista. If you have a recent camera that doesn't work with dcraw or dcrawMS, try downloading the latest versions from Pukkita's Digital Darkroom Corner to the Imatest installation folder (C:\Program files\Imatest in most English language installations). LibRaw is a DLL library based on dcraw.

Bayer RAW dialog box

Bayer RAW files

When monochrome files are opened by an Imatest Master module that supports Bayer RAW analysis, the dialog box shown on the right appears. Standard monochrome file should be selected for regular monochrome files that do not contain Bayer RAW data. The color scheme of the buttons depends on the "Red in RmCn" selection, which affects some of the displays.  The correct setting can be found using the Rawview utility.


Recognizing Bayer RAW patterns

Bayer RAW images are typically monochrome files (though they can be color if they are acquired using screen capture— Alt-PrtSc). They sometimes appear unexpectedly in development/prototyping systems. You can avoid serious headaches if you recognize them. They are characterized by square patterns similar to those shown on the right.

Bayer raw pattern   Bayer raw pattern
Bayer RAW characteristic patterns

 

If Bayer RAW R1C1, R1C2, ... is selected, one of the four image planes starting with R1C1 (Row 1 Column 1), R1C2, R2C1, or R2C2 is selected for analysis. (Recall that there is no universal standard mapping an image plane with a color.) The four planes are shown below. Note that the pixel height and width of the image in each plane is half that of the image as a whole and the spacing between pixels is twice that of the sensor pixel spacing.

R1C1
 
R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C3
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 

R1C1
R1C1
 
R1C3
 
R3C1
 
R3C3
 
R1C1
 

R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C2
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 

R1C2
R1C2
 

R1C4
 
R3C2
 
R3C4
 
R1C1
 
R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C2
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 

R2C1
R2C1
 

R2C3
 
R4C1
 
R4C3
 
R1C1
 
R1C2
 
R1C3
 
R1C4
 
R2C1
 
R2C2
 
R2C3
 
R2C4
 
R3C1
 
R3C2
 
R3C3
 
R3C4
 
R4C1
 
R4C2
 
R4C3
 
R4C4
 

R2C2
R2C2
 

R2C4
 
R4C2
 
R4C4
 
The four image planes: one Red, one Blue, two Greens:
(There is no universal standard for mapping colors to image plane.)

Demosaiced files with dcraw

The best way to be sure an image file faithfully resembles the RAW file— that it has a straight gamma curve, no sharpening, and no noise reduction— is to read a RAW file into Imatest and convert it to a standard format (TIFF, PPM, etc.) using Dave Coffin's dcraw. If you select any of the standard RAW formats (CRW, NEF, etc.) or if raw data is detected (e.g., Phase One TIF files) the dialog box shown above appears. It is a front-end to dcraw that allows you to choose between several RAW conversion options.

dcraw/dcrawMS conversion details:

If Normal RAW conversion (demosaiced) has been selected, the Demosaicing box contains several options, most of which are explained in more detail in the dcraw Manpage. The following output color spaces are available for demosaiced output.

RAW (not true RAW pixels).
Demosaiced with no color space conversion or White Balance.
The camera's color profile is needed to correctly interpret the colors. Gamma = 0.45.
sRGB, Adobe RGB (1998), Wide Gamut RGB, ProPhoto RGB, and XYZ.

Profile tags are embedded in the output file for the four standard color spaces. For 24-bit conversion, gamma appears to be fixed at 0.45, independent of the output color space. (It should be different for ProPhoto, which is designed for display at gamma = 1.8.) For 48-bit conversion gamma is fixed at 1 (linear conversion; does not correspond to standard color spaces). Several demosaicing quality levels (using different algorithms) are available (and worth exploring). See the dcraw Manpage for details.

Auto-exposure is always applied for 24-bit conversion (but not for 48-bit). I've changed the default to make it slightly darker than the dcraw default (using -b 0.99 in the command line) to reduce the likelihood of clipping pixels. For now we're stuck with auto-exposure for 24-bit conversion: see dcraw Frequently Asked Questions.

Dcraw converts RAW files into TIFF files, with the obscure PPM format available as an option. If check the box to delete the converted (TIFF) file, you may save the image in one of two compressed formats: PNG (lossless compression; takes longer) or JPEG (lossy compression; maximum quality; 24-bit only).

A list of supported cameras appears on the dcraw site. New cameras are typically added within one to three months of their introduction. The dcraw version included with Imatest sometimes falls behind. According to Dave Coffin, "Francisco Montilla provides Mac OS and Windows executables on his website. And Benjamin Lebsanft has volunteered to maintain Windows executables optimized for specific CPUs."

Rawview utility

The Rawview utility allows you to determine the color arrangement of Bayer RAW files (Red in R1C1, R1C2, R2C1, or R2C2) by trying each and selecting the one that looks best. The image can be saved as an RGB file if needed. It can be opened by pressing the Rawview button on the far right of the Imatest main window, just to the right of dcraw. When Rawview is first opened, instructions similar to those shown below appear. (They are likely to be more up-to-date.)

Welcome to Rawview. Preview Bayer RAW files.You can find the relationship between colors and pixel locations.
The input file should be a monochrome file (r x n x 1 pixels) that contains Bayer Raw data.
The image is not fully demosaiced: it has only half-resolution.
There is no White Balance. Colors may appear faint.
16/48 bit images are lightened to improve visibility.
The input image should have some strong, identifiable colors.

Start by pressing "Read (Bayer) RAW image file."
Click on "Red in RmCn..." popup menu and determine which selection looks correct.

After the image file has been read, the Rawview window looks like this:

Rawview window (not zoomed)
The Rawview window (no zoom)

Zoom of Rawview

The zoom function is always active: the image can be zoomed in to better view key details, like the red dots on the mirror, illustrated on the right, which only appear to be red when Red in R1C1 is selected. The Save button is enabled whenever the Red in RmCn selection is changed. It sets the default for opening Bayer RAW files in Imatest modules, affecting some displays.

Note that Rawview performs no White Balance, so the balance you see represents the color balance in the sensor, which may be of interest. It is rarely neutral.

Two options are available for saving images as an RGB files: Save low res RGB file saves the image as a low resolution RGB file using the R, (one of the) G, and B channels without demosaicing. The vertical and horizontal resolution is half that of the original file (total pixel count is 1/4 of the original). Save full res demosaiced RGB file uses Malab's standard gradient-corrected linear interpolation demosaicing algorithm to save the image in a full resolution RGB file.

Rawview dropdown menus include display options and Save commands.

File

Settings

Help

Generalized Read Raw

RAW files created by engineering development systems are rarely supported by dcraw. In many such cases you can read them (even if they're not standard image files) using the Read RAW capability released in Imatest 3.6. This new capability allows you to demosaic these files or to measure MTF of undemosaiced files (interpreted as standard monochrome files) when White Balance (WB) is applied.

To read generalized RAW files you must specify a file extension (up to five can be registered) and associate read parameters with that extension. This can be done in the following steps.

Click Settings, Read Raw setup. The following dialog box opens. Note that it's labeled Beta. Its capabilities will be enhanced through 2010. Entries should be based on manufacturer's data sheets, but some trial-and-error is usually required. If you make an incorrect choice, two plots will be displayed (below) to help determine the correct values.

Read Raw setup
Read Raw setup dialog box

SFR edge: no white balance SFR edge: white balanced
SFR edges: without and with White Balance

Estimating image width and height


If the number of pixels read, excluding the skipped pixels at the beginning or end of the file, is not equal to the specified (Width x Height), two diagnostic plots are produced and the periodicity (normally equal to the pixel width) is estimated. The image on the right is what we get if we enter a deliberately incorrect value for Width (2590 instead of 2592). It's only a little off. Most of the time the resulting image is unintelligible.

The first added plot shows the levels of 215 = 32768 pixels near the middle of the file. It gives a good idea of the periodicity, but it's very difficult to read the exact number from this plot.


Read Raw signal plot

The second added plot estimates the periodicity (the most likely image width in pixels). It is derived by taking the fourier transform of the data in the first plot, calculating the absolute value (i.e., removing the phase), then taking the inverse fourier transform. The location of the first major peak (above 0) is the most likely periodicity. In this case it's 2592, shown on the upper right. This estimate may not work for all images. If necessary, you can zoom in by drawing rectangles around the region of interest. You may need to zoom in more than once to find the precise location of a feature. Double-click to zoom out.

Read Raw periodicity plot




Subscribe to the Imatest Newsletter