Commit c002f4c9 authored by Steven vanZyl's avatar Steven vanZyl
Browse files

On the trail of fixing the bug

parent bfabcf99
......@@ -14,10 +14,10 @@ fn main() -> Result<(), Error> {
);
let mut args = std::env::args().skip(1);
if let Some(path1) = args.next() {
if let Some(path2) = args.next() {
Image::from_png_file(path1, COMPRESS)?
.to_file(path2, false)
if let Some(inpath) = args.next() {
if let Some(outpath) = args.next() {
Image::from_png_file(inpath, COMPRESS)?
.to_file(outpath, COMPRESS)
.unwrap();
Ok(())
} else {
......
......@@ -11,7 +11,7 @@ impl Image {
/// The compress flag enables or disables compression.
pub fn to_bytes(self, compress: bool) -> Result<Vec<u8>, Error> {
// Set the header from the image struct
let buf = vec![
let mut buf = vec![
0xAE,
0xAE,
self.version,
......@@ -21,19 +21,26 @@ impl Image {
self.height.to_be_bytes()[1],
];
let data = self
.data
.into_iter()
.coalesce(|p, c| {
if p.color == c.color && compress {
Ok(Run::counted(p.color, p.count + c.count))
} else {
Err((p, c))
}
})
.map(|p| Vec::from(&p.bytes() as &[u8]))
.flatten();
Ok(buf.into_iter().chain(data).collect())
let width = self.width as usize;
buf.append(
&mut self
.data
.into_iter()
.coalesce(|p, c| {
let ct = p.count as usize + c.count as usize;
if compress && p.color == c.color && ct < u8::max_value() as usize && ct < width
{
Ok(Run::counted(p.color, p.count + c.count))
} else {
Err((p, c))
}
})
.map(|p| Vec::from(&p.bytes() as &[u8]))
.flatten()
.collect(),
);
Ok(buf)
}
/// Encodes an SSIF image and writes it to a file at the given path.
......
......@@ -231,7 +231,7 @@ impl Pixels {
}
}
const SLOW_AND_CORRECT: bool = false;
const SLOW_AND_CORRECT: bool = true;
impl Iterator for Pixels {
type Item = Color;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment