Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Tests/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,15 @@ def test_quantize_to_palette(
result = bench(lambda: im._new(im.im.convert(output_mode, dither, palette.im)))
assert result.mode == output_mode
benchmark_save(result)


@pytest.mark.benchmark

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other instance in Tests/benchmarks.py has a group=.... You didn't want to use one here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions for a group name? I didn't have one :)

@pytest.mark.parametrize("mode", MODES)
@pytest.mark.parametrize("size", SIZES, ids=_format_size)
def test_get_flattened_data(
bench: BenchmarkFixture,
mode: str,
size: tuple[int, int],
) -> None:
im = make_pillow_image(mode, size)
bench(im.get_flattened_data)
29 changes: 24 additions & 5 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,27 @@ float16tofloat32(const FLOAT16 in) {
return out[0];
}

static inline PyObject *
make_pixel_tuple(const UINT8 *b, Py_ssize_t bands) {
PyObject *tuple = PyTuple_New(bands);
if (tuple == NULL) {
return NULL;
}
for (Py_ssize_t i = 0; i < bands; i++) {
PyObject *v = PyLong_FromLong(b[i]);
if (v == NULL) {
Py_DECREF(tuple);
return NULL;
}
PyTuple_SET_ITEM(tuple, i, v);
}
// We know these tuples will only have small integers,
// so we can tell the garbage collector to not look inside
// for cycles.
PyObject_GC_UnTrack(tuple);
return tuple;
}

static inline PyObject *
getpixel(Imaging im, ImagingAccess access, int x, int y) {
union {
Expand Down Expand Up @@ -552,13 +573,11 @@ getpixel(Imaging im, ImagingAccess access, int x, int y) {
case 1:
return PyLong_FromLong(pixel.b[0]);
case 2:
return Py_BuildValue("BB", pixel.b[0], pixel.b[1]);
return make_pixel_tuple(pixel.b, 2);
case 3:
return Py_BuildValue("BBB", pixel.b[0], pixel.b[1], pixel.b[2]);
return make_pixel_tuple(pixel.b, 3);
case 4:
return Py_BuildValue(
"BBBB", pixel.b[0], pixel.b[1], pixel.b[2], pixel.b[3]
);
return make_pixel_tuple(pixel.b, 4);
}
break;
case IMAGING_TYPE_INT32:
Expand Down
Loading