Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dng fixes #285

Merged
merged 2 commits into from Apr 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions image/dng.cpp
Expand Up @@ -304,6 +304,14 @@ void dng_save(std::vector<libcamera::Span<uint8_t>> const &mem, StreamInfo const
// Create a separate IFD just for the EXIF tags. Why we couldn't simply have
// DNG tags for these, which would have made life so much easier, I have no idea.
Comment on lines 304 to 305
Copy link

@kmilos kmilos May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you do - it's called TIFF/EP (which DNG extends if you've read the spec carefully, as do other raw containers like NEF) and these tags go straight into IFD0 and replicate most of Exif tags. Everything is in these two specs:

http://www.barrypearson.co.uk/top2009/downloads/TAG2000-22_DIS12234-2.pdf
https://helpx.adobe.com/content/dam/help/en/photoshop/pdf/dng_spec_1_6_0_0.pdf

So typically DNGs don't have an ExifIFD unless there are really some other tags needed that don't exist in TIFF/EP+DNG. Usually you just have IFD0 w/ preview and all the metadata, and SubIFD w/ the raw image.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for that information. In fact I do seem to remember trying to get it to accept those tags in IFD0 but I never got it to work. I always got "unknown tag" errors, unless I was putting them in the EXIFID. Do you know if libtiff supports this feature? Perhaps libtiff isn't the best library to use, do you know of any alternatives? Thanks again!

Copy link

@kmilos kmilos May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, haven't tried that in a while, might be a problem indeed. Perhaps that's best raised w/ libtiff as an unnecessary limitation.

As a workaround, you could close the file w/o these "Exif" specific tags w/ libtiff, then add them via exiv2 library API to the Exif.Image directory (i.e. IFD0), rather than Exif.Photo (ExifIFD). (There is also libexif which might be lighter but don't know if it'll do the job...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I've found writing DNG files in C/C++ to be unreasonably difficult. Given that dcraw, RawTherapee, DarkTable and the Python libraries we use in our own software all seem to accept them, I'm not very keen to spend more time on it. Moving forwards we're going to be making ever more use of Picamera2 which supports DNG using the PiDNG Python library.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the Adobe DNG SDK then, perhaps that is easier...

In any case, requested w/ libtiff.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting that, let's see what happens!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidplowman The PiDNG description at https://github.com/schoolpost/PiDNG kind of implies it should have generic DNG support, any reason it was named PiDNG as opposed to PyDNG?

Thanks for mentioning that, it looks like it might be the solution to some of my headaches of writing DNGs from Python after reading raw data with rawpy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the PiDNG library was originally for the Pi but it has expanded since. You'd have to check with the author who has no affiliation with Raspberry Pi but has very generously been happy to help us make use of his work.

Copy link

@kmilos kmilos May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it went the other way for some reason: https://github.com/schoolpost/PyDNG

TIFFCreateEXIFDirectory(tif);

time_t t;
time(&t);
struct tm *time_info = localtime(&t);
char time_str[32];
strftime(time_str, 32, "%Y:%m:%d %H:%M:%S", time_info);
TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, time_str);

TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, &iso);
TIFFSetField(tif, EXIFTAG_EXPOSURETIME, exp_time);

Expand All @@ -317,6 +325,14 @@ void dng_save(std::vector<libcamera::Span<uint8_t>> const &mem, StreamInfo const
TIFFSetField(tif, TIFFTAG_EXIFIFD, offset_exififd);
TIFFWriteDirectory(tif);

// For reasons unknown, the last sub-IFD that we make seems to reappear at the
// end of the file as IDF1, and some tools (exiftool for example) are prone to
// complain about it. As far as I can see the code above is doing the correct
// things, and I can't find any references to this problem anywhere. So frankly
// I have no idea what is happening - please let us know if you do. Anyway,
// this bodge appears to make the problem go away...
TIFFUnlinkDirectory(tif, 2);

TIFFClose(tif);
}
catch (std::exception const &e)
Expand Down