Skip to content

Commit

Permalink
clean up trival casts
Browse files Browse the repository at this point in the history
  • Loading branch information
mcroomp committed Dec 2, 2024
1 parent d89a65a commit 7aa04dc
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/structs/bit_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ use std::io::Cursor;
// test reading a simple bit pattern with an escaped 0xff inside it.
#[test]
fn read_simple() {
let arr = [0x12 as u8, 0x34, 0x45, 0x67, 0x89, 0xff, 00, 0xee];
let arr = [0x12u8, 0x34, 0x45, 0x67, 0x89, 0xff, 00, 0xee];

let mut b = BitReader::new(Cursor::new(&arr));

Expand Down Expand Up @@ -338,7 +338,7 @@ fn read_simple() {
// what happens when a file has 0xff as the last character (assume that it is an escaped 0xff)
#[test]
fn read_truncate_ff() {
let arr = [0x12 as u8, 0xff];
let arr = [0x12u8, 0xff];

let mut b = BitReader::new(Cursor::new(&arr));

Expand Down
4 changes: 2 additions & 2 deletions src/structs/bit_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ use crate::structs::bit_reader::BitReader;
// write a test pattern with an escape and see if it matches
#[test]
fn write_simple() {
let arr = [0x12 as u8, 0x34, 0x45, 0x67, 0x89, 0xff, 00, 0xee];
let arr = [0x12, 0x34, 0x45, 0x67, 0x89, 0xff, 00, 0xee];

let mut b = BitWriter::new(1024);

Expand All @@ -178,7 +178,7 @@ fn roundtrip_bits() {
{
let mut b = BitWriter::new(1024);
for i in 1..2048 {
b.write(i, u32_bit_length(i as u32) as u32);
b.write(i, u32_bit_length(i) as u32);
}

b.pad(0xff);
Expand Down
4 changes: 2 additions & 2 deletions src/structs/block_based_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl BlockBasedImage {
if relative_offset < self.image.len() {
// rewrite already written block
if let Some(b) = block_to_write {
self.image[relative_offset as usize] = b;
self.image[relative_offset] = b;
}
} else {
// need to extend the image length and add any necessary
Expand All @@ -166,7 +166,7 @@ impl BlockBasedImage {
self.image.push(block_to_write.unwrap_or_default());
}

return &mut self.image[relative_offset as usize];
return &mut self.image[relative_offset];
}

pub fn set_block_data(&mut self, dpos: u32, block_data: AlignedBlock) {
Expand Down
2 changes: 1 addition & 1 deletion src/structs/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn test_all_probabilities() {
continue;
}

let mut new_f = Branch { counts: i as u16 };
let mut new_f = Branch { counts: i };

for _k in 0..10 {
old_f.record_obs_and_update(false);
Expand Down
91 changes: 63 additions & 28 deletions src/structs/jpeg_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use crate::structs::component_info::ComponentInfo;
use crate::structs::lepton_header::LeptonHeader;
use crate::structs::quantization_tables::QuantizationTables;
use crate::structs::truncate_components::TruncateComponents;
use crate::LeptonError;

#[derive(Copy, Clone, Debug)]
pub struct HuffCodes {
Expand Down Expand Up @@ -268,31 +269,68 @@ impl HuffTree {

#[derive(Debug, Clone)]
pub struct JPegHeader {
pub q_tables: [[u16; 64]; 4], // quantization tables 4 x 64
h_codes: [[HuffCodes; 4]; 2], // huffman codes (access via get_huff_xx_codes)
h_trees: [[HuffTree; 4]; 2], // huffman decoding trees (access via get_huff_xx_tree)
pub ht_set: [[u8; 4]; 2], // 1 if huffman table is set
pub cmp_info: [ComponentInfo; 4], // components
pub cmpc: usize, // component count
pub img_width: u32, // width of image
pub img_height: u32, // height of image
/// quantization tables 4 x 64
pub q_tables: [[u16; 64]; 4],

/// huffman codes (access via get_huff_xx_codes)
h_codes: [[HuffCodes; 4]; 2],

/// huffman decoding trees (access via get_huff_xx_tree)
h_trees: [[HuffTree; 4]; 2],

/// 1 if huffman table is set
pub ht_set: [[u8; 4]; 2],

/// components
pub cmp_info: [ComponentInfo; 4],

/// component count
pub cmpc: usize,

/// width of image
pub img_width: u32,

/// height of image
pub img_height: u32,

pub jpeg_type: JPegType,
pub sfhm: u32, // max horizontal sample factor
pub sfvm: u32, // max verical sample factor
pub mcuv: NonZeroU32, // mcus per line
pub mcuh: NonZeroU32, // mcus per collumn
pub mcuc: u32, // count of mcus

pub rsti: u32, // restart interval
pub cs_cmpc: usize, // component count in current scan
pub cs_cmp: [usize; 4], // component numbers in current scan
/// max horizontal sample factor
pub sfhm: u32,

/// max verical sample factor
pub sfvm: u32,

// mcus per line
pub mcuv: NonZeroU32,

/// mcus per column
pub mcuh: NonZeroU32,

/// count of mcus
pub mcuc: u32,

/// restart interval
pub rsti: u32,

/// component count in current scan
pub cs_cmpc: usize,

/// component numbers in current scan
pub cs_cmp: [usize; 4],

// variables: info about current scan
pub cs_from: u8, // begin - band of current scan ( inclusive )
pub cs_to: u8, // end - band of current scan ( inclusive )
pub cs_sah: u8, // successive approximation bit pos high
pub cs_sal: u8, // successive approximation bit pos low
/// begin - band of current scan ( inclusive )
pub cs_from: u8,

/// end - band of current scan ( inclusive )
pub cs_to: u8,

/// successive approximation bit pos high
pub cs_sah: u8,

/// successive approximation bit pos low
pub cs_sal: u8,
}

pub struct JPegEncodingInfo {
Expand Down Expand Up @@ -442,10 +480,12 @@ impl JPegHeader {
self.mcuv = NonZeroU32::new(
(1.0 * self.img_height as f64 / (8.0 * self.sfhm as f64)).ceil() as u32,
)
.unwrap();
.ok_or_else(|| LeptonError::new(ExitCode::UnsupportedJpeg, "mcuv is zero"))?;

self.mcuh =
NonZeroU32::new((1.0 * self.img_width as f64 / (8.0 * self.sfvm as f64)).ceil() as u32)
.unwrap();
.ok_or_else(|| LeptonError::new(ExitCode::UnsupportedJpeg, "mcuh is zero"))?;

self.mcuc = self.mcuv.get() * self.mcuh.get();

for cmp in 0..self.cmpc {
Expand Down Expand Up @@ -1004,12 +1044,7 @@ pub fn generate_huff_table_from_distribution(freq: &[usize; 256]) -> HuffCodes {
pq.pop().unwrap()
}

fn generate_codes(
root: &Node,
codes: &mut std::collections::HashMap<u8, (u16, u8)>,
prefix: u16,
length: u8,
) {
fn generate_codes(root: &Node, codes: &mut HashMap<u8, (u16, u8)>, prefix: u16, length: u8) {
if let Some(symbol) = root.symbol {
codes.insert(symbol, (prefix, length));
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/structs/jpeg_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ fn decode_ac_prg_fs<R: BufRead>(
} else {
// decode eobrun
let s = l;
let n = bit_reader.read(u32::from(s))? as u16;
let n = bit_reader.read(u32::from(s))?;
state.eobrun = decode_eobrun_bits(s, n);

state.eobrun -= 1; // decrement eobrun ( for this one )
Expand Down Expand Up @@ -694,7 +694,7 @@ fn decode_ac_prg_sa<R: BufRead>(
// decode eobrun
eob = bpos;
let s = l;
let n = bit_reader.read(u32::from(s))? as u16;
let n = bit_reader.read(u32::from(s))?;
state.eobrun = decode_eobrun_bits(s, n);

// since we hit EOB, the rest can be done with the zero block decoder
Expand Down
2 changes: 1 addition & 1 deletion src/structs/lepton_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn decode_one_edge<R: Read, const ALL_PRESENT: bool, const HORIZONTAL: bool>(
if coef != 0 {
num_non_zeros_edge -= 1;
here_mut.set_coefficient(coord_tr, coef);
raster[coord_tr as usize] =
raster[coord_tr] =
i32::from(coef) * i32::from(qt.get_quantization_table_transposed()[coord_tr]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/structs/lepton_file_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ impl LeptonFileReader {
0
} as u8;

for i in 0..lh.rst_err[0] as u8 {
let rst = (jpeg_code::RST0 + ((cumulative_reset_markers + i) & 7)) as u8;
for i in 0..lh.rst_err[0] {
let rst = jpeg_code::RST0 + ((cumulative_reset_markers + i) & 7);
markers.push(0xFF);
markers.push(rst);
}
Expand Down
2 changes: 1 addition & 1 deletion src/structs/lepton_file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ fn split_row_handoffs_to_threads(

info!("Number of threads: {0}", num_threads);

let mut selected_splits = Vec::with_capacity(num_threads as usize);
let mut selected_splits = Vec::with_capacity(num_threads);

if num_threads == 1 {
// Single thread execution - no split, run on the whole range
Expand Down
4 changes: 2 additions & 2 deletions src/structs/lepton_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl LeptonHeader {
// beginning here: recovery information (needed for exact JPEG recovery)
// read further recovery information if any
loop {
let mut current_lepton_marker = [0 as u8; 3];
let mut current_lepton_marker = [0u8; 3];
match header_reader.read_exact(&mut current_lepton_marker) {
Ok(_) => {}
Err(e) => {
Expand Down Expand Up @@ -468,7 +468,7 @@ impl LeptonHeader {
mrw.write_u32::<LittleEndian>(self.rst_cnt.len() as u32)?;

for i in 0..self.rst_cnt.len() {
mrw.write_u32::<LittleEndian>(self.rst_cnt[i] as u32)?;
mrw.write_u32::<LittleEndian>(self.rst_cnt[i])?;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Don't allow any unsafe code by default. Since this code has to potentially deal with
// badly/maliciously formatted images, we want this extra level of safety.
#![forbid(unsafe_code)]
#![forbid(trivial_numeric_casts)]

mod bit_reader;
mod bit_writer;
Expand Down
2 changes: 1 addition & 1 deletion src/structs/quantization_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl QuantizationTables {
freq_max /= retval.quantization_table[coord];
}

let max_len = u16_bit_length(freq_max) as u8;
let max_len = u16_bit_length(freq_max);
if max_len > RESIDUAL_NOISE_FLOOR as u8 {
retval.min_noise_threshold[i] = max_len - RESIDUAL_NOISE_FLOOR as u8;
}
Expand Down
4 changes: 2 additions & 2 deletions src/structs/row_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl RowSpec {
let mut mcu_multiple = 0;

for i in 0..num_cmp {
heights.push(image_data[i].get_original_height() as u32);
component_multiple.push(heights[i] / mcuv as u32);
heights.push(image_data[i].get_original_height());
component_multiple.push(heights[i] / mcuv);
mcu_multiple += component_multiple[i];
}

Expand Down
4 changes: 2 additions & 2 deletions src/structs/simple_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl SimpleHashProvider for u32 {

impl SimpleHashProvider for u64 {
fn get_u64(&self) -> u64 {
return *self as u64;
return *self;
}
}

Expand All @@ -41,7 +41,7 @@ impl SimpleHash {
}

pub fn hash<T: SimpleHashProvider>(&mut self, v: T) {
self.hash = (Wrapping(self.hash as u64) * Wrapping(13u64) + Wrapping(v.get_u64())).0;
self.hash = (Wrapping(self.hash) * Wrapping(13u64) + Wrapping(v.get_u64())).0;
}

pub fn get(&self) -> u32 {
Expand Down
2 changes: 1 addition & 1 deletion src/structs/truncate_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl TruncateComponents {
let mut retval = Vec::<u32>::new();

for i in 0..self.components_count {
retval.push(self.trunc_info[i].trunc_bcv as u32);
retval.push(self.trunc_info[i].trunc_bcv);
}
return retval;
}
Expand Down

0 comments on commit 7aa04dc

Please sign in to comment.