EXIF library (libexif) Internals 0.6.26
test-extract.c
Go to the documentation of this file.
1
24#include "libexif/exif-data.h"
25
26#include <string.h>
27#include <stdio.h>
28#include <stdlib.h>
29
30
31static const unsigned char header[4] = {'\xff', '\xd8', '\xff', '\xe1'};
32
33int main(const int argc, const char *argv[])
34{
35 int first = 1;
36 const char *fn = "input.jpg";
37 const char *outfn = "output.exif";
38 ExifData *d;
39 unsigned char *buf;
40 unsigned int len;
41 FILE *f;
42 unsigned char lenbuf[2];
43
44 if (argc > 1 && !strcmp(argv[1], "-o")) {
45 outfn = argv[2];
46 first += 2;
47 }
48 if (argc > first) {
49 fn = argv[first];
50 ++first;
51 }
52 if (argc > first) {
53 fprintf (stderr, "Too many arguments\n");
54 return 1;
55 }
56
58 if (!d) {
59 fprintf (stderr, "Could not load data from '%s'!\n", fn);
60 return 1;
61 }
62
63 exif_data_save_data(d, &buf, &len);
65
66 if (!buf) {
67 fprintf (stderr, "Could not extract EXIF data!\n");
68 return 2;
69 }
70
71 f = fopen(outfn, "wb");
72 if (!f) {
73 fprintf (stderr, "Could not open '%s' for writing!\n", outfn);
74 return 1;
75 }
76 /* Write EXIF with headers and length. */
77 if (fwrite(header, 1, sizeof(header), f) != sizeof(header)) {
78 fprintf (stderr, "Could not write to '%s'!\n", outfn);
79 return 3;
80 }
81 /*
82 * FIXME: The buffer from exif_data_save_data() seems to contain extra 0xffd8
83 * 0xffd9 JPEG markers at the end that I wasn't expecting, making the length
84 * seem too long. Should those markers really be included?
85 */
87 if (fwrite(lenbuf, 1, 2, f) != 2) {
88 fprintf (stderr, "Could not write to '%s'!\n", outfn);
89 return 3;
90 }
91 if (fwrite(buf, 1, len, f) != len) {
92 fprintf (stderr, "Could not write to '%s'!\n", outfn);
93 return 3;
94 }
95 if (fclose(f) != 0) {
96 fprintf (stderr, "Could not close '%s'!\n", outfn);
97 return 3;
98 }
99 free(buf);
100 fprintf (stderr, "Wrote EXIF data to '%s'\n", outfn);
101
102 return 0;
103}
@ EXIF_BYTE_ORDER_MOTOROLA
Big-endian byte order.
void exif_data_unref(ExifData *data)
Definition exif-data.c:1110
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_save_data(ExifData *data, unsigned char **d, unsigned int *ds)
Store raw EXIF data representing the ExifData structure into a memory buffer.
Definition exif-data.c:1042
Defines the ExifData type and the associated functions.
void exif_set_short(unsigned char *b, ExifByteOrder order, ExifShort value)
Store an ExifShort value into memory in EXIF format.
Definition exif-utils.c:126
int main(void)
Represents the entire EXIF data found in an image.
Definition exif-data.h:48
static const unsigned char header[4]

libexif Generated by doxygen