-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Pickering <[email protected]>
- Loading branch information
1 parent
568f742
commit 56a1b9e
Showing
3 changed files
with
54 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { loadOmeZarr } from 'components/data-exploration/spatial/loadOmeZarr'; | ||
import { root as zarrRoot } from 'zarrita'; | ||
import { ZipFileStore } from '@zarrita/storage'; | ||
|
||
// Load OME-Zarr and return the pyramid and loader (an example) | ||
const getImageUrls = async (omeZarrUrl) => { | ||
const omeZarrRoot = zarrRoot(ZipFileStore.fromUrl(omeZarrUrl)); | ||
|
||
const { data } = await loadOmeZarr(omeZarrRoot); | ||
const base = data[0]; | ||
|
||
const imageUrl = await imageDataToUrl(base); | ||
return imageUrl; | ||
}; | ||
|
||
const imageDataToUrl = async (base) => { | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
// Extract dimensions from the first channel | ||
const { data, width, height } = await base.getRaster({ selection: { c: 0, x: 0, y: 0 } }); | ||
|
||
canvas.width = width; | ||
canvas.height = height; | ||
|
||
const rgbaData = new Uint8ClampedArray(width * height * 4); | ||
|
||
// Process each RGB channel | ||
await Promise.all([0, 1, 2].map(async (c) => { | ||
const selection = { c, x: 0, y: 0 }; | ||
const { data: pixelData } = await base.getRaster({ selection }); | ||
|
||
pixelData.forEach((value, i) => { | ||
rgbaData[i * 4 + c] = value; | ||
}); | ||
})); | ||
|
||
// Set alpha channel to fully opaque | ||
rgbaData.forEach((_, i) => { | ||
if ((i + 1) % 4 === 0) { | ||
rgbaData[i] = 255; | ||
} | ||
}); | ||
|
||
const imageDataObject = new ImageData(rgbaData, width, height); | ||
ctx.putImageData(imageDataObject, 0, 0); | ||
|
||
const imageUrl = canvas.toDataURL(); | ||
return { imageUrl, imageWidth: width, imageHeight: height }; | ||
}; | ||
|
||
export default getImageUrls; |