Skip to content

Commit 48e7a66

Browse files
committed
Rename "layer" to "level" to match OpenSlide terminology changes
1 parent 96659d5 commit 48e7a66

File tree

3 files changed

+101
-100
lines changed

3 files changed

+101
-100
lines changed

openslide/__init__.py

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,27 @@ def close(self):
6060
raise NotImplementedError
6161

6262
@property
63-
def layer_count(self):
64-
"""The number of layers in the image."""
63+
def level_count(self):
64+
"""The number of levels in the image."""
6565
raise NotImplementedError
6666

6767
@property
68-
def layer_dimensions(self):
69-
"""A list of (width, height) tuples, one for each layer of the image.
68+
def level_dimensions(self):
69+
"""A list of (width, height) tuples, one for each level of the image.
7070
71-
layer_dimensions[n] contains the dimensions of layer n."""
71+
level_dimensions[n] contains the dimensions of level n."""
7272
raise NotImplementedError
7373

7474
@property
7575
def dimensions(self):
76-
"""A (width, height) tuple for layer 0 of the image."""
77-
return self.layer_dimensions[0]
76+
"""A (width, height) tuple for level 0 of the image."""
77+
return self.level_dimensions[0]
7878

7979
@property
80-
def layer_downsamples(self):
81-
"""A list of downsampling factors for each layer of the image.
80+
def level_downsamples(self):
81+
"""A list of downsampling factors for each level of the image.
8282
83-
layer_downsample[n] contains the downsample factor of layer n."""
83+
level_downsample[n] contains the downsample factor of level n."""
8484
raise NotImplementedError
8585

8686
@property
@@ -97,16 +97,16 @@ def associated_images(self):
9797
This is a map: image name -> PIL.Image."""
9898
raise NotImplementedError
9999

100-
def get_best_layer_for_downsample(self, downsample):
101-
"""Return the best layer for displaying the given downsample."""
100+
def get_best_level_for_downsample(self, downsample):
101+
"""Return the best level for displaying the given downsample."""
102102
raise NotImplementedError
103103

104-
def read_region(self, location, layer, size):
104+
def read_region(self, location, level, size):
105105
"""Return a PIL.Image containing the contents of the region.
106106
107-
location: (x, y) tuple giving the top left pixel in the layer 0
107+
location: (x, y) tuple giving the top left pixel in the level 0
108108
reference frame.
109-
layer: the layer number.
109+
level: the level number.
110110
size: (width, height) tuple giving the region size."""
111111
raise NotImplementedError
112112

@@ -116,8 +116,8 @@ def get_thumbnail(self, size):
116116
size: the maximum size of the thumbnail."""
117117
downsample = max(*[dim / thumb for dim, thumb in
118118
zip(self.dimensions, size)])
119-
layer = self.get_best_layer_for_downsample(downsample)
120-
tile = self.read_region((0, 0), layer, self.layer_dimensions[layer])
119+
level = self.get_best_level_for_downsample(downsample)
120+
tile = self.read_region((0, 0), level, self.level_dimensions[level])
121121
# Apply on solid background
122122
bg_color = '#' + self.properties.get(PROPERTY_NAME_BACKGROUND_COLOR,
123123
'ffffff')
@@ -154,25 +154,25 @@ def close(self):
154154
lowlevel.close(self._osr)
155155

156156
@property
157-
def layer_count(self):
158-
"""The number of layers in the image."""
159-
return lowlevel.get_layer_count(self._osr)
157+
def level_count(self):
158+
"""The number of levels in the image."""
159+
return lowlevel.get_level_count(self._osr)
160160

161161
@property
162-
def layer_dimensions(self):
163-
"""A list of (width, height) tuples, one for each layer of the image.
162+
def level_dimensions(self):
163+
"""A list of (width, height) tuples, one for each level of the image.
164164
165-
layer_dimensions[n] contains the dimensions of layer n."""
166-
return tuple(lowlevel.get_layer_dimensions(self._osr, i)
167-
for i in range(self.layer_count))
165+
level_dimensions[n] contains the dimensions of level n."""
166+
return tuple(lowlevel.get_level_dimensions(self._osr, i)
167+
for i in range(self.level_count))
168168

169169
@property
170-
def layer_downsamples(self):
171-
"""A list of downsampling factors for each layer of the image.
170+
def level_downsamples(self):
171+
"""A list of downsampling factors for each level of the image.
172172
173-
layer_downsample[n] contains the downsample factor of layer n."""
174-
return tuple(lowlevel.get_layer_downsample(self._osr, i)
175-
for i in range(self.layer_count))
173+
level_downsample[n] contains the downsample factor of level n."""
174+
return tuple(lowlevel.get_level_downsample(self._osr, i)
175+
for i in range(self.level_count))
176176

177177
@property
178178
def properties(self):
@@ -191,22 +191,22 @@ def associated_images(self):
191191
are not premultiplied."""
192192
return _AssociatedImageMap(self._osr)
193193

194-
def get_best_layer_for_downsample(self, downsample):
195-
"""Return the best layer for displaying the given downsample."""
196-
return lowlevel.get_best_layer_for_downsample(self._osr, downsample)
194+
def get_best_level_for_downsample(self, downsample):
195+
"""Return the best level for displaying the given downsample."""
196+
return lowlevel.get_best_level_for_downsample(self._osr, downsample)
197197

198-
def read_region(self, location, layer, size):
198+
def read_region(self, location, level, size):
199199
"""Return a PIL.Image containing the contents of the region.
200200
201-
location: (x, y) tuple giving the top left pixel in the layer 0
201+
location: (x, y) tuple giving the top left pixel in the level 0
202202
reference frame.
203-
layer: the layer number.
203+
level: the level number.
204204
size: (width, height) tuple giving the region size.
205205
206206
Unlike in the C interface, the image data returned by this
207207
function is not premultiplied."""
208208
return lowlevel.read_region(self._osr, location[0], location[1],
209-
layer, size[0], size[1])
209+
level, size[0], size[1])
210210

211211

212212
class _OpenSlideMap(Mapping):
@@ -274,22 +274,22 @@ def close(self):
274274
self._image = None
275275

276276
@property
277-
def layer_count(self):
278-
"""The number of layers in the image."""
277+
def level_count(self):
278+
"""The number of levels in the image."""
279279
return 1
280280

281281
@property
282-
def layer_dimensions(self):
283-
"""A list of (width, height) tuples, one for each layer of the image.
282+
def level_dimensions(self):
283+
"""A list of (width, height) tuples, one for each level of the image.
284284
285-
layer_dimensions[n] contains the dimensions of layer n."""
285+
level_dimensions[n] contains the dimensions of level n."""
286286
return (self._image.size,)
287287

288288
@property
289-
def layer_downsamples(self):
290-
"""A list of downsampling factors for each layer of the image.
289+
def level_downsamples(self):
290+
"""A list of downsampling factors for each level of the image.
291291
292-
layer_downsample[n] contains the downsample factor of layer n."""
292+
level_downsample[n] contains the downsample factor of level n."""
293293
return (1.0,)
294294

295295
@property
@@ -306,19 +306,19 @@ def associated_images(self):
306306
This is a map: image name -> PIL.Image."""
307307
return {}
308308

309-
def get_best_layer_for_downsample(self, _downsample):
310-
"""Return the best layer for displaying the given downsample."""
309+
def get_best_level_for_downsample(self, _downsample):
310+
"""Return the best level for displaying the given downsample."""
311311
return 0
312312

313-
def read_region(self, location, layer, size):
313+
def read_region(self, location, level, size):
314314
"""Return a PIL.Image containing the contents of the region.
315315
316-
location: (x, y) tuple giving the top left pixel in the layer 0
316+
location: (x, y) tuple giving the top left pixel in the level 0
317317
reference frame.
318-
layer: the layer number.
318+
level: the level number.
319319
size: (width, height) tuple giving the region size."""
320-
if layer != 0:
321-
raise OpenSlideError("Invalid layer")
320+
if level != 0:
321+
raise OpenSlideError("Invalid level")
322322
if ['fail' for s in size if s < 0]:
323323
raise OpenSlideError("Size %s must be non-negative" % (size,))
324324
# Any corner of the requested region may be outside the bounds of
@@ -358,8 +358,8 @@ def open_slide(filename):
358358
print "PIL can open:", ImageSlide.can_open(sys.argv[1])
359359
with open_slide(sys.argv[1]) as _slide:
360360
print "Dimensions:", _slide.dimensions
361-
print "Layers:", _slide.layer_count
362-
print "Layer dimensions:", _slide.layer_dimensions
363-
print "Layer downsamples:", _slide.layer_downsamples
361+
print "Levels:", _slide.level_count
362+
print "Level dimensions:", _slide.level_dimensions
363+
print "Level downsamples:", _slide.level_downsamples
364364
print "Properties:", _slide.properties
365365
print "Associated images:", _slide.associated_images

openslide/deepzoom.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# openslide-python - Python bindings for the OpenSlide library
33
#
4-
# Copyright (c) 2010-2011 Carnegie Mellon University
4+
# Copyright (c) 2010-2012 Carnegie Mellon University
55
#
66
# This library is free software; you can redistribute it and/or modify it
77
# under the terms of version 2.1 of the GNU Lesser General Public License
@@ -44,18 +44,18 @@ def __init__(self, osr, tile_size=256, overlap=1):
4444
# We have four coordinate planes:
4545
# - Row and column of the tile within the Deep Zoom level (t_)
4646
# - Pixel coordinates within the Deep Zoom level (z_)
47-
# - Pixel coordinates within the slide layer (l_)
48-
# - Pixel coordinates within slide layer 0 (l0_)
47+
# - Pixel coordinates within the slide level (l_)
48+
# - Pixel coordinates within slide level 0 (l0_)
4949

5050
self._osr = osr
5151
self._z_t_downsample = tile_size
5252
self._z_overlap = overlap
5353

5454
# Precompute dimensions
55-
# Layer
56-
self._l_dimensions = osr.layer_dimensions
55+
# Slide level
56+
self._l_dimensions = osr.level_dimensions
5757
self._l0_dimensions = self._l_dimensions[0]
58-
# Level
58+
# Deep Zoom level
5959
z_size = self._l0_dimensions
6060
z_dimensions = [z_size]
6161
while z_size[0] > 1 or z_size[1] > 1:
@@ -67,24 +67,24 @@ def __init__(self, osr, tile_size=256, overlap=1):
6767
self._t_dimensions = tuple((tiles(z_w), tiles(z_h))
6868
for z_w, z_h in self._z_dimensions)
6969

70-
# Level count
71-
self._levels = len(self._z_dimensions)
70+
# Deep Zoom level count
71+
self._dz_levels = len(self._z_dimensions)
7272

73-
# Total downsamples for each level
74-
l0_z_downsamples = tuple(2 ** (self._levels - level - 1)
75-
for level in xrange(self._levels))
73+
# Total downsamples for each Deep Zoom level
74+
l0_z_downsamples = tuple(2 ** (self._dz_levels - dz_level - 1)
75+
for dz_level in xrange(self._dz_levels))
7676

77-
# Preferred layers for each level
78-
self._layer_from_level = tuple(
79-
self._osr.get_best_layer_for_downsample(d)
77+
# Preferred slide levels for each Deep Zoom level
78+
self._slide_from_dz_level = tuple(
79+
self._osr.get_best_level_for_downsample(d)
8080
for d in l0_z_downsamples)
8181

8282
# Piecewise downsamples
83-
self._l0_l_downsamples = self._osr.layer_downsamples
83+
self._l0_l_downsamples = self._osr.level_downsamples
8484
self._l_z_downsamples = tuple(
85-
l0_z_downsamples[level] /
86-
self._l0_l_downsamples[self._layer_from_level[level]]
87-
for level in range(self._levels))
85+
l0_z_downsamples[dz_level] /
86+
self._l0_l_downsamples[self._slide_from_dz_level[dz_level]]
87+
for dz_level in range(self._dz_levels))
8888

8989
# Slide background color
9090
self._bg_color = '#' + self._osr.properties.get(
@@ -93,7 +93,7 @@ def __init__(self, osr, tile_size=256, overlap=1):
9393
@property
9494
def level_count(self):
9595
"""The number of Deep Zoom levels in the image."""
96-
return self._levels
96+
return self._dz_levels
9797

9898
@property
9999
def level_tiles(self):
@@ -131,50 +131,51 @@ def get_tile(self, level, address):
131131

132132
return tile
133133

134-
def _get_tile_info(self, level, t_location):
134+
def _get_tile_info(self, dz_level, t_location):
135135
# Check parameters
136-
if level < 0 or level >= self._levels:
136+
if dz_level < 0 or dz_level >= self._dz_levels:
137137
raise ValueError("Invalid level")
138-
for t, t_lim in zip(t_location, self._t_dimensions[level]):
138+
for t, t_lim in zip(t_location, self._t_dimensions[dz_level]):
139139
if t < 0 or t >= t_lim:
140140
raise ValueError("Invalid address")
141141

142-
# Get preferred layer
143-
layer = self._layer_from_level[level]
142+
# Get preferred slide level
143+
slide_level = self._slide_from_dz_level[dz_level]
144144

145145
# Calculate top/left and bottom/right overlap
146146
z_overlap_tl = tuple(self._z_overlap * int(t != 0)
147147
for t in t_location)
148148
z_overlap_br = tuple(self._z_overlap * int(t != t_lim - 1)
149-
for t, t_lim in zip(t_location, self.level_tiles[level]))
149+
for t, t_lim in
150+
zip(t_location, self.level_tiles[dz_level]))
150151

151152
# Get final size of the tile
152153
z_size = tuple(min(self._z_t_downsample,
153154
z_lim - self._z_t_downsample * t) + z_tl + z_br
154155
for t, z_lim, z_tl, z_br in
155-
zip(t_location, self._z_dimensions[level], z_overlap_tl,
156-
z_overlap_br))
156+
zip(t_location, self._z_dimensions[dz_level],
157+
z_overlap_tl, z_overlap_br))
157158

158159
# Obtain the region coordinates
159160
z_location = [self._z_from_t(t) for t in t_location]
160-
l_location = [self._l_from_z(level, z) - z_tl
161+
l_location = [self._l_from_z(dz_level, z) - z_tl
161162
for z, z_tl in zip(z_location, z_overlap_tl)]
162163
# Round location down and size up
163-
l0_location = tuple(int(self._l0_from_l(layer, l))
164+
l0_location = tuple(int(self._l0_from_l(slide_level, l))
164165
for l in l_location)
165-
l_size = tuple(int(min(math.ceil(self._l_from_z(level, dz)),
166+
l_size = tuple(int(min(math.ceil(self._l_from_z(dz_level, dz)),
166167
l_lim - math.ceil(l)))
167168
for l, dz, l_lim in
168-
zip(l_location, z_size, self._l_dimensions[layer]))
169+
zip(l_location, z_size, self._l_dimensions[slide_level]))
169170

170171
# Return read_region() parameters plus tile size for final scaling
171-
return ((l0_location, layer, l_size), z_size)
172+
return ((l0_location, slide_level, l_size), z_size)
172173

173-
def _l0_from_l(self, layer, l):
174-
return self._l0_l_downsamples[layer] * l
174+
def _l0_from_l(self, slide_level, l):
175+
return self._l0_l_downsamples[slide_level] * l
175176

176-
def _l_from_z(self, level, z):
177-
return self._l_z_downsamples[level] * z
177+
def _l_from_z(self, dz_level, z):
178+
return self._l_z_downsamples[dz_level] * z
178179

179180
def _z_from_t(self, t):
180181
return self._z_t_downsample * t

0 commit comments

Comments
 (0)