Added support for I;16 modes in putdata()

This commit is contained in:
Andrew Murray 2022-12-26 15:46:14 +11:00
parent 925e27c77f
commit 08816f43ae
2 changed files with 16 additions and 19 deletions

View File

@ -55,10 +55,11 @@ def test_mode_with_L_with_float():
assert im.getpixel((0, 0)) == 2 assert im.getpixel((0, 0)) == 2
def test_mode_i(): @pytest.mark.parametrize("mode", ("I", "I;16", "I;16L", "I;16B"))
def test_mode_i(mode):
src = hopper("L") src = hopper("L")
data = list(src.getdata()) data = list(src.getdata())
im = Image.new("I", src.size, 0) im = Image.new(mode, src.size, 0)
im.putdata(data, 2, 256) im.putdata(data, 2, 256)
target = [2 * elt + 256 for elt in data] target = [2 * elt + 256 for elt in data]

View File

@ -1531,25 +1531,21 @@ if (PySequence_Check(op)) { \
PyErr_SetString(PyExc_TypeError, must_be_sequence); PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL; return NULL;
} }
int endian = strncmp(image->mode, "I;16", 4) == 0 ? (strcmp(image->mode, "I;16B") == 0 ? 2 : 1) : 0;
double value; double value;
if (scale == 1.0 && offset == 0.0) { for (i = x = y = 0; i < n; i++) {
/* Clipped data */ set_value_to_item(seq, i);
for (i = x = y = 0; i < n; i++) { if (scale != 1.0 || offset != 0.0) {
set_value_to_item(seq, i); value = value * scale + offset;
image->image8[y][x] = (UINT8)CLIP8(value);
if (++x >= (int)image->xsize) {
x = 0, y++;
}
} }
if (endian == 0) {
} else { image->image8[y][x] = (UINT8)CLIP8(value);
/* Scaled and clipped data */ } else {
for (i = x = y = 0; i < n; i++) { image->image8[y][x * 2 + (endian == 2 ? 1 : 0)] = CLIP8((int)value % 256);
set_value_to_item(seq, i); image->image8[y][x * 2 + (endian == 2 ? 0 : 1)] = CLIP8((int)value >> 8);
image->image8[y][x] = CLIP8(value * scale + offset); }
if (++x >= (int)image->xsize) { if (++x >= (int)image->xsize) {
x = 0, y++; x = 0, y++;
}
} }
} }
PyErr_Clear(); /* Avoid weird exceptions */ PyErr_Clear(); /* Avoid weird exceptions */