Getting all available data and still get the right active area from DNG

Hello, I have a DNG file that specifies DefaultCropOrigin and DefaultCropSize in its metadata. Using libraw I'd like to get all the sensor data available in the DNG file while still retaining informations about the default crop, so that the extra data doesn't get trimmed at this point.
I've patched dcraw_common.cpp to read the default crop info, but I'm still unable to get the result I want.

For reference this is my patch, though at this point I'm not sure this is the right way to do it:

diff --git a/internal/dcraw_common.cpp b/internal/dcraw_common.cpp
index 35af338..8b7e2c9 100644
--- a/internal/dcraw_common.cpp
+++ b/internal/dcraw_common.cpp
@@ -10216,6 +10216,8 @@ int CLASS parse_tiff_ifd (int base)
FILE *sfp;
+ int def_crop_x = 0, def_crop_y = 0;
+ int def_crop_width = 0, def_crop_height = 0;

if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
return 1;
@@ -11090,6 +11092,15 @@ guess_cfa_pc:
if(pixel_aspect > 0.995 && pixel_aspect < 1.005)
pixel_aspect = 1.0;
+ case 50719: /* DefaultCropOrigin */
+ def_crop_x = getint(type);
+ def_crop_y = getint(type);
+ break;
+ case 50720: /* DefaultCropSize */
+ def_crop_width = getint(type);
+ def_crop_height = getint(type);
+ break;
case 50778:
imgdata.color.dng_color[0].illuminant = get2();
@@ -11247,6 +11258,14 @@ guess_cfa_pc:
fseek (ifp, save, SEEK_SET);
+ top_margin += def_crop_y;
+ left_margin += def_crop_x;
+ if (def_crop_width) {
+ width = def_crop_width;
+ }
+ if (def_crop_height) {
+ height = def_crop_height;
+ }
if (sony_length && sony_length < 10240000 && (buf = (unsigned *) malloc(sony_length))) {
fseek (ifp, sony_offset, SEEK_SET);
fread (buf, sony_length, 1, ifp);

Following the explanation here, setting width and height like that causes the internal data array to be of the cropped size, thus in my code (oiiotool from openimageio really) I get black borders around the active area and the original data is lost. How should I go about preserving the original data and still be able to retrieve the active area size?


Thank you for your patch, but

Thank you for your patch, but...

1st: to get TIFF/EXIF fields (for your own use) you do not need to patch parse_tiff_ifd/parse_exif code. Just install callbacks.exif_cb and you'll get all tag data into your code.

2nd: Current LibRaw/Github master branch (public snapshot) already parse DNG DefaultCrop tags into

If you need to apply this crop, add LIBRAW_PROCESSING_USE_DNG_DEFAULT_CROP bit to imgdata.params.raw_processing_options

And, yes, this is noted in aug-2017 snapshot announce: support for DefaultCrop Origin/Size tags (add LIBRAW_PROCESSING_USE_DNG_DEFAULT_CROP to raw_processing_options to enable)

3rd: DNG is more complex, than 'simple parse tag from some nested IFD'. Same tags (DefaultCrop* for example) may be specified separately for each IFD (and/or for IFD0). If some DNG file contains several different-resolution renderings (e.g. full data + fast load data), these tags should be applied in accurate way (see identify() function code for that).

DefaultCrop tags may be rational, so getint() is not useful here, getreal(type) is suggested.

-- Alex Tutubalin