Im wondering if im using the image crate correctly

It takes a full 30 seconds to turn it into grey scale!:sob:

use image::{DynamicImage, GenericImageView, ImageFormat};
use std::io::{Cursor, Write};

#[flutter_rust_bridge::frb(sync)]
pub fn bytes(bytes: Vec) -> Vec {
// Load image from bytes
let image = match image::load_from_memory(&bytes) {
Ok(img) => img,
Err(_) => panic!("Failed to load image from bytes"),
};

// Convert image to grayscale
let img_gray = image.grayscale();

// Encode grayscale image as JPEG into a buffer
let mut result_bytes = Vec::new();
let mut cursor = Cursor::new(&mut result_bytes);
img_gray.write_to(&mut cursor, ImageFormat::Jpeg)
    .expect("Failed to encode image as JPEG");

// Flush the cursor to ensure all data is written to result_bytes
cursor.flush().expect("Failed to flush cursor");

result_bytes

}

Are you runnning with optimizations enabled (cargo run --release)?

i wasn't before but i just tryed and its a little faster but still not instant it took 24 seconds to complete

And is that purely the image processing part? You are apparently doing Flutter stuff. Is 24 seconds the wall time elapsed between entering and leaving this piece of Rust code, or is that the total running time of your application, along with launching, etc.?

Also, how big is the image?

So I print as soon as i send the bytes to the rust back end im using the rust flutter bridge I timed it as soon as it printed that it sent to the backend and then it prints when it recived the bytes.

I also tried just to return the bytes i sent and that was instant.

I started to delete code to see where its taking a long time and its when I read it into memory, my laptop is really beefy so I dont think its a problem with my computer being slow.

// Load image from bytes
let image = match image::load_from_memory(&bytes) {
Ok(img) => img,
Err(_) => panic!("Failed to load image from bytes"),
};

this is the part where it takes a long time to load

D/MediaScannerConnection(11729): Scanned

/data/user/0/com.example.pic_to_bytes/cache/501f2afd-3ee7-4df3-9155-1006ee1e193e8913665737455728552.jpg to null

####################################
I/flutter (11729): Image bytes received: 1989508
###################################

I/flutter (11729): Bytes from Rust: [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 2, 0, 0, 1, 0, 1, 0, 0, 255, 192, 0, 11, 8, 11, 208, 15, 192, 1, 1, 17, 0, 255, 219, 0, 67, 0, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26, 31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, 255, 196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151,

the image is 1989508 bytes

I timed it as soon as it enters the rust block and exits the rust block

Reading 2 MB into memory absolutely, positively doesn't take 24 seconds even on a microcontroller nowadays. Something else is going on.

Are you running Windows with some sort of anti-virus software continuously running in the background and scanning everything you do? If so, turn it off.

Are you printing all of the received bytes to the console? If so, that may be a problem; stdout can be pretty slow depending on various factors; terminal emulators are certainly not optimized for handling a lot of data very efficiently.

Im using ubuntu 24.04 its fresh, and im not printing anything from rust.

I have 0 anti virus stuff running.

my front end is not the problem becuse i can send bytes to and from rust instantly

What format is your image? The file name suggests JPEG, but you are using format auto-discovery. Can you post the exact image file itself (if there are no intellectual property issues) so we can try to reproduce the issue?

Also, can you use load_from_memory_with_format() instead of load_from_memory()? At this point, I have no idea what the problem may be – it might as well be the format auto-detection… (although that would be exceptionally weird IMO).

One reply by @Jsonbourne got lost unfortunately, as they ran into rate limits for new users:

1 Like

Do you know if it’s load_from_memory() that’s slow, or if it’s grayscale()?

If it’s the grayscale op that’s slow, then I’d try shrinking the image down before doing the color filter. A lot of Android phones produce dimensionally huge 8K images with aggressive compression for little reason.

2 Likes

Hey I figured it out i was passing it bad bytes from my flutter side! thanks your your help tho!!!!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.