Skip to content

Commit 397a940

Browse files
authored
Merge pull request #6094 from radarhere/decoder
Improved codec documentation
2 parents c16737d + 8e9d320 commit 397a940

File tree

3 files changed

+79
-54
lines changed

3 files changed

+79
-54
lines changed

docs/handbook/writing-your-own-image-plugin.rst

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ The ``tile`` attribute
123123
To be able to read the file as well as just identifying it, the ``tile``
124124
attribute must also be set. This attribute consists of a list of tile
125125
descriptors, where each descriptor specifies how data should be loaded to a
126-
given region in the image. In most cases, only a single descriptor is used,
127-
covering the full image.
126+
given region in the image.
127+
128+
In most cases, only a single descriptor is used, covering the full image.
129+
:py:class:`.PsdImagePlugin.PsdImageFile` uses multiple tiles to combine
130+
channels within a single layer, given that the channels are stored separately,
131+
one after the other.
128132

129133
The tile descriptor is a 4-tuple with the following contents::
130134

@@ -324,42 +328,42 @@ The fields are used as follows:
324328
Whether the first line in the image is the top line on the screen (1), or
325329
the bottom line (-1). If omitted, the orientation defaults to 1.
326330

327-
.. _file-decoders:
331+
.. _file-codecs:
328332

329-
Writing Your Own File Decoder in C
330-
==================================
333+
Writing Your Own File Codec in C
334+
================================
331335

332-
There are 3 stages in a file decoder's lifetime:
336+
There are 3 stages in a file codec's lifetime:
333337

334-
1. Setup: Pillow looks for a function in the decoder registry, falling
335-
back to a function named ``[decodername]_decoder`` on the internal
336-
core image object. That function is called with the ``args`` tuple
337-
from the ``tile`` setup in the ``_open`` method.
338+
1. Setup: Pillow looks for a function in the decoder or encoder registry,
339+
falling back to a function named ``[codecname]_decoder`` or
340+
``[codecname]_encoder`` on the internal core image object. That function is
341+
called with the ``args`` tuple from the ``tile``.
338342

339-
2. Decoding: The decoder's decode function is repeatedly called with
340-
chunks of image data.
343+
2. Transforming: The codec's ``decode`` or ``encode`` function is repeatedly
344+
called with chunks of image data.
341345

342-
3. Cleanup: If the decoder has registered a cleanup function, it will
343-
be called at the end of the decoding process, even if there was an
346+
3. Cleanup: If the codec has registered a cleanup function, it will
347+
be called at the end of the transformation process, even if there was an
344348
exception raised.
345349

346350

347351
Setup
348352
-----
349353

350-
The current conventions are that the decoder setup function is named
351-
``PyImaging_[Decodername]DecoderNew`` and defined in ``decode.c``. The
352-
python binding for it is named ``[decodername]_decoder`` and is setup
353-
from within the ``_imaging.c`` file in the codecs section of the
354-
function array.
354+
The current conventions are that the codec setup function is named
355+
``PyImaging_[codecname]DecoderNew`` or ``PyImaging_[codecname]EncoderNew``
356+
and defined in ``decode.c`` or ``encode.c``. The Python binding for it is
357+
named ``[codecname]_decoder`` or ``[codecname]_encoder`` and is set up from
358+
within the ``_imaging.c`` file in the codecs section of the function array.
355359

356-
The setup function needs to call ``PyImaging_DecoderNew`` and at the
357-
very least, set the ``decode`` function pointer. The fields of
358-
interest in this object are:
360+
The setup function needs to call ``PyImaging_DecoderNew`` or
361+
``PyImaging_EncoderNew`` and at the very least, set the ``decode`` or
362+
``encode`` function pointer. The fields of interest in this object are:
359363

360-
**decode**
361-
Function pointer to the decode function, which has access to
362-
``im``, ``state``, and the buffer of data to be added to the image.
364+
**decode**/**encode**
365+
Function pointer to the decode or encode function, which has access to
366+
``im``, ``state``, and the buffer of data to be transformed.
363367

364368
**cleanup**
365369
Function pointer to the cleanup function, has access to ``state``.
@@ -369,36 +373,34 @@ interest in this object are:
369373

370374
**state**
371375
An ImagingCodecStateInstance, will be set by Pillow. The ``context``
372-
member is an opaque struct that can be used by the decoder to store
376+
member is an opaque struct that can be used by the codec to store
373377
any format specific state or options.
374378

375-
**pulls_fd**
376-
**EXPERIMENTAL** -- **WARNING**, interface may change. If set to 1,
377-
``state->fd`` will be a pointer to the Python file like object. The
378-
decoder may use the functions in ``codec_fd.c`` to read directly
379-
from the file like object rather than have the data pushed through a
380-
buffer. Note that this implementation may be refactored until this
381-
warning is removed.
379+
**pulls_fd**/**pushes_fd**
380+
If the decoder has ``pulls_fd`` or the encoder has ``pushes_fd`` set to 1,
381+
``state->fd`` will be a pointer to the Python file like object. The codec may
382+
use the functions in ``codec_fd.c`` to read or write directly with the file
383+
like object rather than have the data pushed through a buffer.
382384

383385
.. versionadded:: 3.3.0
384386

385387

386-
Decoding
387-
--------
388+
Transforming
389+
------------
388390

389-
The decode function is called with the target (core) image, the
390-
decoder state structure, and a buffer of data to be decoded.
391+
The decode or encode function is called with the target (core) image, the codec
392+
state structure, and a buffer of data to be transformed.
391393

392-
**Experimental** -- If ``pulls_fd`` is set, then the decode function
393-
is called once, with an empty buffer. It is the decoder's
394-
responsibility to decode the entire tile in that one call. The rest of
395-
this section only applies if ``pulls_fd`` is not set.
394+
It is the codec's responsibility to pull as much data as possible out of the
395+
buffer and return the number of bytes consumed. The next call to the codec will
396+
include the previous unconsumed tail. The codec function will be called
397+
multiple times as the data processed.
396398

397-
It is the decoder's responsibility to pull as much data as possible
398-
out of the buffer and return the number of bytes consumed. The next
399-
call to the decoder will include the previous unconsumed tail. The
400-
decoder function will be called multiple times as the data is read
401-
from the file like object.
399+
Alternatively, if ``pulls_fd`` or ``pushes_fd`` is set, then the decode or
400+
encode function is called once, with an empty buffer. It is the codec's
401+
responsibility to transform the entire tile in that one call. Using this will
402+
provide a codec with more freedom, but that freedom may mean increased memory
403+
usage if the entire tile is held in memory at once by the codec.
402404

403405
If an error occurs, set ``state->errcode`` and return -1.
404406

@@ -407,10 +409,9 @@ Return -1 on success, without setting the errcode.
407409
Cleanup
408410
-------
409411

410-
The cleanup function is called after the decoder returns a negative
411-
value, or if there is a read error from the file. This function should
412-
free any allocated memory and release any resources from external
413-
libraries.
412+
The cleanup function is called after the codec returns a negative
413+
value, or if there is an error. This function should free any allocated
414+
memory and release any resources from external libraries.
414415

415416
.. _file-codecs-py:
416417

@@ -425,11 +426,32 @@ They should be registered using :py:meth:`PIL.Image.register_decoder` and
425426
the file codecs, there are three stages in the lifetime of a
426427
Python-based file codec:
427428

428-
1. Setup: Pillow looks for the decoder in the registry, then
429+
1. Setup: Pillow looks for the codec in the decoder or encoder registry, then
429430
instantiates the class.
430431

431432
2. Transforming: The instance's ``decode`` method is repeatedly called with
432433
a buffer of data to be interpreted, or the ``encode`` method is repeatedly
433434
called with the size of data to be output.
434435

435-
3. Cleanup: The instance's ``cleanup`` method is called.
436+
Alternatively, if the decoder's ``_pulls_fd`` property (or the encoder's
437+
``_pushes_fd`` property) is set to ``True``, then ``decode`` and ``encode``
438+
will only be called once. In the decoder, ``self.fd`` can be used to access
439+
the file-like object. Using this will provide a codec with more freedom, but
440+
that freedom may mean increased memory usage if entire file is held in
441+
memory at once by the codec.
442+
443+
In ``decode``, once the data has been interpreted, ``set_as_raw`` can be
444+
used to populate the image.
445+
446+
3. Cleanup: The instance's ``cleanup`` method is called once the transformation
447+
is complete. This can be used to clean up any resources used by the codec.
448+
449+
If you set ``_pulls_fd`` or ``_pushes_fd`` to ``True`` however, then you
450+
probably chose to perform any cleanup tasks at the end of ``decode`` or
451+
``encode``.
452+
453+
For an example :py:class:`PIL.ImageFile.PyDecoder`, see `DdsImagePlugin
454+
<https://github.com/python-pillow/Pillow/blob/main/docs/example/DdsImagePlugin.py>`_.
455+
For a plugin that uses both :py:class:`PIL.ImageFile.PyDecoder` and
456+
:py:class:`PIL.ImageFile.PyEncoder`, see `BlpImagePlugin
457+
<https://github.com/python-pillow/Pillow/blob/main/src/PIL/BlpImagePlugin.py>`_

src/PIL/Image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,9 +2780,9 @@ def frombytes(mode, size, data, decoder_name="raw", *args):
27802780
In its simplest form, this function takes three arguments
27812781
(mode, size, and unpacked pixel data).
27822782
2783-
You can also use any pixel decoder supported by PIL. For more
2783+
You can also use any pixel decoder supported by PIL. For more
27842784
information on available decoders, see the section
2785-
:ref:`Writing Your Own File Decoder <file-decoders>`.
2785+
:ref:`Writing Your Own File Codec <file-codecs>`.
27862786
27872787
Note that this function decodes pixel data only, not entire images.
27882788
If you have an entire image in a string, wrap it in a

src/PIL/ImageFile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,9 @@ def encode(self, bufsize):
718718

719719
def encode_to_pyfd(self):
720720
"""
721+
If ``pushes_fd`` is ``True``, then this method will be used,
722+
and ``encode()`` will only be called once.
723+
721724
:returns: A tuple of ``(bytes consumed, errcode)``.
722725
Err codes are from :data:`.ImageFile.ERRORS`.
723726
"""

0 commit comments

Comments
 (0)