Skip to content

Commit

Permalink
Added support for I;16 modes in putdata()
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 26, 2022
1 parent 925e27c commit 08816f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Tests/test_image_putdata.py
Expand Up @@ -55,10 +55,11 @@ def test_mode_with_L_with_float():
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")
data = list(src.getdata())
im = Image.new("I", src.size, 0)
im = Image.new(mode, src.size, 0)
im.putdata(data, 2, 256)

target = [2 * elt + 256 for elt in data]
Expand Down
28 changes: 12 additions & 16 deletions src/_imaging.c
Expand Up @@ -1531,25 +1531,21 @@ if (PySequence_Check(op)) { \
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
int endian = strncmp(image->mode, "I;16", 4) == 0 ? (strcmp(image->mode, "I;16B") == 0 ? 2 : 1) : 0;
double value;
if (scale == 1.0 && offset == 0.0) {
/* Clipped data */
for (i = x = y = 0; i < n; i++) {
set_value_to_item(seq, i);
for (i = x = y = 0; i < n; i++) {
set_value_to_item(seq, i);
if (scale != 1.0 || offset != 0.0) {
value = value * scale + offset;
}
if (endian == 0) {
image->image8[y][x] = (UINT8)CLIP8(value);
if (++x >= (int)image->xsize) {
x = 0, y++;
}
} else {
image->image8[y][x * 2 + (endian == 2 ? 1 : 0)] = CLIP8((int)value % 256);
image->image8[y][x * 2 + (endian == 2 ? 0 : 1)] = CLIP8((int)value >> 8);
}

} else {
/* Scaled and clipped data */
for (i = x = y = 0; i < n; i++) {
set_value_to_item(seq, i);
image->image8[y][x] = CLIP8(value * scale + offset);
if (++x >= (int)image->xsize) {
x = 0, y++;
}
if (++x >= (int)image->xsize) {
x = 0, y++;
}
}
PyErr_Clear(); /* Avoid weird exceptions */
Expand Down

0 comments on commit 08816f4

Please sign in to comment.