4545from itertools import count
4646import platform
4747
48- import PIL . Image
48+ from PIL import Image
4949
5050from . import _convert
5151
@@ -199,7 +199,7 @@ def from_param(cls, obj):
199199def _load_image (buf , size ):
200200 '''buf must be a mutable buffer.'''
201201 _convert .argb2rgba (buf )
202- return PIL . Image .frombuffer ('RGBA' , size , buf , 'raw' , 'RGBA' , 0 , 1 )
202+ return Image .frombuffer ('RGBA' , size , buf , 'raw' , 'RGBA' , 0 , 1 )
203203
204204
205205# check for errors opening an image file and wrap the resulting handle
@@ -263,14 +263,28 @@ def _func(name, restype, argtypes, errcheck=_check_error, minimum_version=None):
263263 def function_unavailable (* _args ):
264264 raise OpenSlideVersionError (minimum_version )
265265
266+ # allow checking for availability without calling the function
267+ function_unavailable .available = False
268+
266269 return function_unavailable
267270 func .argtypes = argtypes
268271 func .restype = restype
269272 if errcheck is not None :
270273 func .errcheck = errcheck
274+ func .available = True
271275 return func
272276
273277
278+ def _wraps_funcs (wrapped ):
279+ def decorator (f ):
280+ f .available = True
281+ for w in wrapped :
282+ f .available = f .available and w .available
283+ return f
284+
285+ return decorator
286+
287+
274288try :
275289 detect_vendor = _func ('openslide_detect_vendor' , c_char_p , [_utf8_p ], _check_string )
276290except AttributeError :
@@ -289,6 +303,7 @@ def function_unavailable(*_args):
289303)
290304
291305
306+ @_wraps_funcs ([_get_level_dimensions ])
292307def get_level_dimensions (slide , level ):
293308 w , h = c_int64 (), c_int64 ()
294309 _get_level_dimensions (slide , level , byref (w ), byref (h ))
@@ -310,6 +325,7 @@ def get_level_dimensions(slide, level):
310325)
311326
312327
328+ @_wraps_funcs ([_read_region ])
313329def read_region (slide , x , y , level , w , h ):
314330 if w < 0 or h < 0 :
315331 # OpenSlide would catch this, but not before we tried to allocate
@@ -318,8 +334,8 @@ def read_region(slide, x, y, level, w, h):
318334 "negative width (%d) or negative height (%d) not allowed" % (w , h )
319335 )
320336 if w == 0 or h == 0 :
321- # PIL. Image.frombuffer() would raise an exception
322- return PIL . Image .new ('RGBA' , (w , h ))
337+ # Image.frombuffer() would raise an exception
338+ return Image .new ('RGBA' , (w , h ))
323339 buf = (w * h * c_uint32 )()
324340 _read_region (slide , buf , x , y , level , w , h )
325341 return _load_image (buf , (w , h ))
@@ -349,6 +365,7 @@ def read_region(slide, x, y, level, w, h):
349365)
350366
351367
368+ @_wraps_funcs ([_get_associated_image_dimensions ])
352369def get_associated_image_dimensions (slide , name ):
353370 w , h = c_int64 (), c_int64 ()
354371 _get_associated_image_dimensions (slide , name , byref (w ), byref (h ))
@@ -360,6 +377,7 @@ def get_associated_image_dimensions(slide, name):
360377)
361378
362379
380+ @_wraps_funcs ([get_associated_image_dimensions , _read_associated_image ])
363381def read_associated_image (slide , name ):
364382 w , h = get_associated_image_dimensions (slide , name )
365383 buf = (w * h * c_uint32 )()
0 commit comments