|
| 1 | +import { getImageSize, MultiscaleImageLayer } from "@hms-dbmi/viv"; |
| 2 | +import { clamp } from "math.gl"; |
| 3 | +import { TileLayer } from "deck.gl"; |
| 4 | +import * as utils from "../utils"; |
| 5 | + |
| 6 | +import type { Matrix4 } from "math.gl"; |
| 7 | +import type { ZarrPixelSource } from "../ZarrPixelSource"; |
| 8 | +import type { PixelData } from "@vivjs/types"; |
| 9 | +import { BitmapLayer } from "deck.gl"; |
| 10 | + |
| 11 | +export type ImageLabelLayerProps = { |
| 12 | + id: string; |
| 13 | + loader: Array<ZarrPixelSource>; |
| 14 | + selection: Array<number>; |
| 15 | + opacity: number; |
| 16 | + visible?: boolean; |
| 17 | + modelMatrix?: Matrix4; |
| 18 | + color?: [r: number, g: number, b: number]; |
| 19 | +}; |
| 20 | + |
| 21 | +// just subclass viv for now |
| 22 | +export class _ImageLabelLayer extends MultiscaleImageLayer<string[]> { |
| 23 | + constructor(props: ImageLabelLayerProps) { |
| 24 | + super({ |
| 25 | + id: `labels-${props.id}`, |
| 26 | + loader: props.loader, |
| 27 | + opacity: props.opacity, |
| 28 | + // @ts-expect-error - Viv types aren't great |
| 29 | + selections: [props.selection], |
| 30 | + colors: [props.color ?? [255, 255, 255]], |
| 31 | + channelsVisible: [true], |
| 32 | + contrastLimits: [[0, 1]], |
| 33 | + modelMatrix: props.modelMatrix, |
| 34 | + }); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export default class ImageLabelLayer extends TileLayer<PixelData> { |
| 39 | + constructor(props: ImageLabelLayerProps) { |
| 40 | + const resolutions = props.loader; |
| 41 | + const dimensions = getImageSize(resolutions[0]); |
| 42 | + super({ |
| 43 | + // Base props |
| 44 | + id: `labels-${props.id}`, |
| 45 | + visible: props.visible, |
| 46 | + opacity: props.opacity, |
| 47 | + modelMatrix: props.modelMatrix, |
| 48 | + tileSize: getTileSizeForResolutions(resolutions), |
| 49 | + extent: [0, 0, dimensions.width, dimensions.height], |
| 50 | + minZoom: Math.round(-(resolutions.length - 1)), |
| 51 | + maxZoom: 0, |
| 52 | + async getTileData(props: any) { |
| 53 | + // types don't match |
| 54 | + const { x, y, z } = props.index; |
| 55 | + const resolution = resolutions[Math.round(-z)]; |
| 56 | + const { data, width, height } = await resolution.getTile({ |
| 57 | + x, |
| 58 | + y, |
| 59 | + signal, |
| 60 | + selection: props.selection, |
| 61 | + }); |
| 62 | + return [{ data, width, height }]; |
| 63 | + }, |
| 64 | + renderSubLayers: (props: any) => { |
| 65 | + console.log(props); |
| 66 | + const [[left, bottom], [right, top]] = props.tile.boundingBox; |
| 67 | + const { width, height } = dimensions; |
| 68 | + const { data, ...otherProps } = props; |
| 69 | + console.log(props.data); |
| 70 | + return new BitmapLayer(otherProps, { |
| 71 | + image: props.data, |
| 72 | + bounds: [clamp(left, 0, width), clamp(top, 0, height), clamp(right, 0, width), clamp(bottom, 0, height)], |
| 73 | + }); |
| 74 | + }, |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function getTileSizeForResolutions(resolutions: Array<ZarrPixelSource>): number { |
| 80 | + const tileSize = resolutions[0].tileSize; |
| 81 | + utils.assert( |
| 82 | + resolutions.every((resolution) => resolution.tileSize === tileSize), |
| 83 | + "resolutions must all have the same tile size", |
| 84 | + ); |
| 85 | + return tileSize; |
| 86 | +} |
0 commit comments