forked from TeXitoi/structopt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TeXitoi#5 from dgriffen/transformations
early transformation decoding
Showing
12 changed files
with
365 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod header; | ||
pub mod metadata; | ||
pub mod transformations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::io::Read; | ||
use components::header::{Header, SecondHeader}; | ||
use error::*; | ||
use numbers::near_zero::NearZeroCoder; | ||
use numbers::rac::ChanceTable; | ||
use numbers::rac::Rac; | ||
use super::Transformation; | ||
|
||
#[derive(Debug)] | ||
pub struct Bounds { | ||
min: [i16; 4], | ||
max: [i16; 4], | ||
} | ||
|
||
impl Bounds { | ||
pub fn new<R: Read, T: ?Sized + Transformation>( | ||
rac: &mut Rac<R>, | ||
trans: &T, | ||
(ref header, ref second): (&Header, &SecondHeader), | ||
) -> Result<Bounds> { | ||
let mut context = ChanceTable::new(second.alpha_divisor, second.cutoff); | ||
let mut min = [0; 4]; | ||
let mut max = [0; 4]; | ||
for c in 0..header.channels as usize { | ||
min[c] = rac.read_near_zero(0, trans.max(c as u8) - trans.min(c as u8), &mut context)? | ||
+ trans.min(c as u8); | ||
max[c] = rac.read_near_zero(0, trans.max(c as u8) - min[c], &mut context)? + min[c]; | ||
|
||
// set real min and max | ||
min[c] = ::std::cmp::max(min[c], trans.min(c as u8)); | ||
max[c] = ::std::cmp::min(max[c], trans.max(c as u8)); | ||
} | ||
|
||
Ok(Bounds { min, max }) | ||
} | ||
} | ||
|
||
impl Transformation for Bounds { | ||
fn snap(&self, _channel: u8, _values: i16, _pixel: i16) -> i16 { | ||
unimplemented!() | ||
} | ||
|
||
fn min(&self, channel: u8) -> i16 { | ||
self.min[channel as usize] | ||
} | ||
|
||
fn max(&self, channel: u8) -> i16 { | ||
self.max[channel as usize] | ||
} | ||
|
||
fn cmin(&self, _channel: u8, _values: i16) -> i16 { | ||
unimplemented!() | ||
} | ||
|
||
fn cmax(&self, _channel: u8, _values: i16) -> i16 { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::io::Read; | ||
use components::header::{Header, SecondHeader}; | ||
use error::*; | ||
use numbers::near_zero::NearZeroCoder; | ||
use numbers::rac::ChanceTable; | ||
use numbers::rac::Rac; | ||
use super::Transformation; | ||
|
||
#[derive(Debug)] | ||
pub struct ChannelCompact { | ||
max: [i16; 4], | ||
decompacted: [Vec<i16>; 4], | ||
} | ||
impl ChannelCompact { | ||
pub fn new<R: Read, T: ?Sized + Transformation>( | ||
rac: &mut Rac<R>, | ||
transformation: &T, | ||
(ref header, ref second): (&Header, &SecondHeader), | ||
) -> Result<ChannelCompact> { | ||
let mut context = ChanceTable::new(second.alpha_divisor, second.cutoff); | ||
let mut t = ChannelCompact { | ||
max: [0; 4], | ||
decompacted: [Vec::new(), Vec::new(), Vec::new(), Vec::new()], | ||
}; | ||
|
||
for c in 0..header.channels as usize { | ||
t.max[c] = rac.read_near_zero( | ||
0, | ||
transformation.max(c as u8) - transformation.min(c as u8), | ||
&mut context, | ||
)?; | ||
let mut min = transformation.min(c as u8); | ||
for i in 0..t.max[c] { | ||
t.decompacted[c].push( | ||
min | ||
+ rac.read_near_zero( | ||
0, | ||
transformation.max(c as u8) - (min + (t.max[c] - i)), | ||
&mut context, | ||
)?, | ||
); | ||
min = t.decompacted[c][i as usize]; | ||
} | ||
} | ||
|
||
Ok(t) | ||
} | ||
} | ||
|
||
impl Transformation for ChannelCompact { | ||
fn snap(&self, _channel: u8, _values: i16, _pixel: i16) -> i16 { | ||
unimplemented!() | ||
} | ||
|
||
fn min(&self, _channel: u8) -> i16 { | ||
0 | ||
} | ||
|
||
fn max(&self, channel: u8) -> i16 { | ||
self.max[channel as usize] | ||
} | ||
|
||
fn cmin(&self, _channel: u8, _values: i16) -> i16 { | ||
0 | ||
} | ||
|
||
fn cmax(&self, channel: u8, _values: i16) -> i16 { | ||
self.max[channel as usize] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::io::Read; | ||
use components::header::{Header, SecondHeader}; | ||
use error::*; | ||
use numbers::rac::Rac; | ||
use numbers::symbol::UniformSymbolCoder; | ||
use self::channel_compact::ChannelCompact; | ||
use self::bounds::Bounds; | ||
use self::ycocg::YCoGg; | ||
|
||
mod bounds; | ||
mod channel_compact; | ||
mod ycocg; | ||
|
||
pub trait Transformation: ::std::fmt::Debug { | ||
fn snap(&self, channel: u8, values: i16, pixel: i16) -> i16; | ||
|
||
fn min(&self, channel: u8) -> i16; | ||
|
||
fn max(&self, channel: u8) -> i16; | ||
|
||
fn cmin(&self, channel: u8, values: i16) -> i16; | ||
|
||
fn cmax(&self, channel: u8, values: i16) -> i16; | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Orig; | ||
|
||
impl Transformation for Orig { | ||
fn snap(&self, _channel: u8, _values: i16, pixel: i16) -> i16 { | ||
pixel | ||
} | ||
|
||
fn min(&self, _channel: u8) -> i16 { | ||
0 | ||
} | ||
|
||
fn max(&self, _channel: u8) -> i16 { | ||
255 | ||
} | ||
|
||
fn cmin(&self, _channel: u8, _values: i16) -> i16 { | ||
0 | ||
} | ||
|
||
fn cmax(&self, _channel: u8, _values: i16) -> i16 { | ||
255 | ||
} | ||
} | ||
|
||
pub fn load_transformations<R: Read>( | ||
rac: &mut Rac<R>, | ||
(ref header, ref second): (&Header, &SecondHeader), | ||
) -> Result<Vec<Box<Transformation>>> { | ||
let mut transforms: Vec<Box<Transformation>> = Vec::new(); | ||
transforms.push(Box::new(Orig)); | ||
while rac.read_bit()? { | ||
let id = rac.read_val(0, 13)?; | ||
let t = match id { | ||
0 => Box::new(ChannelCompact::new( | ||
rac, | ||
transforms[transforms.len() - 1].as_ref(), | ||
(header, second), | ||
)?), | ||
1 => Box::new(YCoGg::new(transforms[transforms.len() - 1].as_ref())) | ||
as Box<Transformation>, | ||
4 => Box::new(Bounds::new( | ||
rac, | ||
transforms[transforms.len() - 1].as_ref(), | ||
(header, second), | ||
)?), | ||
_ => { | ||
break; | ||
} | ||
}; | ||
transforms.push(t); | ||
} | ||
|
||
Ok(transforms) | ||
} |
Oops, something went wrong.