Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * libexif example program to display the contents of a number of specific
- * EXIF and MakerNote tags. The tags selected are those that may aid in
- * identification of the photographer who took the image.
- *
- * Placed into the public domain by Dan Fandrich
- */
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <libexif/exif-data.h>
- #define FILE_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
- /* Remove spaces on the right of the string */
- static void trim_spaces(char *buf)
- {
- char *s = buf-1;
- for (; *buf; ++buf) {
- if (*buf != ' ')
- s = buf;
- }
- *++s = 0; /* nul terminate the string on the first of the final spaces */
- }
- /* Show the tag name and contents if the tag exists */
- static void show_tag(ExifData *d, ExifIfd ifd, ExifTag tag)
- {
- /* See if this tag exists */
- ExifEntry *entry = exif_content_get_entry(d->ifd[ifd],tag);
- if (entry) {
- char buf[1024];
- /* Get the contents of the tag in human-readable form */
- exif_entry_get_value(entry, buf, sizeof(buf));
- /* Don't bother printing it if it's entirely blank */
- trim_spaces(buf);
- if (*buf) {
- printf("%s: %s\n", exif_tag_get_name_in_ifd(tag,ifd), buf);
- }
- }
- }
- static char* read_tag(ExifData *ed, ExifIfd eid, ExifTag tag){
- static char result[1024];
- ExifEntry *entry = exif_content_get_entry(ed->ifd[eid], tag);
- if (entry){
- char buf[1024];
- exif_entry_get_value(entry, buf, sizeof(buf));
- trim_spaces(buf);
- if (*buf) strcpy(result, buf);
- else strcpy(result, "NULL");
- }
- else strcpy(result, "NULL");
- return result;
- }
- /* Show the given MakerNote tag if it exists */
- static void show_mnote_tag(ExifData *d, unsigned tag)
- {
- ExifMnoteData *mn = exif_data_get_mnote_data(d);
- if (mn) {
- int num = exif_mnote_data_count(mn);
- int i;
- /* Loop through all MakerNote tags, searching for the desired one */
- for (i=0; i < num; ++i) {
- char buf[1024];
- if (exif_mnote_data_get_id(mn, i) == tag) {
- if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
- /* Don't bother printing it if it's entirely blank */
- trim_spaces(buf);
- if (*buf) {
- printf("%s: %s\n", exif_mnote_data_get_title(mn, i),
- buf);
- }
- }
- }
- }
- }
- }
- static ExifEntry *init_tag(ExifData *exif, ExifIfd ifd, ExifTag tag)
- {
- ExifEntry *entry;
- /* Return an existing tag if one exists */
- if (!((entry = exif_content_get_entry (exif->ifd[ifd], tag)))) {
- /* Allocate a new entry */
- entry = exif_entry_new ();
- assert(entry != NULL); /* catch an out of memory condition */
- entry->tag = tag; /* tag must be set before calling
- exif_content_add_entry */
- /* Attach the ExifEntry to an IFD */
- exif_content_add_entry (exif->ifd[ifd], entry);
- /* Allocate memory for the entry and fill with default data */
- exif_entry_initialize (entry, tag);
- /* Ownership of the ExifEntry has now been passed to the IFD.
- * One must be very careful in accessing a structure after
- * unref'ing it; in this case, we know "entry" won't be freed
- * because the reference count was bumped when it was added to
- * the IFD.
- */
- exif_entry_unref(entry);
- }
- return entry;
- }
- int main(int argc, char **argv)
- {
- ExifData *ed;
- ExifEntry *entry;
- unsigned char *data;
- unsigned int data_size;
- if (argc < 2) {
- printf("Usage: %s image.jpg\n", argv[0]);
- printf("Displays tags potentially relating to ownership "
- "of the image.\n");
- return 1;
- }
- /* Load an ExifData object from an EXIF file */
- ed = exif_data_new_from_file(argv[1]);
- if (!ed) {
- printf("File not readable or no EXIF data in file %s\n", argv[1]);
- return 2;
- }
- printf("Model : %s\n", read_tag(ed, EXIF_IFD_0, EXIF_TAG_MODEL));
- printf("Software: %s\n", read_tag(ed, EXIF_IFD_0, EXIF_TAG_SOFTWARE));
- entry = init_tag(ed, EXIF_IFD_EXIF, EXIF_TAG_MODEL);
- exif_set_long(entry->data, FILE_BYTE_ORDER, atol("Nokia 3330"));
- exif_data_save_data(ed, &data, &data_size);
- /* Free the EXIF data */
- exif_data_unref(ed);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement