Check of my understanding - Locating Thumbnail in Raw File or finding IFD1

Started by Skids, May 11, 2024, 02:05:50 AM

Previous topic - Next topic

Skids

Hi,
I have been looking at a html dump of a Panasonic Raw (.rw2) file with the aim of understanding how code might written to extract the jpeg thumbnail for display.

The simplest approach outside Exiftool is to search the raw file for the start bytes of "FFD8 FFDB" followed by the next end of image marker "FFD9".  Surprisingly this appears to be reliable with the raw files I have to hand.  It is also fast.  However, I freely admit this brute force approach is likely to fail on some raw files and is not the "proper" way to do it.

Looking for a better way of coding the extraction of the thumbnail forced me to review the Tiff format which appears to apply to many raw formats including the Panasonic .rw2 files. 

What confused me when reviewing the html output from Exiftool is that the Tiff/Raw IFD0 ends with a zero offset to the next IFD i.e. there is only the single Tiff IFD.  However, when a search is made for the "Thumbnail" tag it is found inside IFD1.

I think I have worked out the structure; am I correct in thinking that the way to extract the thumbnail is to first extract the data pointed to by the IFD0 tag "JpgFromRaw"?  The extracted data is a large JPEG image of 380kB which contains the thumbnail jpeg.  This image data should then be treated as a new image file with its own IFD0 which thankfully ends with an offset pointing to IFD1 and thus leads to the thumbnail itself.

Am I correct ?

Having hopefully correctly worked out how the rw2 file is structured I shall now concentrate on how to use ExifTool to read the data from within my code.

Thanks for reading,

S

Phil Harvey

The thumbnail is in the JpgFromRaw image, which is referenced from IFD0 of the main TIFF structure.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

FrankB

I know it's Delphi/Pascal, and probably not your programming language. But...

In ExifToolGui some functions read the raw files directly, to speed up things. Amongst others filetype .RW2 is also handled.

Maybe this sourcecode can help you.
https://github.com/FrankBijnen/ExifToolGui/blob/main/Source/ExifInfo.pas

Look for 'ReadTIFF' line 871.
It calls ParseIfd0 line 889
That finds the JPGfromRAWoffset at line 603
That offset is used to call ReadJpeg at line 1411.
ReadJpeg does nothing with the Thumbnail Image, but you get the idea hopefully.

Credits go to Bogdan Hrastnik, I only restructured it.



Skids

Thanks for your replies and the confirmation that in the case of rw2 files the thumbnail is inside the jpeg file jpgfromRaw.

Delphi is a blast from the past!

best wishes
S