Skip to content
Merged
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
23 changes: 19 additions & 4 deletions Tests/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,27 @@ def test_font_getmask(bench: BenchmarkFixture, mode: str) -> None:


@pytest.mark.benchmark(group="quantize")
@pytest.mark.parametrize("mode", [m for m in MODES if m in ("L", "RGB", "RGBA")])
@pytest.mark.parametrize(
"mode, method",
[
("L", Image.Quantize.MEDIANCUT),
("L", Image.Quantize.MAXCOVERAGE),
("RGB", Image.Quantize.MEDIANCUT),
("RGB", Image.Quantize.MAXCOVERAGE),
("RGBA", Image.Quantize.FASTOCTREE),
],
ids=lambda p: getattr(p, "name", p),
Comment thread
akx marked this conversation as resolved.
)
@pytest.mark.parametrize("size", SIZES, ids=_format_size)
def test_quantize(bench: BenchmarkFixture, mode: str, size: tuple[int, int]) -> None:
def test_quantize(
bench: BenchmarkFixture,
mode: str,
method: Image.Quantize,
size: tuple[int, int],
) -> None:
im = make_pillow_image(mode, size)
bench.extra_info["label"] = [f"quantize {mode}"]
result = bench(im.quantize, 256)
bench.extra_info["label"] = [f"quantize {mode} {method.name}"]
result = bench(im.quantize, 256, method=method)
assert result.mode == "P"


Expand Down
7 changes: 6 additions & 1 deletion src/libImaging/Quant.c
Original file line number Diff line number Diff line change
Expand Up @@ -1831,10 +1831,15 @@ ImagingQuantize(Imaging im, int colors, int mode, int kmeans) {
}
ImagingSectionEnter(&cookie);

// restrict safe: imOut is a fresh allocation and
// newData was just allocated by the quantizer.
for (i = y = 0; y < ysize; y++) {
UINT8 *restrict out = imOut->image8[y];
const uint32_t *restrict in = newData + i;
for (x = 0; x < xsize; x++) {
imOut->image8[y][x] = (unsigned char)newData[i++];
out[x] = (UINT8)in[x];
}
i += xsize;
}

free(newData);
Expand Down
Loading