EXIF library (libexif) Internals 0.6.26
test-parse-from-data.c
Go to the documentation of this file.
1
24#include "libexif/exif-data.h"
25#include "libexif/exif-system.h"
26
27#include <string.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <sys/stat.h>
33
34
35static unsigned entry_count;
36
38static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
39{
40 char buf[2000];
41 exif_entry_get_value(entry, buf, sizeof(buf));
42 printf(" Entry %u: %s (%s)\n"
43 " Size, Comps: %d, %d\n"
44 " Value: %s\n",
46 exif_tag_get_name(entry->tag),
48 entry->size,
49 (int)(entry->components),
50 exif_entry_get_value(entry, buf, sizeof(buf)));
52}
53
54
56static void data_foreach_func(ExifContent *content, void *callback_data)
57{
58 static unsigned content_count;
59 entry_count = 0;
60 printf(" Content %u: ifd=%d\n", content_count, exif_content_get_ifd(content));
61 exif_content_foreach_entry(content, content_foreach_func, callback_data);
62 ++content_count;
63}
64
65static void dump_makernote(ExifData *d) {
67 if (mn) {
68 char buf[2000];
69 int i;
70 int num = exif_mnote_data_count(mn);
71 printf(" MakerNote\n");
72 for (i=0; i < num; ++i) {
73 if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
74 const char *name = exif_mnote_data_get_name(mn, i);
75 unsigned int id = exif_mnote_data_get_id(mn, i);
76 if (!name)
77 name = "(unknown)";
78 printf(" Entry %u: %u, %s\n"
79 " Size: %u\n"
80 " Value: %s\n", i, id, name, (unsigned)strlen(buf), buf);
81 }
82 }
83 }
84}
85
87static void test_parse(const char *filename, void *callback_data, int swap)
88{
89 ExifData *d;
90 int fd;
91 unsigned char *data;
92 struct stat stbuf;
93
94 /* Skip over path to display only the file name */
95 const char *fn = strrchr(filename, '/');
96 if (fn)
97 ++fn;
98 else
99 fn = filename;
100 printf("File %s\n", fn);
101
102 d = exif_data_new_from_file(filename);
104
105 fd = open(filename,O_RDONLY);
106 if (fd == -1) {
107 perror(filename);
108 return;
109 }
110 if (-1 == fstat(fd, &stbuf)) {
111 perror(filename);
112 return;
113 }
114 data = malloc(stbuf.st_size);
115 if (!data) {
116 fprintf (stderr, "Failed to allocate %ld bytes for reading %s\n", (long)stbuf.st_size, filename);
117 return;
118 }
119 if (-1 == read(fd, data, stbuf.st_size)) {
120 perror ("read");
121 free(data);
122 close(fd);
123 return;
124 }
125 close(fd);
126
127 d = exif_data_new_from_data(data, stbuf.st_size);
128 free(data);
129 if (!d) {
130 fprintf (stderr, "Could not load data from '%s'!\n", filename);
131 return;
132 }
133 printf("Byte order: %s\n",
135
136 if (swap) {
138 if (exif_data_get_byte_order(d) == order) {
140 }
141 /* This switches the byte order of the entire EXIF data structure,
142 * including the MakerNote */
143 exif_data_set_byte_order(d, order);
144 printf("New byte order: %s\n",
146 }
147
149
151
153}
154
155
157typedef void (*test_parse_func) (const char *filename, void *callback_data, int swap);
158
159
161static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
162{
163 const char *start = string;
164 const char *p = start;
165 for (;;) {
166 if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == '\0' ) {
167 size_t len = p-start;
168 if (len > 0) {
169 /* emulate strndup */
170 char *str = malloc(1+len);
171 if (str) {
172 memcpy(str, start, len);
173 str[len] = '\0';
174 func(str, callback_data, 0);
175 free(str);
176 start = p+1;
177 }
178 } else {
179 start = p+1;
180 }
181 }
182 if (*p == '\0') {
183 break;
184 }
185 p++;
186 }
187}
188
189
191int main(const int argc, const char *argv[])
192{
193 int i;
194 void *callback_data = NULL;
195 int swap = 0;
196 int first = 1;
197
198 if (argc > 1 && !strcmp(argv[1], "--swap-byte-order")) {
199 swap = 1;
200 ++first;
201 }
202
203 if (argc > first) {
204 for (i=first; i<argc; i++) {
205 test_parse(argv[i], callback_data, swap);
206 }
207 } else {
208 /* If no command-line argument is found, get the file names from
209 the environment. */
210 const char *envar = getenv("TEST_IMAGES");
211 if (envar) {
212 split_ws_string(envar, test_parse, callback_data);
213 }
214 }
215
216 return 0;
217}
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.
void exif_content_foreach_entry(ExifContent *content, ExifContentForeachEntryFunc func, void *data)
Executes function on each EXIF tag in this IFD in turn.
void exif_data_set_byte_order(ExifData *data, ExifByteOrder order)
Set the byte order to use for this EXIF data.
Definition exif-data.c:1229
void exif_data_unref(ExifData *data)
Definition exif-data.c:1110
ExifByteOrder exif_data_get_byte_order(ExifData *data)
Return the byte order in use by this EXIF structure.
Definition exif-data.c:1185
ExifMnoteData * exif_data_get_mnote_data(ExifData *d)
Return the MakerNote data out of the EXIF data.
Definition exif-data.c:92
ExifData * exif_data_new_from_data(const unsigned char *data, unsigned int size)
Allocate a new ExifData and load EXIF data from a memory buffer.
Definition exif-data.c:156
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:1087
void exif_data_foreach_content(ExifData *data, ExifDataForeachContentFunc func, void *user_data)
Execute a function on each IFD in turn.
Definition exif-data.c:1194
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:854
const char * exif_format_get_name(ExifFormat format)
Return a textual representation of the given EXIF data type.
Definition exif-format.c:55
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.
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.
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_count(ExifMnoteData *d)
Return the number of tags in the MakerNote.
System specific definitions, not for installation!
#define UNUSED(param)
Definition exif-system.h:31
const char * exif_tag_get_name(ExifTag tag)
Definition exif-tag.c:1151
const char * name
const char * string
int main(void)
Represents the entire EXIF data found in an image.
Definition exif-data.h:48
Data found in one EXIF tag.
Definition exif-entry.h:45
unsigned long components
Number of elements in the array, if this is an array entry.
Definition exif-entry.h:54
ExifFormat format
Type of data in this entry.
Definition exif-entry.h:50
ExifTag tag
EXIF tag for this entry.
Definition exif-entry.h:47
unsigned int size
Number of bytes in the buffer at data.
Definition exif-entry.h:63
void(* test_parse_func)(const char *filename, void *callback_data, int swap)
static unsigned entry_count
static void data_foreach_func(ExifContent *content, void *callback_data)
static void test_parse(const char *filename, void *callback_data, int swap)
static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
static void dump_makernote(ExifData *d)
static void split_ws_string(const char *string, test_parse_func func, void *callback_data)

libexif Generated by doxygen