image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.96k stars 618 forks source link

Documentation Misleading? #2220

Closed tmheath closed 6 months ago

tmheath commented 6 months ago

From my code

fn merge(prime_image: RgbaImage, second_image: RgbaImage) -> RgbaImage {
    let mut buffer = RgbaImage::new(larger(prime_image.width(), second_image.width()), larger(prime_image.height(), second_image.height()));
    let buffer_height = buffer.height();
    for (index, row) in buffer.enumerate_rows_mut() {
        for (x, y, pixel) in row {
            if index > buffer_height {
                pixel.0.map(|_|{250});
            }
            else {}
        }
    }
    buffer
}

x and y each are u32, the documentation states correctly that iteration results in (u32, u32, Pixel), but it says nothing about what the numbers are.

My question is what is being returned here. My previous attempt at solving what I need to do involved a bunch of unsafe raw pointer operations causing multiple segfaults. If you know of a better way offhand or general advise then it's welcome.

Relevant documentation https://docs.rs/image/0.25.1/i686-pc-windows-msvc/image/buffer/struct.EnumerateRowsMut.html https://docs.rs/image/0.25.1/i686-pc-windows-msvc/image/buffer/struct.EnumeratePixelsMut.html#method.next

fintelia commented 6 months ago

I'm not really sure what you question is, but pixel has type &mut Pixel so you probably want to update it via:

*pixel = pixel.0.map(|_| { 250 })
tmheath commented 6 months ago

Thank you... Each iteration yields the two numbers and the pixel, I don't understand what the numbers are

fintelia commented 6 months ago

The two integers are the (x, y) coordinate of the pixel, with (0, 0) corresponding to the top left of the image

tmheath commented 6 months ago

Thank you, I'd assumed that but wasn't sure, iteration over rows gives the row and cells so I thought it would have been odd the enumeration over I'm understanding right?