How to get Raw data from CR2 format. page9 Decoding Lossless JPEG part3 - Decoding the data

since:Aug 31,2014
last update:Sep 6,2014
previous page  

top > photograph and camera > How to get Raw data from CR2 format. page1 > Decoding Lossless JPEG part3 - Decoding the data

4.5 Decoding the data

Now we are ready to decode.

In JPEG, 0xFF means special marker, and if "0xFF" is stored as data, it must be expressed "0xFF 0x00".

Then, if RST marker exists in data, we handle this in the way of specification. However, in CR2 format, it seems that no marker exists until 0xFFD9 marker (End Of Image) appears. Therefore, the way of other marker handling is omitted in this document.



"0xFF 0x00" means "0xFF" in data, and actual data is below.

0xFF 0x00 0xFF 0xFC 0x00 0xEF 0xF7 0x00 0x3F 0x98 0xC7 ......

The way to decode is below.
The initial value is 2^(P-1). P is the sample precision value.
In the sample, as P is 14, the initial value is 0x2000 (8192 = 2^13).

In the first line, we must use predict algorithm #1 which is the difference between this value and the left value.
In the sample, the predict algorithm is 1, so we use #1 algorithm in all lines.
If we start a new line, we must use the predict algorithm #2 which is the difference between this value and the above value, for the first value.

Let's start to decode the first 6 byte.

1111 1111  0000 0000  1111 1111  1111 1100  0000 0000  1110 1111

We start to handle #1 image component. We refer #0 DC Huffman table as stated in #1 image component. We check from higher bit until it matches Huffman table code.
In this case, it matches 1111 1111 0.

1111 1111  0 000 0000  1111 1111  1111 1100  0000 0000  1110 1111

In #0 DC Huffman table, this value is 0x0D.

Next, we get 13bit (0x0D) length data.

1111 1111  0000 0000  1111 1111  1111 1100  0000 0000  1110 1111

If the highest bit is 0, it means minus value, we subtract the previous value from bit reverse value we got.

111 1111 0000 00 =0x1FC0 (8128). Then, 8192 - 8128 = 64 (0x0040)

If the highest bit is 1, we add the previous value and the value we got.

Note: In another way, this can calculate using two's complement.


Next, we start to handle #2 image component. We refer #1 DC Huffman table as stated in #2 image component.
We check from higher bit until it matches Huffman table code.
Then we get "1111 1111 0".

1111 1111  0000 0000  1111 1111  1111 1100  0000 0000  1110 1111

In #1 DC Huffman table, this value is 0x0D.

Next, we get 13bit (0x0D) length data.

1111 1111  0000 0000  1111 1111  1111 1100  0000 0000  1110 1111

If the highest bit is 0, it means minus value, we subtract the previous value from bit reverse value we got.
8192 - 8177 = 15 (0x000F)

Here, we'll try to check the correctness of these values.

Below is the partial raw data dump of uncompressed DNG file from this CR2 file.
(See IMG_2026_RAW_Data_from_DNG.bin)



Cetainly the first 2 byte is "0x0040" and next 2 byte is "0x000F". (Byte order of this file is the little endian.)

We calculate for component #3 and #4 in the same way.
Then we calculate for component #1 again. Then we calculate the value using previous value (0x0040) that we got above.


By the way, a certain point later, the raw data in uncompressed DNG do not match the value we get, for RawImageSegmentation (cr2_slice) stated in IFD3.
Then we must sort this data same as sensor layout.

Lossless JPEG data is below layout.


We sort these like below.


For these data include masked data etc, if you would like to get the image data, please clip into effective area of the sensor.

(End of Document)



previous page [1] [2] [3] [4] [5] [6] [7] [8] [9]