/
composite.go
263 lines (236 loc) · 7.49 KB
/
composite.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
/*
Functinos that support compositing label and image blocks into a pseudo-color representation.
*/
package labelarray
import (
"bytes"
"encoding/binary"
"fmt"
"sync"
"github.com/janelia-flyem/dvid/datastore"
"github.com/janelia-flyem/dvid/datatype/common/labels"
"github.com/janelia-flyem/dvid/datatype/imageblk"
"github.com/janelia-flyem/dvid/dvid"
"github.com/janelia-flyem/dvid/server"
"github.com/janelia-flyem/dvid/storage"
)
type compositeOp struct {
grayscale *imageblk.Data
composite *imageblk.Data
versionID dvid.VersionID
}
// CreateComposite creates a new rgba8 image by combining hash of labels + the grayscale
func (d *Data) CreateComposite(request datastore.Request, reply *datastore.Response) error {
timedLog := dvid.NewTimeLog()
// Parse the request
var uuidStr, dataName, cmdStr, grayscaleName, destName string
request.CommandArgs(1, &uuidStr, &dataName, &cmdStr, &grayscaleName, &destName)
// Get the version
uuid, v, err := datastore.MatchingUUID(uuidStr)
if err != nil {
return err
}
// Log request
if err = datastore.AddToNodeLog(uuid, []string{request.Command.String()}); err != nil {
return err
}
// Get the grayscale data.
dataservice, err := datastore.GetDataByUUIDName(uuid, dvid.InstanceName(grayscaleName))
if err != nil {
return err
}
grayscale, ok := dataservice.(*imageblk.Data)
if !ok {
return fmt.Errorf("%s is not the name of uint8 data", grayscaleName)
}
// Create a new rgba8blk data.
var compservice datastore.DataService
compservice, err = datastore.GetDataByUUIDName(uuid, dvid.InstanceName(destName))
if err == nil {
return fmt.Errorf("Data instance with name %q already exists", destName)
}
typeService, err := datastore.TypeServiceByName("rgba8blk")
if err != nil {
return fmt.Errorf("Could not get rgba8 type service from DVID")
}
config := dvid.NewConfig()
compservice, err = datastore.NewData(uuid, typeService, dvid.InstanceName(destName), config)
if err != nil {
return err
}
composite, ok := compservice.(*imageblk.Data)
if !ok {
return fmt.Errorf("Error: %s was unable to be set to rgba8 data", destName)
}
// Iterate through all labels and grayscale chunks incrementally in Z, a layer at a time.
wg := new(sync.WaitGroup)
op := &compositeOp{grayscale, composite, v}
chunkOp := &storage.ChunkOp{op, wg}
store, err := datastore.GetOrderedKeyValueDB(d)
if err != nil {
return err
}
ctx := datastore.NewVersionedCtx(d, v)
extents := d.Extents()
blockBeg := imageblk.NewTKey(extents.MinIndex)
blockEnd := imageblk.NewTKey(extents.MaxIndex)
err = store.ProcessRange(ctx, blockBeg, blockEnd, chunkOp, storage.ChunkFunc(d.CreateCompositeChunk))
wg.Wait()
// Set new mapped data to same extents.
composite.Properties.Extents = grayscale.Properties.Extents
if err := datastore.SaveDataByUUID(uuid, composite); err != nil {
dvid.Infof("Could not save new data '%s': %v\n", destName, err)
}
timedLog.Infof("Created composite of %s and %s", grayscaleName, destName)
return nil
}
// CreateCompositeChunk processes each chunk of labels and grayscale data,
// saving the composited result into an rgba8.
// Only some multiple of the # of CPU cores can be used for chunk handling before
// it waits for chunk processing to abate via the buffered server.HandlerToken channel.
func (d *Data) CreateCompositeChunk(chunk *storage.Chunk) error {
server.CheckChunkThrottling()
go d.createCompositeChunk(chunk)
return nil
}
var curZ int32
var curZMutex sync.Mutex
func (d *Data) createCompositeChunk(chunk *storage.Chunk) {
defer func() {
// After processing a chunk, return the token.
server.HandlerToken <- 1
// Notify the requestor that this chunk is done.
if chunk.Wg != nil {
chunk.Wg.Done()
}
}()
op := chunk.Op.(*compositeOp)
// Get the spatial index associated with this chunk.
zyx, err := imageblk.DecodeTKey(chunk.K)
if err != nil {
dvid.Errorf("Error in %s.ChunkApplyMap(): %v", d.Data.DataName(), err)
return
}
// Initialize the label buffers. For voxels, this data needs to be uncompressed and deserialized.
curZMutex.Lock()
if zyx[2] > curZ {
curZ = zyx[2]
minZ := zyx.MinPoint(d.BlockSize()).Value(2)
maxZ := zyx.MaxPoint(d.BlockSize()).Value(2)
dvid.Debugf("Now creating composite blocks for Z %d to %d\n", minZ, maxZ)
}
curZMutex.Unlock()
deserialization, _, err := dvid.DeserializeData(chunk.V, true)
if err != nil {
dvid.Errorf("Unable to deserialize block in %q: %v\n", d.DataName(), err)
return
}
var block labels.Block
if err = block.UnmarshalBinary(deserialization); err != nil {
dvid.Errorf("Unable to unmarshal binary block: %v\n", err)
return
}
labelData, _ := block.MakeLabelVolume()
blockBytes := len(labelData)
if blockBytes%8 != 0 {
dvid.Infof("Retrieved, deserialized block is wrong size: %d bytes\n", blockBytes)
return
}
// Get the corresponding grayscale block.
store, err := datastore.GetOrderedKeyValueDB(op.grayscale)
if err != nil {
dvid.Errorf("Unable to retrieve store for %q: %v\n", op.grayscale.DataName(), err)
return
}
grayscaleCtx := datastore.NewVersionedCtx(op.grayscale, op.versionID)
blockData, err := store.Get(grayscaleCtx, chunk.K)
if err != nil {
dvid.Errorf("Error getting grayscale block for index %s\n", zyx)
return
}
grayscaleData, _, err := dvid.DeserializeData(blockData, true)
if err != nil {
dvid.Errorf("Unable to deserialize block in '%s': %v\n", op.grayscale.DataName(), err)
return
}
// Compute the composite block.
// TODO -- Exploit run lengths, use cache of hash?
compositeBytes := blockBytes / 2
compositeData := make([]byte, compositeBytes, compositeBytes)
compositeI := 0
labelI := 0
hashBuf := make([]byte, 4, 4)
for _, grayscale := range grayscaleData {
//murmurhash3(labelData[labelI:labelI+8], hashBuf)
//hashBuf[3] = grayscale
writePseudoColor(grayscale, labelData[labelI:labelI+8], hashBuf)
copy(compositeData[compositeI:compositeI+4], hashBuf)
compositeI += 4
labelI += 8
}
// Store the composite block into the rgba8 data.
serialization, err := dvid.SerializeData(compositeData, d.Compression(), d.Checksum())
if err != nil {
dvid.Errorf("Unable to serialize composite block %s: %v\n", zyx, err)
return
}
compositeCtx := datastore.NewVersionedCtx(op.composite, op.versionID)
err = store.Put(compositeCtx, chunk.K, serialization)
if err != nil {
dvid.Errorf("Unable to PUT composite block %s: %v\n", zyx, err)
return
}
}
func writePseudoColor(grayscale uint8, in64bits, out32bits []byte) {
murmurhash3(in64bits, out32bits)
var t uint64
t = uint64(out32bits[0]) * uint64(grayscale)
t >>= 8
out32bits[0] = uint8(t)
t = uint64(out32bits[1]) * uint64(grayscale)
t >>= 8
out32bits[1] = uint8(t)
t = uint64(out32bits[2]) * uint64(grayscale)
t >>= 8
out32bits[2] = uint8(t)
out32bits[3] = 255
}
func murmurhash3(in64bits, out32bits []byte) {
length := len(in64bits)
var c1, c2 uint32 = 0xcc9e2d51, 0x1b873593
nblocks := length / 4
var h, k uint32
buf := bytes.NewBuffer(in64bits)
for i := 0; i < nblocks; i++ {
binary.Read(buf, binary.LittleEndian, &k)
k *= c1
k = (k << 15) | (k >> (32 - 15))
k *= c2
h ^= k
h = (h << 13) | (h >> (32 - 13))
h = (h * 5) + 0xe6546b64
}
k = 0
tailIndex := nblocks * 4
switch length & 3 {
case 3:
k ^= uint32(in64bits[tailIndex+2]) << 16
fallthrough
case 2:
k ^= uint32(in64bits[tailIndex+1]) << 8
fallthrough
case 1:
k ^= uint32(in64bits[tailIndex])
k *= c1
k = (k << 15) | (k >> (32 - 15))
k *= c2
h ^= k
}
h ^= uint32(length)
h ^= h >> 16
h *= 0x85ebca6b
h ^= h >> 13
h *= 0xc2b2ae35
h ^= h >> 16
binary.BigEndian.PutUint32(out32bits, h)
}