1 /** \file test-extract.c
2 * \brief Extract EXIF data from a file and write it to another file.
4 * Copyright (C) 2019 Dan Fandrich <dan@coneharvesters.com>
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.
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.
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.
23 #include "libexif/exif-data.h"
30 static const unsigned char header[4] = {'\xff', '\xd8', '\xff', '\xe1'};
32 int main(const int argc, const char *argv[])
35 const char *fn = "input.jpg";
36 const char *outfn = "output.exif";
41 unsigned char lenbuf[2];
43 if (argc > 1 && !strcmp(argv[1], "-o")) {
52 fprintf (stderr, "Too many arguments\n");
56 d = exif_data_new_from_file(fn);
58 fprintf (stderr, "Could not load data from '%s'!\n", fn);
62 exif_data_save_data(d, &buf, &len);
66 fprintf (stderr, "Could not extract EXIF data!\n");
70 f = fopen(outfn, "wb");
72 fprintf (stderr, "Could not open '%s' for writing!\n", outfn);
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);
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?
85 exif_set_short(lenbuf, EXIF_BYTE_ORDER_MOTOROLA, len);
86 if (fwrite(lenbuf, 1, 2, f) != 2) {
87 fprintf (stderr, "Could not write to '%s'!\n", outfn);
90 if (fwrite(buf, 1, len, f) != len) {
91 fprintf (stderr, "Could not write to '%s'!\n", outfn);
95 fprintf (stderr, "Could not close '%s'!\n", outfn);
99 fprintf (stderr, "Wrote EXIF data to '%s'\n", outfn);