Git Product home page Git Product logo

Comments (2)

fintelia avatar fintelia commented on September 26, 2024 1

There's a lot of different ways you can store a 16-bit grayscale image in a u8 buffer. You may need load_from_memory_with_format, or perhaps you need to iterate over the buffer calling u16::from_le_bytes for each 2-byte chunk, or from_be_bytes, or the buffer is in native endian and the bytemuck crate will let you cast it, etc.

from image.

sjbeskur avatar sjbeskur commented on September 26, 2024

Ok Thank you for that response. I was able to solve this with the following:

let mut img_bytes: Vec<u8> = Vec::new();
let mut buffer: Vec<u8> = Vec::new();
// buffer gets filled from a tcp read

// Reading 16 bit grayscale image from memory buffer"

 // We have to convert the buffer of u8 to a buffer of u16.  
// I don't know a better way to do this but 
let mut pixel_data = Vec::with_capacity(buffer.len()/2);
for i in (0 .. buffer.len()).step_by(2){
       let val = LittleEndian::read_u16(&buffer[i..i+2]);
       pixel_data.push(val);
}
                    
debug!("Converting to dyn image (luma16)");
let img_buf = ImageBuffer::from_raw(payload.width, payload.height, pixel_data).unwrap();
let dyn_img = DynamicImage::ImageLuma16(img_buf);

let mut output_buffer = Cursor::new(buffer);
let encoder = image::codecs::png::PngEncoder::new(&mut output_buffer);
// writing 16 bit lumaA image to buffer"
encoder.write_image(dyn_img.as_bytes(), payload.width, payload.height, image::ColorType::L16)?

img_bytes = output_buffer.into_inner(); ///  my result 

I'm not in love with this solution and it needs some clean up but it's working

from image.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.