|
54 | 54 | else: |
55 | 55 | _lib = cdll.LoadLibrary('libopenslide.so.0') |
56 | 56 |
|
| 57 | +try: |
| 58 | + from . import _convert |
| 59 | + def _load_image(buf, size): |
| 60 | + '''buf must be a mutable buffer.''' |
| 61 | + _convert.argb2rgba(buf) |
| 62 | + return PIL.Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1) |
| 63 | +except ImportError: |
| 64 | + def _load_image(buf, size): |
| 65 | + '''buf can be a string, but should be a ctypes buffer to avoid an |
| 66 | + extra copy in the caller.''' |
| 67 | + # First reorder the bytes in a pixel from native-endian aRGB to |
| 68 | + # big-endian RGBa to work around limitations in RGBa loader |
| 69 | + rawmode = (sys.byteorder == 'little') and 'BGRA' or 'ARGB' |
| 70 | + buf = PIL.Image.frombuffer('RGBA', size, buf, 'raw', rawmode, 0, |
| 71 | + 1).tostring() |
| 72 | + # Now load the image as RGBA, undoing premultiplication |
| 73 | + return PIL.Image.frombuffer('RGBA', size, buf, 'raw', 'RGBa', 0, 1) |
| 74 | + |
57 | 75 | class OpenSlideError(Exception): |
58 | 76 | """An error produced by the OpenSlide library. |
59 | 77 |
|
@@ -161,17 +179,6 @@ def _func(name, restype, argtypes, errcheck=_check_error): |
161 | 179 | func.errcheck = errcheck |
162 | 180 | return func |
163 | 181 |
|
164 | | -def _load_image(buf, size): |
165 | | - '''buf can be a string, but should be a ctypes buffer to avoid an extra |
166 | | - copy in the caller.''' |
167 | | - # First reorder the bytes in a pixel from native-endian aRGB to |
168 | | - # big-endian RGBa to work around limitations in RGBa loader |
169 | | - rawmode = (sys.byteorder == 'little') and 'BGRA' or 'ARGB' |
170 | | - buf = PIL.Image.frombuffer('RGBA', size, buf, 'raw', rawmode, 0, |
171 | | - 1).tostring() |
172 | | - # Now load the image as RGBA, undoing premultiplication |
173 | | - return PIL.Image.frombuffer('RGBA', size, buf, 'raw', 'RGBa', 0, 1) |
174 | | - |
175 | 182 | try: |
176 | 183 | detect_vendor = _func('openslide_detect_vendor', c_char_p, [_utf8_p], |
177 | 184 | _check_string) |
|
0 commit comments