You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This problem happens when trying to save a DynamicImage of color type L16 or Rgb16 to any PNM format (.pbm, .pgm, ppm) via its save method.
This is somewhat related with #1687, but it isn't the same because this problem also happens to grayscale images.
Expected
The encoder should be able to recognize that the data coming from the DynamicImage object contains wide samples (2 bytes per sample), and write the image with that in mind. Other encoders such as the PNG encoder can handle this well.
Actual behaviour
The PNM encoder API is funneled to the method write_image, which only accepts a &[u8] as the image buffer, and subsequently interprets the data as flat samples of u8, regardless of the color type in the original dynamic image object. As a consequence, the header dimension check fails with the error DimensionMismatch ("The Image's dimensions are either too small or too large") for finding twice the amount of expected samples.
Reproduction steps
fnmain(){let input = std::env::args().nth(1).unwrap();let output = input.clone() + ".pgm";let img = image::open(input).expect("failed to open");
img.save(&output).expect("failed to save");}
❯ file basi0g16.png
basi0g16.png: PNG image data, 32 x 32, 16-bit grayscale, interlaced
❯ cargo run -- basi0g16.png
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
Running `target\debug\to-pnm.exe basi0g16.png`
thread 'main' panicked at src\main.rs:6:23:
failed to save: Parameter(ParameterError { kind: DimensionMismatch, underlying: None })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\to-pnm.exe basi0g16.png` (exit code: 101)
Any 16-bit grayscale image should work. Here is one from the pngsuite project.
The text was updated successfully, but these errors were encountered:
Enet4
changed the title
Cannot save 16-bit images directly to PNM
Cannot save 16-bit images directly to PNM via save
Feb 26, 2025
The write_image method also takes a ColorType value that indicates the meaning of the samples. The problem is that the buffer length is checked before confirming that the specified color type is supported.
Separately, the encoder could be improved to also support storing 16-bit images.
This problem happens when trying to save a
DynamicImage
of color typeL16
orRgb16
to any PNM format (.pbm
,.pgm
,ppm
) via itssave
method.This is somewhat related with #1687, but it isn't the same because this problem also happens to grayscale images.
Expected
The encoder should be able to recognize that the data coming from the
DynamicImage
object contains wide samples (2 bytes per sample), and write the image with that in mind. Other encoders such as the PNG encoder can handle this well.Actual behaviour
The PNM encoder API is funneled to the method
write_image
, which only accepts a&[u8]
as the image buffer, and subsequently interprets the data as flat samples ofu8
, regardless of the color type in the original dynamic image object. As a consequence, the header dimension check fails with the errorDimensionMismatch
("The Image's dimensions are either too small or too large") for finding twice the amount of expected samples.Reproduction steps
Any 16-bit grayscale image should work. Here is one from the pngsuite project.
The text was updated successfully, but these errors were encountered: