@cleerlycode/cornerstone-wado-image-loader
Version:
Cornerstone ImageLoader for DICOM WADO-URI
195 lines (149 loc) • 6.62 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>imageLoader/wadouri/dataSetCacheManager.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#addTask">addTask</a></li><li><a href="global.html#arrayBufferToString">arrayBufferToString</a></li><li><a href="global.html#cacheSizeInBytes">cacheSizeInBytes</a></li><li><a href="global.html#cancelTask">cancelTask</a></li><li><a href="global.html#framesAreFragmented">framesAreFragmented</a></li><li><a href="global.html#getNumberString">getNumberString</a></li><li><a href="global.html#getNumberValues">getNumberValues</a></li><li><a href="global.html#getStatistics">getStatistics</a></li><li><a href="global.html#getTransferSyntaxForContentType">getTransferSyntaxForContentType</a></li><li><a href="global.html#getUncompressedImageFrame">getUncompressedImageFrame</a></li><li><a href="global.html#getValue">getValue</a></li><li><a href="global.html#handleMessageFromWorker">handleMessageFromWorker</a></li><li><a href="global.html#handler">handler</a></li><li><a href="global.html#initialize">initialize</a></li><li><a href="global.html#loadCodecs">loadCodecs</a></li><li><a href="global.html#loadWebWorkerTask">loadWebWorkerTask</a></li><li><a href="global.html#registerLoaders">registerLoaders</a></li><li><a href="global.html#registerTaskHandler">registerTaskHandler</a></li><li><a href="global.html#setPixelDataType">setPixelDataType</a></li><li><a href="global.html#setTaskPriority">setTaskPriority</a></li><li><a href="global.html#spawnWebWorker">spawnWebWorker</a></li><li><a href="global.html#startTaskOnWebWorker">startTaskOnWebWorker</a></li><li><a href="global.html#unpackBinaryFrame">unpackBinaryFrame</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">imageLoader/wadouri/dataSetCacheManager.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import { dicomParser, external } from '../../externalModules.js';
import { xhrRequest } from '../internal/index.js';
/**
* This object supports loading of DICOM P10 dataset from a uri and caching it so it can be accessed
* by the caller. This allows a caller to access the datasets without having to go through cornerstone's
* image loader mechanism. One reason a caller may need to do this is to determine the number of frames
* in a multiframe sop instance so it can create the imageId's correctly.
*/
let cacheSizeInBytes = 0;
let loadedDataSets = {};
let promises = {};
// returns true if the wadouri for the specified index has been loaded
function isLoaded (uri) {
return loadedDataSets[uri] !== undefined;
}
function get (uri) {
if (!loadedDataSets[uri]) {
return;
}
return loadedDataSets[uri].dataSet;
}
// loads the dicom dataset from the wadouri sp
function load (uri, loadRequest = xhrRequest, imageId) {
const cornerstone = external.cornerstone;
// if already loaded return it right away
if (loadedDataSets[uri]) {
// console.log('using loaded dataset ' + uri);
return new Promise((resolve) => {
loadedDataSets[uri].cacheCount++;
resolve(loadedDataSets[uri].dataSet);
});
}
// if we are currently loading this uri, increment the cacheCount and return its promise
if (promises[uri]) {
// console.log('returning existing load promise for ' + uri);
promises[uri].cacheCount++;
return promises[uri];
}
// This uri is not loaded or being loaded, load it via an xhrRequest
const loadDICOMPromise = loadRequest(uri, imageId);
// handle success and failure of the XHR request load
const promise = new Promise((resolve, reject) => {
loadDICOMPromise.then(function (dicomPart10AsArrayBuffer/* , xhr*/) {
const byteArray = new Uint8Array(dicomPart10AsArrayBuffer);
// Reject the promise if parsing the dicom file fails
let dataSet;
try {
dataSet = dicomParser.parseDicom(byteArray);
} catch (error) {
return reject(error);
}
loadedDataSets[uri] = {
dataSet,
cacheCount: promise.cacheCount
};
cacheSizeInBytes += dataSet.byteArray.length;
resolve(dataSet);
cornerstone.triggerEvent(cornerstone.events, 'datasetscachechanged', {
uri,
action: 'loaded',
cacheInfo: getInfo()
});
}, reject).then(() => {
// Remove the promise if success
delete promises[uri];
}, () => {
// Remove the promise if failure
delete promises[uri];
});
});
promise.cacheCount = 1;
promises[uri] = promise;
return promise;
}
// remove the cached/loaded dicom dataset for the specified wadouri to free up memory
function unload (uri) {
const cornerstone = external.cornerstone;
// console.log('unload for ' + uri);
if (loadedDataSets[uri]) {
loadedDataSets[uri].cacheCount--;
if (loadedDataSets[uri].cacheCount === 0) {
// console.log('removing loaded dataset for ' + uri);
cacheSizeInBytes -= loadedDataSets[uri].dataSet.byteArray.length;
delete loadedDataSets[uri];
cornerstone.triggerEvent(cornerstone.events, 'datasetscachechanged', {
uri,
action: 'unloaded',
cacheInfo: getInfo()
});
}
}
}
export function getInfo () {
return {
cacheSizeInBytes,
numberOfDataSetsCached: Object.keys(loadedDataSets).length
};
}
// removes all cached datasets from memory
function purge () {
loadedDataSets = {};
promises = {};
}
export default {
isLoaded,
load,
unload,
getInfo,
purge,
get
};
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>