Skip to content

Commit

Permalink
changeImageSource instead of recreating instance when new image is lo…
Browse files Browse the repository at this point in the history
…aded
  • Loading branch information
PrafulB committed Feb 25, 2024
1 parent 7af790b commit efcb890
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion demo/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ imgBox.loadTile = async ({fileURL, tileX, tileY, tileWidth, tileHeight, tileSize
tileSizeElement.value = tileSize
}

if (imgBox.image?.getImageSource() !== decodeURIComponent(fileURL)) {
if (!imgBox.image) {
const numWorkers = 4
imgBox.image = new Imagebox3(decodeURIComponent(fileURL), numWorkers)
await imgBox.image.init()
} else if (imgBox.image?.getImageSource() !== decodeURIComponent(fileURL)) {
await imgBox.image.changeImageSource(fileURL)
}

tileElement.src = URL.createObjectURL(await (await imgBox.image.getTile(tileX, tileY, tileWidth, tileHeight, tileSize)).blob())
Expand Down
10 changes: 7 additions & 3 deletions imagebox3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fromBlob, fromUrl, Pool, getDecoder, globals } from "https://cdn.jsdeli
class Imagebox3 {
constructor(imageSource, numWorkers) {
if (imageSource instanceof File || typeof(imageSource) === 'string') {
this.imageSource = imageSource
this.imageSource = typeof(imageSource) === 'string' ? decodeURIComponent(imageSource) : imageSource
} else {
throw new Error("Unsupported image type for ImageBox3")
}
Expand All @@ -14,7 +14,6 @@ class Imagebox3 {
this.numWorkers = typeof(numWorkers) === 'number' ? numWorkers : Math.max(Math.floor(navigator.hardwareConcurrency / 2), 1)
this.workerPool = undefined
this.supportedDecoders = undefined

}

async init() {
Expand All @@ -40,11 +39,17 @@ class Imagebox3 {
return this.imageSource
}

async changeImageSource(newImageSource) {
this.imageSource = typeof(newImageSource) === 'string' ? decodeURIComponent(newImageSource) : newImageSource
await this.init()
}

getPyramid() {
return this.tiff
}

async createWorkerPool(numWorkers) {
// TODO: Load only the decoders necessary for the current image, instead of having them all active.
if (this.workerPool) {
destroyPool(this.workerPool)
}
Expand Down Expand Up @@ -382,7 +387,6 @@ export const createPool = async (tiffImage, numWorkers=0, supportedDecoders) =>
await getDecoder(tiffImage.fileDirectory)
} catch (e) {
if (e.message.includes("Unknown compression method")) {
console.log(e.message)
const decoderForCompression = supportedDecoders?.[imageCompression]
if (decoderForCompression) {
const baseURL = import.meta.url.split("/").slice(0,-1).join("/");
Expand Down

0 comments on commit efcb890

Please sign in to comment.