Ejemplo n.º 1
0
// getTileImage returns a 2d tile image or a placeholder, useful for further stitching before
// delivery of a final image.
func (d *Data) getTileImage(ctx storage.Context, req TileReq) (image.Image, error) {
	if d.Levels == nil || len(d.Levels) == 0 {
		return nil, ErrNoMetadataSet
	}
	data, err := d.getTileData(ctx, req)
	if err != nil {
		return nil, err
	}

	if len(data) == 0 {
		if d.Placeholder {
			if req.scale < 0 || req.scale >= Scaling(len(d.Levels)) {
				return nil, fmt.Errorf("Could not find tile specification at given scale %d", req.scale)
			}
			message := fmt.Sprintf("%s Tile coord %s @ scale %d", req.plane, req.tile, req.scale)
			return dvid.PlaceholderImage(req.plane, d.Levels[req.scale].TileSize, message)
		}
		return nil, nil // Not found
	}

	var goImg image.Image
	switch d.Encoding {
	case LZ4:
		var img dvid.Image
		err = img.Deserialize(data)
		if err != nil {
			return nil, err
		}
		goImg = img.Get()
	case PNG:
		pngBuffer := bytes.NewBuffer(data)
		goImg, err = png.Decode(pngBuffer)
	case JPG:
		jpgBuffer := bytes.NewBuffer(data)
		goImg, err = jpeg.Decode(jpgBuffer)
	default:
		return nil, fmt.Errorf("Unknown tile encoding: %s", d.Encoding)
	}
	return goImg, err
}
Ejemplo n.º 2
0
// GetTile returns a 2d tile image or a placeholder
func (d *Data) GetTile(ctx storage.Context, shape dvid.DataShape, scaling Scaling, index dvid.IndexZYX) (image.Image, error) {
	data, err := d.getTileData(ctx, shape, scaling, index)
	if err != nil {
		return nil, err
	}
	tileIndex := &IndexTile{&index, shape, scaling}

	if data == nil {
		if d.Placeholder {
			if d.Levels == nil || scaling < 0 || scaling >= Scaling(len(d.Levels)) {
				return nil, fmt.Errorf("Could not find tile specification at given scale %d", scaling)
			}
			message := fmt.Sprintf("%s Tile coord %s @ scale %d", shape, tileIndex, scaling)
			return dvid.PlaceholderImage(shape, d.Levels[scaling].TileSize, message)
		}
		return nil, nil // Not found
	}

	var goImg image.Image
	switch d.Encoding {
	case LZ4:
		var img dvid.Image
		err = img.Deserialize(data)
		if err != nil {
			return nil, err
		}
		goImg = img.Get()
	case PNG:
		pngBuffer := bytes.NewBuffer(data)
		goImg, err = png.Decode(pngBuffer)
	case JPG:
		jpgBuffer := bytes.NewBuffer(data)
		goImg, err = jpeg.Decode(jpgBuffer)
	default:
		return nil, fmt.Errorf("Unknown tile encoding: %s", d.Encoding)
	}
	return goImg, err
}