|
Packit Service |
271c72 |
/** \file test-extract.c
|
|
Packit Service |
271c72 |
* \brief Extract EXIF data from a file and write it to another file.
|
|
Packit Service |
271c72 |
*
|
|
Packit Service |
271c72 |
* Copyright (C) 2019 Dan Fandrich <dan@coneharvesters.com>
|
|
Packit Service |
271c72 |
*
|
|
Packit Service |
271c72 |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
271c72 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
271c72 |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
271c72 |
* version 2 of the License, or (at your option) any later version.
|
|
Packit Service |
271c72 |
*
|
|
Packit Service |
271c72 |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
271c72 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
271c72 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
271c72 |
* Lesser General Public License for more details.
|
|
Packit Service |
271c72 |
*
|
|
Packit Service |
271c72 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
271c72 |
* License along with this library; if not, write to the
|
|
Packit Service |
271c72 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Packit Service |
271c72 |
* Boston, MA 02110-1301 USA.
|
|
Packit Service |
271c72 |
*
|
|
Packit Service |
271c72 |
*/
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
#include "libexif/exif-data.h"
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
#include <string.h>
|
|
Packit Service |
271c72 |
#include <stdio.h>
|
|
Packit Service |
271c72 |
#include <stdlib.h>
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
static const unsigned char header[4] = {'\xff', '\xd8', '\xff', '\xe1'};
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
int main(const int argc, const char *argv[])
|
|
Packit Service |
271c72 |
{
|
|
Packit Service |
271c72 |
int first = 1;
|
|
Packit Service |
271c72 |
const char *fn = "input.jpg";
|
|
Packit Service |
271c72 |
const char *outfn = "output.exif";
|
|
Packit Service |
271c72 |
ExifData *d;
|
|
Packit Service |
271c72 |
unsigned char *buf;
|
|
Packit Service |
271c72 |
unsigned int len;
|
|
Packit Service |
271c72 |
FILE *f;
|
|
Packit Service |
271c72 |
unsigned char lenbuf[2];
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
if (argc > 1 && !strcmp(argv[1], "-o")) {
|
|
Packit Service |
271c72 |
outfn = argv[2];
|
|
Packit Service |
271c72 |
first += 2;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
if (argc > first) {
|
|
Packit Service |
271c72 |
fn = argv[first];
|
|
Packit Service |
271c72 |
++first;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
if (argc > first) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Too many arguments\n");
|
|
Packit Service |
271c72 |
return 1;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
d = exif_data_new_from_file(fn);
|
|
Packit Service |
271c72 |
if (!d) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Could not load data from '%s'!\n", fn);
|
|
Packit Service |
271c72 |
return 1;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
exif_data_save_data(d, &buf, &len;;
|
|
Packit Service |
271c72 |
exif_data_unref(d);
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
if (!buf) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Could not extract EXIF data!\n");
|
|
Packit Service |
271c72 |
return 2;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
f = fopen(outfn, "wb");
|
|
Packit Service |
271c72 |
if (!f) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Could not open '%s' for writing!\n", outfn);
|
|
Packit Service |
271c72 |
return 1;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
/* Write EXIF with headers and length. */
|
|
Packit Service |
271c72 |
if (fwrite(header, 1, sizeof(header), f) != sizeof(header)) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Could not write to '%s'!\n", outfn);
|
|
Packit Service |
271c72 |
return 3;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
/*
|
|
Packit Service |
271c72 |
* FIXME: The buffer from exif_data_save_data() seems to contain extra 0xffd8
|
|
Packit Service |
271c72 |
* 0xffd9 JPEG markers at the end that I wasn't expecting, making the length
|
|
Packit Service |
271c72 |
* seem too long. Should those markers really be included?
|
|
Packit Service |
271c72 |
*/
|
|
Packit Service |
271c72 |
exif_set_short(lenbuf, EXIF_BYTE_ORDER_MOTOROLA, len);
|
|
Packit Service |
271c72 |
if (fwrite(lenbuf, 1, 2, f) != 2) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Could not write to '%s'!\n", outfn);
|
|
Packit Service |
271c72 |
return 3;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
if (fwrite(buf, 1, len, f) != len) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Could not write to '%s'!\n", outfn);
|
|
Packit Service |
271c72 |
return 3;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
if (fclose(f) != 0) {
|
|
Packit Service |
271c72 |
fprintf (stderr, "Could not close '%s'!\n", outfn);
|
|
Packit Service |
271c72 |
return 3;
|
|
Packit Service |
271c72 |
}
|
|
Packit Service |
271c72 |
free(buf);
|
|
Packit Service |
271c72 |
fprintf (stderr, "Wrote EXIF data to '%s'\n", outfn);
|
|
Packit Service |
271c72 |
|
|
Packit Service |
271c72 |
return 0;
|
|
Packit Service |
271c72 |
}
|