aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
index bee5782..ab446f4 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -108,3 +108,36 @@ pub fn load_file<P: AsRef<Path>>(path: P) -> Result<Image, io::Error> {
108 image.read_to_end(&mut buf)?; 108 image.read_to_end(&mut buf)?;
109 Ok(Image::decode(&mut (Cursor::new(buf))).unwrap()) // TODO: obi error 109 Ok(Image::decode(&mut (Cursor::new(buf))).unwrap()) // TODO: obi error
110} 110}
111
112pub fn compress<T: PartialEq + Clone>(scanline: &[T]) -> Vec<(T, usize)> {
113 let mut runs = vec![];
114 if scanline.is_empty() {
115 return runs;
116 }
117 let mut idx = 0;
118 loop {
119 let first = &scanline[idx];
120 let run_length = scanline[idx..]
121 .iter()
122 .take_while(|&item| item == first)
123 .count();
124
125 runs.push((first.clone(), run_length));
126
127 idx += run_length;
128 if idx > scanline.len() - 1 {
129 break;
130 }
131 }
132 runs
133}
134
135#[cfg(test)]
136mod tests {
137 use super::compress;
138 #[test]
139 fn compression() {
140 let sl = [1, 1, 1, 2, 2, 2];
141 assert_eq!(compress(&sl), vec![(1, 3), (2, 3)]);
142 }
143}