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

libexif Generated by doxygen