xref: /aosp_15_r20/external/libexif/contrib/examples/photographer.c (revision 735d6239c16e246968a03ef6e2db00d67bad6cdc)
1*735d6239SKiyoung Kim /*
2*735d6239SKiyoung Kim  * libexif example program to display the contents of a number of specific
3*735d6239SKiyoung Kim  * EXIF and MakerNote tags. The tags selected are those that may aid in
4*735d6239SKiyoung Kim  * identification of the photographer who took the image.
5*735d6239SKiyoung Kim  *
6*735d6239SKiyoung Kim  * Placed into the public domain by Dan Fandrich
7*735d6239SKiyoung Kim  */
8*735d6239SKiyoung Kim 
9*735d6239SKiyoung Kim #include <stdio.h>
10*735d6239SKiyoung Kim #include <string.h>
11*735d6239SKiyoung Kim #include <libexif/exif-data.h>
12*735d6239SKiyoung Kim 
13*735d6239SKiyoung Kim /* Remove spaces on the right of the string */
trim_spaces(char * buf)14*735d6239SKiyoung Kim static void trim_spaces(char *buf)
15*735d6239SKiyoung Kim {
16*735d6239SKiyoung Kim     char *s = buf-1;
17*735d6239SKiyoung Kim     for (; *buf; ++buf) {
18*735d6239SKiyoung Kim         if (*buf != ' ')
19*735d6239SKiyoung Kim             s = buf;
20*735d6239SKiyoung Kim     }
21*735d6239SKiyoung Kim     *++s = 0; /* nul terminate the string on the first of the final spaces */
22*735d6239SKiyoung Kim }
23*735d6239SKiyoung Kim 
24*735d6239SKiyoung Kim /* Show the tag name and contents if the tag exists */
show_tag(ExifData * d,ExifIfd ifd,ExifTag tag)25*735d6239SKiyoung Kim static void show_tag(ExifData *d, ExifIfd ifd, ExifTag tag)
26*735d6239SKiyoung Kim {
27*735d6239SKiyoung Kim     /* See if this tag exists */
28*735d6239SKiyoung Kim     ExifEntry *entry = exif_content_get_entry(d->ifd[ifd],tag);
29*735d6239SKiyoung Kim     if (entry) {
30*735d6239SKiyoung Kim         char buf[1024];
31*735d6239SKiyoung Kim 
32*735d6239SKiyoung Kim         /* Get the contents of the tag in human-readable form */
33*735d6239SKiyoung Kim         exif_entry_get_value(entry, buf, sizeof(buf));
34*735d6239SKiyoung Kim 
35*735d6239SKiyoung Kim         /* Don't bother printing it if it's entirely blank */
36*735d6239SKiyoung Kim         trim_spaces(buf);
37*735d6239SKiyoung Kim         if (*buf) {
38*735d6239SKiyoung Kim             printf("%s: %s\n", exif_tag_get_name_in_ifd(tag,ifd), buf);
39*735d6239SKiyoung Kim         }
40*735d6239SKiyoung Kim     }
41*735d6239SKiyoung Kim }
42*735d6239SKiyoung Kim 
43*735d6239SKiyoung Kim /* Show the given MakerNote tag if it exists */
show_mnote_tag(ExifData * d,unsigned tag)44*735d6239SKiyoung Kim static void show_mnote_tag(ExifData *d, unsigned tag)
45*735d6239SKiyoung Kim {
46*735d6239SKiyoung Kim     ExifMnoteData *mn = exif_data_get_mnote_data(d);
47*735d6239SKiyoung Kim     if (mn) {
48*735d6239SKiyoung Kim         int num = exif_mnote_data_count(mn);
49*735d6239SKiyoung Kim         int i;
50*735d6239SKiyoung Kim 
51*735d6239SKiyoung Kim         /* Loop through all MakerNote tags, searching for the desired one */
52*735d6239SKiyoung Kim         for (i=0; i < num; ++i) {
53*735d6239SKiyoung Kim             char buf[1024];
54*735d6239SKiyoung Kim             if (exif_mnote_data_get_id(mn, i) == tag) {
55*735d6239SKiyoung Kim                 if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
56*735d6239SKiyoung Kim                     /* Don't bother printing it if it's entirely blank */
57*735d6239SKiyoung Kim                     trim_spaces(buf);
58*735d6239SKiyoung Kim                     if (*buf) {
59*735d6239SKiyoung Kim                         printf("%s: %s\n", exif_mnote_data_get_title(mn, i),
60*735d6239SKiyoung Kim                             buf);
61*735d6239SKiyoung Kim                     }
62*735d6239SKiyoung Kim                 }
63*735d6239SKiyoung Kim             }
64*735d6239SKiyoung Kim         }
65*735d6239SKiyoung Kim     }
66*735d6239SKiyoung Kim }
67*735d6239SKiyoung Kim 
main(int argc,char ** argv)68*735d6239SKiyoung Kim int main(int argc, char **argv)
69*735d6239SKiyoung Kim {
70*735d6239SKiyoung Kim     ExifData *ed;
71*735d6239SKiyoung Kim     ExifEntry *entry;
72*735d6239SKiyoung Kim 
73*735d6239SKiyoung Kim     if (argc < 2) {
74*735d6239SKiyoung Kim         printf("Usage: %s image.jpg\n", argv[0]);
75*735d6239SKiyoung Kim         printf("Displays tags potentially relating to ownership "
76*735d6239SKiyoung Kim                 "of the image.\n");
77*735d6239SKiyoung Kim         return 1;
78*735d6239SKiyoung Kim     }
79*735d6239SKiyoung Kim 
80*735d6239SKiyoung Kim     /* Load an ExifData object from an EXIF file */
81*735d6239SKiyoung Kim     ed = exif_data_new_from_file(argv[1]);
82*735d6239SKiyoung Kim     if (!ed) {
83*735d6239SKiyoung Kim         printf("File not readable or no EXIF data in file %s\n", argv[1]);
84*735d6239SKiyoung Kim         return 2;
85*735d6239SKiyoung Kim     }
86*735d6239SKiyoung Kim 
87*735d6239SKiyoung Kim     /* Show all the tags that might contain information about the
88*735d6239SKiyoung Kim      * photographer
89*735d6239SKiyoung Kim      */
90*735d6239SKiyoung Kim     show_tag(ed, EXIF_IFD_0, EXIF_TAG_ARTIST);
91*735d6239SKiyoung Kim     show_tag(ed, EXIF_IFD_0, EXIF_TAG_XP_AUTHOR);
92*735d6239SKiyoung Kim     show_tag(ed, EXIF_IFD_0, EXIF_TAG_COPYRIGHT);
93*735d6239SKiyoung Kim 
94*735d6239SKiyoung Kim     /* These are much less likely to be useful */
95*735d6239SKiyoung Kim     show_tag(ed, EXIF_IFD_EXIF, EXIF_TAG_USER_COMMENT);
96*735d6239SKiyoung Kim     show_tag(ed, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION);
97*735d6239SKiyoung Kim     show_tag(ed, EXIF_IFD_1, EXIF_TAG_IMAGE_DESCRIPTION);
98*735d6239SKiyoung Kim 
99*735d6239SKiyoung Kim     /* A couple of MakerNote tags can contain useful data.  Read the
100*735d6239SKiyoung Kim      * manufacturer tag to see if this image could have one of the recognized
101*735d6239SKiyoung Kim      * MakerNote tags.
102*735d6239SKiyoung Kim      */
103*735d6239SKiyoung Kim     entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_MAKE);
104*735d6239SKiyoung Kim     if (entry) {
105*735d6239SKiyoung Kim         char buf[64];
106*735d6239SKiyoung Kim 
107*735d6239SKiyoung Kim         /* Get the contents of the manufacturer tag as a string */
108*735d6239SKiyoung Kim         if (exif_entry_get_value(entry, buf, sizeof(buf))) {
109*735d6239SKiyoung Kim             trim_spaces(buf);
110*735d6239SKiyoung Kim 
111*735d6239SKiyoung Kim             if (!strcmp(buf, "Canon")) {
112*735d6239SKiyoung Kim                 show_mnote_tag(ed, 9); /* MNOTE_CANON_TAG_OWNER */
113*735d6239SKiyoung Kim 
114*735d6239SKiyoung Kim             } else if (!strcmp(buf, "Asahi Optical Co.,Ltd.") ||
115*735d6239SKiyoung Kim                        !strcmp(buf, "PENTAX Corporation")) {
116*735d6239SKiyoung Kim                 show_mnote_tag(ed, 0x23); /* MNOTE_PENTAX2_TAG_HOMETOWN_CITY */
117*735d6239SKiyoung Kim             }
118*735d6239SKiyoung Kim         }
119*735d6239SKiyoung Kim     }
120*735d6239SKiyoung Kim 
121*735d6239SKiyoung Kim     /* Free the EXIF data */
122*735d6239SKiyoung Kim     exif_data_unref(ed);
123*735d6239SKiyoung Kim 
124*735d6239SKiyoung Kim     return 0;
125*735d6239SKiyoung Kim }
126