From d9c16c3db09d58b017a0fb027038f991e87f15e2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 4 Jul 2023 22:34:12 +1000 Subject: [PATCH] Corrected drawing I;16 points --- Tests/images/imagedraw_rectangle_I.png | Bin 181 -> 186 bytes Tests/test_imagedraw.py | 12 ++++++++++++ src/libImaging/Draw.c | 26 +++++++++++++++++-------- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Tests/images/imagedraw_rectangle_I.png b/Tests/images/imagedraw_rectangle_I.png index 4e94f6943dd8eaac1d3be2b930b835358f1caaf9..cbc9cb7b37297265354902d748071721b4d3e542 100644 GIT binary patch delta 116 zcmdnWxQlUuie00pi(^Q|oVT|&@*XzeVQ{oQQ8w%P?^K3xrlgmNcfPXxwC!hR0D>FO z=EzGv{-1s)_WEb*uoj>9KWqw*@=Z+Dnb^RhG3$Cu&2#r-ldf-OA29L& delta 121 zcmdnRxRr5&ifxUji(^Q|oVT}j@-{f|FgQAtxbOYq&gjx!5tgC0{mofhUj`sJkpArB z#N6Hgdp6hB*Bq5rd~7{U{oIeHiTTQgQi`TdSuIyMRtg3wFL`FJ_t%n^>bP0l+XkK6nHTy diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 7497fdc66a8..2915ef6b29a 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -586,6 +586,18 @@ def test_point(points): assert_image_equal_tofile(im, "Tests/images/imagedraw_point.png") +def test_point_I16(): + # Arrange + im = Image.new("I;16", (1, 1)) + draw = ImageDraw.Draw(im) + + # Act + draw.point((0, 0), fill=0x1234) + + # Assert + assert im.getpixel((0, 0)) == 0x1234 + + @pytest.mark.parametrize("points", POINTS) def test_polygon(points): # Arrange diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index 82f290bd0cd..0ccf22d58dd 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -41,6 +41,7 @@ #define FLOOR(v) ((v) >= 0.0 ? (int)(v) : (int)floor(v)) #define INK8(ink) (*(UINT8 *)ink) +#define INK16(ink) (*(UINT16 *)ink) /* * Rounds around zero (up=away from zero, down=towards zero) @@ -68,8 +69,13 @@ static inline void point8(Imaging im, int x, int y, int ink) { if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) { if (strncmp(im->mode, "I;16", 4) == 0) { - im->image8[y][x * 2] = (UINT8)ink; +#ifdef WORDS_BIGENDIAN + im->image8[y][x * 2] = (UINT8)(ink >> 8); im->image8[y][x * 2 + 1] = (UINT8)ink; +#else + im->image8[y][x * 2] = (UINT8)ink; + im->image8[y][x * 2 + 1] = (UINT8)(ink >> 8); +#endif } else { im->image8[y][x] = (UINT8)ink; } @@ -631,13 +637,17 @@ DRAW draw32rgba = {point32rgba, hline32rgba, line32rgba, polygon32rgba}; /* Interface */ /* -------------------------------------------------------------------- */ -#define DRAWINIT() \ - if (im->image8) { \ - draw = &draw8; \ - ink = INK8(ink_); \ - } else { \ - draw = (op) ? &draw32rgba : &draw32; \ - memcpy(&ink, ink_, sizeof(ink)); \ +#define DRAWINIT() \ + if (im->image8) { \ + draw = &draw8; \ + if (strncmp(im->mode, "I;16", 4) == 0) { \ + ink = INK16(ink_); \ + } else { \ + ink = INK8(ink_); \ + } \ + } else { \ + draw = (op) ? &draw32rgba : &draw32; \ + memcpy(&ink, ink_, sizeof(ink)); \ } int