4 Replies Latest reply on Feb 29, 2008 5:47 AM by (Andreas_Heidenreich)

    Problems to read DNG files

      I converted a Canon .CR2 image to a .DNG image using the Adobe DNG
      converter, because I want to read raw files by my own C programs.

      As far as I understood, the DNG format is an extended TIFF format.
      Therefore, as a first step, I wrote a small C test program to read out the
      image file directories (IFDs). I succeeded only partly.
      My problems are the following:

      The DNG file apparently contains several images, the high resolution raw image
      and two low-resolution/thumbnail images. The IFDs are linked in the way as TIFF
      file trees are.

      The first IFD belongs to a low resolution image. With this first image I have
      no problems to read the relevant image characterisics such as the horizontal
      and vertical dimensions of the image. The first image also contains tag 330,
      which gives information about the subIFDs. The value of directory entry "count"
      is 2, which means that there are 2 child images. The value of the IFD entry is
      572, which is apparently the offset to the first child image. Here is a first
      ambiguity in the TIFF documentation (TIFF6_Specification_Sup1.pdf): It is not
      explicitely stated that the value is the offset of the next SubIFD.
      The NextIFD field at the end of the IFD is 0, which makes sense, because I
      don't expect further main images in the TIFF file.

      Reading the number of tags of the SubIFD at offset 572, I get the incredible
      high number of 18006 tags. I doubt that such a high number makes sense.
      For this child image, I can neither read the horizontal and vertical image
      dimensions nor the offset of the image body, and the offset of the next SubIFD
      (read at the end of the SubIFD) also does not make sense, because it is larger
      than the file size.

      Can anybody help? What mistake have I made? Is there a clearer documentation
      about TIFF/DNG available?
        • 1. Re: Problems to read DNG files
          Thomas Knoll Level 2
          Did you download the source code DNG SDK? If so, what does the dng_validate tool report in
          "-v" mode report for your DNG file? If not, why not?
          • 2. Re: Problems to read DNG files
            Panoholic Level 2
            > The first image also contains tag 330, which gives information about the subIFDs. The value of directory entry "count" is 2, which means that there are 2 child images. The value of the IFD entry is 572, which is apparently the offset to the first child image

            Basics: if the value(s) does not/do not fit in the four bytes of the tag, then the value in the tag is *always* an offset *to those values*.

            In this case there are two values, each being an offset, i.e. four bytes long. Accordingly, the value 572 is the offset *to two four-byte values*, which in this case happen to be offsets on their own, to the two sub IFDs.

            > Here is a first ambiguity in the TIFF documentation TIFF6_Specification_Sup1.pdf): It is not explicitely stated that the value is the offset of the next SubIFD

            These two sub IFDs are not *chained* together, because both are "pointed at" directly from the first IFD.

            > Reading the number of tags of the SubIFD at offset 572, I get the incredible high number of 18006 tags

            Of course, because at 572 there are two offsets to the two sub IFDs, not the sub IFDs themselves.

            Btw, the reason for two sub IFDs here is that you requested the DNG converter to create a preview image, beside the thumbnail. If you specify "none", then the tag 330 contains the offset directly to the (only) sub IFD of the very first IFD.
            • 3. Re: Problems to read DNG files
              Level 1
              Thank you, Thomas, for your answer, which was very helpful. The mistake I made was that I did not understand that all IFD addresses are stored at the place specified by tag 330. Before I thought that all the address of the next child IFD is always found at the end of the previous child IFD. After this misunderstanding was resolved, I can read now the IFDs without problem.

              Still it will take me some time to understand also, how the image body of the high resolution image is read out.

              I did not use DNG SDK, because I didn't know about it. I tried to learn from the TIFF and DNG documentations.

              The purpose of all this effort are applications to astrophotography.
              • 4. Re: Problems to read DNG files
                Level 1
                Another question. From the DNG documentation I inferred that DNG uses the lossless Huffman JPEG compression. Did I get this right?