func TestLabelblkDirectAPI(t *testing.T) { tests.UseStore() defer tests.CloseStore() uuid, versionID := initTestRepo() labels := newDataInstance(uuid, t, "mylabels") labelsCtx := datastore.NewVersionedCtx(labels, versionID) // Create a fake block-aligned label volume offset := dvid.Point3d{32, 0, 64} size := dvid.Point3d{96, 64, 160} subvol := dvid.NewSubvolume(offset, size) data := makeVolume(offset, size) // Store it into datastore at root v, err := labels.NewVoxels(subvol, data) if err != nil { t.Fatalf("Unable to make new labels Voxels: %v\n", err) } if err = labels.PutVoxels(versionID, v, nil); err != nil { t.Errorf("Unable to put labels for %s: %v\n", labelsCtx, err) } if v.NumVoxels() != int64(len(data))/8 { t.Errorf("# voxels (%d) after PutVoxels != # original voxels (%d)\n", v.NumVoxels(), int64(len(data))/8) } // Read the stored image v2, err := labels.NewVoxels(subvol, nil) if err != nil { t.Errorf("Unable to make new labels ExtHandler: %v\n", err) } if err = labels.GetVoxels(versionID, v2, nil); err != nil { t.Errorf("Unable to get voxels for %s: %v\n", labelsCtx, err) } // Make sure the retrieved image matches the original if v.Stride() != v2.Stride() { t.Errorf("Stride in retrieved subvol incorrect\n") } if v.Interpolable() != v2.Interpolable() { t.Errorf("Interpolable bool in retrieved subvol incorrect\n") } if !reflect.DeepEqual(v.Size(), v2.Size()) { t.Errorf("Size in retrieved subvol incorrect: %s vs expected %s\n", v2.Size(), v.Size()) } if v.NumVoxels() != v2.NumVoxels() { t.Errorf("# voxels in retrieved is different: %d vs expected %d\n", v2.NumVoxels(), v.NumVoxels()) } byteData := v2.Data() for i := int64(0); i < v2.NumVoxels()*8; i++ { if byteData[i] != data[i] { t.Logf("Size of data: %d bytes from GET, %d bytes in PUT\n", len(data), len(data)) t.Fatalf("GET subvol (%d) != PUT subvol (%d) @ uint64 #%d", byteData[i], data[i], i) } } }
func TestFloatDirectCalls(t *testing.T) { datastore.OpenTest() defer datastore.CloseTest() uuid, versionID := initTestRepo() server.CreateTestInstance(t, uuid, "float32blk", "floatimg", dvid.Config{}) dataservice, err := datastore.GetDataByUUIDName(uuid, "floatimg") if err != nil { t.Fatal(err) } floatimg, ok := dataservice.(*Data) if !ok { t.Fatalf("Can't convert dataservice %v into imageblk.Data\n", dataservice) } ctx := datastore.NewVersionedCtx(floatimg, versionID) // Create a block-aligned 8-bit grayscale image offset := dvid.Point3d{512, 32, 1024} size := dvid.Point3d{128, 96, 64} subvol := dvid.NewSubvolume(offset, size) testvol := createFloatTestVolume(t, uuid, "floatimg", offset, size) origData := make([]byte, len(testvol.data)) copy(origData, testvol.data) // Store it into datastore at root v, err := floatimg.NewVoxels(subvol, testvol.data) if err != nil { t.Fatalf("Unable to make new floatimg voxels: %v\n", err) } if err = floatimg.IngestVoxels(versionID, 1, v, ""); err != nil { t.Errorf("Unable to put voxels for %s: %v\n", ctx, err) } // Read the stored image v2, err := floatimg.NewVoxels(subvol, nil) if err != nil { t.Errorf("Unable to make new grayscale ExtHandler: %v\n", err) } if err = floatimg.GetVoxels(versionID, v2, ""); err != nil { t.Errorf("Unable to get voxels for %s: %v\n", ctx, err) } // Make sure the retrieved image matches the original if v.Stride() != v2.Stride() { t.Errorf("Stride in retrieved subvol incorrect\n") } if v.Interpolable() != v2.Interpolable() { t.Errorf("Interpolable bool in retrieved subvol incorrect\n") } if !reflect.DeepEqual(v.Size(), v2.Size()) { t.Errorf("Size in retrieved subvol incorrect: %s vs expected %s\n", v2.Size(), v.Size()) } if v.NumVoxels() != v2.NumVoxels() { t.Errorf("# voxels in retrieved is different: %d vs expected %d\n", v2.NumVoxels(), v.NumVoxels()) } if len(v.Data()) != len(v2.Data()) { t.Errorf("Expected %d bytes in retrieved data, got %d bytes\n", len(v.Data()), len(v2.Data())) } received := v2.Data() //dvid.PrintNonZero("original value", origData) //dvid.PrintNonZero("returned value", data) for i := int64(0); i < v2.NumVoxels(); i++ { if received[i] != origData[i] { t.Logf("Data returned != data stored for voxel %d\n", i) t.Logf("Size of data: %d bytes from GET, %d bytes in PUT\n", len(received), len(origData)) t.Fatalf("GET subvol (%d) != PUT subvol (%d) @ index %d", received[i], origData[i], i) } } }
func TestDirectCalls(t *testing.T) { datastore.OpenTest() defer datastore.CloseTest() uuid, versionID := initTestRepo() grayscale := makeGrayscale(uuid, t, "grayscale") grayscaleCtx := datastore.NewVersionedCtx(grayscale, versionID) // Create a block-aligned 8-bit grayscale image offset := dvid.Point3d{512, 32, 1024} size := dvid.Point3d{128, 96, 64} subvol := dvid.NewSubvolume(offset, size) data := makeVolume(offset, size) origData := make([]byte, len(data)) copy(origData, data) // Store it into datastore at root v, err := grayscale.NewVoxels(subvol, data) if err != nil { t.Fatalf("Unable to make new grayscale voxels: %v\n", err) } if err = grayscale.PutVoxels(versionID, v, nil); err != nil { t.Errorf("Unable to put voxels for %s: %v\n", grayscaleCtx, err) } if v.NumVoxels() != int64(len(origData)) { t.Errorf("# voxels (%d) after PutVoxels != # original voxels (%d)\n", v.NumVoxels(), int64(len(origData))) } // Read the stored image v2, err := grayscale.NewVoxels(subvol, nil) if err != nil { t.Errorf("Unable to make new grayscale ExtHandler: %v\n", err) } if err = grayscale.GetVoxels(versionID, v2, nil); err != nil { t.Errorf("Unable to get voxels for %s: %v\n", grayscaleCtx, err) } // Make sure the retrieved image matches the original if v.Stride() != v2.Stride() { t.Errorf("Stride in retrieved subvol incorrect\n") } if v.Interpolable() != v2.Interpolable() { t.Errorf("Interpolable bool in retrieved subvol incorrect\n") } if !reflect.DeepEqual(v.Size(), v2.Size()) { t.Errorf("Size in retrieved subvol incorrect: %s vs expected %s\n", v2.Size(), v.Size()) } if v.NumVoxels() != v2.NumVoxels() { t.Errorf("# voxels in retrieved is different: %d vs expected %d\n", v2.NumVoxels(), v.NumVoxels()) } data = v2.Data() //dvid.PrintNonZero("original value", origData) //dvid.PrintNonZero("returned value", data) for i := int64(0); i < v2.NumVoxels(); i++ { if data[i] != origData[i] { t.Logf("Data returned != data stored for voxel %d\n", i) t.Logf("Size of data: %d bytes from GET, %d bytes in PUT\n", len(data), len(origData)) t.Fatalf("GET subvol (%d) != PUT subvol (%d) @ index %d", data[i], origData[i], i) } } }
func (V3DRawMarshaler) UnmarshalV3DRaw(reader io.Reader) ([]*Channel, error) { magicString := make([]byte, 24) if n, err := reader.Read(magicString); n != 24 || err != nil { return nil, fmt.Errorf("error reading magic string in V3D Raw file: %v", err) } if string(magicString) != "raw_image_stack_by_hpeng" { return nil, fmt.Errorf("bad magic string in V3D Raw File: %s", string(magicString)) } endianType := make([]byte, 1, 1) if n, err := reader.Read(endianType); n != 1 || err != nil { return nil, fmt.Errorf("could not read endianness of V3D Raw file: %v", err) } var byteOrder binary.ByteOrder switch string(endianType) { case "L": byteOrder = binary.LittleEndian case "B": return nil, fmt.Errorf("cannot handle big endian byte order in V3D Raw File") default: return nil, fmt.Errorf("illegal byte order '%s' in V3D Raw File", endianType) } var dataType uint16 if err := binary.Read(reader, byteOrder, &dataType); err != nil { return nil, err } var bytesPerVoxel int32 switch dataType { case 1: bytesPerVoxel = 1 case 2: bytesPerVoxel = 2 default: return nil, fmt.Errorf("cannot handle V3D Raw File with data type %d", dataType) } var width, height, depth, numChannels uint32 if err := binary.Read(reader, byteOrder, &width); err != nil { return nil, fmt.Errorf("error reading width in V3D Raw File: %v", err) } if err := binary.Read(reader, byteOrder, &height); err != nil { return nil, fmt.Errorf("error reading height in V3D Raw File: %v", err) } if err := binary.Read(reader, byteOrder, &depth); err != nil { return nil, fmt.Errorf("error reading depth in V3D Raw File: %v", err) } if err := binary.Read(reader, byteOrder, &numChannels); err != nil { return nil, fmt.Errorf("error reading # channels in V3D Raw File: %v", err) } // Allocate the V3DRaw struct for the # channels totalBytes := int(bytesPerVoxel) * int(width*height*depth) size := dvid.Point3d{int32(width), int32(height), int32(depth)} volume := dvid.NewSubvolume(dvid.Point3d{0, 0, 0}, size) v3draw := make([]*Channel, numChannels, numChannels) var c int32 for c = 0; c < int32(numChannels); c++ { data := make([]uint8, totalBytes, totalBytes) var t dvid.DataType switch bytesPerVoxel { case 1: t = dvid.T_uint8 case 2: t = dvid.T_uint16 } values := dvid.DataValues{ { T: t, Label: fmt.Sprintf("channel%d", c), }, } v := imageblk.NewVoxels(volume, values, data, int32(width)*bytesPerVoxel) v3draw[c] = &Channel{ Voxels: v, channelNum: c + 1, } } // Read in the data for each channel for c = 0; c < int32(numChannels); c++ { if err := binary.Read(reader, byteOrder, v3draw[c].Data()); err != nil { return nil, fmt.Errorf("error reading data for channel %d: %v", c, err) } } return v3draw, nil }