Rust style fixes

This commit is contained in:
Nigel Tao committed 2018-01-31 16:50:38 +11:00
1 parent d74b3a3e29
commit bcc4450624
1 file changed
+33 -44
+33 -44
View File
@@ -41,13 +41,7 @@ const FIRST_PIXEL: u8 = 1; // Top left pixel's red component is 0x01.
const LAST_PIXEL: u8 = 3; // Bottom right pixel's blue component is 0x03.
fn main() {
let mut dst: Vec<u8> = Vec::with_capacity(NUM_BYTES);
// This unsafe code is based on decode_lzw_test in github.com/Geal/gif.rs's
// parser.rs.
unsafe {
dst.set_len(NUM_BYTES);
}
let mut dst = [0; NUM_BYTES];
let src = include_bytes!("../../../test/testdata/harvesters.gif");
let start = Instant::now();
@@ -61,12 +55,17 @@ fn main() {
let elapsed_nanos = elapsed.as_secs() * 1_000_000_000 + (elapsed.subsec_nanos() as u64);
let total_pixels: u64 = ((WIDTH * HEIGHT) as u64) * (REPS as u64);
let kb_per_s: u64 = total_pixels * 1_000_000 / elapsed_nanos;
let kp_per_s: u64 = total_pixels * 1_000_000 / elapsed_nanos;
// Use eprint instead of print because the https://github.com/Geal/gif.rs
// library can print its own debugging messages. When running this program,
// it can be useful to redirect stdout (but not stderr) to /dev/null.
eprint!("{}.{:03} MB/s\n", kb_per_s / 1_000, kb_per_s % 1_000);
// library can print its own debugging messages (this is filed as
// https://github.com/Geal/gif.rs/issues/4). When running this program, it
// can be useful to redirect stdout (but not stderr) to /dev/null.
eprint!(
"{}.{:03} megapixels/second\n",
kp_per_s / 1_000,
kp_per_s % 1_000
);
}
fn decode(dst: &mut [u8], src: &[u8]) {
@@ -74,39 +73,29 @@ fn decode(dst: &mut [u8], src: &[u8]) {
dst[0] = 0;
dst[NUM_BYTES - 1] = 0;
// This nested code is based on decode_lzw_test in github.com/Geal/gif.rs's
// parser.rs.
let (_, colors) =
gif::parser::color_table(&src[COLOR_TABLE_OFFSET..], COLOR_TABLE_ELEMENT_COUNT).unwrap();
match gif::parser::color_table(&src[COLOR_TABLE_OFFSET..], COLOR_TABLE_ELEMENT_COUNT) {
Ok((_, colors)) => {
match gif::parser::graphic_block(&src[GRAPHIC_BLOCK_OFFSET..]) {
Ok((_, gif::parser::Block::GraphicBlock(_, rendering))) => {
match rendering {
gif::parser::GraphicRenderingBlock::TableBasedImage(_,
code_size,
blocks) => {
match gif::lzw::decode_lzw(&colors, code_size as usize, blocks, dst) {
Ok(num_bytes) => {
if num_bytes != NUM_BYTES {
panic!("wrong num_bytes")
}
// A hard-coded sanity check that we
// decoded the image's pixel data properly.
if (dst[0] != FIRST_PIXEL) ||
(dst[NUM_BYTES - 1] != LAST_PIXEL)
{
panic!("wrong dst pixels")
}
}
_ => panic!("could not decode"),
}
}
_ => panic!("plaintext extension"),
}
}
_ => panic!("cannot parse graphic block"),
}
}
_ => panic!("cannot parse global color table"),
let (_, block) = gif::parser::graphic_block(&src[GRAPHIC_BLOCK_OFFSET..]).unwrap();
let rendering = match block {
gif::parser::Block::GraphicBlock(_, x) => x,
_ => panic!("not a graphic block"),
};
let (code_size, blocks) = match rendering {
gif::parser::GraphicRenderingBlock::TableBasedImage(_, x, y) => (x, y),
_ => panic!("not a table based image"),
};
let num_bytes = gif::lzw::decode_lzw(&colors, code_size as usize, blocks, dst).unwrap();
if num_bytes != NUM_BYTES {
panic!("wrong num_bytes")
}
// A hard-coded sanity check that we decoded the pixel data correctly.
if (dst[0] != FIRST_PIXEL) || (dst[NUM_BYTES - 1] != LAST_PIXEL) {
panic!("wrong dst pixels")
}
}