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.
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.
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.