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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
//! gipfel-cli: headless command line tools, mirroring the C binary's
//! `-p/--export-position`, `-e/--export-hills` and stitch modes.
use gipfel_core::hill::FLAG_EXPORT;
use gipfel_core::metadata::{apply_image_metadata, image_dimensions, ImageMetaData};
use gipfel_core::panorama::Panorama;
use gipfel_core::stitch::{self, ScanMode};
fn usage() {
eprintln!(
"usage: gipfel-cli <command> [options]\n\
\n\
commands:\n\
\x20 export-position <img> print position CSV line\n\
\x20 export-hills <img> [-d <data>] [-e <hills>] export hill positions\n\
\x20 info <img> show parsed metadata\n\
\x20 stitch [-t out.tif [-4]] [-j out.jpg] [-w N]\n\
\x20 [-h N] [-r from,to] [-b] img... cylindrical panorama\n\
\x20 profiles list distortion profiles\n\
\x20 profile-save <name> <k0> <k1> <x0> save a profile\n"
);
}
fn export_position(img: &str) -> i32 {
// C reads metadata directly (no image decode); missing values print as nan
let mut md = ImageMetaData::new();
md.load_image(img);
// C: printf(",%s,,%f,%f,%d\n", img_file, latitude, longitude, (int)rint(height))
println!(
",{},,{},{},{}",
img,
fmt_f(md.latitude),
fmt_f(md.longitude),
md.height.round_ties_even() as i64
);
0
}
fn fmt_f(v: f64) -> String {
if v.is_nan() {
"nan".to_string()
} else {
format!("{:.6}", v)
}
}
fn export_hills(img: &str, data: &str, export_file: Option<&str>) -> i32 {
let Some((w, h)) = image_dimensions(img) else {
eprintln!("could not read image {}", img);
return 1;
};
let mut md = ImageMetaData::new();
md.load_image(img);
let mut pan = Panorama::new();
let have_info = apply_image_metadata(&mut pan, &md, w, h);
if let Err(e) = pan.load_data(data) {
eprintln!("Could not load datafile {}: {}", data, e);
return 1;
}
pan.set_height_dist_ratio(0.07); // C default "visibility"
if !have_info {
eprintln!("No gipfel info available for {}.", img);
return 0;
}
// -e: restrict export to the hills from the given file
if let Some(file) = export_file {
match gipfel_core::hill::load(file) {
Ok(mut extra) => {
for m in extra.iter_mut() {
m.flags |= FLAG_EXPORT;
}
pan.add_hills(&extra);
}
Err(_) => return 1,
}
}
println!("#\n# name\theight\tx\ty\tdistance\tflags\n#");
for row in pan.export_hill_rows(w as i64, h as i64, export_file.is_some()) {
println!("{}", row);
}
if export_file.is_some() {
for m in pan.hills.iter_mut() {
m.flags &= !FLAG_EXPORT;
}
}
0
}
/// Port of the C stitch mode: resample the given images into one
/// cylindrical panorama. Flags follow the C binary: -w/-h output size,
/// -r from,to azimuth range in degrees, -b bicubic, -4 16-bit TIFF,
/// -j/-t output file (tiff wins if both given, as in C).
fn stitch(imgs: &[String], opts: &StitchOpts) -> i32 {
if imgs.is_empty() || (opts.tiff.is_none() && opts.jpeg.is_none()) {
usage();
return 2;
}
let mut sources = Vec::new();
for img in imgs {
match stitch::StitchSource::load(img) {
Ok(s) => sources.push(s),
Err(e) => {
eprintln!("{e}");
return 1;
}
}
}
let mode = if opts.bicubic { ScanMode::Bicubic } else { ScanMode::Nearest };
let out = stitch::resample(&sources, mode, opts.w, opts.h, opts.from, opts.to);
let hit = out.hit.iter().filter(|h| **h).count();
eprintln!(
"stitched {}x{}: {}/{} pixels covered",
out.w, out.h, hit, out.hit.len()
);
let res = if let Some(path) = &opts.tiff {
stitch::write_tiff(path, &out, opts.bits16)
} else {
stitch::write_jpeg(opts.jpeg.as_ref().unwrap(), &out, 90)
};
match res {
Ok(()) => 0,
Err(e) => {
eprintln!("{e}");
1
}
}
}
struct StitchOpts {
w: u32,
h: u32,
from: f64,
to: f64,
bicubic: bool,
bits16: bool,
tiff: Option<String>,
jpeg: Option<String>,
}
/// C defaults: stitch_w=2000, stitch_h=500, stitch_from=0.0, stitch_to=380.0
fn stitch_defaults() -> StitchOpts {
StitchOpts {
w: 2000,
h: 500,
from: 0.0,
to: 380.0,
bicubic: false,
bits16: false,
tiff: None,
jpeg: None,
}
}
fn stitch_cmd(args: &[String]) -> i32 {
let mut o = stitch_defaults();
let mut imgs = Vec::new();
let mut i = 0;
while i < args.len() {
let a = args[i].as_str();
let need = |i: &mut usize| -> Option<String> {
if *i + 1 < args.len() {
*i += 1;
Some(args[*i].clone())
} else {
None
}
};
let ok = match a {
"-w" => match need(&mut i) {
Some(v) => {
o.w = v.parse().unwrap_or(2000);
true
}
None => false,
},
"-h" => match need(&mut i) {
Some(v) => {
o.h = v.parse().unwrap_or(500);
true
}
None => false,
},
"-r" => match need(&mut i) {
Some(v) => {
// C: atof(optarg) [, atof(after comma)] — atof reads the
// numeric prefix before the comma
let (first, second) = v.split_once(',').unwrap_or((v.as_str(), ""));
o.from = first.trim().parse().unwrap_or(0.0);
if !second.trim().is_empty() {
o.to = second.trim().parse().unwrap_or(380.0);
}
true
}
None => false,
},
"-j" => match need(&mut i) {
Some(v) => {
o.jpeg = Some(v);
true
}
None => false,
},
"-t" => match need(&mut i) {
Some(v) => {
o.tiff = Some(v);
true
}
None => false,
},
"-b" => {
o.bicubic = true;
true
}
"-4" => {
o.bits16 = true;
true
}
_ => {
if a.starts_with('-') {
false
} else {
imgs.push(a.to_string());
true
}
}
};
if !ok {
usage();
return 2;
}
i += 1;
}
stitch(&imgs, &o)
}
fn info(img: &str) -> i32 {
let mut md = ImageMetaData::new();
md.load_image(img);
println!("manufacturer: {:?}", md.manufacturer);
println!("model: {:?}", md.model);
println!("longitude: {}", md.longitude);
println!("latitude: {}", md.latitude);
println!("height: {}", md.height);
println!("direction: {}", md.direction);
println!("pitch: {}", md.pitch);
println!("roll: {}", md.roll);
println!("focal_length: {}", md.focal_length);
println!("focal_length_35mm: {}", md.focal_length_35mm);
println!("projection type: {}", md.projection_type);
println!("k0: {}", md.k0);
println!("k1: {}", md.k1);
println!("x0: {}", md.x0);
0
}
fn profiles() -> i32 {
let all = gipfel_core::profile::list();
if all.is_empty() {
eprintln!("no profiles in {}", gipfel_core::profile::prefs_path().display());
}
for (name, p) in all {
println!("{name}\tk0={}\tk1={}\tx0={}", p.k0, p.k1, p.x0);
}
0
}
fn profile_save(name: &str, k0: f64, k1: f64, x0: f64) -> i32 {
let p = gipfel_core::profile::Profile { k0, k1, x0 };
match gipfel_core::profile::save(name, p, true) {
Ok(()) => {
eprintln!("saved profile {name}");
0
}
Err(e) => {
eprintln!("{e}");
1
}
}
}
fn profile_save_cmd(args: &[String]) -> i32 {
if args.len() != 4 {
usage();
return 2;
}
let (Ok(k0), Ok(k1), Ok(x0)) = (
args[1].parse::<f64>(),
args[2].parse::<f64>(),
args[3].parse::<f64>(),
) else {
usage();
return 2;
};
profile_save(&args[0], k0, k1, x0)
}
fn main() {
let args: Vec<String> = std::env::args().collect();
let Some(cmd) = args.get(1) else {
usage();
std::process::exit(2);
};
let code = match cmd.as_str() {
"export-position" => {
if args.len() != 3 {
usage();
2
} else {
export_position(&args[2])
}
}
"export-hills" => {
if args.len() < 3 {
usage();
2
} else {
let img = &args[2];
let mut data = "gipfel.dat".to_string();
let mut export_file: Option<String> = None;
let mut i = 3;
while i < args.len() {
match args[i].as_str() {
"-d" => {
if i + 1 < args.len() {
data = args[i + 1].clone();
i += 1;
}
}
"-e" => {
if i + 1 < args.len() {
export_file = Some(args[i + 1].clone());
i += 1;
}
}
_ => {}
}
i += 1;
}
export_hills(img, &data, export_file.as_deref())
}
}
"info" => {
if args.len() != 3 {
usage();
2
} else {
info(&args[2])
}
}
"profiles" => profiles(),
"profile-save" => profile_save_cmd(&args[2..]),
"stitch" => stitch_cmd(&args[2..]),
_ => {
usage();
2
}
};
std::process::exit(code);
}
|