Skip to content

Commit

Permalink
Fix panic when compressor is presented with two identical images in a…
Browse files Browse the repository at this point in the history
… row
  • Loading branch information
drojf committed Jul 15, 2018
1 parent e5b4c61 commit cf1b265
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 101 deletions.
111 changes: 38 additions & 73 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 44 additions & 26 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,41 +353,29 @@ pub struct BlockXYIterator {
impl BlockXYIterator {
pub fn new(block_size : usize, dimensions : (usize, usize)) -> BlockXYIterator
{
let num_x_blocks = dimensions.0 / block_size + if dimensions.0 % block_size == 0 {0} else {1};
let num_y_blocks = dimensions.1 / block_size + if dimensions.1 % block_size == 0 {0} else {1};
let mut safe_dimensions = dimensions;
let mut safe_i = 0;

// if dimensions given are 0,0, force dimensions to 1,1, then set i so that iterator
// will terminate immediately. This avoids a modulo (%) by zero error and does 0 iterations.
if dimensions.0 == 0 || dimensions.1 == 0 {
safe_dimensions = (1, 1);
safe_i = 2;
}

let num_x_blocks = safe_dimensions.0 / block_size + if safe_dimensions.0 % block_size == 0 {0} else {1};
let num_y_blocks = safe_dimensions.1 / block_size + if safe_dimensions.1 % block_size == 0 {0} else {1};

BlockXYIterator {
block_size,
dimensions,
dimensions : safe_dimensions,
num_x_blocks,
num_y_blocks,
i : 0,
i : safe_i,
}
}
}



/*
#[allow(unused_imports)]
use common::BlockXYIterator;
#[allow(unused_imports)]
use image::RgbaImage;
test for iterator
let im = RgbaImage::new(3,5);
let mut count = 0;
for (x,y) in BlockXYIterator::new(2, (3, 5)) {
count += 1;
let pix = im.get_pixel(x,y);
println!("x: {} y:{} {:?} {}", x,y, pix, count);
}
return;
*/
impl Iterator for BlockXYIterator {
type Item = (u32, u32);

Expand Down Expand Up @@ -437,6 +425,36 @@ type Item = (u32, u32);
}




/*
Tests for xy iterator
//test iterating (50, width = 0, height = 0)
//test odd/even pixel width
//test images equal exactly the block width, and not equal to block width
#[allow(unused_imports)]
use common::BlockXYIterator;
#[allow(unused_imports)]
use image::RgbaImage;
test for iterator
let im = RgbaImage::new(3,5);
let mut count = 0;
for (x,y) in BlockXYIterator::new(2, (3, 5)) {
count += 1;
let pix = im.get_pixel(x,y);
println!("x: {} y:{} {:?} {}", x,y, pix, count);
}
return;
*/


pub fn try_get_pixel(prev_xy : (i64, i64), prev_image : &image::RgbaImage) -> Option<image::Rgba<u8>>
{
let prev_x = prev_xy.0; //original_pixel_xy.0 + prev_x_offset;
Expand Down
2 changes: 1 addition & 1 deletion src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ pub fn extract_archive_alt(brotli_archive_path : &str, oxipng_options : Option<o
print!("full: ({:4},{:4}) ", metadata.output_width, metadata.output_height);
print!("{}", metadata.output_path);
println!("");
if debug_mode { println!("meta: {:?}", metadata); }

//take a slice which contains only the desired region
//read out the required number of bytes
let expected_cropped_bitmap_size = metadata.diff_width as usize * metadata.diff_height as usize;
if debug_mode { println!("meta: {:?}", metadata); println!("Trying to extract {} bytes of bitmap data", expected_cropped_bitmap_size)}

let mut cropped_bitmap = vec![0u8; expected_cropped_bitmap_size];
bitmap_info_decompressor.read_exact(&mut cropped_bitmap).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn main()
let output_folder = "output_images";
let brotli_archive_path = "compressed_images.brotli";

println!("Spritezip version 0.1.2\n");
println!("Spritezip version 0.1.3\n");

//create input images folder if it doesn't already exist:
let input_path = Path::new(input_folder);
Expand Down

0 comments on commit cf1b265

Please sign in to comment.