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
use crate::{
vprintln,
image::MarsImage,
enums,
path,
util,
inpaintmask,
calprofile
};
pub fn process_with_profiles(input_file:&str, red_scalar:f32, green_scalar:f32, blue_scalar:f32, no_ilt:bool, hpc_threshold:f32, only_new:bool, filename_suffix:&String, profile_names:&Vec<&str>) {
if profile_names.len() > 0 {
for f in profile_names.iter() {
process_with_profile(input_file, red_scalar, green_scalar, blue_scalar, no_ilt, hpc_threshold, only_new, filename_suffix, Some(&f.to_string()));
}
} else {
process_with_profile(input_file, red_scalar, green_scalar, blue_scalar, no_ilt, hpc_threshold, only_new, filename_suffix, None);
}
}
pub fn process_with_profile(input_file:&str, red_scalar:f32, green_scalar:f32, blue_scalar:f32, no_ilt:bool, hpc_threshold:f32, only_new:bool, filename_suffix:&String, profile_name_opt:Option<&String>) {
if let Some(profile_name) = profile_name_opt {
match calprofile::load_calibration_profile(&profile_name.to_string()) {
Ok(profile) => {
process_file(input_file, profile.red_scalar, profile.green_scalar, profile.blue_scalar, !profile.apply_ilt, profile.hot_pixel_detection_threshold, only_new, &profile.filename_suffix);
},
Err(why) => {
eprintln!("Error loading calibration profile: {}", why);
panic!("Error loading calibration profile");
}
}
} else {
process_file(input_file, red_scalar, green_scalar, blue_scalar, no_ilt, hpc_threshold, only_new, &filename_suffix);
}
}
pub fn process_file(input_file:&str, red_scalar:f32, green_scalar:f32, blue_scalar:f32, _no_ilt:bool, hpc_threshold:f32, only_new:bool, filename_suffix:&String) {
let out_file = util::append_file_name(input_file, &filename_suffix);
if path::file_exists(&out_file) && only_new {
vprintln!("Output file exists, skipping. ({})", out_file);
return;
}
let mut instrument = enums::Instrument::MslNavCamRight;
if util::filename_char_at_pos(&input_file, 0) == 'N' {
if util::filename_char_at_pos(&input_file, 1) == 'L' {
instrument = enums::Instrument::MslNavCamLeft;
} else {
instrument = enums::Instrument::MslNavCamRight;
}
} else if util::filename_char_at_pos(&input_file, 0) == 'F' {
if util::filename_char_at_pos(&input_file, 1) == 'L' {
instrument = enums::Instrument::MslFrontHazLeft;
} else {
instrument = enums::Instrument::MslFrontHazRight;
}
} else if util::filename_char_at_pos(&input_file, 0) == 'R' {
if util::filename_char_at_pos(&input_file, 1) == 'L' {
instrument = enums::Instrument::MslRearHazLeft;
} else {
instrument = enums::Instrument::MslRearHazRight;
}
}
let mut raw = MarsImage::open(String::from(input_file), instrument);
if inpaintmask::inpaint_supported_for_instrument(instrument) && raw.image.height >= 1022 {
vprintln!("Inpainting...");
raw.apply_inpaint_fix();
} else {
vprintln!("Inpainting not supported for instrument {:?}", instrument);
}
if hpc_threshold > 0.0 {
vprintln!("Hot pixel correction with variance threshold {}...", hpc_threshold);
raw.hot_pixel_correction(3, hpc_threshold);
}
let data_max = 255.0;
if raw.image.height >= 1022 {
vprintln!("Flatfielding...");
raw.flatfield();
}
vprintln!("Applying color weights...");
raw.apply_weight(red_scalar, green_scalar, blue_scalar);
vprintln!("Normalizing...");
raw.image.normalize_to_16bit_with_max(data_max);
let crop_to_width = raw.image.width - 2;
let crop_to_height = raw.image.height - 2;
raw.image.crop(1, 1, crop_to_width, crop_to_height);
vprintln!("Writing to disk...");
raw.save(&out_file);
}