Skip to content

Commit

Permalink
Allow configuring JPEG restart marker interval on save
Browse files Browse the repository at this point in the history
libjpeg allows specifying the marker interval either in MCU blocks or in
MCU rows.  Support both, via separate parameters, rather than requiring
callers to do the math.
  • Loading branch information
bgilbert committed Oct 23, 2023
1 parent d05ff50 commit 3e97b28
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,30 @@ def test_save_low_quality_baseline_qtables(self):
assert max(im2.quantization[0]) <= 255
assert max(im2.quantization[1]) <= 255

def test_restart_markers(self):
im = Image.new("RGB", (32, 32)) # 16 MCUs
cases = (
(0, 0, 0),
(1, 0, 15),
(3, 0, 5),
(8, 0, 1),
(0, 1, 3),
(0, 2, 1),
)
for blocks, rows, markers in cases:
out = BytesIO()
im.save(
out, format='JPEG',
restart_marker_blocks=blocks, restart_marker_rows=rows,
# force 8x8 pixel MCUs
subsampling=0
)
found_markers = sum(
len(out.getvalue().split(bytes([0xff, 0xd0 + i]))) - 1
for i in range(8)
)
assert markers == found_markers

@pytest.mark.skipif(not djpeg_available(), reason="djpeg not available")
def test_load_djpeg(self):
with Image.open(TEST_FILE) as img:
Expand Down
12 changes: 12 additions & 0 deletions docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,18 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:

If absent, the setting will be determined by libjpeg or libjpeg-turbo.

**restart_marker_blocks**
If present, emit a restart marker whenever the specified number of MCU
blocks has been produced.

.. versionadded:: 10.2.0

**restart_marker_rows**
If present, emit a restart marker whenever the specified number of MCU
rows has been produced.

.. versionadded:: 10.2.0

**qtables**
If present, sets the qtables for the encoder. This is listed as an
advanced option for wizards in the JPEG documentation. Use with
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ def validate_qtables(qtables):
dpi[0],
dpi[1],
subsampling,
info.get("restart_marker_blocks", 0),
info.get("restart_marker_rows", 0),
qtables,
comment,
extra,
Expand Down
8 changes: 7 additions & 1 deletion src/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,8 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
Py_ssize_t streamtype = 0; /* 0=interchange, 1=tables only, 2=image only */
Py_ssize_t xdpi = 0, ydpi = 0;
Py_ssize_t subsampling = -1; /* -1=default, 0=none, 1=medium, 2=high */
Py_ssize_t restart_marker_blocks = 0;
Py_ssize_t restart_marker_rows = 0;
PyObject *qtables = NULL;
unsigned int *qarrays = NULL;
int qtablesLen = 0;
Expand All @@ -1057,7 +1059,7 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {

if (!PyArg_ParseTuple(
args,
"ss|nnnnnnnnOz#y#y#",
"ss|nnnnnnnnnnOz#y#y#",
&mode,
&rawmode,
&quality,
Expand All @@ -1068,6 +1070,8 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
&xdpi,
&ydpi,
&subsampling,
&restart_marker_blocks,
&restart_marker_rows,
&qtables,
&comment,
&comment_size,
Expand Down Expand Up @@ -1156,6 +1160,8 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
((JPEGENCODERSTATE *)encoder->state.context)->streamtype = streamtype;
((JPEGENCODERSTATE *)encoder->state.context)->xdpi = xdpi;
((JPEGENCODERSTATE *)encoder->state.context)->ydpi = ydpi;
((JPEGENCODERSTATE *)encoder->state.context)->restart_marker_blocks = restart_marker_blocks;
((JPEGENCODERSTATE *)encoder->state.context)->restart_marker_rows = restart_marker_rows;
((JPEGENCODERSTATE *)encoder->state.context)->comment = comment;
((JPEGENCODERSTATE *)encoder->state.context)->comment_size = comment_size;
((JPEGENCODERSTATE *)encoder->state.context)->extra = extra;
Expand Down
4 changes: 4 additions & 0 deletions src/libImaging/Jpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ typedef struct {
/* Chroma Subsampling (-1=default, 0=none, 1=medium, 2=high) */
int subsampling;

/* Restart marker interval, in MCU blocks or MCU rows, or 0 for none */
unsigned int restart_marker_blocks;
int restart_marker_rows;

/* Converter input mode (input to the shuffler) */
char rawmode[8 + 1];

Expand Down
2 changes: 2 additions & 0 deletions src/libImaging/JpegEncode.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
}
context->cinfo.smoothing_factor = context->smooth;
context->cinfo.optimize_coding = (boolean)context->optimize;
context->cinfo.restart_interval = context->restart_marker_blocks;
context->cinfo.restart_in_rows = context->restart_marker_rows;
if (context->xdpi > 0 && context->ydpi > 0) {
context->cinfo.write_JFIF_header = TRUE;
context->cinfo.density_unit = 1; /* dots per inch */
Expand Down

0 comments on commit 3e97b28

Please sign in to comment.