-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkfbreader.cpp
executable file
·241 lines (214 loc) · 7.93 KB
/
kfbreader.cpp
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
#include "kfbreader.h"
const char* names[] = {"openslide.mpp-x", "openslide.mpp-t", "openslide.vendor", "scanScale", nullptr};
const char* assoNames[] = {"label", "macro", "thumbnail", nullptr};
ImgHandle* kfbslide_open(const char * dllPath, const char* filename) {
void *handle = NULL;
handle = dlopen(dllPath, RTLD_LAZY);
if(!handle){
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
ImgHandle* s = new ImgHandle;
s->handle = handle;
DLLInitImageFileFunc InitImageFile = (DLLInitImageFileFunc)dlsym(s->handle,"InitImageFileFunc");
DLLGetHeaderInfoFunc GetHeaderInfo = (DLLGetHeaderInfoFunc) dlsym(s->handle,"GetHeaderInfoFunc");
if(!InitImageFile || !GetHeaderInfo)
{
printf("%s\n", "Error: dlsym failed.");
exit(EXIT_FAILURE);
}
if(!InitImageFile(s->imgStruct, filename)) {
delete s;
return nullptr;
}
HeaderInfoStruct headerInfo;
int getHeaderInfoRtn = GetHeaderInfo(s->imgStruct,
&(headerInfo.Height),
&(headerInfo.Width),
&(headerInfo.ScanScale),
&(headerInfo.SpendTime),
&(headerInfo.ScanTime),
&(headerInfo.CapRes),
&(headerInfo.BlockSize));
if(!getHeaderInfoRtn) {
delete s;
return nullptr;
}
s->properties["openslide.mpp-x"] = to_string(headerInfo.CapRes);
s->properties["openslide.mpp-t"] = to_string(headerInfo.CapRes);
s->properties["openslide.vendor"] = string("Kfbio");
s->properties["scanScale"] = to_string(headerInfo.ScanScale);
s->scanScale = headerInfo.ScanScale;
s->height = headerInfo.Height;
s->width = headerInfo.Width;
s->maxLevel = min(6, (int)(log(max(s->height, s->width)) / log(2)));
return s;
}
void kfbslide_close(ImgHandle* s) {
DLLUnInitImageFileFunc UnInitImageFile = (DLLUnInitImageFileFunc)dlsym(s->handle, "UnInitImageFileFunc");
if(!UnInitImageFile) {
printf("%s\n", "Error: dlsym failed.");
exit(EXIT_FAILURE);
}
UnInitImageFile(s->imgStruct);
delete s;
}
const char * kfbslide_detect_vendor(const char *) {
return "kfbio";
}
const char * kfbslide_property_value(ImgHandle* preader, const char * attribute_name) {
map<string, string> & m = preader->properties;
string s = attribute_name;
auto iter = m.find(s);
if(iter != m.end()) {
return iter->second.c_str();
}
return nullptr;
}
const char** kfbslide_property_names(ImgHandle* preader) {
return names;
}
/*
Level Related...
*/
double kfbslide_get_level_downsample(ImgHandle* s, int level) {
if(s->maxLevel > level && level >= 0) return double(1LL << level);
return 0.0;
}
int kfbslide_get_best_level_for_downsample(ImgHandle* s, double downsample) {
if(downsample < 1) return 0;
double downsample_factor = downsample;
for(int i = 0; i < s->maxLevel; i++) {
if((1LL << (i + 1)) > downsample_factor ) return i;
}
return s->maxLevel - 1;
}
int kfbslide_get_level_count(ImgHandle* s) {
return int(s->maxLevel);
}
ll kfbslide_get_level_dimensions(ImgHandle* s, int level, ll* width, ll* height) {
if(level >= s->maxLevel || level < 0) return 0;
if(!width || !height) {
printf("You must pass width and height ptr ByRef!");
return 0;
}
*width = s->width >> level;
*height = s->height >> level;
return *height;
}
/*
Associated Image
*/
//! 希望对读出数据的任何改变都不会影响下一次读取, 因此必须拷贝
BYTE* kfbslide_read_associated_image(ImgHandle* s, const char* name) {
string n(name);
auto iter = s->assoImages.find(n);
if(iter != s->assoImages.end()) {
BYTE* buf = new BYTE[iter->second.nBytes];
memcpy(buf, iter->second.buf.get(), iter->second.nBytes);
s->alloc_mem.push_back(buf);
return buf;
}
return nullptr;
}
void kfbslide_get_associated_image_dimensions(ImgHandle* s, const char* name, ll* width, ll*height, ll*nBytes) {
if(!width || !height || !nBytes) {
printf("You must pass width, height and nBytes ptr ByRef!");
return;
}
string n(name);
auto iter = s->assoImages.find(n);
if(iter != s->assoImages.end()) {
*width = iter->second.width;
*height = iter->second.height;
*nBytes = iter->second.nBytes;
return;
}
*width = 0;
*height = 0;
*nBytes = 0;
}
const char** kfbslide_get_associated_image_names(ImgHandle* s) {
if(s->assoNames) return s->assoNames;
s->assoNames = new const char*[4]{nullptr};
// Try to call getThumb, getPreview, getLabel
DLLGetImageFunc GetThumbnailImageFunc, GetPreviewImageFunc, GetLabelImageFunc;
GetThumbnailImageFunc = (DLLGetImageFunc)dlsym(s->handle,"GetThumnailImageFunc");
GetPreviewImageFunc = (DLLGetImageFunc)dlsym(s->handle,"GetPriviewInfoFunc");
GetLabelImageFunc = (DLLGetImageFunc)dlsym(s->handle,"GetLableInfoFunc");
if(!GetThumbnailImageFunc || !GetPreviewImageFunc || !GetLabelImageFunc) {
printf("%s\n", "Error: dlsym failed.");
exit(EXIT_FAILURE);
}
BYTE* buf = nullptr;
// bytesize width height
int ret[3] = {0, 0, 0};
int cnt = 0;
if(GetLabelImageFunc(s->imgStruct, &buf, ret, ret + 1, ret + 2)) {
s->assoImages["label"] = AssoImage{ret[0], ret[1], ret[2], shared_ptr<BYTE>(buf, default_delete<BYTE []>())};
s->assoNames[cnt++] = "label";
}
if(GetThumbnailImageFunc(s->imgStruct, &buf, ret, ret + 1, ret + 2)) {
s->assoImages["thumbnail"] = AssoImage{ret[0], ret[1], ret[2],shared_ptr<BYTE>(buf, default_delete<BYTE []>())};
s->assoNames[cnt++] = "thumbnail";
}
if(GetPreviewImageFunc(s->imgStruct, &buf, ret, ret + 1, ret + 2)) {
s->assoImages["macro"] = AssoImage{ret[0], ret[1], ret[2],shared_ptr<BYTE>(buf, default_delete<BYTE []>())};
s->assoNames[cnt++] = "macro";
}
return s->assoNames;
}
/*
Load Data
*/
bool kfbslide_read_region(ImgHandle* s, int level, int x, int y, int* nBytes, BYTE** buf) {
if(level < 0 || level >= s->maxLevel) return false;
if(!buf || !nBytes) {
printf("You must pass nBytes and buf ptr ByRef!");
return false;
}
DLLGetImageStreamFunc GetImageStreamFunc = (DLLGetImageStreamFunc)dlsym(s->handle,"GetImageStreamFunc");
if(GetImageStreamFunc == NULL)
{
printf("%s\n", "Error: dlsym failed.");
exit(EXIT_FAILURE);
}
float fScale = s->scanScale / kfbslide_get_level_downsample(s, level);
void* ptr = GetImageStreamFunc(s->imgStruct, fScale, x, y, nBytes, buf);
s->alloc_mem.push_back(*buf);
return *nBytes > 0;
}
bool kfbslide_get_image_roi_stream(ImgHandle* s, int level, int x, int y, int width, int height, int* nBytes, BYTE** buf) {
if(level < 0 || level >= s->maxLevel) return false;
if(!buf || !nBytes) {
printf("You must pass nBytes and buf ptr ByRef!");
return false;
}
DLLGetImageDataRoiFunc GetImageDataRoi = (DLLGetImageDataRoiFunc)dlsym(s->handle,"GetImageDataRoiFunc");
if(GetImageDataRoi == NULL)
{
printf("%s\n", "Error: dlsym failed.");
exit(EXIT_FAILURE);
}
double downsample_factor = kfbslide_get_level_downsample(s, level);
float fScale = s->scanScale / downsample_factor;
x = x / downsample_factor;
y = y / downsample_factor;
bool ret = GetImageDataRoi(s->imgStruct, fScale, x, y, width, height, buf, nBytes, true);
s->alloc_mem.push_back(*buf);
return ret;
}
/*
free resource
*/
bool kfbslide_buffer_free(ImgHandle* s, BYTE* buf) {
if(!buf) return false;
for(auto iter=s->alloc_mem.begin(); iter != s->alloc_mem.end(); iter++) {
if(*iter == buf) {
delete [] buf;
s->alloc_mem.erase(iter);
return true;
}
}
return false;
}