summaryrefslogtreecommitdiff
path: root/src/JpgcomImageMetaData.cxx
blob: 407b6afda145a0f7512b5e743e009c49879fb770 (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
// 
// Copyright 2006 by Johannes Hofmann
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>

#include "util.h"

#include "JpgcomImageMetaData.H"

#define GIPFEL_FORMAT_1 "gipfel: longitude %lf, latitude %lf, height %lf, direction %lf, nick %lf, tilt %lf, scale %lf, projection type %d"
#define GIPFEL_FORMAT_2 "gipfel: longitude %lf, latitude %lf, height %lf, direction %lf, nick %lf, tilt %lf, focallength_sensor_ratio %lf, projection type %d"

int
JpgcomImageMetaData::load_image(char *name) {
	char * args[32];
	FILE *p;
	pid_t pid;
	int status;
	char buf[1024];
	double lo, la, he, dir, ni, ti, fr;
	int pt;
	int ret = 1;

	args[0] = "rdjpgcom";
	args[1] = name;
	args[2] = NULL;
  
	p = pexecvp(args[0], args, &pid, "r");

	if (p) {
		while (fgets(buf, sizeof(buf), p) != NULL) {
			if (sscanf(buf, GIPFEL_FORMAT_2,
					&lo, &la, &he, &dir, &ni, &ti, &fr, &pt) >= 7) {

				longitude = lo;
				latitude  = la;
				height    = he;
				direction = dir;
				nick      = ni;
				tilt      = ti;
				focallength_sensor_ratio = fr;
				projection_type = pt;	

				ret = 0;

				break;
			} else if (sscanf(buf, GIPFEL_FORMAT_1,
					&lo, &la, &he, &dir, &ni, &ti, &fr, &pt) >= 7) {

				longitude = lo;
				latitude  = la;
				height    = he;
				direction = dir;
				nick      = ni;
				tilt      = ti;
				focallength_sensor_ratio = fr;
				projection_type = pt;	

				ret = 2; // special return value for compatibility with
				         // old format

				break;
			}
		}

		fclose(p);
		waitpid(pid, &status, 0); 
		if (WEXITSTATUS(status) == 127 || WEXITSTATUS(status) == 126) {
			fprintf(stderr, "%s not found\n", args[0]);
		}
	}

	return ret;
}

int
JpgcomImageMetaData::save_image(char *in_img, char *out_img) {
	char * args[32];
	FILE *p, *out;
	pid_t pid;
	char buf[1024];
	int status;
	size_t n;
	struct stat in_stat, out_stat;

	if (stat(in_img, &in_stat) != 0) {
		perror("stat");
		return 1;
	}

	if (stat(out_img, &out_stat) == 0) {
		if (in_stat.st_ino == out_stat.st_ino) {
			fprintf(stderr, "Input image %s and output image %s are the same file\n",
				in_img, out_img);
			return 1;
		}
	}

	out = fopen(out_img, "w");
	if (out == NULL) {
		perror("fopen");
		return 1;
	}

	snprintf(buf, sizeof(buf), GIPFEL_FORMAT_2,
		longitude,
		latitude,
		height,
		direction,
		nick,
		tilt,
		focallength_sensor_ratio,
		projection_type);

	// try to save gipfel data in JPEG comment section
	args[0] = "wrjpgcom";
	args[1] = "-replace";
	args[2] = "-comment";
	args[3] = buf;
	args[4] = in_img;
	args[5] = NULL;

	p = pexecvp(args[0], args, &pid, "r");

	if (p) {
		while ((n = fread(buf, 1, sizeof(buf), p)) != 0) {
			if (fwrite(buf, 1, n, out) != n) {
				perror("fwrite");
				fclose(out);
				fclose(p);
				waitpid(pid, &status, 0);
			}
		}
		fclose(p);
		waitpid(pid, &status, 0);
		if (WEXITSTATUS(status) == 127 || WEXITSTATUS(status) == 126) {
			fprintf(stderr, "%s not found\n", args[0]);
		}
	}

	fclose(out);
	return 0;
}