kornelski / lodepng-rust

All-in-one PNG image encoder/decoder in pure Rust
https://lib.rs/lodepng
zlib License
100 stars 23 forks source link

Wrong encoding of text keys #56

Open moxian opened 2 years ago

moxian commented 2 years ago

Problem: png's encoded by lodepng seem to lack text_keys, as seen by the png crate and a certain non-rust closed-source application that i care about. Given png's very strongly worded claims of standard compliance (and that png-produced images are recognized by the aforementioned non-rust application, whereas the lodepng ones are not), i assume the fault lies with lodepng. Sadly i have not delved deep enough into the standard to provide any more specifics.

Code demonstrating the issue: ```rust extern crate lodepng; extern crate png; fn main() { let image = vec![255u8, 0, 0, 255]; // single red pixel let mut encoder = lodepng::Encoder::new(); encoder.info_png_mut().add_text("hello", "world").unwrap(); let encoded_image = encoder.encode(&image, 1, 1).unwrap(); // parse it back { println!("\n -- lodepng view of the metadata:"); let mut decoder = lodepng::Decoder::new(); let decoded = decoder.decode(&encoded_image).unwrap(); // displays keys properly println!("the following text keys are present:"); for (k, v) in decoder.info_png().text_keys() { println!( "{:?} - {:?}", std::str::from_utf8(k), std::str::from_utf8(v), ) } } { println!("\n -- png view of the metadata:"); let decoder = png::Decoder::new(encoded_image.as_slice()); let mut r = decoder.read_info().unwrap(); println!("{:?}", r.info()); // displays nothing println!( " compressed text: {:?}\n uncompressed text: {:?}", r.info().compressed_latin1_text, r.info().uncompressed_latin1_text ); } } ``` Output: ``` -- lodepng view of the metadata: the following text keys are present: Ok("hello") - Ok("world") -- png view of the metadata: Info { width: 1, height: 1, bit_depth: Eight, color_type: Rgb, interlaced: false, trns: None, pixel_dims: None, palette: None, gama_chunk: None, chrm_chunk: None, frame_control: None, animation_control: None, compression: Fast, source_gamma: None, source_chromaticities: None, srgb: None, icc_profile: None, uncompressed_latin1_text: [], compressed_latin1_text: [], utf8_text: [] } compressed text: [] uncompressed text: [] ```
kornelski commented 2 years ago

Have you checked the utf8_text field?

moxian commented 2 years ago

Yes, i have. It's empty as well (again, as per png crate; i have not inspected the encoded bytes). Playing with encoder.set_text_compression() does not change things either.