mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-03 19:33:07 +03:00
Simplify code (#8863)
This commit is contained in:
commit
05636dca17
|
@ -134,10 +134,10 @@ def grabclipboard() -> Image.Image | list[str] | None:
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
o = struct.unpack_from("I", data)[0]
|
o = struct.unpack_from("I", data)[0]
|
||||||
if data[16] != 0:
|
if data[16] == 0:
|
||||||
files = data[o:].decode("utf-16le").split("\0")
|
|
||||||
else:
|
|
||||||
files = data[o:].decode("mbcs").split("\0")
|
files = data[o:].decode("mbcs").split("\0")
|
||||||
|
else:
|
||||||
|
files = data[o:].decode("utf-16le").split("\0")
|
||||||
return files[: files.index("")]
|
return files[: files.index("")]
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, bytes):
|
||||||
data = io.BytesIO(data)
|
data = io.BytesIO(data)
|
||||||
|
|
|
@ -762,8 +762,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
extra = info.get("extra", b"")
|
extra = info.get("extra", b"")
|
||||||
|
|
||||||
MAX_BYTES_IN_MARKER = 65533
|
MAX_BYTES_IN_MARKER = 65533
|
||||||
xmp = info.get("xmp")
|
if xmp := info.get("xmp"):
|
||||||
if xmp:
|
|
||||||
overhead_len = 29 # b"http://ns.adobe.com/xap/1.0/\x00"
|
overhead_len = 29 # b"http://ns.adobe.com/xap/1.0/\x00"
|
||||||
max_data_bytes_in_marker = MAX_BYTES_IN_MARKER - overhead_len
|
max_data_bytes_in_marker = MAX_BYTES_IN_MARKER - overhead_len
|
||||||
if len(xmp) > max_data_bytes_in_marker:
|
if len(xmp) > max_data_bytes_in_marker:
|
||||||
|
@ -772,8 +771,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
size = o16(2 + overhead_len + len(xmp))
|
size = o16(2 + overhead_len + len(xmp))
|
||||||
extra += b"\xff\xe1" + size + b"http://ns.adobe.com/xap/1.0/\x00" + xmp
|
extra += b"\xff\xe1" + size + b"http://ns.adobe.com/xap/1.0/\x00" + xmp
|
||||||
|
|
||||||
icc_profile = info.get("icc_profile")
|
if icc_profile := info.get("icc_profile"):
|
||||||
if icc_profile:
|
|
||||||
overhead_len = 14 # b"ICC_PROFILE\0" + o8(i) + o8(len(markers))
|
overhead_len = 14 # b"ICC_PROFILE\0" + o8(i) + o8(len(markers))
|
||||||
max_data_bytes_in_marker = MAX_BYTES_IN_MARKER - overhead_len
|
max_data_bytes_in_marker = MAX_BYTES_IN_MARKER - overhead_len
|
||||||
markers = []
|
markers = []
|
||||||
|
@ -831,7 +829,6 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
# in a shot. Guessing on the size, at im.size bytes. (raw pixel size is
|
# in a shot. Guessing on the size, at im.size bytes. (raw pixel size is
|
||||||
# channels*size, this is a value that's been used in a django patch.
|
# channels*size, this is a value that's been used in a django patch.
|
||||||
# https://github.com/matthewwithanm/django-imagekit/issues/50
|
# https://github.com/matthewwithanm/django-imagekit/issues/50
|
||||||
bufsize = 0
|
|
||||||
if optimize or progressive:
|
if optimize or progressive:
|
||||||
# CMYK can be bigger
|
# CMYK can be bigger
|
||||||
if im.mode == "CMYK":
|
if im.mode == "CMYK":
|
||||||
|
@ -848,7 +845,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
else:
|
else:
|
||||||
# The EXIF info needs to be written as one block, + APP1, + one spare byte.
|
# The EXIF info needs to be written as one block, + APP1, + one spare byte.
|
||||||
# Ensure that our buffer is big enough. Same with the icc_profile block.
|
# Ensure that our buffer is big enough. Same with the icc_profile block.
|
||||||
bufsize = max(bufsize, len(exif) + 5, len(extra) + 1)
|
bufsize = max(len(exif) + 5, len(extra) + 1)
|
||||||
|
|
||||||
ImageFile._save(
|
ImageFile._save(
|
||||||
im, fp, [ImageFile._Tile("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize
|
im, fp, [ImageFile._Tile("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize
|
||||||
|
|
|
@ -101,7 +101,7 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* for now, single block images */
|
/* for now, single block images */
|
||||||
if (!(im->blocks_count == 0 || im->blocks_count == 1)) {
|
if (im->blocks_count > 1) {
|
||||||
return IMAGING_ARROW_MEMORY_LAYOUT;
|
return IMAGING_ARROW_MEMORY_LAYOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ export_single_channel_array(Imaging im, struct ArrowArray *array) {
|
||||||
int length = im->xsize * im->ysize;
|
int length = im->xsize * im->ysize;
|
||||||
|
|
||||||
/* for now, single block images */
|
/* for now, single block images */
|
||||||
if (!(im->blocks_count == 0 || im->blocks_count == 1)) {
|
if (im->blocks_count > 1) {
|
||||||
return IMAGING_ARROW_MEMORY_LAYOUT;
|
return IMAGING_ARROW_MEMORY_LAYOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,7 +202,7 @@ export_fixed_pixel_array(Imaging im, struct ArrowArray *array) {
|
||||||
int length = im->xsize * im->ysize;
|
int length = im->xsize * im->ysize;
|
||||||
|
|
||||||
/* for now, single block images */
|
/* for now, single block images */
|
||||||
if (!(im->blocks_count == 0 || im->blocks_count == 1)) {
|
if (im->blocks_count > 1) {
|
||||||
return IMAGING_ARROW_MEMORY_LAYOUT;
|
return IMAGING_ARROW_MEMORY_LAYOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -439,9 +439,7 @@ draw_horizontal_lines(
|
||||||
* Filled polygon draw function using scan line algorithm.
|
* Filled polygon draw function using scan line algorithm.
|
||||||
*/
|
*/
|
||||||
static inline int
|
static inline int
|
||||||
polygon_generic(
|
polygon_generic(Imaging im, int n, Edge *e, int ink, int eofill, hline_handler hline) {
|
||||||
Imaging im, int n, Edge *e, int ink, int eofill, hline_handler hline, int hasAlpha
|
|
||||||
) {
|
|
||||||
Edge **edge_table;
|
Edge **edge_table;
|
||||||
float *xx;
|
float *xx;
|
||||||
int edge_count = 0;
|
int edge_count = 0;
|
||||||
|
@ -461,6 +459,7 @@ polygon_generic(
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int hasAlpha = hline == hline32rgba;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
if (ymin > e[i].ymin) {
|
if (ymin > e[i].ymin) {
|
||||||
ymin = e[i].ymin;
|
ymin = e[i].ymin;
|
||||||
|
@ -590,21 +589,6 @@ polygon_generic(
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int
|
|
||||||
polygon8(Imaging im, int n, Edge *e, int ink, int eofill) {
|
|
||||||
return polygon_generic(im, n, e, ink, eofill, hline8, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int
|
|
||||||
polygon32(Imaging im, int n, Edge *e, int ink, int eofill) {
|
|
||||||
return polygon_generic(im, n, e, ink, eofill, hline32, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int
|
|
||||||
polygon32rgba(Imaging im, int n, Edge *e, int ink, int eofill) {
|
|
||||||
return polygon_generic(im, n, e, ink, eofill, hline32rgba, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
add_edge(Edge *e, int x0, int y0, int x1, int y1) {
|
add_edge(Edge *e, int x0, int y0, int x1, int y1) {
|
||||||
/* printf("edge %d %d %d %d\n", x0, y0, x1, y1); */
|
/* printf("edge %d %d %d %d\n", x0, y0, x1, y1); */
|
||||||
|
@ -641,12 +625,11 @@ typedef struct {
|
||||||
void (*point)(Imaging im, int x, int y, int ink);
|
void (*point)(Imaging im, int x, int y, int ink);
|
||||||
void (*hline)(Imaging im, int x0, int y0, int x1, int ink);
|
void (*hline)(Imaging im, int x0, int y0, int x1, int ink);
|
||||||
void (*line)(Imaging im, int x0, int y0, int x1, int y1, int ink);
|
void (*line)(Imaging im, int x0, int y0, int x1, int y1, int ink);
|
||||||
int (*polygon)(Imaging im, int n, Edge *e, int ink, int eofill);
|
|
||||||
} DRAW;
|
} DRAW;
|
||||||
|
|
||||||
DRAW draw8 = {point8, hline8, line8, polygon8};
|
DRAW draw8 = {point8, hline8, line8};
|
||||||
DRAW draw32 = {point32, hline32, line32, polygon32};
|
DRAW draw32 = {point32, hline32, line32};
|
||||||
DRAW draw32rgba = {point32rgba, hline32rgba, line32rgba, polygon32rgba};
|
DRAW draw32rgba = {point32rgba, hline32rgba, line32rgba};
|
||||||
|
|
||||||
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
||||||
/* Interface */
|
/* Interface */
|
||||||
|
@ -731,7 +714,7 @@ ImagingDrawWideLine(
|
||||||
add_edge(e + 2, vertices[2][0], vertices[2][1], vertices[3][0], vertices[3][1]);
|
add_edge(e + 2, vertices[2][0], vertices[2][1], vertices[3][0], vertices[3][1]);
|
||||||
add_edge(e + 3, vertices[3][0], vertices[3][1], vertices[0][0], vertices[0][1]);
|
add_edge(e + 3, vertices[3][0], vertices[3][1], vertices[0][0], vertices[0][1]);
|
||||||
|
|
||||||
draw->polygon(im, 4, e, ink, 0);
|
polygon_generic(im, 4, e, ink, 0, draw->hline);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -839,7 +822,7 @@ ImagingDrawPolygon(
|
||||||
if (xy[i * 2] != xy[0] || xy[i * 2 + 1] != xy[1]) {
|
if (xy[i * 2] != xy[0] || xy[i * 2 + 1] != xy[1]) {
|
||||||
add_edge(&e[n++], xy[i * 2], xy[i * 2 + 1], xy[0], xy[1]);
|
add_edge(&e[n++], xy[i * 2], xy[i * 2 + 1], xy[0], xy[1]);
|
||||||
}
|
}
|
||||||
draw->polygon(im, n, e, ink, 0);
|
polygon_generic(im, n, e, ink, 0, draw->hline);
|
||||||
free(e);
|
free(e);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -1989,7 +1972,7 @@ ImagingDrawOutline(
|
||||||
|
|
||||||
DRAWINIT();
|
DRAWINIT();
|
||||||
|
|
||||||
draw->polygon(im, outline->count, outline->edges, ink, 0);
|
polygon_generic(im, outline->count, outline->edges, ink, 0, draw->hline);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user