• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** \file test-extract.c
2  * \brief Extract EXIF data from a file and write it to another file.
3  *
4  * Copyright (C) 2019 Dan Fandrich <dan@coneharvesters.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA  02110-1301  USA.
20  *
21  * SPDX-License-Identifier: LGPL-2.0-or-later
22  */
23 
24 #include "libexif/exif-data.h"
25 
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 
31 static const unsigned char header[4] = {'\xff', '\xd8', '\xff', '\xe1'};
32 
main(const int argc,const char * argv[])33 int 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 
57   d = exif_data_new_from_file(fn);
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);
64   exif_data_unref(d);
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    */
86   exif_set_short(lenbuf, EXIF_BYTE_ORDER_MOTOROLA, len);
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 }
104