RFC: Pure Python Decoder objects by wiredfool · Pull Request #1938 · python-pillow/Pillow

File format decoders are currently complicated to put together.

  1. There's no registry, we're just doing a getattr on the image.core object. This PR adds a registry, then falls back to the current method.
  2. The decoders are all in C, a fast but unsafe language. Python is significantly slower, but safe. This is a tradeoff that we should be able to make. (in either direction)
  3. Currently, if we do implement a decoder in python, the load phase is stuffed into the open phase. This breaks the convention of open being a relatively quick operation and by deferring the heavy work to the load phase. As an added bonus, parsing errors here will no longer lead to "cannot identify image file" errors.

This pr builds on the Python fd changes from #1934, but the underlying concept doesn't need to rely on that. It's still likely that the interface chosen there will change, requiring changes here.

Changes proposed in this pull request:

  • Add Image.register_decoder and Image.register_encoder to map decoder name -> a callable that returns a de/encoder.
  • Adds the ImageFile.PyDecoder class that implements the interface used by the C layer decoder object. (Undone, an encoder object)
  • Refactors the DDS image Plugin to use the new registry and decoder class.