Skip to content

Commit

Permalink
Apply clippy suggestions; update dependencies (#90)
Browse files Browse the repository at this point in the history
* Apply clippy suggestions

This certainly clarifies things, but I'm not completely confident the
`image2tensor` code is arranging the bytes as it should. Then again,
perhaps more documentation is in order.

* Update `image` dependency
  • Loading branch information
abrown committed Aug 25, 2023
1 parent 59ebb88 commit 436d50e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 52 deletions.
70 changes: 40 additions & 30 deletions image2tensor/Cargo.lock

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

2 changes: 1 addition & 1 deletion image2tensor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition = "2018"
publish = true

[dependencies]
image = { version = "0.24.4", default-features = false, features = [
image = { version = "0.24", default-features = false, features = [
"gif",
"jpeg",
"ico",
Expand Down
34 changes: 15 additions & 19 deletions image2tensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ pub fn convert_image_to_bytes(
) -> Result<Vec<u8>, String> {
// Open the file and create the reader.
let raw_file = Reader::open(path)
.or_else(|_| Err(format!("Failed to open the file: {:?}", path)))
.map_err(|_| format!("Failed to open the file: {:?}", path))
.unwrap();

// Create the DynamicImage by decoding the image.
let decoded = raw_file
.decode()
.or_else(|_| Err(format!("Failed to decode the file: {:?}", path)))
.map_err(|_| format!("Failed to decode the file: {:?}", path))
.unwrap();

// Resize the image to the specified W/H and get an array of u8 RGB values.
Expand All @@ -53,21 +53,20 @@ pub fn calculate_buffer_size(width: u32, height: u32, precision: TensorType) ->
}

// Save the bytes into the specified TensorType format.
fn save_bytes(arr: &[u8], tt: TensorType) -> Vec<u8> {
fn save_bytes(buffer: &[u8], tt: TensorType) -> Vec<u8> {
let mut out: Vec<u8> = vec![];

for i in 0..arr.len() {
for &byte in buffer {
// Split out the bytes based on the TensorType.
let bytes: Vec<u8>;
bytes = match tt {
TensorType::F16 => (arr[i] as f32).to_ne_bytes().to_vec(),
TensorType::F32 => (arr[i] as f32).to_ne_bytes().to_vec(),
TensorType::U8 => (arr[i]).to_ne_bytes().to_vec(),
TensorType::I32 => (arr[i] as i32).to_ne_bytes().to_vec(),
let ne_bytes = match tt {
TensorType::F16 => todo!("unable to convert to f16 yet"),
TensorType::F32 => (byte as f32).to_ne_bytes().to_vec(),
TensorType::U8 => (byte).to_ne_bytes().to_vec(),
TensorType::I32 => (byte as i32).to_ne_bytes().to_vec(),
};

for j in 0..bytes.len() {
out.push(bytes[j]);
for byte in ne_bytes {
out.push(byte);
}
}
out
Expand All @@ -82,12 +81,9 @@ fn get_bytes_per_pixel(precision: TensorType) -> usize {
}

// Converts an RGB array to BGR
fn rgb_to_bgr(arr: &mut [u8]) -> &[u8] {
for i in (0..arr.len()).step_by(3) {
let b_bak = arr[i + 2];
// swap R and B
arr[i + 2] = arr[i];
arr[i] = b_bak;
fn rgb_to_bgr(buffer: &mut [u8]) -> &[u8] {
for i in (0..buffer.len()).step_by(3) {
buffer.swap(i + 2, i);
}
arr
buffer
}
4 changes: 2 additions & 2 deletions rust/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl<'a> GraphExecutionContext<'a> {
let buf = unsafe {
core::slice::from_raw_parts(
data_slice.as_ptr().cast::<u8>(),
data_slice.len() * std::mem::size_of::<T>(),
std::mem::size_of_val(data_slice),
)
};
let tensor_for_call = Tensor::new(dimensions, tensor_type, buf);
Expand All @@ -266,7 +266,7 @@ impl<'a> GraphExecutionContext<'a> {
let out_buf = unsafe {
core::slice::from_raw_parts_mut(
out_buffer.as_mut_ptr().cast::<u8>(),
out_buffer.len() * std::mem::size_of::<T>(),
std::mem::size_of_val(out_buffer),
)
};
syscall::get_output(self.ctx_handle, index, out_buf)
Expand Down

0 comments on commit 436d50e

Please sign in to comment.