|
37 | 37 | import platform |
38 | 38 | import sys |
39 | 39 |
|
| 40 | +from . import _convert |
| 41 | + |
40 | 42 | if platform.system() == 'Windows': |
41 | 43 | _lib = cdll.LoadLibrary('libopenslide-0.dll') |
42 | 44 | elif platform.system() == 'Darwin': |
|
57 | 59 | else: |
58 | 60 | _lib = cdll.LoadLibrary('libopenslide.so.0') |
59 | 61 |
|
60 | | -try: |
61 | | - from . import _convert |
62 | | - def _load_image(buf, size): |
63 | | - '''buf must be a mutable buffer.''' |
64 | | - _convert.argb2rgba(buf) |
65 | | - return PIL.Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1) |
66 | | -except ImportError: |
67 | | - def _load_image(buf, size): |
68 | | - '''buf must be a buffer.''' |
69 | | - |
70 | | - # Load entire buffer at once if possible |
71 | | - MAX_PIXELS_PER_LOAD = (1 << 29) - 1 |
72 | | - # Otherwise, use chunks smaller than the maximum to reduce memory |
73 | | - # requirements |
74 | | - PIXELS_PER_LOAD = 1 << 26 |
75 | | - |
76 | | - def do_load(buf, size): |
77 | | - '''buf can be a string, but should be a ctypes buffer to avoid an |
78 | | - extra copy in the caller.''' |
79 | | - # First reorder the bytes in a pixel from native-endian aRGB to |
80 | | - # big-endian RGBa to work around limitations in RGBa loader |
81 | | - rawmode = (sys.byteorder == 'little') and 'BGRA' or 'ARGB' |
82 | | - buf = PIL.Image.frombuffer('RGBA', size, buf, 'raw', rawmode, 0, 1) |
83 | | - # Image.tobytes() is named tostring() in Pillow 1.x and PIL |
84 | | - buf = (getattr(buf, 'tobytes', None) or buf.tostring)() |
85 | | - # Now load the image as RGBA, undoing premultiplication |
86 | | - return PIL.Image.frombuffer('RGBA', size, buf, 'raw', 'RGBa', 0, 1) |
87 | | - |
88 | | - # Fast path for small buffers |
89 | | - w, h = size |
90 | | - if w * h <= MAX_PIXELS_PER_LOAD: |
91 | | - return do_load(buf, size) |
92 | | - |
93 | | - # Load in chunks to avoid OverflowError in PIL.Image.frombuffer() |
94 | | - # https://github.com/python-pillow/Pillow/issues/1475 |
95 | | - if w > PIXELS_PER_LOAD: |
96 | | - # We could support this, but it seems like overkill |
97 | | - raise ValueError('Width %d is too large (maximum %d)' % |
98 | | - (w, PIXELS_PER_LOAD)) |
99 | | - rows_per_load = PIXELS_PER_LOAD // w |
100 | | - img = PIL.Image.new('RGBA', (w, h)) |
101 | | - for y in range(0, h, rows_per_load): |
102 | | - rows = min(h - y, rows_per_load) |
103 | | - if sys.version[0] == '2': |
104 | | - chunk = buffer(buf, 4 * y * w, 4 * rows * w) |
105 | | - else: |
106 | | - # PIL.Image.frombuffer() won't take a memoryview or |
107 | | - # bytearray, so we can't avoid copying |
108 | | - chunk = memoryview(buf)[y * w:(y + rows) * w].tobytes() |
109 | | - img.paste(do_load(chunk, (w, rows)), (0, y)) |
110 | | - return img |
111 | | - |
112 | 62 | class OpenSlideError(Exception): |
113 | 63 | """An error produced by the OpenSlide library. |
114 | 64 |
|
@@ -167,6 +117,11 @@ def from_param(cls, obj): |
167 | 117 | else: |
168 | 118 | raise TypeError('Incorrect type') |
169 | 119 |
|
| 120 | +def _load_image(buf, size): |
| 121 | + '''buf must be a mutable buffer.''' |
| 122 | + _convert.argb2rgba(buf) |
| 123 | + return PIL.Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1) |
| 124 | + |
170 | 125 | # check for errors opening an image file and wrap the resulting handle |
171 | 126 | def _check_open(result, _func, _args): |
172 | 127 | if result is None: |
|
0 commit comments