EXIF library (libexif) Internals  0.6.24
test-parse.c
Go to the documentation of this file.
1 
23 #include "libexif/exif-data.h"
24 #include "libexif/exif-system.h"
25 
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 static unsigned entry_count;
31 
33 static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
34 {
35  char buf[2000];
36  exif_entry_get_value(entry, buf, sizeof(buf));
37  printf(" Entry %u: %s (%s)\n"
38  " Size, Comps: %d, %d\n"
39  " Value: %s\n",
41  exif_tag_get_name(entry->tag),
43  entry->size,
44  (int)(entry->components),
45  exif_entry_get_value(entry, buf, sizeof(buf)));
46  ++entry_count;
47 }
48 
49 
51 static void data_foreach_func(ExifContent *content, void *callback_data)
52 {
53  static unsigned content_count;
54  entry_count = 0;
55  printf(" Content %u: ifd=%d\n", content_count, exif_content_get_ifd(content));
56  exif_content_foreach_entry(content, content_foreach_func, callback_data);
57  ++content_count;
58 }
59 
60 static void dump_makernote(ExifData *d) {
62  if (mn) {
63  char buf[2000];
64  int i;
65  int num = exif_mnote_data_count(mn);
66  printf(" MakerNote\n");
67  for (i=0; i < num; ++i) {
68  if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
69  const char *name = exif_mnote_data_get_name(mn, i);
70  unsigned int id = exif_mnote_data_get_id(mn, i);
71  if (!name)
72  name = "(unknown)";
73  printf(" Entry %u: %u, %s\n"
74  " Size: %u\n"
75  " Value: %s\n", i, id, name, (unsigned)strlen(buf), buf);
76  }
77  }
78  }
79 }
80 
82 static void test_parse(const char *filename, void *callback_data, int swap)
83 {
84  ExifData *d;
85 
86  /* Skip over path to display only the file name */
87  const char *fn = strrchr(filename, '/');
88  if (fn)
89  ++fn;
90  else
91  fn = filename;
92  printf("File %s\n", fn);
93 
94  d = exif_data_new_from_file(filename);
95  if (!d) {
96  fprintf (stderr, "Could not load data from '%s'!\n", filename);
97  return;
98  }
99  printf("Byte order: %s\n",
101 
102  if (swap) {
104  if (exif_data_get_byte_order(d) == order) {
105  order = EXIF_BYTE_ORDER_MOTOROLA;
106  }
107  /* This switches the byte order of the entire EXIF data structure,
108  * including the MakerNote */
109  exif_data_set_byte_order(d, order);
110  printf("New byte order: %s\n",
112  }
113 
114  exif_data_foreach_content(d, data_foreach_func, callback_data);
115 
116  dump_makernote(d);
117 
118  exif_data_unref(d);
119 }
120 
121 
123 typedef void (*test_parse_func) (const char *filename, void *callback_data, int swap);
124 
125 
127 static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
128 {
129  const char *start = string;
130  const char *p = start;
131  for (;;) {
132  if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == '\0' ) {
133  size_t len = p-start;
134  if (len > 0) {
135  /* emulate strndup */
136  char *str = malloc(1+len);
137  if (str) {
138  memcpy(str, start, len);
139  str[len] = '\0';
140  func(str, callback_data, 0);
141  free(str);
142  start = p+1;
143  }
144  } else {
145  start = p+1;
146  }
147  }
148  if (*p == '\0') {
149  break;
150  }
151  p++;
152  }
153 }
154 
155 
157 int main(const int argc, const char *argv[])
158 {
159  int i;
160  void *callback_data = NULL;
161  int swap = 0;
162  int first = 1;
163 
164  if (argc > 1 && !strcmp(argv[1], "--swap-byte-order")) {
165  swap = 1;
166  ++first;
167  }
168 
169  if (argc > first) {
170  for (i=first; i<argc; i++) {
171  test_parse(argv[i], callback_data, swap);
172  }
173  } else {
174  /* If no command-line argument is found, get the file names from
175  the environment. */
176  const char *envar = getenv("TEST_IMAGES");
177  if (envar) {
178  split_ws_string(envar, test_parse, callback_data);
179  }
180  }
181 
182  return 0;
183 }
const char * exif_byte_order_get_name(ExifByteOrder order)
Return a short, localized, textual name for the given byte order.
ExifByteOrder
Which byte order to use.
@ EXIF_BYTE_ORDER_INTEL
Little-endian byte order.
@ EXIF_BYTE_ORDER_MOTOROLA
Big-endian byte order.
ExifIfd exif_content_get_ifd(ExifContent *c)
Return the IFD number in which the given ExifContent is found.
Definition: exif-content.c:240
void exif_content_foreach_entry(ExifContent *content, ExifContentForeachEntryFunc func, void *data)
Executes function on each EXIF tag in this IFD in turn.
Definition: exif-content.c:216
void exif_data_set_byte_order(ExifData *data, ExifByteOrder order)
Set the byte order to use for this EXIF data.
Definition: exif-data.c:1217
void exif_data_unref(ExifData *data)
Definition: exif-data.c:1098
ExifByteOrder exif_data_get_byte_order(ExifData *data)
Return the byte order in use by this EXIF structure.
Definition: exif-data.c:1173
ExifMnoteData * exif_data_get_mnote_data(ExifData *d)
Return the MakerNote data out of the EXIF data.
Definition: exif-data.c:90
ExifData * exif_data_new_from_file(const char *path)
Allocate a new ExifData and load EXIF data from a JPEG file.
Definition: exif-data.c:1075
void exif_data_foreach_content(ExifData *data, ExifDataForeachContentFunc func, void *user_data)
Execute a function on each IFD in turn.
Definition: exif-data.c:1182
Defines the ExifData type and the associated functions.
const char * exif_entry_get_value(ExifEntry *e, char *val, unsigned int maxlen)
Return a localized textual representation of the value of the EXIF entry.
Definition: exif-entry.c:846
const char * exif_format_get_name(ExifFormat format)
Return a textual representation of the given EXIF data type.
Definition: exif-format.c:53
char * exif_mnote_data_get_value(ExifMnoteData *d, unsigned int n, char *val, unsigned int maxlen)
Return a textual representation of the value of the MakerNote entry.
const char * exif_mnote_data_get_name(ExifMnoteData *d, unsigned int n)
Returns textual name of the given MakerNote tag.
unsigned int exif_mnote_data_get_id(ExifMnoteData *d, unsigned int n)
Return the MakerNote tag number for the tag at the specified index within the MakerNote.
unsigned int exif_mnote_data_count(ExifMnoteData *d)
Return the number of tags in the MakerNote.
System specific definitions, not for installation!
#define UNUSED(param)
Definition: exif-system.h:29
const char * exif_tag_get_name(ExifTag tag)
Definition: exif-tag.c:1147
const char * name
const char * string
Represents the entire EXIF data found in an image.
Definition: exif-data.h:47
Data found in one EXIF tag.
Definition: exif-entry.h:43
unsigned long components
Number of elements in the array, if this is an array entry.
Definition: exif-entry.h:52
ExifFormat format
Type of data in this entry.
Definition: exif-entry.h:48
ExifTag tag
EXIF tag for this entry.
Definition: exif-entry.h:45
unsigned int size
Number of bytes in the buffer at data.
Definition: exif-entry.h:61
void(* test_parse_func)(const char *filename, void *callback_data, int swap)
Definition: test-parse.c:123
static unsigned entry_count
Definition: test-parse.c:30
static void data_foreach_func(ExifContent *content, void *callback_data)
Definition: test-parse.c:51
static void test_parse(const char *filename, void *callback_data, int swap)
Definition: test-parse.c:82
static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
Definition: test-parse.c:33
static void dump_makernote(ExifData *d)
Definition: test-parse.c:60
static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
Definition: test-parse.c:127
int main(const int argc, const char *argv[])
Definition: test-parse.c:157

libexif Generated by doxygen