summaryrefslogtreecommitdiff
path: root/src/ImageMetaData.cxx
blob: b54da740967cf45437e97efd9e023da8c8958025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// Copyright 2006-2009 Johannes Hofmann <Johannes.Hofmann@gmx.de>
//
// This software may be used and distributed according to the terms
// of the GNU General Public License, incorporated herein by reference.

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>
#include <assert.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <exiv2/image.hpp>
#include <exiv2/exif.hpp>

#include "../config.h"
#include "ImageMetaData.H"

#if !defined(O_BINARY)
#define O_BINARY 0
#endif

ImageMetaData::ImageMetaData() {
	_manufacturer = NULL;
    _model = NULL;
	clear();
}

ImageMetaData::~ImageMetaData() {
	if (_manufacturer)
		free(_manufacturer);
	if (_model)
		free(_model);
}

void
ImageMetaData::clear() {
	if (_manufacturer)
		free(_manufacturer);
	_manufacturer = NULL;
	if (_model)
		free(_model);
    _model = NULL;
	_longitude = NAN;
	_latitude = NAN;
	_height = NAN;
	_direction = NAN;
	_nick = NAN;
	_tilt = NAN;
	_k0 = NAN;
	_k1 = NAN;
	_x0 = NAN;
	_focal_length = NAN;
	_focal_length_35mm = NAN;
	_scale = NAN;
	_projection_type = 0;
}

int
ImageMetaData::load_image(char *name) {
	clear();
	load_image_jpgcom(name);
	load_image_exif(name); // fill missing values from exif data
	return 0;
}

int
ImageMetaData::save_image(char *in_img, char *out_img) {
	return save_image_jpgcom(in_img, out_img);
}

static void
exifSetValue(double *destVal, Exiv2::ExifData *exifData, const char *name) {
	Exiv2::ExifKey key(name);
	Exiv2::ExifData::iterator pos = exifData->findKey(key);
	pos = exifData->findKey(key);
	if (pos != exifData->end() && pos->toFloat() >= 0)
		*destVal = pos->toFloat();
}

static void
exifSetCoordinate(double *destVal, Exiv2::ExifData *exifData, const char *name) {
	Exiv2::ExifKey key(name);
	Exiv2::ExifData::iterator pos = exifData->findKey(key);

	if (pos != exifData->end()) {
		if (pos->toFloat() >= 0)
			*destVal = pos->toFloat();
		if (pos->toFloat(1) >= 0)
			*destVal += pos->toFloat(1) / 60;
		if (pos->toFloat(2) >= 0)
			*destVal += pos->toFloat(2) / 3600;
	}
}

int
ImageMetaData::load_image_exif(char *name) {
	Exiv2::Image::AutoPtr image;

	try {
		image = Exiv2::ImageFactory::open(name);
		image->readMetadata();
	} catch (Exiv2::Error error) {
		fprintf(stderr, "Error reading metadata\n");
		return 1;
	}

    Exiv2::ExifData &exifData = image->exifData();
    if (exifData.empty()) {
		fprintf(stderr, "%s: No Exif data found in the file", name);
		return 1;
	}

    Exiv2::ExifData::iterator pos = exifData.end();

    if (!_manufacturer ) {
		Exiv2::ExifKey key("Exif.Image.Make"); // tag auch wirklich vorhanden?
		pos = exifData.findKey(key);
		if (pos != exifData.end() && pos->size())
			_manufacturer = strdup(pos->toString().c_str());
	}

    if (!_model) {
		Exiv2::ExifKey key("Exif.Image.Model");
		pos = exifData.findKey(key);
		if (pos != exifData.end() && pos->size() )
			_model = strdup(pos->toString().c_str());
	}

    if (isnan(_focal_length))
		exifSetValue(&_focal_length, &exifData, "Exif.Photo.FocalLength");

    if (isnan(_focal_length_35mm))
		exifSetValue(&_focal_length_35mm, &exifData, "Exif.Photo.FocalLengthIn35mmFilm");

    if (isnan(_longitude))
		exifSetCoordinate(&_longitude, &exifData, "Exif.GPSInfo.GPSLongitude");

    if (isnan(_latitude))
		exifSetCoordinate(&_latitude, &exifData, "Exif.GPSInfo.GPSLatitude");

    if (isnan(_height))
		exifSetValue(&_height, &exifData, "Exif.GPSInfo.GPSAltitude");

    return 0;
}


#define GIPFEL_FORMAT "gipfel: longitude " FMT_DOUBLE ", latitude " FMT_DOUBLE ", height " FMT_DOUBLE ", direction " FMT_DOUBLE ", nick " FMT_DOUBLE ", tilt " FMT_DOUBLE ", focal_length_35mm " FMT_DOUBLE ", projection type %d, k0 " FMT_DOUBLE ", k1 " FMT_DOUBLE ", x0 " FMT_DOUBLE ""

#define FMT_DOUBLE "%lf"
static const char *gipfel_format_scan = GIPFEL_FORMAT;

#undef FMT_DOUBLE
#define FMT_DOUBLE "%f"
static const char *gipfel_format_prnt = GIPFEL_FORMAT;

#undef FMT_DOUBLE
#undef GIPFEL_FORMAT

int
ImageMetaData::load_image_jpgcom(char *name) {
    double lo, la, he, dir, ni, ti, fr, k0, k1, x0 = 0.0;
    int pt = 0;
    int n, ret = 1;
	Exiv2::Image::AutoPtr image;

	try {
		image = Exiv2::ImageFactory::open(name);
		image->readMetadata();
	} catch (Exiv2::Error error) {
		fprintf(stderr, "Error reading metadata\n");
		return 1;
	}

    const char *com = image->comment().c_str();

    if ((n = sscanf(com, gipfel_format_scan,
            &lo, &la, &he, &dir, &ni, &ti, &fr, &pt, &k0, &k1, &x0)) >= 8) {
        _longitude = lo;
        _latitude  = la;
        _height    = he;
        _direction = dir;
        _nick      = ni;
        _tilt      = ti;
        _focal_length_35mm = fr;
        _projection_type = pt;

        if (n >= 10) {
            _k0 = k0;
            _k1 = k1;
            _x0 = x0;
		}

		ret = 0;
	}

    return ret;
}

int
ImageMetaData::save_image_jpgcom(char *in_img, char *out_img) {
    char buf[1024], *tmpname;
    int n, in_fd, tmp_fd, err = 0;

    char* dirbuf = strdup(out_img);
#if HAVE_MKSTEMP
	char tmpbuf[MAXPATHLEN];
	snprintf(tmpbuf, sizeof(tmpbuf), "%s/.gipfelXXXXXX", dirname(dirbuf));
	tmp_fd = mkstemp(tmpbuf);
	tmpname = tmpbuf;
#else
	tmpname = tempnam(dirname(dirbuf), ".gipfel");
	tmp_fd = open(tmpname, O_WRONLY|O_TRUNC|O_CREAT|O_BINARY, S_IRUSR|S_IWUSR);
#endif
	free(dirbuf);

	if (tmp_fd == -1) {
		perror("mkstemp");
		return 1;
	}

	in_fd = open(in_img, O_RDONLY|O_BINARY);
	if (in_fd == -1) {
		perror("open");
		unlink(tmpname);
		close(tmp_fd);
		return 1;
	}

	while ((n = read(in_fd, buf, sizeof(buf))) > 0) {
		if (n < 0 || write(tmp_fd, buf, n) != n) {
			perror("write");
			err++;
			break;
		}
	}

	close(in_fd);

    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(tmpname);
    if (!image.get())
		err++;

    image->readMetadata();
    image->clearComment();

    snprintf(buf, sizeof(buf), gipfel_format_prnt,
        _longitude,
        _latitude,
        _height,
        _direction,
        _nick,
        _tilt,
        _focal_length_35mm,
        _projection_type,
        _k0, _k1, _x0);

    image->setComment(buf);

	try {
		image->writeMetadata();
	} catch (Exiv2::Error error) {
		fprintf(stderr, "Error writing metadata\n");
		err++;
	}

#ifdef HAVE_FSYNC
	fsync(tmp_fd); // make sure data is on disk before replacing orig file
#endif

	close(tmp_fd);

	if (err == 0) { // only overwrite existing image if everything was ok
#ifdef WIN32
		// Workaround as Windows does not seem to replace files on rename()
		struct stat stFileInfo;
		if (stat(out_img, &stFileInfo) == 0)
			unlink(out_img);
#endif

		if (rename(tmpname, out_img) != 0) {
			perror("rename");
			err++;
			unlink(tmpname);
		}
	}

    return (err != 0);
}

void
ImageMetaData::distortion_params(double *k0, double *k1, double *x0) {
	*k0 = _k0;	
	*k1 = _k1;	
	*x0 = _x0;	
}

void
ImageMetaData::distortion_params(double k0, double k1, double x0) {
	_k0 = k0;	
	_k1 = k1;	
	_x0 = x0;	
}