Arte offers a nice service to watch broadcastings you weren't able to see on TV. This little script makes it easy to download and archive the videos. Unfortunately, the service is limited to French and German residents.

Panasonic includes lens distortion correction data in their RAW files as an EXIF tag. Unfortunately, they did not release the specification for this tag. Which is really annoying for people willing to use a RAW converter which is not vetted by Panasonic.

Inspired by this blog post : Dissecting Panasonic RW2 files. I decided to take the plunge and finally find out what's behind the format.

The following command allows us to get the raw hex bytes of the 0x119 tag, which includes the correction data :

$ exiv2 pr -ph -u sample.rw2 | grep -A2 0x0119 0x0119 PanasonicRaw 0x0119 Undefined 32 32 0000 29 54 9b 48 fc 00 00 00 69 01 00 00 e0 01 01 00 )T.H....i....... 0010 7f 0f 34 01 56 02 81 fb c4 09 28 03 ce 5a d6 8e ..4.V.....(..Z..

The data is only 32 bytes long, which should make it quite easy to parse.

After some (err a lot of) reverse engineering work, I finally understand enough to write a parser : the data is infact 16 short (16 bits) integers, represented in little endian order :

5429 489b 00fc 0000 0169 0000 01e0 0001 0f7f 0134 0256 fb81 09c4 0328 5ace 8ed6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Words 0, 1, 14 and 15 are checksums, see the code for the algorithm.

Word 7 is a flag : 0 means the distortion correction data shouldn't be applied, 1 means it should. Other values seem to be invalid



The rest seems to represent 2 types of data, but only one seems to be used, consisting of words 12, 5, 8, 4 and 11. Changing words 2, 3, 6, 9, 10 and 13 doesn't seem to have any effect.

word 12 seems to be always be equal to 2500, changing it disables distortion correction

words 5, 8, 4 and 11 are floating point numbers encoded as signed integers. Divide them by 32768 to get the original value. Word 5 is a scale factor. Word 8 is the primary (a) coefficient for the distortion correction Word 4 is the second (b) coefficient for the distortion correction Word 11 is the third (c) coefficient for the distortion correction



This can be summarized by :

n = data[12]; scale = 1.0/(1.0+(data[5]/32768.0)); a = data[8]/32768.0; b = data[4]/32768.0; c = data[11]/32768.0;

Now after playing with Adobe DNG Converter to get a clue on the equation used, it seemed parameters a b and c roughly match the following equation :

Ru = scale*(Rd + a*Rd^3 + b*Rd^5 + c*Rd^7)

Reversing SilkyPix confirms it. But it computes it in a weird way :

double table[100]; for(i=0; i<100; i++) table[i] = f(i/25.0);

And stops computing if the derivative becomes negative or if the value exceeds 2.

Unfortunately, more tests with lensfun got me confused, and for example, trying the "poly3" model gives completely different results.

You can find below some tools which helped me reverse engineer the format, including the code to fix checksums.

Please check Andrew Johnston's work on the subject : http://www.andrewj.com/mft/

TODO

Tag 0x011b contains information to correct chromatic aberrations.

History

30/04/2011 : 0.1 : first public release

Download it here : panarw2-v0.1.tar

Windows, with binaries : panarw2-0.1.zip

Github : https://github.com/trou/panasonic-rw2