-
Notifications
You must be signed in to change notification settings - Fork 24
/
wk_layer.js
123 lines (99 loc) · 3.45 KB
/
wk_layer.js
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
/**
* wk_layer.js
* @flow
*/
import Layer, { REQUEST_TIMEOUT } from "oxalis/model/binary/layers/layer";
import type { BucketRequestOptions } from "oxalis/model/binary/layers/layer";
import BucketBuilder from "oxalis/model/binary/layers/bucket_builder";
import type { BucketInfo } from "oxalis/model/binary/layers/bucket_builder";
import Request from "libs/request";
import MultipartData from "libs/multipart_data";
import type { Vector4 } from "oxalis/constants";
import type { DataLayerType, DataStoreInfoType } from "oxalis/store";
// TODO: Non-reactive
class WkLayer extends Layer {
constructor(layerInfo: DataLayerType, dataStoreInfo: DataStoreInfoType) {
super(layerInfo, dataStoreInfo);
if (this.dataStoreInfo.typ !== "webknossos-store") {
throw new Error("WkLayer should only be instantiated with webknossos-store");
}
this.fourBit = false;
}
setFourBit(newFourBit: boolean) {
// No op if this is not a color layer
if (this.category === "color") {
this.fourBit = newFourBit;
}
}
buildBuckets(batch: Array<Vector4>, options: ?BucketRequestOptions) {
if (options == null) {
options = { fourBit: this.fourBit };
} else {
options.fourBit = this.fourBit;
}
return super.buildBuckets(batch, options);
}
async requestFromStoreImpl(batch: Array<BucketInfo>, token: string): Promise<Uint8Array> {
const wasFourBit = this.fourBit;
const requestData = new MultipartData();
for (const bucket of batch) {
requestData.addPart({
"X-Bucket": JSON.stringify(bucket),
});
}
const datasetName = this.getDatasetName();
const data = await requestData.dataPromise();
const responseBuffer = await Request.sendArraybufferReceiveArraybuffer(
`${this.dataStoreInfo.url}/data/datasets/${datasetName}/layers/${this.name}/data?token=${token}`,
{
data,
headers: {
"Content-Type": `multipart/mixed; boundary=${requestData.boundary}`,
},
timeout: REQUEST_TIMEOUT,
compress: true,
doNotCatch: true,
});
let result = new Uint8Array(responseBuffer);
if (wasFourBit) {
result = this.decodeFourBit(result);
}
return result;
}
decodeFourBit(bufferArray: Uint8Array): Uint8Array {
// Expand 4-bit data
const newColors = new Uint8Array(bufferArray.length << 1);
let index = 0;
while (index < newColors.length) {
const value = bufferArray[index >> 1];
newColors[index] = value & 0b11110000;
index++;
newColors[index] = value << 4;
index++;
}
return newColors;
}
async sendToStoreImpl(batch: Array<BucketInfo>, getBucketData: (Vector4) => Uint8Array, token: string): Promise<void> {
const transmitData = new MultipartData();
for (const bucket of batch) {
transmitData.addPart(
{ "X-Bucket": JSON.stringify(bucket) },
getBucketData(BucketBuilder.bucketToZoomedAddress(bucket)));
}
const datasetName = this.getDatasetName();
const data = await transmitData.dataPromise();
await Request.sendArraybufferReceiveArraybuffer(
`${this.dataStoreInfo.url}/data/datasets/${datasetName}/layers/${this.name}/data?token=${token}`, {
method: "PUT",
data,
headers: {
"Content-Type": `multipart/mixed; boundary=${transmitData.boundary}`,
},
timeout: REQUEST_TIMEOUT,
compress: true,
doNotCatch: true,
},
);
}
}
export default WkLayer;