forked from saalfeldlab/bigcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabelUtils.java
70 lines (56 loc) · 1.94 KB
/
LabelUtils.java
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
package bdv.labels.labelset;
import java.nio.ByteBuffer;
import bdv.labels.labelset.Multiset.Entry;
import gnu.trove.list.array.TIntArrayList;
import net.imglib2.IterableInterval;
public class LabelUtils {
// TODO don't return bytes, take as argument
public static byte[] serializeLabelMultisetTypes(IterableInterval<LabelMultisetType> lmts, int numElements) {
int[] data = new int[numElements];
final LongMappedAccessData listData = LongMappedAccessData.factory.createStorage( 32 );
final LabelMultisetEntryList list = new LabelMultisetEntryList( listData, 0 );
final LabelMultisetEntryList list2 = new LabelMultisetEntryList();
final TIntArrayList listHashesAndOffsets = new TIntArrayList();
final LabelMultisetEntry tentry = new LabelMultisetEntry( 0, 1 );
int nextListOffset = 0;
int o = 0;
for ( LabelMultisetType lmt : lmts )
{
list.createListAt( listData, nextListOffset );
for(Entry<Label> entry : lmt.entrySet()) {
tentry.setId( entry.getElement().id() );
tentry.setCount( entry.getCount() );
list.add( tentry );
}
boolean makeNewList = true;
final int hash = list.hashCode();
for ( int i = 0; i < listHashesAndOffsets.size(); i += 2 )
{
if ( hash == listHashesAndOffsets.get( i ) )
{
list2.referToDataAt( listData, listHashesAndOffsets.get( i + 1 ) );
if ( list.equals( list2 ) )
{
makeNewList = false;
data[ o++ ] = listHashesAndOffsets.get( i + 1 ) ;
break;
}
}
}
if ( makeNewList )
{
data[ o++ ] = nextListOffset;
listHashesAndOffsets.add( hash );
listHashesAndOffsets.add( nextListOffset );
nextListOffset += list.getSizeInBytes();
}
}
final byte[] bytes = new byte[ 4 * data.length + nextListOffset ];
ByteBuffer bb = ByteBuffer.wrap( bytes );
for ( final int d : data )
bb.putInt(d);
for ( int i = 0; i < nextListOffset; ++i )
bb.put(ByteUtils.getByte( listData.data, i ));
return bytes;
}
}