Skip to content

Commit 056dc89

Browse files
authored
Correct drawing I;16 horizontal lines (#8985)
1 parent ff624fe commit 056dc89

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed
0 Bytes
Binary file not shown.

Tests/test_imagedraw.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,10 @@ def test_rectangle_I16(bbox: Coords) -> None:
783783
draw = ImageDraw.Draw(im)
784784

785785
# Act
786-
draw.rectangle(bbox, outline=0xFFFF)
786+
draw.rectangle(bbox, outline=0xCDEF)
787787

788788
# Assert
789+
assert im.getpixel((X0, Y0)) == 0xCDEF
789790
assert_image_equal_tofile(im, "Tests/images/imagedraw_rectangle_I.tiff")
790791

791792

src/libImaging/Draw.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ point32rgba(Imaging im, int x, int y, int ink) {
104104

105105
static inline void
106106
hline8(Imaging im, int x0, int y0, int x1, int ink, Imaging mask) {
107-
int pixelwidth;
108-
109107
if (y0 >= 0 && y0 < im->ysize) {
110108
if (x0 < 0) {
111109
x0 = 0;
@@ -118,20 +116,30 @@ hline8(Imaging im, int x0, int y0, int x1, int ink, Imaging mask) {
118116
x1 = im->xsize - 1;
119117
}
120118
if (x0 <= x1) {
121-
pixelwidth = strncmp(im->mode, "I;16", 4) == 0 ? 2 : 1;
122-
if (mask == NULL) {
123-
memset(
124-
im->image8[y0] + x0 * pixelwidth,
125-
(UINT8)ink,
126-
(x1 - x0 + 1) * pixelwidth
127-
);
119+
int bigendian = -1;
120+
if (strncmp(im->mode, "I;16", 4) == 0) {
121+
bigendian =
122+
(
123+
#ifdef WORDS_BIGENDIAN
124+
strcmp(im->mode, "I;16") == 0 || strcmp(im->mode, "I;16L") == 0
125+
#else
126+
strcmp(im->mode, "I;16B") == 0
127+
#endif
128+
)
129+
? 1
130+
: 0;
131+
}
132+
if (mask == NULL && bigendian == -1) {
133+
memset(im->image8[y0] + x0, (UINT8)ink, (x1 - x0 + 1));
128134
} else {
129135
UINT8 *p = im->image8[y0];
130136
while (x0 <= x1) {
131-
if (mask->image8[y0][x0]) {
132-
p[x0 * pixelwidth] = ink;
133-
if (pixelwidth == 2) {
134-
p[x0 * pixelwidth + 1] = ink;
137+
if (mask == NULL || mask->image8[y0][x0]) {
138+
if (bigendian == -1) {
139+
p[x0] = ink;
140+
} else {
141+
p[x0 * 2 + (bigendian ? 1 : 0)] = ink;
142+
p[x0 * 2 + (bigendian ? 0 : 1)] = ink >> 8;
135143
}
136144
}
137145
x0++;

0 commit comments

Comments
 (0)