Skip to content

Commit 2b9cc3c

Browse files
committed
Documented writing your own encoder in C
1 parent a7e8a38 commit 2b9cc3c

File tree

2 files changed

+48
-50
lines changed

2 files changed

+48
-50
lines changed

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

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -328,42 +328,42 @@ The fields are used as follows:
328328
Whether the first line in the image is the top line on the screen (1), or
329329
the bottom line (-1). If omitted, the orientation defaults to 1.
330330

331-
.. _file-decoders:
331+
.. _file-codecs:
332332

333-
Writing Your Own File Decoder in C
334-
==================================
333+
Writing Your Own File Codec in C
334+
================================
335335

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

338-
1. Setup: Pillow looks for a function in the decoder registry, falling
339-
back to a function named ``[decodername]_decoder`` on the internal
340-
core image object. That function is called with the ``args`` tuple
341-
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``.
342342

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

346-
3. Cleanup: If the decoder has registered a cleanup function, it will
347-
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
348348
exception raised.
349349

350350

351351
Setup
352352
-----
353353

354-
The current conventions are that the decoder setup function is named
355-
``PyImaging_[Decodername]DecoderNew`` and defined in ``decode.c``. The
356-
python binding for it is named ``[decodername]_decoder`` and is setup
357-
from within the ``_imaging.c`` file in the codecs section of the
358-
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 setup from
358+
within the ``_imaging.c`` file in the codecs section of the function array.
359359

360-
The setup function needs to call ``PyImaging_DecoderNew`` and at the
361-
very least, set the ``decode`` function pointer. The fields of
362-
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:
363363

364-
**decode**
365-
Function pointer to the decode function, which has access to
366-
``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.
367367

368368
**cleanup**
369369
Function pointer to the cleanup function, has access to ``state``.
@@ -373,35 +373,34 @@ interest in this object are:
373373

374374
**state**
375375
An ImagingCodecStateInstance, will be set by Pillow. The ``context``
376-
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
377377
any format specific state or options.
378378

379-
**pulls_fd**
380-
If set to 1, ``state->fd`` will be a pointer to the Python file like
381-
object. The decoder may use the functions in ``codec_fd.c`` to read
382-
directly from the file like object rather than have the data pushed
383-
through a buffer.
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.
384384

385385
.. versionadded:: 3.3.0
386386

387387

388-
Decoding
389-
--------
388+
Transforming
389+
------------
390390

391-
The decode function is called with the target (core) image, the
392-
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.
393393

394-
It is the decoder's responsibility to pull as much data as possible
395-
out of the buffer and return the number of bytes consumed. The next
396-
call to the decoder will include the previous unconsumed tail. The
397-
decoder function will be called multiple times as the data is read
398-
from the file like object.
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.
399398

400-
Alternatively, if ``pulls_fd`` is set, then the decode function is
401-
called once, with an empty buffer. It is the decoder's responsibility
402-
to decode the entire tile in that one call. Using this will provide a
403-
codec with more freedom, but that freedom may mean increased memory usage
404-
if entire tile is held in memory at once by the codec.
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 entire tile is held in memory at once by the codec.
405404

406405
If an error occurs, set ``state->errcode`` and return -1.
407406

@@ -410,10 +409,9 @@ Return -1 on success, without setting the errcode.
410409
Cleanup
411410
-------
412411

413-
The cleanup function is called after the decoder returns a negative
414-
value, or if there is a read error from the file. This function should
415-
free any allocated memory and release any resources from external
416-
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.
417415

418416
.. _file-codecs-py:
419417

@@ -428,7 +426,7 @@ They should be registered using :py:meth:`PIL.Image.register_decoder` and
428426
the file codecs, there are three stages in the lifetime of a
429427
Python-based file codec:
430428

431-
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
432430
instantiates the class.
433431

434432
2. Transforming: The instance's ``decode`` method is repeatedly called with

src/PIL/Image.py

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

0 commit comments

Comments
 (0)