简体   繁体   中英

Unable to consume Rgba<u8> (no methods) in Rust

let png = image::open("src/heightmaps/ALPSMLC30_N050E014_DSM_8_BIT.png").unwrap();
    for p in png.pixels() {
        let p: image::Rgba<u8> = p.2;
    }

p is (u32, u32, image::Rgba<u8>) . What I don't know is how to access the u8 out of Rgba (the VSCode doesn't find any methods for it). How do I proceed? Here is the link to the Rgba struct: https://docs.rs/image/0.23.13/image/struct.Rgba.html .

image::Rgba is a tuple struct, you can access fields of a tuple struct using struct.0 syntax or more ergonomically through destructuring:

let png = image::open("src/heightmaps/ALPSMLC30_N050E014_DSM_8_BIT.png").unwrap();
for (_, _, image::Rgba(p)) in png.pixels() {
    let [r, g, b, a] = p;
}

Looking at the definition for image::Rgba<T> , it appears that the only field is public so you should be able to just access it directly.

Eg with verbsoe destructuring:

let png = image::open("src/heightmaps/ALPSMLC30_N050E014_DSM_8_BIT.png").unwrap();
for p in png.pixels() {
    let p: image::Rgba<u8> = p.2;
    let rgba: [u8;4] = p.0;
    let [r,g,b,a] = rgba;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM