image icon indicating copy to clipboard operation
image copied to clipboard

What error variant are encoders supposed to return when they don't support the given color type?

Open RunDevelopment opened this issue 8 months ago • 1 comments

I'm working on integrating the new DDS de/encoder, so I have to handle the case where the encoder doesn't support the ExtendedColorType given by the user. I have to return an error, but which one?

My question is: what error variant am I supposed to return?


I tried looking at what other encoders are doing, but it's a colorful mix. There are 3 main categories:

  1. avif, jpeg, tga, webp, farbfeld, gif, and tiff return ImageError::Unsupported with UnsupportedErrorKind::Color.
  2. hdr, openexr, and qoi return ImageError::Encoding with a simple string error (message).
  3. And the weird ones...
    • bmp returns ImageError::IoError with io::ErrorKind::InvalidInput. (It even creates a new std::io::Error for it...)
    • pnm returns either ImageError::Unsupported with UnsupportedErrorKind::Color OR ImageError::Parameter.
    • png returns either ImageError::Unsupported with UnsupportedErrorKind::Color OR ImageError::Encoding with its custom BadPngRepresentation::ColorType error.

(As for the formats not included above: ico uses the PNG encoder under the hood, and dds and pcx didn't have encoders.)

If I had to guess how this came to be, I'd say that ImageError::Unsupported with UnsupportedErrorKind::Color was the indented error, but then PNG came along with its BadPngRepresentation and that influenced the formats that came after it to use ImageError::Encoding. Oh, and BMP is just weird.

If ImageError::Unsupported with UnsupportedErrorKind::Color really is the intended error, I'm willing to make a PR that (1) documents this fact and (2) changes all encoders to use this error. Just let me know.

RunDevelopment avatar Apr 17 '25 10:04 RunDevelopment

I think that UnsupportedErrorKind::Color is probably best. Though I've also been meaning to double check what the actual printed message from that looks like. I think I recall the output being a bit confusing (not specifying whether encoding or decoding was happening, whether the encoder or the format itself is to blame for lack of support, etc.)

fintelia avatar Apr 20 '25 19:04 fintelia