File: lowlevel.py

package info (click to toggle)
openslide-python 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,840 kB
  • sloc: javascript: 19,981; python: 2,418; ansic: 86; makefile: 18
file content (578 lines) | stat: -rw-r--r-- 17,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#
# openslide-python - Python bindings for the OpenSlide library
#
# Copyright (c) 2010-2013 Carnegie Mellon University
# Copyright (c) 2016-2024 Benjamin Gilbert
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

"""
Low-level interface to the OpenSlide library.

Most users should use the openslide.OpenSlide class rather than this
module.

This module provides nearly direct equivalents to the OpenSlide C API.
(As an implementation detail, conversion of premultiplied image data
returned by OpenSlide into a non-premultiplied PIL.Image happens here
rather than in the high-level interface.)
"""

from __future__ import annotations

from ctypes import (
    CDLL,
    POINTER,
    _Pointer,
    byref,
    c_char,
    c_char_p,
    c_double,
    c_int32,
    c_int64,
    c_size_t,
    c_uint32,
    c_void_p,
    cdll,
)
from itertools import count
import os
import platform
from typing import TYPE_CHECKING, Any, Callable, Protocol, TypeVar, cast

from PIL import Image

from . import _convert

if TYPE_CHECKING:
    # Python 3.10+
    from typing import ParamSpec, TypeAlias

    from _convert import _Buffer


def _load_library() -> CDLL:
    try:
        import openslide_bin

        return openslide_bin.libopenslide1
    except (AttributeError, ModuleNotFoundError):
        pass

    def try_load(names: list[str]) -> CDLL:
        try:
            return cdll.LoadLibrary(names[0])
        except OSError:
            remaining = names[1:]
            if remaining:
                # handle recursively so implicit exception chaining captures
                # all the failures
                return try_load(remaining)
            else:
                raise

    if platform.system() == 'Windows':
        try:
            return try_load(['libopenslide-1.dll', 'libopenslide-0.dll'])
        except FileNotFoundError as exc:
            raise ModuleNotFoundError(
                "Couldn't locate OpenSlide DLL. "
                "Try `pip install openslide-bin`, "
                "or if you're using an OpenSlide binary package, "
                "ensure you've called os.add_dll_directory(). "
                "https://openslide.org/api/python/#installing"
            ) from exc
    elif platform.system() == 'Darwin':
        try:
            return try_load(['libopenslide.1.dylib', 'libopenslide.0.dylib'])
        except OSError as exc:
            # MacPorts doesn't add itself to the dyld search path, but
            # does add itself to the find_library() search path
            # (DEFAULT_LIBRARY_FALLBACK in ctypes.macholib.dyld).
            import ctypes.util

            lib = ctypes.util.find_library('openslide')
            if lib is None:
                raise ModuleNotFoundError(
                    "Couldn't locate OpenSlide dylib. "
                    "Try `pip install openslide-bin`. "
                    "https://openslide.org/api/python/#installing"
                ) from exc
            return cdll.LoadLibrary(lib)
    else:
        try:
            return try_load(['libopenslide.so.1', 'libopenslide.so.0'])
        except OSError as exc:
            raise ModuleNotFoundError(
                "Couldn't locate OpenSlide shared library. "
                "Try `pip install openslide-bin`. "
                "https://openslide.org/api/python/#installing"
            ) from exc


_lib = _load_library()


class OpenSlideError(Exception):
    """An error produced by the OpenSlide library.

    Import this from openslide rather than from openslide.lowlevel.
    """


class OpenSlideVersionError(OpenSlideError):
    """This version of OpenSlide does not support the requested functionality.

    Import this from openslide rather than from openslide.lowlevel.
    """

    def __init__(self, minimum_version: str):
        super().__init__(f'OpenSlide >= {minimum_version} required')
        self.minimum_version = minimum_version


class OpenSlideUnsupportedFormatError(OpenSlideError):
    """OpenSlide does not support the requested file.

    Import this from openslide rather than from openslide.lowlevel.
    """


class _OpenSlide:
    """Wrapper class to make sure we correctly pass an OpenSlide handle."""

    def __init__(self, ptr: c_void_p):
        self._as_parameter_ = ptr
        self._valid = True
        # Retain a reference to close() to avoid GC problems during
        # interpreter shutdown
        self._close = close

    def __del__(self) -> None:
        if self._valid:
            self._close(self)

    def invalidate(self) -> None:
        self._valid = False

    @classmethod
    def from_param(cls, obj: _OpenSlide) -> _OpenSlide:
        if obj.__class__ != cls:
            raise ValueError("Not an OpenSlide reference")
        if not obj._as_parameter_:
            raise ValueError("Passing undefined slide object")
        if not obj._valid:
            raise ValueError("Passing closed slide object")
        return obj


class _OpenSlideCache:
    """Wrapper class to make sure we correctly pass an OpenSlide cache."""

    def __init__(self, ptr: c_void_p):
        self._as_parameter_ = ptr
        # Retain a reference to cache_release() to avoid GC problems during
        # interpreter shutdown
        self._cache_release = cache_release

    def __del__(self) -> None:
        self._cache_release(self)

    @classmethod
    def from_param(cls, obj: _OpenSlideCache) -> _OpenSlideCache:
        if obj.__class__ != cls:
            raise ValueError("Not an OpenSlide cache reference")
        if not obj._as_parameter_:
            raise ValueError("Passing undefined cache object")
        return obj


if TYPE_CHECKING:
    # Python 3.10+
    Filename: TypeAlias = str | bytes | os.PathLike[Any]


class _filename_p:
    """Wrapper class to convert filename arguments to bytes."""

    @classmethod
    def from_param(cls, obj: Filename) -> bytes:
        # fspath and fsencode raise TypeError on unexpected types
        if platform.system() == 'Windows':
            # OpenSlide 4.0.0+ requires UTF-8 on Windows
            obj = os.fspath(obj)
            if isinstance(obj, str):
                return obj.encode('UTF-8')
            else:
                return obj
        else:
            return os.fsencode(obj)


class _utf8_p:
    """Wrapper class to convert string arguments to bytes."""

    @classmethod
    def from_param(cls, obj: str | bytes) -> bytes:
        if isinstance(obj, bytes):
            return obj
        elif isinstance(obj, str):
            return obj.encode('UTF-8')
        else:
            raise TypeError('Incorrect type')


class _size_t:
    """Wrapper class to convert size_t arguments to c_size_t."""

    @classmethod
    def from_param(cls, obj: int) -> c_size_t:
        if not isinstance(obj, int):
            raise TypeError('Incorrect type')
        if obj < 0:
            raise ValueError('Value out of range')
        return c_size_t(obj)


def _load_image(buf: _Buffer, size: tuple[int, int]) -> Image.Image:
    '''buf must be a mutable buffer.'''
    _convert.argb2rgba(buf)
    return Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)


# check for errors opening an image file and wrap the resulting handle
def _check_open(result: int | None, _func: Any, _args: Any) -> _OpenSlide:
    if result is None:
        raise OpenSlideUnsupportedFormatError("Unsupported or missing image file")
    slide = _OpenSlide(c_void_p(result))
    err = get_error(slide)
    if err is not None:
        raise OpenSlideError(err)
    return slide


# prevent further operations on slide handle after it is closed
def _check_close(_result: Any, _func: Any, args: tuple[_OpenSlide]) -> None:
    args[0].invalidate()


# wrap the handle returned when creating a cache
def _check_cache_create(result: int, _func: Any, _args: Any) -> _OpenSlideCache:
    return _OpenSlideCache(c_void_p(result))


# Convert returned byte array, if present, into a string
def _check_string(result: Any, func: _CTypesFunc[..., Any], _args: Any) -> Any:
    if func.restype is c_char_p and result is not None:
        return result.decode('UTF-8', 'replace')
    else:
        return result


# check if the library got into an error state after each library call
def _check_error(result: Any, func: Any, args: tuple[Any, ...]) -> Any:
    assert isinstance(args[0], _OpenSlide)
    err = get_error(args[0])
    if err is not None:
        raise OpenSlideError(err)
    return _check_string(result, func, args)


# Convert returned NULL-terminated char** into a list of strings
def _check_name_list(result: _Pointer[c_char_p], func: Any, args: Any) -> list[str]:
    _check_error(result, func, args)
    names = []
    for i in count():
        name = result[i]
        if not name:
            break
        names.append(name.decode('UTF-8', 'replace'))
    return names


class _FunctionUnavailable:
    '''Standin for a missing optional function.  Fails when called.'''

    def __init__(self, minimum_version: str):
        self._minimum_version = minimum_version
        # allow checking for availability without calling the function
        self.available = False

    def __call__(self, *_args: Any) -> Any:
        raise OpenSlideVersionError(self._minimum_version)


# gate runtime code that requires ParamSpec, Python 3.10+
if TYPE_CHECKING:
    _P = ParamSpec('_P')
    _T = TypeVar('_T', covariant=True)

    class _Func(Protocol[_P, _T]):
        available: bool

        def __call__(self, *args: _P.args) -> _T: ...

    class _CTypesFunc(_Func[_P, _T]):
        restype: type | None
        argtypes: list[type]
        errcheck: _ErrCheck

    _ErrCheck: TypeAlias = (
        Callable[[Any, _CTypesFunc[..., Any], tuple[Any, ...]], Any] | None
    )


# resolve and return an OpenSlide function with the specified properties
def _func(
    name: str,
    restype: type | None,
    argtypes: list[type],
    errcheck: _ErrCheck = _check_error,
    minimum_version: str | None = None,
) -> _Func[_P, _T]:
    try:
        func: _CTypesFunc[_P, _T] = getattr(_lib, name)
    except AttributeError:
        if minimum_version is None:
            raise
        else:
            return _FunctionUnavailable(minimum_version)
    func.argtypes = argtypes
    func.restype = restype
    if errcheck is not None:
        func.errcheck = errcheck
    func.available = True
    return func


def _wraps_funcs(
    wrapped: list[_Func[..., Any]]
) -> Callable[[Callable[_P, _T]], _Func[_P, _T]]:
    def decorator(fn: Callable[_P, _T]) -> _Func[_P, _T]:
        if TYPE_CHECKING:
            # requires ParamSpec, Python 3.10+
            f = cast(_Func[_P, _T], fn)
        else:
            f = fn
        f.available = True
        for w in wrapped:
            f.available = f.available and w.available
        return f

    return decorator


try:
    detect_vendor: _Func[[Filename], str] = _func(
        'openslide_detect_vendor', c_char_p, [_filename_p], _check_string
    )
except AttributeError:
    raise OpenSlideVersionError('3.4.0')

open: _Func[[Filename], _OpenSlide] = _func(
    'openslide_open', c_void_p, [_filename_p], _check_open
)

close: _Func[[_OpenSlide], None] = _func(
    'openslide_close', None, [_OpenSlide], _check_close
)

get_level_count: _Func[[_OpenSlide], int] = _func(
    'openslide_get_level_count', c_int32, [_OpenSlide]
)

_get_level_dimensions: _Func[
    [_OpenSlide, int, _Pointer[c_int64], _Pointer[c_int64]], None
] = _func(
    'openslide_get_level_dimensions',
    None,
    [_OpenSlide, c_int32, POINTER(c_int64), POINTER(c_int64)],
)


@_wraps_funcs([_get_level_dimensions])
def get_level_dimensions(slide: _OpenSlide, level: int) -> tuple[int, int]:
    w, h = c_int64(), c_int64()
    _get_level_dimensions(slide, level, byref(w), byref(h))
    return w.value, h.value


get_level_downsample: _Func[[_OpenSlide, int], float] = _func(
    'openslide_get_level_downsample', c_double, [_OpenSlide, c_int32]
)

get_best_level_for_downsample: _Func[[_OpenSlide, float], int] = _func(
    'openslide_get_best_level_for_downsample', c_int32, [_OpenSlide, c_double]
)

_read_region: _Func[[_OpenSlide, _Pointer[c_uint32], int, int, int, int, int], None] = (
    _func(
        'openslide_read_region',
        None,
        [_OpenSlide, POINTER(c_uint32), c_int64, c_int64, c_int32, c_int64, c_int64],
    )
)


@_wraps_funcs([_read_region])
def read_region(
    slide: _OpenSlide, x: int, y: int, level: int, w: int, h: int
) -> Image.Image:
    if w < 0 or h < 0:
        # OpenSlide would catch this, but not before we tried to allocate
        # a negative-size buffer
        raise OpenSlideError(
            "negative width (%d) or negative height (%d) not allowed" % (w, h)
        )
    if w == 0 or h == 0:
        # Image.frombuffer() would raise an exception
        return Image.new('RGBA', (w, h))
    buf = (w * h * c_uint32)()
    _read_region(slide, buf, x, y, level, w, h)
    return _load_image(buf, (w, h))


get_icc_profile_size: _Func[[_OpenSlide], int] = _func(
    'openslide_get_icc_profile_size',
    c_int64,
    [_OpenSlide],
    minimum_version='4.0.0',
)

_read_icc_profile: _Func[[_OpenSlide, _Pointer[c_char]], None] = _func(
    'openslide_read_icc_profile',
    None,
    [_OpenSlide, POINTER(c_char)],
    minimum_version='4.0.0',
)


@_wraps_funcs([get_icc_profile_size, _read_icc_profile])
def read_icc_profile(slide: _OpenSlide) -> bytes | None:
    size = get_icc_profile_size(slide)
    if size == 0:
        return None
    buf = (size * c_char)()
    _read_icc_profile(slide, buf)
    return buf.raw


get_error: _Func[[_OpenSlide], str] = _func(
    'openslide_get_error', c_char_p, [_OpenSlide], _check_string
)

get_property_names: _Func[[_OpenSlide], list[str]] = _func(
    'openslide_get_property_names', POINTER(c_char_p), [_OpenSlide], _check_name_list
)

get_property_value: _Func[[_OpenSlide, str | bytes], str] = _func(
    'openslide_get_property_value', c_char_p, [_OpenSlide, _utf8_p]
)

get_associated_image_names: _Func[[_OpenSlide], list[str]] = _func(
    'openslide_get_associated_image_names',
    POINTER(c_char_p),
    [_OpenSlide],
    _check_name_list,
)

_get_associated_image_dimensions: _Func[
    [_OpenSlide, str | bytes, _Pointer[c_int64], _Pointer[c_int64]], None
] = _func(
    'openslide_get_associated_image_dimensions',
    None,
    [_OpenSlide, _utf8_p, POINTER(c_int64), POINTER(c_int64)],
)


@_wraps_funcs([_get_associated_image_dimensions])
def get_associated_image_dimensions(
    slide: _OpenSlide, name: str | bytes
) -> tuple[int, int]:
    w, h = c_int64(), c_int64()
    _get_associated_image_dimensions(slide, name, byref(w), byref(h))
    return w.value, h.value


_read_associated_image: _Func[[_OpenSlide, str | bytes, _Pointer[c_uint32]], None] = (
    _func(
        'openslide_read_associated_image',
        None,
        [_OpenSlide, _utf8_p, POINTER(c_uint32)],
    )
)


@_wraps_funcs([get_associated_image_dimensions, _read_associated_image])
def read_associated_image(slide: _OpenSlide, name: str | bytes) -> Image.Image:
    w, h = get_associated_image_dimensions(slide, name)
    buf = (w * h * c_uint32)()
    _read_associated_image(slide, name, buf)
    return _load_image(buf, (w, h))


get_associated_image_icc_profile_size: _Func[[_OpenSlide, str | bytes], int] = _func(
    'openslide_get_associated_image_icc_profile_size',
    c_int64,
    [_OpenSlide, _utf8_p],
    minimum_version='4.0.0',
)

_read_associated_image_icc_profile: _Func[
    [_OpenSlide, str | bytes, _Pointer[c_char]], None
] = _func(
    'openslide_read_associated_image_icc_profile',
    None,
    [_OpenSlide, _utf8_p, POINTER(c_char)],
    minimum_version='4.0.0',
)


@_wraps_funcs(
    [get_associated_image_icc_profile_size, _read_associated_image_icc_profile]
)
def read_associated_image_icc_profile(
    slide: _OpenSlide, name: str | bytes
) -> bytes | None:
    size = get_associated_image_icc_profile_size(slide, name)
    if size == 0:
        return None
    buf = (size * c_char)()
    _read_associated_image_icc_profile(slide, name, buf)
    return buf.raw


get_version: _Func[[], str] = _func(
    'openslide_get_version', c_char_p, [], _check_string
)

cache_create: _Func[[int], _OpenSlideCache] = _func(
    'openslide_cache_create',
    c_void_p,
    [_size_t],
    _check_cache_create,
    minimum_version='4.0.0',
)

set_cache: _Func[[_OpenSlide, _OpenSlideCache], None] = _func(
    'openslide_set_cache',
    None,
    [_OpenSlide, _OpenSlideCache],
    minimum_version='4.0.0',
)

cache_release: _Func[[_OpenSlideCache], None] = _func(
    'openslide_cache_release', None, [_OpenSlideCache], None, minimum_version='4.0.0'
)