-
Notifications
You must be signed in to change notification settings - Fork 0
/
openslide.go
305 lines (260 loc) · 10.4 KB
/
openslide.go
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
/*
* openslide-go - Unofficial Golang bindings for the OpenSlide library
*
* Copyright (c) 2020 GitHub user jammy-dodgers
* https://github.com/jammy-dodgers/gophenslide
* Copyright (c) 2022 Jonas Teuwen, Netherlands Cancer Institute
*
* The bindings have been modified from
* https://github.com/jammy-dodgers/gophenslide
*
* 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.
*/
package openslide
// #cgo CFLAGS: -g -Wall
// #cgo LDFLAGS: -lopenslide
// #include <stdio.h>
// #include <stdlib.h>
// #include <stdint.h>
// #include <openslide.h>
// #include <openslide_go.h>
import "C"
import (
"errors"
"golang.org/x/image/draw"
"image"
"math"
"strconv"
"unsafe"
)
// Slide struct holding the OpenSlide pointer.
type Slide struct {
ptr *C.openslide_t
}
// Open Get the Slide object referring to an OpenSlide image.
// Do not forget to defer opening the slide.
// This is an expensive operation, you will want to cache the result.
func Open(filename string) (Slide, error) {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
slideData := C.openslide_open(cFilename)
if slideData == nil {
return Slide{nil}, errors.New("File " + filename + " unrecognized.")
}
return Slide{slideData}, nil
}
// Close Closes a slide.
func (slide Slide) Close() {
C.openslide_close(slide.ptr)
}
// LevelCount Get the number of levels in the whole slide image.
func (slide Slide) LevelCount() int {
return int(C.openslide_get_level_count(slide.ptr))
}
// LargestLevelDimensions Get the dimensions of level 0, the largest level (aka get_level0_dimensions).
func (slide Slide) LargestLevelDimensions() [2]int {
var a, b C.int64_t
C.openslide_get_level0_dimensions(slide.ptr, &a, &b)
return [2]int{int(a), int(b)}
}
// LevelDimensions Get the dimensions of a level.
func (slide Slide) LevelDimensions(level int) [2]int {
var a, b C.int64_t
C.openslide_get_level_dimensions(slide.ptr, C.int32_t(level), &a, &b)
return [2]int{int(a), int(b)}
}
// LevelDownsample Get the downsampling factor of the given level
func (slide Slide) LevelDownsample(level int) float64 {
return float64(C.openslide_get_level_downsample(slide.ptr, C.int32_t(level)))
}
// LevelDownsamples Get the downsampling factors for all levels
func (slide Slide) LevelDownsamples() []float64 {
downSamples := make([]float64, 0)
for i := 0; i < int(slide.LevelCount()); i++ {
levelDownsample := slide.LevelDownsample(i)
downSamples = append(downSamples, levelDownsample)
}
return downSamples
}
// BestLevelForDownsample Get the best level to use for a particular downsampling factor
func (slide Slide) BestLevelForDownsample(downsample float64) int {
return int(C.openslide_get_best_level_for_downsample(slide.ptr, C.double(downsample)))
}
// ReadRegion Read a region of the image as an RGBA image
func (slide Slide) ReadRegion(x, y int, level int, w, h int) (image.Image, error) {
length := w * h * 4
rawPtr := C.malloc(C.size_t(length))
defer C.free(rawPtr)
C.openslide_read_region(slide.ptr, (*C.uint32_t)(rawPtr), C.int64_t(x), C.int64_t(y), C.int32_t(level), C.int64_t(w), C.int64_t(h))
if txt := C.openslide_get_error(slide.ptr); txt != nil {
return nil, errors.New(C.GoString(txt))
}
// Convert ARGB to RGBA
C.argb2rgba((*C.uint32_t)(rawPtr), C.int(length/4))
rawArray := C.GoBytes(rawPtr, C.int(length))
upLeft := image.Point{}
lowRight := image.Point{X: w, Y: h}
region := image.NewRGBA(image.Rectangle{Min: upLeft, Max: lowRight})
region.Pix = rawArray
return region, nil
}
// AssociatedImageNames Get the names of the associated images
func (slide Slide) AssociatedImageNames() []string {
cAssociatedImageNames := C.openslide_get_associated_image_names(slide.ptr)
var strings []string
for i := 0; C.str_at(cAssociatedImageNames, C.int(i)) != nil; i++ {
strings = append(strings, C.GoString(C.str_at(cAssociatedImageNames, C.int(i))))
}
return strings
}
// AssociatedImageDimensions Get the dimensions of the associated images
func (slide Slide) AssociatedImageDimensions() map[string][2]int {
associatedNames := slide.AssociatedImageNames()
output := make(map[string][2]int)
for _, associatedName := range associatedNames {
var a, b C.int64_t
C.openslide_get_associated_image_dimensions(slide.ptr, C.CString(associatedName), &a, &b)
output[associatedName] = [2]int{int(a), int(b)}
}
return output
}
// ReadAssociatedImage Read an associated image as an RGBA image.
func (slide Slide) ReadAssociatedImage(associatedName string) (image.Image, error) {
dimensions, ok := slide.AssociatedImageDimensions()[associatedName]
if !ok {
return nil, errors.New("associated image does not exist")
}
length := dimensions[0] * dimensions[1] * 4
rawPtr := C.malloc(C.size_t(length))
defer C.free(rawPtr)
C.openslide_read_associated_image(slide.ptr, C.CString(associatedName), (*C.uint32_t)(rawPtr))
if txt := C.openslide_get_error(slide.ptr); txt != nil {
return nil, errors.New(C.GoString(txt))
}
// Convert ARGB to RGBA
C.argb2rgba((*C.uint32_t)(rawPtr), C.int(length/4))
rawArray := C.GoBytes(rawPtr, C.int(length))
upLeft := image.Point{}
lowRight := image.Point{X: dimensions[0], Y: dimensions[1]}
region := image.NewRGBA(image.Rectangle{Min: upLeft, Max: lowRight})
region.Pix = rawArray
return region, nil
}
// DetectVendor Quickly determine whether a whole slide image is recognized.
func DetectVendor(filename string) (string, error) {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
slideVendor := C.openslide_detect_vendor(cFilename)
if slideVendor == nil {
return "", errors.New("No vendor for " + filename)
}
return C.GoString(slideVendor), nil
}
// PropertyNames Get all property names available for this slide.
func (slide Slide) PropertyNames() []string {
cPropNames := C.openslide_get_property_names(slide.ptr)
var strings []string
for i := 0; C.str_at(cPropNames, C.int(i)) != nil; i++ {
strings = append(strings, C.GoString(C.str_at(cPropNames, C.int(i))))
}
return strings
}
// PropertyValue Get the value for a specific property.
func (slide Slide) PropertyValue(propName string) string {
cPropName := C.CString(propName)
defer C.free(unsafe.Pointer(cPropName))
cPropValue := C.openslide_get_property_value(slide.ptr, cPropName)
return C.GoString(cPropValue)
}
// Properties Get all properties as a map.
func (slide Slide) Properties() map[string]string {
propertyNames := slide.PropertyNames()
output := make(map[string]string)
for _, propertyName := range propertyNames {
if slide.PropertyValue(propertyName) != "" {
output[propertyName] = slide.PropertyValue(propertyName)
}
}
return output
}
// GetSpacing Get the spacing of the slide
func (slide Slide) GetSpacing() ([2]float64, error) {
// TODO: For a TIFF different tags need to be read
mppX := slide.PropertyValue(PropMPPX)
mppY := slide.PropertyValue(PropMPPY)
var output [2]float64
if mppX == "" || mppY == "" {
err := errors.New("mpp property not available")
return output, err
}
mppXfloat, err0 := strconv.ParseFloat(mppX, 64)
mppYfloat, err1 := strconv.ParseFloat(mppY, 64)
if err0 != nil && err1 != nil {
err := errors.New("cannot parse mpp values")
return output, err
}
output = [2]float64{mppXfloat, mppYfloat}
return output, nil
}
// GetThumbnail Get thumbnail of the image
func (slide Slide) GetThumbnail(size int) (image.Image, error) {
var dimensions = slide.LargestLevelDimensions()
var downsample float64 = 0
for _, dim := range dimensions {
currDownsample := float64(dim) / float64(size)
if currDownsample >= downsample {
downsample = currDownsample
}
}
bestLevel := slide.BestLevelForDownsample(downsample)
var thumbSize = slide.LevelDimensions(bestLevel)
img, err := slide.ReadRegion(0, 0, bestLevel, thumbSize[0], thumbSize[1])
// Compute the new size
var outputSize [2]int
var scaling float64
if thumbSize[0] <= thumbSize[1] {
outputSize[1] = size
scaling = float64(thumbSize[1]) / float64(size)
outputSize[0] = int(math.Floor(float64(thumbSize[0]) / scaling))
} else {
outputSize[0] = size
scaling = float64(thumbSize[0]) / float64(size)
outputSize[1] = int(math.Floor(float64(thumbSize[1]) / scaling))
}
outputImage := image.NewRGBA(image.Rect(0, 0, int(outputSize[0]), int(outputSize[1])))
draw.BiLinear.Scale(outputImage, outputImage.Bounds(), img, img.Bounds(), draw.Over, nil)
return outputImage, err
}
// Version Get the current version of OpenSlide as a string
func Version() string {
cVer := C.openslide_get_version()
return C.GoString(cVer)
}
// PropBackgroundColor The name of the property containing a slide's background color, if any.
// It is represented as an RGB hex triplet.
const PropBackgroundColor = "openslide.background-color"
// PropBoundsHeight The name of the property containing the height of the rectangle bounding the non-empty region of the slide, if available.
const PropBoundsHeight = "openslide.bounds-height"
// PropBoundsWidth The name of the property containing the width of the rectangle bounding the non-empty region of the slide, if available.
const PropBoundsWidth = "openslide.bounds-width"
// PropBoundsX The name of the property containing the X coordinate of the rectangle bounding the non-empty region of the slide, if available.
const PropBoundsX = "openslide.bounds-x"
// PropBoundsY The name of the property containing the Y coordinate of the rectangle bounding the non-empty region of the slide, if available.
const PropBoundsY = "openslide.bounds-y"
// PropMPPX The name of the property containing the number of microns per pixel in the X dimension of level 0, if known.
const PropMPPX = "openslide.mpp-x"
// PropMPPY The name of the property containing the number of microns per pixel in the Y dimension of level 0, if known.
const PropMPPY = "openslide.mpp-y"
// PropObjectivePower The name of the property containing a slide's objective power, if known.
const PropObjectivePower = "openslide.objective-power"