Advertisement
anta40

read-exif-new

Apr 19th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. /*
  2.  * libexif example program to display the contents of a number of specific
  3.  * EXIF and MakerNote tags. The tags selected are those that may aid in
  4.  * identification of the photographer who took the image.
  5.  *
  6.  * Placed into the public domain by Dan Fandrich
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <libexif/exif-data.h>
  13.  
  14. #define FILE_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
  15.  
  16. /* Remove spaces on the right of the string */
  17. static void trim_spaces(char *buf)
  18. {
  19.     char *s = buf-1;
  20.     for (; *buf; ++buf) {
  21.         if (*buf != ' ')
  22.             s = buf;
  23.     }
  24.     *++s = 0; /* nul terminate the string on the first of the final spaces */
  25. }
  26.  
  27. /* Show the tag name and contents if the tag exists */
  28. static void show_tag(ExifData *d, ExifIfd ifd, ExifTag tag)
  29. {
  30.     /* See if this tag exists */
  31.     ExifEntry *entry = exif_content_get_entry(d->ifd[ifd],tag);
  32.     if (entry) {
  33.         char buf[1024];
  34.  
  35.         /* Get the contents of the tag in human-readable form */
  36.         exif_entry_get_value(entry, buf, sizeof(buf));
  37.  
  38.         /* Don't bother printing it if it's entirely blank */
  39.         trim_spaces(buf);
  40.         if (*buf) {
  41.             printf("%s: %s\n", exif_tag_get_name_in_ifd(tag,ifd), buf);
  42.         }
  43.     }
  44. }
  45.  
  46. static char* read_tag(ExifData *ed, ExifIfd eid, ExifTag tag){
  47.     static char result[1024];
  48.     ExifEntry *entry = exif_content_get_entry(ed->ifd[eid], tag);
  49.    
  50.     if (entry){
  51.         char buf[1024];
  52.        
  53.         exif_entry_get_value(entry, buf, sizeof(buf));
  54.         trim_spaces(buf);
  55.    
  56.         if (*buf) strcpy(result, buf);
  57.         else strcpy(result, "NULL");
  58.     }
  59.     else strcpy(result, "NULL");
  60.    
  61.     return result;
  62. }
  63.  
  64. /* Show the given MakerNote tag if it exists */
  65. static void show_mnote_tag(ExifData *d, unsigned tag)
  66. {
  67.     ExifMnoteData *mn = exif_data_get_mnote_data(d);
  68.     if (mn) {
  69.         int num = exif_mnote_data_count(mn);
  70.         int i;
  71.  
  72.         /* Loop through all MakerNote tags, searching for the desired one */
  73.         for (i=0; i < num; ++i) {
  74.             char buf[1024];
  75.             if (exif_mnote_data_get_id(mn, i) == tag) {
  76.                 if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
  77.                     /* Don't bother printing it if it's entirely blank */
  78.                     trim_spaces(buf);
  79.                     if (*buf) {
  80.                         printf("%s: %s\n", exif_mnote_data_get_title(mn, i),
  81.                             buf);
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. static ExifEntry *init_tag(ExifData *exif, ExifIfd ifd, ExifTag tag)
  90. {
  91.     ExifEntry *entry;
  92.     /* Return an existing tag if one exists */
  93.     if (!((entry = exif_content_get_entry (exif->ifd[ifd], tag)))) {
  94.         /* Allocate a new entry */
  95.         entry = exif_entry_new ();
  96.         assert(entry != NULL); /* catch an out of memory condition */
  97.         entry->tag = tag; /* tag must be set before calling
  98.                  exif_content_add_entry */
  99.  
  100.         /* Attach the ExifEntry to an IFD */
  101.         exif_content_add_entry (exif->ifd[ifd], entry);
  102.  
  103.         /* Allocate memory for the entry and fill with default data */
  104.         exif_entry_initialize (entry, tag);
  105.  
  106.         /* Ownership of the ExifEntry has now been passed to the IFD.
  107.          * One must be very careful in accessing a structure after
  108.          * unref'ing it; in this case, we know "entry" won't be freed
  109.          * because the reference count was bumped when it was added to
  110.          * the IFD.
  111.          */
  112.         exif_entry_unref(entry);
  113.     }
  114.     return entry;
  115. }
  116.  
  117.  
  118. int main(int argc, char **argv)
  119. {
  120.     ExifData *ed;
  121.     ExifEntry *entry;
  122.     unsigned char *data;
  123.     unsigned int data_size;
  124.  
  125.     if (argc < 2) {
  126.         printf("Usage: %s image.jpg\n", argv[0]);
  127.         printf("Displays tags potentially relating to ownership "
  128.                 "of the image.\n");
  129.         return 1;
  130.     }
  131.  
  132.     /* Load an ExifData object from an EXIF file */
  133.     ed = exif_data_new_from_file(argv[1]);
  134.     if (!ed) {
  135.         printf("File not readable or no EXIF data in file %s\n", argv[1]);
  136.         return 2;
  137.     }
  138.  
  139.     printf("Model : %s\n", read_tag(ed, EXIF_IFD_0, EXIF_TAG_MODEL));
  140.     printf("Software: %s\n", read_tag(ed, EXIF_IFD_0, EXIF_TAG_SOFTWARE));
  141.  
  142.     entry = init_tag(ed, EXIF_IFD_EXIF, EXIF_TAG_MODEL);
  143.     exif_set_long(entry->data, FILE_BYTE_ORDER, atol("Nokia 3330"));
  144.     exif_data_save_data(ed, &data, &data_size);
  145.     /* Free the EXIF data */
  146.     exif_data_unref(ed);
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement