chenchenyuyu / chenchenyuyu.github.io

记录点点滴滴
http://cychenyu.com/
2 stars 0 forks source link

cornerstoneWADOImageLOader.js #60

Open chenchenyuyu opened 6 years ago

chenchenyuyu commented 6 years ago

/! cornerstone-wado-image-loader - v0.14.3 - 2017-04-04 | (c) 2016 Chris Hafey | https://github.com/chafey/cornerstoneWADOImageLoader / // // This is a cornerstone image loader for WADO-URI requests. //

if(typeof cornerstone === 'undefined'){ cornerstone = {}; } if(typeof cornerstoneWADOImageLoader === 'undefined'){ cornerstoneWADOImageLoader = { wadouri: {

},
wadors: {

},
internal: {
  options : {
    // callback allowing customization of the xhr (e.g. adding custom auth headers, cors, etc)
    beforeSend: function (xhr) {
    },
    // callback allowing modification of newly created image objects
    imageCreated : function(image) {
    }
  }
}

}; }

(function ($, cornerstone, cornerstoneWADOImageLoader) {

"use strict";

// add a decache callback function to clear out our dataSetCacheManager function addDecache(image) { image.decache = function() { //console.log('decache'); var parsedImageId = cornerstoneWADOImageLoader.wadouri.parseImageId(image.imageId); cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.unload(parsedImageId.url); }; }

function getPixelData(dataSet, frameIndex) { var pixelDataElement = dataSet.elements.x7fe00010;

if(pixelDataElement.encapsulatedPixelData) {
  return cornerstoneWADOImageLoader.wadouri.getEncapsulatedImageFrame(dataSet, frameIndex);
} else {
  return cornerstoneWADOImageLoader.wadouri.getUncompressedImageFrame(dataSet, frameIndex);
}

}

function loadDataSetFromPromise(xhrRequestPromise, imageId, frame, sharedCacheKey, options) {

var start = new Date().getTime();
frame = frame || 0;
var deferred = $.Deferred();
xhrRequestPromise.then(function(dataSet/*, xhr*/) {
  var pixelData = getPixelData(dataSet, frame);
  var transferSyntax =  dataSet.string('x00020010');
  var loadEnd = new Date().getTime();
  var imagePromise = cornerstoneWADOImageLoader.createImage(imageId, pixelData, transferSyntax, options);
  imagePromise.then(function(image) {
    image.data = dataSet;
    var end = new Date().getTime();
    image.loadTimeInMS = loadEnd - start;
    image.totalTimeInMS = end - start;
    addDecache(image);
    deferred.resolve(image);
  });
}, function(error) {
  deferred.reject(error);
});
return deferred;

}

function getLoaderForScheme(scheme) { if(scheme === 'dicomweb' || scheme === 'wadouri') { return cornerstoneWADOImageLoader.internal.xhrRequest; } else if(scheme === 'dicomfile') { return cornerstoneWADOImageLoader.wadouri.loadFileRequest; } }

function loadImage(imageId, options) { var parsedImageId = cornerstoneWADOImageLoader.wadouri.parseImageId(imageId);

var loader = getLoaderForScheme(parsedImageId.scheme);

// if the dataset for this url is already loaded, use it
if(cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.isLoaded(parsedImageId.url)) {
  return loadDataSetFromPromise(cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.load(parsedImageId.url, loader, imageId), imageId, parsedImageId.frame, parsedImageId.url, options);
}

// load the dataSet via the dataSetCacheManager
return loadDataSetFromPromise(cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.load(parsedImageId.url, loader, imageId), imageId, parsedImageId.frame, parsedImageId.url, options);

}

// register dicomweb and wadouri image loader prefixes cornerstone.registerImageLoader('dicomweb', loadImage); cornerstone.registerImageLoader('wadouri', loadImage); cornerstone.registerImageLoader('dicomfile', loadImage); }($, cornerstone, cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

"use strict";

function convertPALETTECOLOR( imageFrame, rgbaBuffer ) { var numPixels = imageFrame.columns * imageFrame.rows; var palIndex=0; var rgbaIndex=0; var pixelData = imageFrame.pixelData; var start = imageFrame.redPaletteColorLookupTableDescriptor[1]; var rData = imageFrame.redPaletteColorLookupTableData; var gData = imageFrame.greenPaletteColorLookupTableData; var bData = imageFrame.bluePaletteColorLookupTableData; var shift = imageFrame.redPaletteColorLookupTableDescriptor[2] === 8 ? 0 : 8; var len = imageFrame.redPaletteColorLookupTableData.length; if(len === 0) { len = 65535; }

for( var i=0 ; i < numPixels ; ++i ) {
  var value=pixelData[palIndex++];
  if( value < start )
    value=0;
  else if( value > start + len -1 )
    value=len-1;
  else
    value=value-start;

  rgbaBuffer[ rgbaIndex++ ] = rData[value] >> shift;
  rgbaBuffer[ rgbaIndex++ ] = gData[value] >> shift;
  rgbaBuffer[ rgbaIndex++ ] = bData[value] >> shift;
  rgbaBuffer[ rgbaIndex++ ] = 255;
}

}

// module exports cornerstoneWADOImageLoader.convertPALETTECOLOR = convertPALETTECOLOR;

}(cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

function convertRGBColorByPixel(imageFrame, rgbaBuffer) {
    if(imageFrame === undefined) {
        throw "decodeRGB: rgbBuffer must not be undefined";
    }
    if(imageFrame.length % 3 !== 0) {
        throw "decodeRGB: rgbBuffer length must be divisible by 3";
    }

    var numPixels = imageFrame.length / 3;
    var rgbIndex = 0;
    var rgbaIndex = 0;
    for(var i= 0; i < numPixels; i++) {
        rgbaBuffer[rgbaIndex++] = imageFrame[rgbIndex++]; // red
        rgbaBuffer[rgbaIndex++] = imageFrame[rgbIndex++]; // green
        rgbaBuffer[rgbaIndex++] = imageFrame[rgbIndex++]; // blue
        rgbaBuffer[rgbaIndex++] = 255; //alpha
    }
}

// module exports
cornerstoneWADOImageLoader.convertRGBColorByPixel = convertRGBColorByPixel;

}(cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

function convertRGBColorByPlane(imageFrame, rgbaBuffer) { if(imageFrame === undefined) { throw "decodeRGB: rgbBuffer must not be undefined"; } if(imageFrame.length % 3 !== 0) { throw "decodeRGB: rgbBuffer length must be divisible by 3"; }

var numPixels = imageFrame.length / 3;
var rgbaIndex = 0;
var rIndex = 0;
var gIndex = numPixels;
var bIndex = numPixels*2;
for(var i= 0; i < numPixels; i++) {
  rgbaBuffer[rgbaIndex++] = imageFrame[rIndex++]; // red
  rgbaBuffer[rgbaIndex++] = imageFrame[gIndex++]; // green
  rgbaBuffer[rgbaIndex++] = imageFrame[bIndex++]; // blue
  rgbaBuffer[rgbaIndex++] = 255; //alpha
}

}

// module exports cornerstoneWADOImageLoader.convertRGBColorByPlane = convertRGBColorByPlane; }(cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

function convertYBRFullByPixel(imageFrame, rgbaBuffer) {
    if(imageFrame === undefined) {
        throw "decodeRGB: ybrBuffer must not be undefined";
    }
    if(imageFrame.length % 3 !== 0) {
        throw "decodeRGB: ybrBuffer length must be divisble by 3";
    }

    var numPixels = imageFrame.length / 3;
    var ybrIndex = 0;
    var rgbaIndex = 0;
    for(var i= 0; i < numPixels; i++) {
        var y = imageFrame[ybrIndex++];
        var cb = imageFrame[ybrIndex++];
        var cr = imageFrame[ybrIndex++];
        rgbaBuffer[rgbaIndex++] = y + 1.40200 * (cr - 128);// red
        rgbaBuffer[rgbaIndex++] = y - 0.34414 * (cb -128) - 0.71414 * (cr- 128); // green
        rgbaBuffer[rgbaIndex++] = y + 1.77200 * (cb - 128); // blue
        rgbaBuffer[rgbaIndex++] = 255; //alpha
    }
}

// module exports
cornerstoneWADOImageLoader.convertYBRFullByPixel = convertYBRFullByPixel;

}(cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

function convertYBRFullByPlane(imageFrame, rgbaBuffer) { if (imageFrame === undefined) { throw "decodeRGB: ybrBuffer must not be undefined"; } if (imageFrame.length % 3 !== 0) { throw "decodeRGB: ybrBuffer length must be divisble by 3"; }

var numPixels = imageFrame.length / 3;
var rgbaIndex = 0;
var yIndex = 0;
var cbIndex = numPixels;
var crIndex = numPixels * 2;
for (var i = 0; i < numPixels; i++) {
  var y = imageFrame[yIndex++];
  var cb = imageFrame[cbIndex++];
  var cr = imageFrame[crIndex++];
  rgbaBuffer[rgbaIndex++] = y + 1.40200 * (cr - 128);// red
  rgbaBuffer[rgbaIndex++] = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
  rgbaBuffer[rgbaIndex++] = y + 1.77200 * (cb - 128); // blue
  rgbaBuffer[rgbaIndex++] = 255; //alpha
}

} // module exports cornerstoneWADOImageLoader.convertYBRFullByPlane = convertYBRFullByPlane; }(cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

"use strict";

function configure(options) { cornerstoneWADOImageLoader.internal.options = options; }

// module exports cornerstoneWADOImageLoader.configure = configure;

}(cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

"use strict";

function convertRGB(imageFrame, rgbaBuffer) { if(imageFrame.planarConfiguration === 0) { cornerstoneWADOImageLoader.convertRGBColorByPixel(imageFrame.pixelData, rgbaBuffer); } else { cornerstoneWADOImageLoader.convertRGBColorByPlane(imageFrame.pixelData, rgbaBuffer); } }

function convertYBRFull(imageFrame, rgbaBuffer) { if(imageFrame.planarConfiguration === 0) { cornerstoneWADOImageLoader.convertYBRFullByPixel(imageFrame.pixelData, rgbaBuffer); } else { cornerstoneWADOImageLoader.convertYBRFullByPlane(imageFrame.pixelData, rgbaBuffer); } }

function convertColorSpace(imageFrame, imageData) { var rgbaBuffer = imageData.data; console.time('convertColorSpace'); // convert based on the photometric interpretation if (imageFrame.photometricInterpretation === "RGB" ) { convertRGB(imageFrame, rgbaBuffer); } else if (imageFrame.photometricInterpretation === "YBR_RCT") { convertRGB(imageFrame, rgbaBuffer); } else if (imageFrame.photometricInterpretation === "YBR_ICT") { convertRGB(imageFrame, rgbaBuffer); } else if( imageFrame.photometricInterpretation === "PALETTE COLOR" ) { cornerstoneWADOImageLoader.convertPALETTECOLOR(imageFrame, rgbaBuffer); } else if( imageFrame.photometricInterpretation === "YBR_FULL_422" ) { convertRGB(imageFrame, rgbaBuffer); } else if(imageFrame.photometricInterpretation === "YBR_FULL" ) { convertYBRFull(imageFrame, rgbaBuffer); } else { throw "no color space conversion for photometric interpretation " + imageFrame.photometricInterpretation; } console.timeEnd('convertColorSpace'); }

// module exports cornerstoneWADOImageLoader.convertColorSpace = convertColorSpace;

}(cornerstoneWADOImageLoader)); /* / (function ($, cornerstone, cornerstoneWADOImageLoader) {

"use strict";

var canvas = document.createElement('canvas'); var lastImageIdDrawn = "";

function isModalityLUTForDisplay(sopClassUid) { // special case for XA and XRF // https://groups.google.com/forum/#!searchin/comp.protocols.dicom/Modality$20LUT$20XA/comp.protocols.dicom/UBxhOZ2anJ0/D0R_QP8V2wIJ return sopClassUid !== '1.2.840.10008.5.1.4.1.1.12.1' && // XA sopClassUid !== '1.2.840.10008.5.1.4.1.1.12.2.1 '; // XRF }

/**

}($, cornerstoneWADOImageLoader)); /* / (function ($, cornerstone, cornerstoneWADOImageLoader) {

"use strict";

function getImageFrame(imageId) { var imagePixelModule = cornerstone.metaData.get('imagePixelModule', imageId);

return {
  samplesPerPixel : imagePixelModule.samplesPerPixel,
  photometricInterpretation : imagePixelModule.photometricInterpretation,
  planarConfiguration : imagePixelModule.planarConfiguration,
  rows : imagePixelModule.rows,
  columns : imagePixelModule.columns,
  bitsAllocated : imagePixelModule.bitsAllocated,
  pixelRepresentation : imagePixelModule.pixelRepresentation, // 0 = unsigned,
  smallestPixelValue: imagePixelModule.smallestPixelValue,
  largestPixelValue: imagePixelModule.largestPixelValue,
  redPaletteColorLookupTableDescriptor : imagePixelModule.redPaletteColorLookupTableDescriptor,
  greenPaletteColorLookupTableDescriptor : imagePixelModule.greenPaletteColorLookupTableDescriptor,
  bluePaletteColorLookupTableDescriptor : imagePixelModule.bluePaletteColorLookupTableDescriptor,
  redPaletteColorLookupTableData : imagePixelModule.redPaletteColorLookupTableData,
  greenPaletteColorLookupTableData : imagePixelModule.greenPaletteColorLookupTableData,
  bluePaletteColorLookupTableData : imagePixelModule.bluePaletteColorLookupTableData,
  pixelData: undefined // populated later after decoding
};

}

cornerstoneWADOImageLoader.getImageFrame = getImageFrame; }($, cornerstone, cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

"use strict";

function getMinMax(storedPixelData) { // we always calculate the min max values since they are not always // present in DICOM and we don't want to trust them anyway as cornerstone // depends on us providing reliable values for these var min = storedPixelData[0]; var max = storedPixelData[0]; var storedPixel; var numPixels = storedPixelData.length; for(var index = 0; index < numPixels; index++) { storedPixel = storedPixelData[index]; min = Math.min(min, storedPixel); max = Math.max(max, storedPixel); }

return {
  min: min,
  max: max
};

}

// module exports cornerstoneWADOImageLoader.getMinMax = getMinMax;

}(cornerstoneWADOImageLoader));

(function (cornerstoneWADOImageLoader) {

"use strict";

function isColorImage(photoMetricInterpretation) { if(photoMetricInterpretation === "RGB" || photoMetricInterpretation === "PALETTE COLOR" || photoMetricInterpretation === "YBR_FULL" || photoMetricInterpretation === "YBR_FULL_422" || photoMetricInterpretation === "YBR_PARTIAL_422" || photoMetricInterpretation === "YBR_PARTIAL_420" || photoMetricInterpretation === "YBR_RCT" || photoMetricInterpretation === "YBR_ICT") { return true; } else { return false; } }

cornerstoneWADOImageLoader.isColorImage = isColorImage;

}(cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

"use strict";

// module exports cornerstoneWADOImageLoader.version = '0.13.3';

}(cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

function checkToken(token, data, dataOffset) {

if(dataOffset + token.length > data.length) {
  return false;
}

var endIndex = dataOffset;

for(var i = 0; i < token.length; i++) {
  if(token[i] !== data[endIndex++]) {
    return false;
  }
}
return true;

}

function stringToUint8Array(str) { var uint=new Uint8Array(str.length); for(var i=0,j=str.length;i<j;i++){ uint[i]=str.charCodeAt(i); } return uint; }

function findIndexOfString(data, str, offset) {

offset = offset || 0;

var token = stringToUint8Array(str);

for(var i=offset; i < data.length; i++) {
  if(token[0] === data[i]) {
    //console.log('match @', i);
    if(checkToken(token, data, i)) {
      return i;
    }
  }
}
return -1;

} cornerstoneWADOImageLoader.wadors.findIndexOfString = findIndexOfString;

}(cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

"use strict";

function findBoundary(header) { for(var i=0; i < header.length; i++) { if(header[i].substr(0,2) === '--') { return header[i]; } } return undefined; }

function findContentType(header) { for(var i=0; i < header.length; i++) { if(header[i].substr(0,13) === 'Content-Type:') { return header[i].substr(13).trim(); } } return undefined; }

function uint8ArrayToString(data, offset, length) { offset = offset || 0; length = length || data.length - offset; var str = ""; for(var i=offset; i < offset + length; i++) { str += String.fromCharCode(data[i]); } return str; }

cornerstoneWADOImageLoader.wadors.getPixelData = function(uri, imageId, mediaType) { mediaType = mediaType || 'application/octet-stream'; var headers = { accept : mediaType };

var deferred = $.Deferred();

var loadPromise = cornerstoneWADOImageLoader.internal.xhrRequest(uri, imageId, headers);
loadPromise.then(function(imageFrameAsArrayBuffer/*, xhr*/) {

  // request succeeded, Parse the multi-part mime response
  var response = new Uint8Array(imageFrameAsArrayBuffer);

  // First look for the multipart mime header
  var tokenIndex = cornerstoneWADOImageLoader.wadors.findIndexOfString(response, '\r\n\r\n');
  if(tokenIndex === -1) {
    deferred.reject('invalid response - no multipart mime header');
  }
  var header = uint8ArrayToString(response, 0, tokenIndex);
  // Now find the boundary  marker
  var split = header.split('\r\n');
  var boundary = findBoundary(split);
  if(!boundary) {
    deferred.reject('invalid response - no boundary marker')
  }
  var offset = tokenIndex + 4; // skip over the \r\n\r\n

  // find the terminal boundary marker
  var endIndex = cornerstoneWADOImageLoader.wadors.findIndexOfString(response, boundary, offset);
  if(endIndex === -1) {
    deferred.reject('invalid response - terminating boundary not found');
  }

  // Remove \r\n from the length
  var length = endIndex - offset - 2;

  // return the info for this pixel data
  deferred.resolve({
    contentType: findContentType(split),
    imageFrame: {
      pixelData: new Uint8Array(imageFrameAsArrayBuffer, offset, length)
    }
  });
});
return deferred.promise();

}; }(cornerstoneWADOImageLoader));

(function ($, cornerstone, cornerstoneWADOImageLoader) {

"use strict";

function getTransferSyntaxForContentType(contentType) { return '1.2.840.10008.1.2'; // hard code to ILE for now }

function loadImage(imageId, options) { var start = new Date().getTime();

var deferred = $.Deferred();

var uri = imageId.substring(7);

// check to make sure we have metadata for this imageId
var metaData = cornerstoneWADOImageLoader.wadors.metaDataManager.get(imageId);
if(metaData === undefined) {
  deferred.reject('no metadata for imageId ' + imageId);
  return deferred.promise();
}

// TODO: load bulk data items that we might need

var mediaType = 'multipart/related; type="application/octet-stream"'; // 'image/dicom+jp2';

// get the pixel data from the server
cornerstoneWADOImageLoader.wadors.getPixelData(uri, imageId, mediaType).then(function(result) {

  var transferSyntax = getTransferSyntaxForContentType(result.contentType);
  var pixelData = result.imageFrame.pixelData;
  var imagePromise = cornerstoneWADOImageLoader.createImage(imageId, pixelData, transferSyntax, options);
  imagePromise.then(function(image) {
    // add the loadTimeInMS property
    var end = new Date().getTime();
    image.loadTimeInMS = end - start;
    deferred.resolve(image);
  })
}).fail(function(reason) {
  deferred.reject(reason);
});

return deferred;

}

// register wadors scheme cornerstone.registerImageLoader('wadors', loadImage);

}($, cornerstone, cornerstoneWADOImageLoader)); /* / (function ($, cornerstone, cornerstoneWADOImageLoader) {

"use strict"; /**

}(cornerstoneWADOImageLoader)); /* / (function ($, cornerstone, cornerstoneWADOImageLoader) {

"use strict"; /**

}(cornerstone, cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

var imageIds = [];

function add(imageId, metadata) { imageIds[imageId] = metadata; }

function get(imageId) { return imageIds[imageId]; }

function remove(imageId) { imageIds[imageId] = undefined; }

function purge() { imageIds = []; }

// module exports cornerstoneWADOImageLoader.wadors.metaDataManager = { add : add, get : get, remove:remove, purge: purge };

}(cornerstoneWADOImageLoader)); /**

}($, cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

var files = [];

function add(file) { var fileIndex = files.push(file); return 'dicomfile:' + (fileIndex - 1); }

function get(index) { return files[index]; }

function remove(index) { files[index] = undefined; }

function purge() { files = []; }

// module exports cornerstoneWADOImageLoader.wadouri.fileManager = { add : add, get : get, remove:remove, purge: purge };

}(cornerstoneWADOImageLoader)); /**

/* / (function (cornerstoneWADOImageLoader) {

"use strict";

function getLutDescriptor(dataSet, tag) { if(!dataSet.elements[tag] || dataSet.elements[tag].length != 6) { return; } return [dataSet.uint16(tag, 0),dataSet.uint16(tag, 1), dataSet.uint16(tag, 2)] }

function getLutData(lutDataSet, tag, lutDescriptor) { var lut = []; var lutData = lutDataSet.elements[tag]; var numLutEntries = lutDescriptor[0]; for (var i = 0; i < numLutEntries; i++) { // Output range is always unsigned if(lutDescriptor[2] === 16) { lut[i] = lutDataSet.uint16(tag, i); } else { lut[i] = lutDataSet.byteArray[i + lutData.dataOffset]; } } return lut; }

function populatePaletteColorLut(dataSet, imagePixelModule) { // return immediately if no palette lut elements if(!dataSet.elements['x00281101']) { return; } imagePixelModule.redPaletteColorLookupTableDescriptor = getLutDescriptor(dataSet, 'x00281101'); imagePixelModule.greenPaletteColorLookupTableDescriptor = getLutDescriptor(dataSet, 'x00281102'); imagePixelModule.bluePaletteColorLookupTableDescriptor = getLutDescriptor(dataSet, 'x00281103');

imagePixelModule.redPaletteColorLookupTableData =  getLutData(dataSet, 'x00281201', imagePixelModule.redPaletteColorLookupTableDescriptor);
imagePixelModule.greenPaletteColorLookupTableData = getLutData(dataSet, 'x00281202', imagePixelModule.greenPaletteColorLookupTableDescriptor);
imagePixelModule.bluePaletteColorLookupTableData = getLutData(dataSet, 'x00281203', imagePixelModule.bluePaletteColorLookupTableDescriptor);

}

function populateSmallestLargestPixelValues(dataSet, imagePixelModule) { var pixelRepresentation = dataSet.uint16('x00280103'); if(pixelRepresentation === 0) { imagePixelModule.smallestPixelValue = dataSet.uint16('x00280106'); imagePixelModule.largestPixelValue = dataSet.uint16('x00280107'); } else { imagePixelModule.smallestPixelValue = dataSet.int16('x00280106'); imagePixelModule.largestPixelValue = dataSet.int16('x00280107'); } }

function getImagePixelModule(dataSet) {

var imagePixelModule = {
  samplesPerPixel: dataSet.uint16('x00280002'),
  photometricInterpretation: dataSet.string('x00280004'),
  rows: dataSet.uint16('x00280010'),
  columns: dataSet.uint16('x00280011'),
  bitsAllocated: dataSet.uint16('x00280100'),
  bitsStored: dataSet.uint16('x00280101'),
  highBit: dataSet.uint16('x00280102'),
  pixelRepresentation: dataSet.uint16('x00280103'),
  planarConfiguration: dataSet.uint16('x00280006'),
  pixelAspectRatio: dataSet.string('x00280034')
};
populateSmallestLargestPixelValues(dataSet, imagePixelModule);
populatePaletteColorLut(dataSet, imagePixelModule);
return imagePixelModule;

}

// module exports cornerstoneWADOImageLoader.wadouri.getImagePixelModule = getImagePixelModule

}(cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

function getLUT(pixelRepresentation, lutDataSet) { var numLUTEntries = lutDataSet.uint16('x00283002', 0); if(numLUTEntries === 0) { numLUTEntries = 65535; } var firstValueMapped = 0; if(pixelRepresentation === 0) { firstValueMapped = lutDataSet.uint16('x00283002', 1); } else { firstValueMapped = lutDataSet.int16('x00283002', 1); } var numBitsPerEntry = lutDataSet.uint16('x00283002', 2); //console.log('LUT(', numLUTEntries, ',', firstValueMapped, ',', numBitsPerEntry, ')'); var lut = { id : '1', firstValueMapped: firstValueMapped, numBitsPerEntry : numBitsPerEntry, lut : [] };

//console.log("minValue=", minValue, "; maxValue=", maxValue);
for (var i = 0; i < numLUTEntries; i++) {
  if(pixelRepresentation === 0) {
    lut.lut[i] = lutDataSet.uint16('x00283006', i);
  } else {
    lut.lut[i] = lutDataSet.int16('x00283006', i);
  }
}
return lut;

}

function getLUTs(pixelRepresentation, lutSequence) { if(!lutSequence || !lutSequence.items || !lutSequence.items.length) { return; } var luts = []; for(var i=0; i < lutSequence.items.length; i++) { var lutDataSet = lutSequence.items[i].dataSet; var lut = getLUT(pixelRepresentation, lutDataSet); if(lut) { luts.push(lut); } } return luts; }

// module exports cornerstoneWADOImageLoader.wadouri.getLUTs = getLUTs

}(cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

function getMinStoredPixelValue(dataSet) { var pixelRepresentation = dataSet.uint16('x00280103'); var bitsStored = dataSet.uint16('x00280101'); if(pixelRepresentation === 0) { return 0; } return -1 << (bitsStored -1); }

// 0 = unsigned / US, 1 = signed / SS function getModalityLUTOutputPixelRepresentation(dataSet) {

// CT SOP Classes are always signed
var sopClassUID = dataSet.string('x00080016');
if(sopClassUID === '1.2.840.10008.5.1.4.1.1.2' ||
  sopClassUID === '1.2.840.10008.5.1.4.1.1.2.1') {
  return 1;
}

// if rescale intercept and rescale slope are present, pass the minimum stored
// pixel value through them to see if we get a signed output range
var rescaleIntercept = dataSet.floatString('x00281052');
var rescaleSlope = dataSet.floatString('x00281053');
if(rescaleIntercept !== undefined && rescaleSlope !== undefined) {
  var minStoredPixelValue = getMinStoredPixelValue(dataSet); //
  var minModalityLutValue = minStoredPixelValue * rescaleSlope + rescaleIntercept;
  if (minModalityLutValue < 0) {
    return 1;
  } else {
    return 0;
  }
}

// Output of non linear modality lut is always unsigned
if(dataSet.elements.x00283000 && dataSet.elements.x00283000.length > 0) {
  return 0;
}

// If no modality lut transform, output is same as pixel representation
return dataSet.uint16('x00280103');

}

// module exports cornerstoneWADOImageLoader.wadouri.getModalityLUTOutputPixelRepresentation = getModalityLUTOutputPixelRepresentation

}(cornerstoneWADOImageLoader)); /* / (function (cornerstoneWADOImageLoader) {

"use strict";

function getNumberValues(dataSet, tag, minimumLength) { var values = []; var valueAsString = dataSet.string(tag); if(!valueAsString) { return; } var split = valueAsString.split('\'); if(minimumLength && split.length < minimumLength) { return; } for(var i=0;i < split.length; i++) { values.push(parseFloat(split[i])); } return values; }

// module exports cornerstoneWADOImageLoader.wadouri.getNumberValues = getNumberValues

}(cornerstoneWADOImageLoader)); /* / (function (cornerstone, cornerstoneWADOImageLoader) {

"use strict";

var getNumberValues = cornerstoneWADOImageLoader.wadouri.getNumberValues;

function metaDataProvider(type, imageId) { var parsedImageId = cornerstoneWADOImageLoader.wadouri.parseImageId(imageId);

var dataSet = cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.get(parsedImageId.url);
if(!dataSet) {
  return;
}

if(type === 'generalSeriesModule') {
  return {
    modality: dataSet.string('x00080060'),
    seriesInstanceUID: dataSet.string('x0020000e'),
    seriesNumber: dataSet.intString('x00200011'),
    studyInstanceUID: dataSet.string('x0020000d'),
    seriesDate: dicomParser.parseDA(dataSet.string('x00080021')),
    seriesTime: dicomParser.parseTM(dataSet.string('x00080031') || '')
  };
}

if(type === 'patientStudyModule') {
  return {
    patientAge: dataSet.intString('x00101010'),
    patientSize: dataSet.floatString('x00101020'),
    patientWeight: dataSet.floatString('x00101030')
  }
}

if (type === 'imagePlaneModule') {
  return {
    pixelSpacing: getNumberValues(dataSet, 'x00280030', 2)? getNumberValues(dataSet, 'x00280030', 2):getNumberValues(dataSet,'x00181164', 2),
    imageOrientationPatient: getNumberValues(dataSet, 'x00200037', 6),
    imagePositionPatient: getNumberValues(dataSet, 'x00200032', 3),
    sliceThickness: dataSet.floatString('x00180050'),
    sliceLocation: dataSet.floatString('x00201041'),
    frameOfReferenceUID: dataSet.string('x00200052')
  };
}

if (type === 'imagePixelModule') {
  return cornerstoneWADOImageLoader.wadouri.getImagePixelModule(dataSet);
}

if (type === 'modalityLutModule') {
  return {
    rescaleIntercept : dataSet.floatString('x00281052'),
    rescaleSlope : dataSet.floatString('x00281053'),
    rescaleType: dataSet.string('x00281054'),
    modalityLUTSequence : cornerstoneWADOImageLoader.wadouri.getLUTs(dataSet.uint16('x00280103'), dataSet.elements.x00283000)
  };
}

if (type === 'voiLutModule') {
  var modalityLUTOutputPixelRepresentation = cornerstoneWADOImageLoader.wadouri.getModalityLUTOutputPixelRepresentation(dataSet);
  return {
    windowCenter : getNumberValues(dataSet, 'x00281050', 1),
    windowWidth : getNumberValues(dataSet, 'x00281051', 1),
    voiLUTSequence : cornerstoneWADOImageLoader.wadouri.getLUTs(modalityLUTOutputPixelRepresentation, dataSet.elements.x00283010)
  };
}

if (type === 'sopCommonModule') {
  return {
    sopClassUID : dataSet.string('x00080016'),
    sopInstanceUID : dataSet.string('x00080018')
  };
}

if (type === 'petIsotopeModule') {
  var radiopharmaceuticalInfo = dataSet.elements.x00540016;
  if (radiopharmaceuticalInfo === undefined) {
    return;
  }

  var firstRadiopharmaceuticalInfoDataSet = radiopharmaceuticalInfo.items[0].dataSet;
  return {
    radiopharmaceuticalInfo: {
      radiopharmaceuticalStartTime: dicomParser.parseTM(firstRadiopharmaceuticalInfoDataSet.string('x00181072') || ''),
      radionuclideTotalDose: firstRadiopharmaceuticalInfoDataSet.floatString('x00181074'),
      radionuclideHalfLife: firstRadiopharmaceuticalInfoDataSet.floatString('x00181075')
    }
  }
}

}

// register our metadata provider cornerstone.metaData.addProvider(metaDataProvider);

// module exports cornerstoneWADOImageLoader.wadouri.metaDataProvider = metaDataProvider

}(cornerstone, cornerstoneWADOImageLoader)); (function (cornerstoneWADOImageLoader) {

"use strict"; function parseImageId(imageId) { // build a url by parsing out the url scheme and frame index from the imageId var firstColonIndex = imageId.indexOf(':'); var url = imageId.substring(firstColonIndex + 1); var frameIndex = url.indexOf('frame='); var frame; if(frameIndex !== -1) { var frameStr = url.substr(frameIndex + 6); frame = parseInt(frameStr); url = url.substr(0, frameIndex-1); } return { scheme: imageId.substr(0, firstColonIndex), url : url, frame: frame }; }

// module exports cornerstoneWADOImageLoader.wadouri.parseImageId = parseImageId;

}(cornerstoneWADOImageLoader)); /**

}($, cornerstoneWADOImageLoader)); (function ($, cornerstone, cornerstoneWADOImageLoader) {

"use strict";

function xhrRequest(url, imageId, headers) { headers = headers || {};

var deferred = $.Deferred();

// Make the request for the DICOM P10 SOP Instance
var xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "arraybuffer";
cornerstoneWADOImageLoader.internal.options.beforeSend(xhr);
Object.keys(headers).forEach(function (key) {
  xhr.setRequestHeader(key, headers[key]);
});

// handle response data
xhr.onreadystatechange = function () {
  // TODO: consider sending out progress messages here as we receive the pixel data
  if (xhr.readyState === 4) {
    if (xhr.status === 200) {
      deferred.resolve(xhr.response, xhr);
    }
    else {
      // request failed, reject the deferred
      deferred.reject(xhr);
    }
  }
};
xhr.onprogress = function (oProgress) {
  // console.log('progress:',oProgress)

  if (oProgress.lengthComputable) {  //evt.loaded the bytes browser receive
    //evt.total the total bytes seted by the header
    //
    var loaded = oProgress.loaded;
    var total = oProgress.total;
    var percentComplete = Math.round((loaded / total) * 100);

    $(cornerstone).trigger('CornerstoneImageLoadProgress', {
      imageId: imageId,
      loaded: loaded,
      total: total,
      percentComplete: percentComplete
    });
  }
};

xhr.send();

return deferred.promise();

}

cornerstoneWADOImageLoader.internal.xhrRequest = xhrRequest; }($, cornerstone, cornerstoneWADOImageLoader));