Made syncer code fuzzier to improve slanted images

This commit is contained in:
Sebastian 2017-12-30 21:49:36 +01:00
parent e0e7db6a02
commit ae3ee11468
3 changed files with 29 additions and 12 deletions

2
Cargo.lock generated
View File

@ -1,4 +1,4 @@
[root] [[package]]
name = "apt-decoder" name = "apt-decoder"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [

View File

@ -32,6 +32,7 @@ pub struct APTSyncer<'a> {
pos : usize, pos : usize,
nones_read: usize, nones_read: usize,
max_level : f32, max_level : f32,
min_level: f32,
iterator: Box<Iterator<Item=f32> + 'a> iterator: Box<Iterator<Item=f32> + 'a>
} }
@ -39,11 +40,13 @@ impl<'a> APTSyncer<'a> {
pub fn from<I>(mut iterator: I) -> APTSyncer<'a> where I: Iterator<Item=f32> + 'a { pub fn from<I>(mut iterator: I) -> APTSyncer<'a> where I: Iterator<Item=f32> + 'a {
let mut state = [0.0; SYNC_LENGHT]; let mut state = [0.0; SYNC_LENGHT];
let mut max_level = 0.0; let mut max_level = 0.0;
let mut min_level = 1.0;
for i in 0..SYNC_LENGHT { for i in 0..SYNC_LENGHT {
match iterator.next() { match iterator.next() {
Some(x) => { Some(x) => {
state[i] = x; state[i] = x;
max_level = f32::max(x, max_level); max_level = 0.25 * x + max_level * 0.75;
min_level = 0.25 * x + min_level * 0.75;
}, },
None => panic!("Could not retrieve enough samples to prime syncer") None => panic!("Could not retrieve enough samples to prime syncer")
} }
@ -54,25 +57,33 @@ impl<'a> APTSyncer<'a> {
pos: 0, pos: 0,
nones_read: 0, nones_read: 0,
max_level: max_level, max_level: max_level,
min_level: min_level,
iterator: Box::new(iterator) iterator: Box::new(iterator)
} }
} }
fn is_marker(&mut self) -> (bool, bool) { fn is_marker(&mut self) -> (bool, bool) {
let mut is_a = true; let mut count_a = 0;
let mut is_b = true; let mut count_b = 0;
for i in 0..SYNC_LENGHT { for i in 0..SYNC_LENGHT {
let sync_pos = (self.pos + i) % SYNC_LENGHT; let sync_pos = (self.pos + i) % SYNC_LENGHT;
let sample = self.state[sync_pos] / self.max_level; let sample = (self.state[sync_pos] - self.min_level) / (self.max_level - self.min_level);
is_a = is_a && ((sample > 0.5 && SYNCA_SEQ[i]) || (sample <= 0.5 && !SYNCA_SEQ[i])); if (sample > 0.5 && SYNCA_SEQ[i]) || (sample <= 0.5 && !SYNCA_SEQ[i]) {
is_b = is_b && ((sample > 0.5 && SYNCB_SEQ[i]) || (sample <= 0.5 && !SYNCB_SEQ[i])); count_a += 1;
}
if (sample > 0.5 && SYNCB_SEQ[i]) || (sample <= 0.5 && !SYNCB_SEQ[i]) {
count_b += 1;
}
if !is_a && !is_b { /*
if !count_a && !count_b {
break; break;
} }
*/
} }
return (is_a, is_b); return (count_a > 35, count_b > 35);
} }
} }
@ -87,7 +98,8 @@ impl<'a> Iterator for APTSyncer<'a> {
match self.iterator.next() { match self.iterator.next() {
Some(x) => { Some(x) => {
self.state[self.pos] = x; self.state[self.pos] = x;
self.max_level = f32::max(x, self.max_level); self.max_level = 0.25 * x + self.max_level * 0.75;
self.min_level = 0.25 * x + self.min_level * 0.75;
}, },
None => self.nones_read += 1 None => self.nones_read += 1
}; };

View File

@ -85,6 +85,8 @@ fn main() {
let mut progress = 0; let mut progress = 0;
let step = sample_count * 13 / 150 / 10; let step = sample_count * 13 / 150 / 10;
let mut last_sync = 0;
print!("0%"); print!("0%");
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
for synced_sample in syncer { for synced_sample in syncer {
@ -95,14 +97,17 @@ fn main() {
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
} }
let sample = match synced_sample { let sample = match synced_sample {
SyncedSample::Sample(s) => s, SyncedSample::Sample(s) => s,
SyncedSample::SyncA(s) =>{ SyncedSample::SyncA(s) =>{
println!(" [Sync A {}]", progress - last_sync);
last_sync = progress;
x = 0; x = 0;
s s
} }
SyncedSample::SyncB(s) =>{ SyncedSample::SyncB(s) =>{
println!(" [Sync B {}]", progress - last_sync);
last_sync = progress;
x = PIXELS_PER_LINE / 2; x = PIXELS_PER_LINE / 2;
s s
} }
@ -127,7 +132,7 @@ fn main() {
Err(e) => panic!("Could not open outputfile: {}", e), Err(e) => panic!("Could not open outputfile: {}", e),
Ok(f) => f Ok(f) => f
}; };
image::ImageLuma8(img).save(fout, image::PNG).unwrap(); image::ImageLuma8(img).save(fout, image::PNG).unwrap();
println!("Done !"); println!("Done !");