docs(encoder): correct write_image panic condition for ExtendedColorType#2982
Conversation
|
Thanks for the PR! I'm not sure if this is the best approach. It's correct but also duplicates information. For example, if I said, "The I think it would be better to say "Panics if |
|
Good point on the duplication risk. If you'd like, I can make |
|
We haven't made it public previously out of concern that the interpretation of it as bits-per-line times height is not sensible for all of them, and that we might silently be incorrect / have unexpected results in some cases. Consider for instance a block layout with a minimum alignment may want to round up to 4 byte boundaries instead of just the byte. (On WebGPU the row alignment would be as high as 256). However, this implies that the documentation needs more fixing. Making it public with an explicit mention of the computation's rounding mode being to the nearest byte and nothing else would be my preferred way to see this handled. We can always add different calculation methods later (and with the new metadata in the decoding pipeline a decoder may choose this appropriately). |
…ge panic Signed-off-by: say <say.apm35@gmail.com>
|
Made |
Fixes #2425.
The
# Panicsdoc onImageEncoder::write_imageand the per-codecencodemethods statedPanics if width * height * color_type.bytes_per_pixel() != buf.len(). These functions take anExtendedColorType, which has nobytes_per_pixel()method (onlybits_per_pixel()), and the formula is wrong for sub-byte color types likeL1.This corrects the documented condition to match the actual requirement: the buffer must hold exactly the bytes for the given dimensions and color type, with each row padded to a whole number of bytes (
height * ((width * bits_per_pixel + 7) / 8)), which is whatExtendedColorType::buffer_sizealready computes.As noted in the issue, the same stale text was copied across several files; updated all six occurrences in
io/encoder.rs,codecs/tiff.rs,codecs/jpeg/encoder.rs,codecs/tga/encoder.rs,codecs/webp/encoder.rs, andcodecs/bmp/encoder.rs.cargo doc --no-deps --libbuilds with no new warnings.