diff --git a/src/ImJoyPluginAPI.js b/src/ImJoyPluginAPI.js index a8edc29c..d6cba164 100644 --- a/src/ImJoyPluginAPI.js +++ b/src/ImJoyPluginAPI.js @@ -358,6 +358,18 @@ class ImJoyPluginAPI { getCompareImages(name) { return this.viewer.getCompareImages(name) } + + getLoadedScale(scale) { + return this.viewer.getLoadedScale(scale) + } + + getCroppedImageWorldBounds() { + return this.viewer.getCroppedImageWorldBounds() + } + + async getCroppedIndexBounds(scale) { + return await this.viewer.getCroppedIndexBounds(scale) + } } export default ImJoyPluginAPI diff --git a/src/createViewer.js b/src/createViewer.js index ea3f2a02..d2590501 100644 --- a/src/createViewer.js +++ b/src/createViewer.js @@ -1,3 +1,4 @@ +import { mat4 } from 'gl-matrix' import { inspect } from '@xstate/inspect' import { interpret } from 'xstate' @@ -13,11 +14,13 @@ import hex2rgb from './UserInterface/hex2rgb' import ViewerStore from './ViewerStore' import toMultiscaleSpatialImage from './IO/toMultiscaleSpatialImage' +import { worldBoundsToIndexBounds } from './IO/MultiscaleSpatialImage' import viewerMachineOptions from './viewerMachineOptions' import createViewerMachine from './createViewerMachine' import ViewerMachineContext from './Context/ViewerMachineContext' import { addCroppingPlanes, + getCropWidgetBounds, updateCroppingParameters, } from './Rendering/VTKJS/Main/croppingPlanes' @@ -1254,6 +1257,33 @@ const createViewer = async ( return context.maxConcurrency } + publicAPI.getLoadedScale = name => { + const imageName = name ?? context.images.selectedName + const actorContext = context.images.actorContext.get(imageName) + return actorContext.loadedScale + } + + publicAPI.getCroppedImageWorldBounds = () => { + return getCropWidgetBounds(context) + } + + publicAPI.getCroppedIndexBounds = async (scale, name) => { + const imageName = name ?? context.images.selectedName + const actorContext = context.images.actorContext.get(imageName) + if (typeof scale === 'undefined' || scale < 0) { + scale = actorContext.loadedScale + } + const image = actorContext.image + const bounds = getCropWidgetBounds(context) + const indexToWorld = await image.scaleIndexToWorld(scale) + const fullIndexBounds = image.getIndexBounds(scale) + return worldBoundsToIndexBounds({ + bounds, + fullIndexBounds, + worldToIndex: mat4.invert([], indexToWorld), + }) + } + addKeyboardShortcuts(context.uiContainer, service) // must come before moving/main image