The sensor data of a CR2 file is a single lossless JPEG stream, pointed to by the StripOffsets and StripByteCounts of IFD 3 (see header and IFDs). This is reverse-engineered knowledge, as used by dcraw, LibRaw and libopenraw.
Lossless JPEG
The stream follows ITU T.81 lossless mode (Huffman, process 14), the same kind DNG uses:
| Marker | Use |
|---|---|
FFD8 SOI | Start of image |
FFC4 DHT | Huffman tables, usually one per component |
FFC3 SOF3 | Frame header: precision (usually 12 or 14 bits), number of lines, samples per line, and 2 or 4 components |
FFDA SOS | Scan header with the predictor (usually 1, the sample to the left) and point transform 0 |
FFD9 EOI | End of image |
The JPEG frame is narrower than the sensor: its “components” are really neighbouring sensor columns. The number of sensor values per JPEG line is samples per line × components; for example a frame of 2 components reports half the real width.
Slices
Most cameras compress the image in vertical slices, to let the camera’s processor
work on strips. Tag 0xC640 in IFD 3 has three SHORT values:
| Value | Meaning |
|---|---|
| 1 | Number of slices, not counting the last one |
| 2 | Width in sensor columns of each of those slices |
| 3 | Width of the last slice |
The decoded JPEG is a single stream of values. The first slice’s width × the image height values fill the left slice from top to bottom, row by row; then the next slice is filled the same way, and so on. Values 1, 1728, 1904, for instance, mean two slices of 1728 and 1904 columns, together 3632 columns wide. When the tag is missing, the stream fills the image row by row.
libopenraw, which documents the tag for the 350D, 5D and 30D, writes the values as
1, x, y and says it does not know what the leading 1 means. dcraw’s code
treats the first value as the slice count (its loop runs for slices 0 up to and
including that value, so the count excludes the last slice) and the other two as the
widths, which is the reading above. libopenraw also notes that the width in the JPEG
frame is half the real width, because the stream counts two values per sample.
Raw layout
- The decoded image is the whole sensor, including covered borders of optical black pixels. The maker note’s SensorInfo gives the borders of the visible area.
- The colours follow a 2 × 2 Bayer pattern of red, two greens and blue.
- The black level is measured from the covered border, or read from the maker note’s colour data; the white level depends on the camera.
sRAW and mRAW
The smaller raw sizes of later cameras are not Bayer data but demosaiced luminance and
colour difference values. The lossless JPEG then has 3 components whose first one is
sampled more finely: 2 × 1 (4:2:2) or 2 × 2 (4:2:0), depending on the camera and the
size chosen. Tag 0xC6C5 in IFD 3 is present for these files. A decoder
converts the values to RGB with coefficients and white balance from the maker note.
dcraw tells the layouts apart by the sampling factors byte of the first frame component: horizontal × vertical factors minus 1, masked with 3, give 0 for plain data, 1 for 2 × 1 (4:2:2) and 3 for 2 × 2 (4:2:0). The decoder halves the frame width, requires at least four values per sample (the components plus that number), clips the result to 14 bits (0x3FFF) and multiplies each colour by a per-camera coefficient shifted right by 10 bits.
The exact meaning of the values in 0xC6C5 and the conversion coefficients
for each camera are not documented publicly in a single place; open source decoders
carry them per model. Neither the libopenraw page nor dcraw’s code read here says what
0xC5D8, 0xC5D9, 0xC5E0 or 0xC6DC
hold.
Sources
- libopenraw: CR2 – the header, IFD 3, slices and lossless JPEG
- dcraw.c (ncruces mirror) –
lossless_jpeg_load_raw,ljpeg_startandcanon_sraw_load_raw, read as a description of the layout - Laurent Clévy’s CR2 notes at lclevy.free.fr/cr2/ could not be reached from here (connection refused); nothing on this page is taken from them directly