Skip to content

Commit 05636dc

Browse files
authored
Simplify code (#8863)
2 parents 8e5a15b + ef1f90f commit 05636dc

File tree

4 files changed

+17
-37
lines changed

4 files changed

+17
-37
lines changed

src/PIL/ImageGrab.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ def grabclipboard() -> Image.Image | list[str] | None:
134134
import struct
135135

136136
o = struct.unpack_from("I", data)[0]
137-
if data[16] != 0:
138-
files = data[o:].decode("utf-16le").split("\0")
139-
else:
137+
if data[16] == 0:
140138
files = data[o:].decode("mbcs").split("\0")
139+
else:
140+
files = data[o:].decode("utf-16le").split("\0")
141141
return files[: files.index("")]
142142
if isinstance(data, bytes):
143143
data = io.BytesIO(data)

src/PIL/JpegImagePlugin.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,7 @@ def validate_qtables(
762762
extra = info.get("extra", b"")
763763

764764
MAX_BYTES_IN_MARKER = 65533
765-
xmp = info.get("xmp")
766-
if xmp:
765+
if xmp := info.get("xmp"):
767766
overhead_len = 29 # b"http://ns.adobe.com/xap/1.0/\x00"
768767
max_data_bytes_in_marker = MAX_BYTES_IN_MARKER - overhead_len
769768
if len(xmp) > max_data_bytes_in_marker:
@@ -772,8 +771,7 @@ def validate_qtables(
772771
size = o16(2 + overhead_len + len(xmp))
773772
extra += b"\xff\xe1" + size + b"http://ns.adobe.com/xap/1.0/\x00" + xmp
774773

775-
icc_profile = info.get("icc_profile")
776-
if icc_profile:
774+
if icc_profile := info.get("icc_profile"):
777775
overhead_len = 14 # b"ICC_PROFILE\0" + o8(i) + o8(len(markers))
778776
max_data_bytes_in_marker = MAX_BYTES_IN_MARKER - overhead_len
779777
markers = []
@@ -831,7 +829,6 @@ def validate_qtables(
831829
# in a shot. Guessing on the size, at im.size bytes. (raw pixel size is
832830
# channels*size, this is a value that's been used in a django patch.
833831
# https://github.com/matthewwithanm/django-imagekit/issues/50
834-
bufsize = 0
835832
if optimize or progressive:
836833
# CMYK can be bigger
837834
if im.mode == "CMYK":
@@ -848,7 +845,7 @@ def validate_qtables(
848845
else:
849846
# The EXIF info needs to be written as one block, + APP1, + one spare byte.
850847
# Ensure that our buffer is big enough. Same with the icc_profile block.
851-
bufsize = max(bufsize, len(exif) + 5, len(extra) + 1)
848+
bufsize = max(len(exif) + 5, len(extra) + 1)
852849

853850
ImageFile._save(
854851
im, fp, [ImageFile._Tile("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize

src/libImaging/Arrow.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
101101
}
102102

103103
/* for now, single block images */
104-
if (!(im->blocks_count == 0 || im->blocks_count == 1)) {
104+
if (im->blocks_count > 1) {
105105
return IMAGING_ARROW_MEMORY_LAYOUT;
106106
}
107107

@@ -159,7 +159,7 @@ export_single_channel_array(Imaging im, struct ArrowArray *array) {
159159
int length = im->xsize * im->ysize;
160160

161161
/* for now, single block images */
162-
if (!(im->blocks_count == 0 || im->blocks_count == 1)) {
162+
if (im->blocks_count > 1) {
163163
return IMAGING_ARROW_MEMORY_LAYOUT;
164164
}
165165

@@ -202,7 +202,7 @@ export_fixed_pixel_array(Imaging im, struct ArrowArray *array) {
202202
int length = im->xsize * im->ysize;
203203

204204
/* for now, single block images */
205-
if (!(im->blocks_count == 0 || im->blocks_count == 1)) {
205+
if (im->blocks_count > 1) {
206206
return IMAGING_ARROW_MEMORY_LAYOUT;
207207
}
208208

src/libImaging/Draw.c

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,7 @@ draw_horizontal_lines(
439439
* Filled polygon draw function using scan line algorithm.
440440
*/
441441
static inline int
442-
polygon_generic(
443-
Imaging im, int n, Edge *e, int ink, int eofill, hline_handler hline, int hasAlpha
444-
) {
442+
polygon_generic(Imaging im, int n, Edge *e, int ink, int eofill, hline_handler hline) {
445443
Edge **edge_table;
446444
float *xx;
447445
int edge_count = 0;
@@ -461,6 +459,7 @@ polygon_generic(
461459
return -1;
462460
}
463461

462+
int hasAlpha = hline == hline32rgba;
464463
for (i = 0; i < n; i++) {
465464
if (ymin > e[i].ymin) {
466465
ymin = e[i].ymin;
@@ -590,21 +589,6 @@ polygon_generic(
590589
return 0;
591590
}
592591

593-
static inline int
594-
polygon8(Imaging im, int n, Edge *e, int ink, int eofill) {
595-
return polygon_generic(im, n, e, ink, eofill, hline8, 0);
596-
}
597-
598-
static inline int
599-
polygon32(Imaging im, int n, Edge *e, int ink, int eofill) {
600-
return polygon_generic(im, n, e, ink, eofill, hline32, 0);
601-
}
602-
603-
static inline int
604-
polygon32rgba(Imaging im, int n, Edge *e, int ink, int eofill) {
605-
return polygon_generic(im, n, e, ink, eofill, hline32rgba, 1);
606-
}
607-
608592
static inline void
609593
add_edge(Edge *e, int x0, int y0, int x1, int y1) {
610594
/* printf("edge %d %d %d %d\n", x0, y0, x1, y1); */
@@ -641,12 +625,11 @@ typedef struct {
641625
void (*point)(Imaging im, int x, int y, int ink);
642626
void (*hline)(Imaging im, int x0, int y0, int x1, int ink);
643627
void (*line)(Imaging im, int x0, int y0, int x1, int y1, int ink);
644-
int (*polygon)(Imaging im, int n, Edge *e, int ink, int eofill);
645628
} DRAW;
646629

647-
DRAW draw8 = {point8, hline8, line8, polygon8};
648-
DRAW draw32 = {point32, hline32, line32, polygon32};
649-
DRAW draw32rgba = {point32rgba, hline32rgba, line32rgba, polygon32rgba};
630+
DRAW draw8 = {point8, hline8, line8};
631+
DRAW draw32 = {point32, hline32, line32};
632+
DRAW draw32rgba = {point32rgba, hline32rgba, line32rgba};
650633

651634
/* -------------------------------------------------------------------- */
652635
/* Interface */
@@ -731,7 +714,7 @@ ImagingDrawWideLine(
731714
add_edge(e + 2, vertices[2][0], vertices[2][1], vertices[3][0], vertices[3][1]);
732715
add_edge(e + 3, vertices[3][0], vertices[3][1], vertices[0][0], vertices[0][1]);
733716

734-
draw->polygon(im, 4, e, ink, 0);
717+
polygon_generic(im, 4, e, ink, 0, draw->hline);
735718
}
736719
return 0;
737720
}
@@ -839,7 +822,7 @@ ImagingDrawPolygon(
839822
if (xy[i * 2] != xy[0] || xy[i * 2 + 1] != xy[1]) {
840823
add_edge(&e[n++], xy[i * 2], xy[i * 2 + 1], xy[0], xy[1]);
841824
}
842-
draw->polygon(im, n, e, ink, 0);
825+
polygon_generic(im, n, e, ink, 0, draw->hline);
843826
free(e);
844827

845828
} else {
@@ -1989,7 +1972,7 @@ ImagingDrawOutline(
19891972

19901973
DRAWINIT();
19911974

1992-
draw->polygon(im, outline->count, outline->edges, ink, 0);
1975+
polygon_generic(im, outline->count, outline->edges, ink, 0, draw->hline);
19931976

19941977
return 0;
19951978
}

0 commit comments

Comments
 (0)