Best way to create a simple TIFF header for .RAW file?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Posts: 49
Joined: 2013-12-13T09:54:14-07:00
Authentication code: 6789

Best way to create a simple TIFF header for .RAW file?

Post by BrianP007 »

The Camera Raw converter "DCRaw" prepends a 1376 byte header to a 16 bit/channel, RGB .raw file and magickally creates a tif file.

What is the easiest way to manufacture such a trivial TIF (PNG?) file header for an uncompressed UINT48, RGB, .RAW file?

The TIFF File Format spec says the first 2 chars are "II" for Intel byte order, a permanent version# of 42 -> "*", a NULL byte followed by a directory offset. Then a series of blocks, tags, fields and image data. DCRaw.c has a tiff_head() with a pageful of tiff_set() calls, a few of which are intelligible.

The DCRaw .TIF can be used by Photoshop and is seen in file explorer as just a (big) image file. Without the TIF header, you can' t see the image, you have to remember the XY res and Photoshop interrogates you every time for XY res, channels, byte order and header.

dcraw.exe -v -w -o 0 -6 -r 1 1 1 1 -g 1 0 -T pf-2015.0531-249399.nef
=-> creates pf-2015.0531-249399.tif , size= 217977008 BYTES

identify.exe pf-2015.0531-249399.tif => ... TIFF 7378x4924 ...

Tif_file_size_217977008 - 7378 * 4924 * 6Bytes/pixel = 1376 Bytes, Header_size

The TIF has all of the raw, uncompressed R, G, B data of a .RAW file and the convenience of a "standard" tif file. It can be passed to a program expecting a "photoshop-generated" .RAW file and easily identified as a .RAW with TIF-Figleaf without conversion.

Why not just build/link to IM???
Processing 217 MB RAW files with UINT64 integers quickly requires 64bit processing. IM only supports ms visual C++ on windoz. The free Express version of VC is 32bit only (afaik). I have had good luck with Strawberry Perl with MinGW, 64 bit.

Building ImageMagick source for Windows requires a modern version of Microsoft Visual Studio IDE

Posts: 49
Joined: 2013-12-13T09:54:14-07:00
Authentication code: 6789

Re: Best way to create a simple TIFF header for .RAW file?

Post by BrianP007 »

Hack Dave Coffin's minute tif header and adjust the binary guts to suit your raw files

You MUST know the X and Y resolution of your RAW file
This solution only works with UINT48, RGB .RAW data (DCRaw and Photoshop output)
Build DCRaw.exe from source optimized for your metal. Runs 6x faster than i386 (?), 32 bit msvc bonary version

This GCC one liner that may be helpful:
gcc -v -Ofast -B e:/strawberry_perl/c -Le:\strawberry_perl\c\x86_64-w64-mingw32\lib -D NODEPS -mtune=knightslanding -march=native e:/strawberry_perl/c/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/cc1.exe -v -iprefix e:/strawberry_perl/c/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/ -isysteme:/strawberry_perl/c/include -D_REENTRANT -D NODEPS c:/bin/dcraw/dcraw.c -dumpbase -lws2_32 -o c:/bin/dcraw.t3.exe

Create a TIF file with DCRaw: dcraw.exe (or c:/bin/dcraw.t3.exe)
c:/bin/dcraw.t3.exe -v -w -o 0 -6 -r 1 1 1 1 -g 1 0 -T Your-Masterpiece-1.nef
subtract Xres * Yres * 6 from the TIF file size created --
With the options above, the TIF excess on top of the raw, RGB data weighs in at 1376 Bytes, 0.000631% overhead above the RAW size

Fopen the TIF file "rb"
fread 1376 bytes into a buffer (which you have already allocated)
fclose the tif
fopen the *.hdr" file "wb"
Write the buffer to the .hdr file and fclose it

Now comes the fun part. Open the header file in a code editor emax, gvim, edlin (?) and look for 2 pair of 4 characters each which look out of place (think The Matrix). Look them up and see if they just happen to map in decimal to your image resolution (possibly with the bytes swapped ! :). Mine happened to land at Xres[16] in uint16_t format, and yres[22]. You don't have to look far and most of the other junk is NULL bytes which you can safely ignore (unless you like really teensy pictures)

Overwrite the wrong X and Y res with your current values.

fopen a tif file "wb"
dump the hacked 1376 bytes of header there
dump the raw data immediately after the header
fclose the tif

see if your file explorer shows you a thumbnail of it
See if Photoshop likes it (I get asked to accept the ProPhoto colorspace)

Test it some more
take any random uint48 image
crop an arbitrary chunk out with photoshop
save it as a .RAW and you may wish to add "${XRES}x$YRES." to the filename to ease the accounting

Process it as above and see if it is a REAL TIF!

For all of your routines which expect a .RAW file, insert a small function to handle TIFs by calculating the header size based on IDENTIFY.exe's XY res and your known bit depth and channels. If's it the good old 1376 bytes, you are home free with an ordinary, easy to use image file on the surface with the raw RGB goodness less than 2k away! And, in < 29 Milliseconds if you write it in C


Code: Select all

c:/bin/raw2tif.a.exe i:/exp/pf.249399/pf-2015.0531-249399.crop.2376x1584.raw -d
Run 2015-12-16 21:24:43
PAV: HdrFN=d:/pic/misc/dcr.1376b.7378x4924.def.hdr, XYres=2376x1584 -> 3.763584 MPix
RRR: Raw_filename: i:/exp/pf.249399/pf-2015.0531-249399.crop.2376x1584.raw  Size: 22.581504MB -> 3.763584 MPixels
RRR: Raw_filename: d:/pic/misc/dcr.1376b.7378x4924.def.hdr  Size: 0.001376MB -> 0.001376 MPixels

Uint16[0]=18761=0X4949, csp[0]=I, csp[1]=I
Uint16[1]=42=0X002A, csp[0]=*, csp[1]=@@
Uint16[16]=7378=0X1CD2, csp[0]=Ò, csp[1]=
Uint16[22]=4924=0X133C, csp[0]=<, csp[1]=

M: Hdra[16] 7378 != Xres 2376. Fixing.
M: FIXED X-Hdra[16] = 2376
M: Hdra[22] 4924 != Yres 1584. Fixing.
M: FIXED Y-Hdra[22] = 1584

MHRT: Wrote 22582880 bytes to tif i:/exp/pf.249399/pf-2015.0531-249399.crop.2376x1584.r.tif; disk size=22582880

TE: Report: Accum time for 4 events=28.96 ms  Run 2015-12-16 21:24:43
  EID_31)  Time=  0.008 sec = 27.322%, Event READ_RGB_UINT48_FILE, hits=2 
  EID_70)  Time=  0.118 ms  =  0.406%, Event HACK_DCRAW_HEADER   , hits=1 
  EID_71)  Time=  0.021 sec = 71.821%, Event MERGE_HDR_RAW2TIF   , hits=1