Thread: Reading Exif data from PNG file

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    4

    Reading Exif data from PNG file

    Hi there,

    I'm trying to read Exif data from PNG files but hit a brick wall, and the lack of or poor documentation is just frustrating.

    Originally I just wanted to tinker around with dithering for reducing bit-depth in pictures, and it all kind of spiralled out of control with adding support for PNG and JPEG, and now finally wanting to preserve meta-data across file formats as good as possible. ;o)

    The Exif data in PNGs is stored in a "Raw profile type exif" text chunk and looks like this:
    Code:
    
    exif
        5156
    45786966000049492a00080000000b000001040001000000640000000101040001000000
    430000000201030003000000920000000f01020009000000980000001001020005000000
    a20000001a01050001000000a80000001b01050001000000b00000002801030001000000
    ...
    which seems to be hexadecimal representation of binary data (no docs found about that).

    Converting that to binary seems promising, I get a block of binary data starting with "Exif".

    So I figured I could just feed that binary data to libexif, but no luck. (and again, no useful documentation)

    From the little documentation found on EXIF library (libexif) API: The libexif library I expected to simply do:
    Code:
    ExifLoader *loader = exif_loader_new();
    exif_loader_write(loader, rawdata, len);
    ExifData *data = exif_loader_get_data(loader);
    But no luck, just a NULL return value.

    Does anybody here have any experience with this?

    I stripped down the code to just the bare minimum but it's still ~300 lines, so I am attaching it instead of pasting it all in here. (along with a small sample PNG file).
    Needs libexif and libpng to compile
    Code:
    sudo apt-get install libpng-dev libexif-dev
    gcc -std=c99 pngexif.c -lpng -lexif
    Thanks a bunch,

    Rupert
    Attached Images Attached Images Reading Exif data from PNG file-testfile-png 
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    No information?

    http://www.libpng.org/pub/png/libpng-manual.txt
    Starting with libpng-1.6.31, the eXIf chunk is supported. Libpng does not
    attempt to decode the Exif profile; it simply returns a byte array
    containing the profile to the calling application which must do its own
    decoding.

    png_get_eXIf_1(png_ptr, info_ptr, &num_exif, &exif);
    (PNG_INFO_eXIf)

    exif - Exif profile (array of png_byte)
    There's a similar set function as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2020
    Posts
    4
    Hi, thanks for your ninput.

    The eXIf chunk you are citing is relatively new and doesn't seem to be in widespread use.
    Instead, Exif info is commonly stored as a text chunk named "Raw profile type exif" as described above. (That is also where e.g. GIMP will store Exif data.)

    So far I haven't had a PNG file yet with eXIf chunk, but I somehow suspect that I'd end up in the same place anyway.

  4. #4
    Registered User
    Join Date
    Jun 2020
    Posts
    4
    Played with it a bit more, and at least I figured out how to get error messages from libexif now:
    Code:
    libexif (ExifLoader): Scanning 5156 byte(s) of data...
    libexif (ExifLoader): The data supplied does not seem to contain EXIF data.
    
    
    exif_loader_write() returned 0
    exif_loader_get_data() failed
    I then tried to let libexif read the whole file instead of just feeding it the Exif part.
    It works for JPEGs, but not for PNGs. (Same error message as above).

    So I guess it's not a C programming question, but rather a compatibility issue with libexif.
    Oh, well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading data from a file
    By blukacs3609 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2015, 08:27 PM
  2. Jpg EXIF decoding and Jpg barcode reading
    By :o) in forum C Programming
    Replies: 6
    Last Post: 12-25-2011, 09:21 AM
  3. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  4. It is not reading the data from the file...
    By musique in forum C++ Programming
    Replies: 5
    Last Post: 05-01-2009, 03:19 PM
  5. Replies: 2
    Last Post: 06-16-2005, 10:03 AM

Tags for this Thread