-
Notifications
You must be signed in to change notification settings - Fork 3
/
libopenslide.jl
148 lines (124 loc) · 4.39 KB
/
libopenslide.jl
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
#TODO exports
# get_property_names,
# get_property_value,
# can_open,
# openslide_open,
# get_level_count,
# close_slide,
# get_level0_dimensions,
# get_level_dimensions,
# get_level_downsample,
# get_best_level_for_downsample,
# read_region,
# get_associated_image_names,
# read_associated_image,
# get_error,
# openslide_version
################################################################################
const OPENSLIDE_PROPERTY_NAME_COMMENT = "openslide.comment"
const OPENSLIDE_PROPERTY_NAME_VENDOR = "openslide.vendor"
const OPENSLIDE_PROPERTY_NAME_QUICKHASH1 = "openslide.quickhash-1"
const OPENSLIDE_PROPERTY_NAME_BACKGROUND_COLOR = "openslide.background-color"
const OPENSLIDE_PROPERTY_NAME_OBJECTIVE_POWER = "openslide.objective-power"
const OPENSLIDE_PROPERTY_NAME_MPP_X = "openslide.mpp-x"
const OPENSLIDE_PROPERTY_NAME_MPP_Y = "openslide.mpp-y"
################################################################################
function _rgbfromrowflat(data::Array{UInt8,1}, dims)
output = zeros(UInt8, *(dims...),3)
@inbounds for i = 1:3
output[:,i] = data[-(i-4):4:end]
end
return reshape(output, dims...,3)
end
function _readstrings(buf::Ptr{Ptr{UInt8}})
rv = Any[]
i = 1
while (n = unsafe_load(buf,i)) != C_NULL
push!(rv, bytestring(n))
i += 1
end
return rv
end
################################################################################
function get_property_names(s::OSt)
nms = ccall( (:openslide_get_property_names, los),
Ptr{Ptr{UInt8}}, (OSt,), s)
_readstrings(nms)
end
function get_property_value(s::OSt, prop::String)
bytestring( ccall( (:openslide_get_property_value, los), Ptr{UInt8}, (OSt, Ptr{UInt8}), s, prop))
end
function can_open(fname::String)
ccall( (:openslide_can_open, los), Cint, (Ptr{UInt8},), fname) == 1 ? true : false
end
function openslide_open(fname::String)
ccall( (:openslide_open, los), Ptr{openslide_t}, (Ptr{UInt8},), fname)
end
function get_level_count(s::OSt)
ccall( (:openslide_get_level_count, los), Cint, (OSt,), s)
end
function close_slide(s::OSt)
ccall( (:openslide_close, los), Cint, (OSt,), s)
end
function get_level0_dimensions(s::OSt)
w = Int64[0]
h = Int64[0]
ccall( (:openslide_get_level0_dimensions, los), Cvoid, (Ptr{openslide_t}, Ptr{Int64}, Ptr{Int64}), s, w, h)
return [w, h]
end
function get_level_dimensions(s::OSt, level::Int)
w = Int[0]
h = Int[0]
ccall( (:openslide_get_level_dimensions, los),
Cvoid,
(Ptr{openslide_t}, Int, Ptr{Cint}, Ptr{Cint}),
convert(Ptr{openslide_t}, s), level, pointer(w), pointer(h))
return [w,h]
end
function get_level_downsample(s::OSt, level)
ccall( (:openslide_get_level_downsample, los),
Cdouble,
(Ptr{openslide_t}, Cint),
s, level)
end
function get_best_level_for_downsample(s::OSt, factor::Cdouble)
ccall( (:openslide_get_best_level_for_downsample, los),
Cint, (OSt, Cdouble),
s, factor)
end
function _read_region(img::OSt, data, x, y, level, w, h)
ccall( (:openslide_read_region, los),
Cvoid,
(OSt, Ptr{Cint}, Int64, Int64, Int32, Int64, Int64),
img, data, x, y, int32(level-1), w, h)
end
function read_region(img::OSt, x, y, level, w, h)
# allocate memory to hold output
data = zeros(UInt32, w*h)
_read_region(img, data, x, y, level, w, h)
return _rgbfromrowflat(reinterpret(UInt8,data), [w, h])
end
function get_associated_image_names(s::OSt)
buf = ccall( (:openslide_get_associated_image_names, los), Ptr{Ptr{UInt8}}, (OSt,), s)
_readstrings(buf)
end
function get_associated_image_dimensions(s::OSt, name::String)
w = h = Int64[0]
ccall( (:openslide_get_associated_image_dimensions, los),
Cvoid, (OSt, Ptr{Cchar}, Ptr{Clong}, Ptr{Clong}),
s, name, w, h)
return [w,h]
end
function read_associated_image(img::OSt, name::String, dims)
data = zeros(UInt32, *(dims...), 4)
ccall( (:openslide_read_associated_image, los), Cvoid,
(OSt, Ptr{Cchar}, Ptr{Cuint}),
osptr, name, data)
return _rgbfromrowflat(data, dims)
end
function get_error(img::OSt)
bytestring(ccall( (:openslide_get_error, los), Ptr{UInt8}, (OSt,), img))
end
function openslide_version()
bytestring(ccall( (:openslide_get_version,los),Ptr{UInt8}, ()))
end