public void Create_TransferSyntaxExplicitLEBitsAllocatedLessThanOrEqualTo8_ReturnsOtherBytePixelDataObject(ushort bitsAllocated) { var dataset = new DicomDataset(DicomTransferSyntax.ExplicitVRLittleEndian); dataset.Add(DicomTag.BitsAllocated, bitsAllocated); var pixelData = DicomPixelData.Create(dataset, true); Assert.Equal("OtherBytePixelData", pixelData.GetType().Name); }
public void Create_TransferSyntaxExplicitLEBitsAllocatedGreaterThan16_ReturnsOtherWordPixelDataObject() { var dataset = new DicomDataset(DicomTransferSyntax.ExplicitVRLittleEndian); dataset.Add(DicomTag.BitsAllocated, (ushort)32); var pixelData = DicomPixelData.Create(dataset, true); Assert.Equal("OtherWordPixelData", pixelData.GetType().Name); }
public void Create_TransferSyntaxExplicitLEBitsAllocatedGreaterThan16_Throws() { var dataset = new DicomDataset(DicomTransferSyntax.ExplicitVRLittleEndian); dataset.Add(DicomTag.BitsAllocated, (ushort)17); var exception = Record.Exception(() => DicomPixelData.Create(dataset, true)); Assert.NotNull(exception); }
/// <summary> /// Loads the pixel data for specified frame and set the internal dataset /// /// </summary> /// <param name="dataset">dataset to load pixeldata from</param> /// <param name="frame">The frame number to create pixeldata for</param> private void Load(DicomDataset dataset, int frame) { Dataset = dataset; if (PixelData == null) { PixelData = DicomPixelData.Create(Dataset); PhotometricInterpretation = PixelData.PhotometricInterpretation; } if (Dataset.InternalTransferSyntax.IsEncapsulated) { // decompress single frame from source dataset DicomCodecParams cparams = null; if (Dataset.InternalTransferSyntax == DicomTransferSyntax.JPEGProcess1 || Dataset.InternalTransferSyntax == DicomTransferSyntax.JPEGProcess2_4) { cparams = new DicomJpegParams { ConvertColorspaceToRGB = true }; } var transcoder = new DicomTranscoder(Dataset.InternalTransferSyntax, DicomTransferSyntax.ExplicitVRLittleEndian); transcoder.InputCodecParams = cparams; transcoder.OutputCodecParams = cparams; var buffer = transcoder.DecodeFrame(Dataset, frame); // clone the dataset because modifying the pixel data modifies the dataset var clone = Dataset.Clone(); clone.InternalTransferSyntax = DicomTransferSyntax.ExplicitVRLittleEndian; var pixelData = DicomPixelData.Create(clone, true); pixelData.AddFrame(buffer); // temporary fix for JPEG compressed YBR images if ((Dataset.InternalTransferSyntax == DicomTransferSyntax.JPEGProcess1 || Dataset.InternalTransferSyntax == DicomTransferSyntax.JPEGProcess2_4) && pixelData.SamplesPerPixel == 3) { pixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb; } _pixelData = PixelDataFactory.Create(pixelData, 0); } else { // pull uncompressed frame from source pixel data _pixelData = PixelDataFactory.Create(PixelData, frame); } _pixelData.Rescale(_scale); _overlays = DicomOverlayData.FromDataset(Dataset).Where(x => x.Type == DicomOverlayType.Graphics && x.Data != null).ToArray(); _currentFrame = frame; CreatePipeline(); }
public void BitsStored_Setter_GreaterThanBitsAllocatedIsNotAllowed(ushort bitsAllocated, ushort bitsStored) { var dataset = new DicomDataset(DicomTransferSyntax.ExplicitVRLittleEndian); dataset.Add(DicomTag.BitsAllocated, bitsAllocated); var pixelData = DicomPixelData.Create(dataset, true); var exception = Record.Exception(() => pixelData.BitsStored = bitsStored); Assert.NotNull(exception); }
/// <summary> /// Loads the pixel data for specified frame and set the internal dataset /// /// </summary> /// <param name="dataset">dataset to load pixeldata from</param> /// <param name="frame">The frame number to create pixeldata for</param> private void Load(DicomDataset dataset, int frame) { Dataset = DicomTranscoder.ExtractOverlays(dataset); if (PixelData == null) { PixelData = DicomPixelData.Create(Dataset); PhotometricInterpretation = PixelData.PhotometricInterpretation; } if (frame < 0) { CurrentFrame = frame; return; } if (Dataset.InternalTransferSyntax.IsEncapsulated) { // decompress single frame from source dataset var transcoder = new DicomTranscoder( this.Dataset.InternalTransferSyntax, DicomTransferSyntax.ExplicitVRLittleEndian); var buffer = transcoder.DecodeFrame(Dataset, frame); // clone the dataset because modifying the pixel data modifies the dataset var clone = Dataset.Clone(); clone.InternalTransferSyntax = DicomTransferSyntax.ExplicitVRLittleEndian; var pixelData = DicomPixelData.Create(clone, true); TrimDecodedPixelDataProperties(pixelData, Dataset.InternalTransferSyntax); pixelData.AddFrame(buffer); _pixelData = PixelDataFactory.Create(pixelData, 0); } else { // pull uncompressed frame from source pixel data _pixelData = PixelDataFactory.Create(PixelData, frame); } _pixelData = _pixelData.Rescale(_scale); _overlays = DicomOverlayData.FromDataset(Dataset) .Where(x => x.Type == DicomOverlayType.Graphics && x.Data != null) .ToArray(); CurrentFrame = frame; if (_pipeline == null) { CreatePipeline(); } }
public void HighBit_Setter_SmallerThanBitsAllocatedIsAllowed(ushort bitsAllocated, ushort highBit) { var dataset = new DicomDataset(DicomTransferSyntax.ExplicitVRLittleEndian); dataset.Add(DicomTag.BitsAllocated, bitsAllocated); var pixelData = DicomPixelData.Create(dataset, true); var exception = Record.Exception(() => pixelData.HighBit = highBit); Assert.Null(exception); Assert.Equal(highBit, pixelData.HighBit); }
/// <summary> /// Create <see cref="GrayscaleRenderOptions"/> from <paramref name="dataset"/> and populate the options properties with values: /// Bit Depth /// Rescale Slope /// Rescale Intercept /// Window Width /// Window Center /// </summary> /// <param name="dataset">Dataset to extract <see cref="GrayscaleRenderOptions"/> from</param> /// <returns>New grayscale render options instance</returns> public static GrayscaleRenderOptions FromDataset(DicomDataset dataset) { var bits = BitDepth.FromDataset(dataset); var options = new GrayscaleRenderOptions(bits); options.RescaleSlope = dataset.Get <double>(DicomTag.RescaleSlope, 1.0); options.RescaleIntercept = dataset.Get <double>(DicomTag.RescaleIntercept, 0.0); if (dataset.Contains(DicomTag.WindowWidth) && dataset.Get <double>(DicomTag.WindowWidth) != 0.0) { //If dataset contains WindowWidth and WindowCenter valid attributes used initially for the grayscale options options.WindowWidth = dataset.Get <double>(DicomTag.WindowWidth); options.WindowCenter = dataset.Get <double>(DicomTag.WindowCenter); } else if (dataset.Contains(DicomTag.SmallestImagePixelValue) && dataset.Contains(DicomTag.LargestImagePixelValue)) { //If dataset contains valid SmallesImagePixelValue and LargesImagePixelValue attributes, use range to calculage //WindowWidth and WindowCenter int smallValue = dataset.Get <int>(DicomTag.SmallestImagePixelValue, 0); int largeValue = dataset.Get <int>(DicomTag.LargestImagePixelValue, 0); largeValue = (int)((largeValue * options.RescaleSlope) + options.RescaleIntercept); smallValue = (int)((smallValue * options.RescaleSlope) + options.RescaleIntercept); if (smallValue != 0 || largeValue != 0) { options.WindowWidth = Math.Abs(largeValue - smallValue); options.WindowCenter = (largeValue + smallValue) / 2.0; } } else { //If reached here, minimum and maximum pixel values calculated from pixels data to calculate //WindowWidth and WindowCenter int padding = dataset.Get <int>(DicomTag.PixelPaddingValue, 0, bits.MinimumValue); var pixelData = DicomPixelData.Create(dataset); var pixels = PixelDataFactory.Create(pixelData, 0); var range = pixels.GetMinMax(padding); range.Maximum = (int)((range.Maximum * options.RescaleSlope) + options.RescaleIntercept); range.Minimum = (int)((range.Minimum * options.RescaleSlope) + options.RescaleIntercept); options.WindowWidth = Math.Abs(range.Maximum - range.Minimum); options.WindowCenter = (range.Maximum + range.Minimum) / 2.0; } options.VOILUTFunction = dataset.Get <string>(DicomTag.VOILUTFunction, "LINEAR"); options.Monochrome1 = dataset.Get <PhotometricInterpretation>(DicomTag.PhotometricInterpretation) == PhotometricInterpretation.Monochrome1; return(options); }
private void Load(DicomDataset dataset) { Dataset = dataset; if (Dataset.InternalTransferSyntax.IsEncapsulated) { Dataset = Dataset.ChangeTransferSyntax(DicomTransferSyntax.ExplicitVRLittleEndian, null); } DicomPixelData pixelData = DicomPixelData.Create(Dataset); _pixelData = PixelDataFactory.Create(pixelData, 0); _overlays = DicomOverlayData.FromDataset(Dataset); }
/// <summary> /// Create pixel data object based on <paramref name="dataset"/>. /// </summary> /// <param name="dataset">Dataset containing pixel data.</param> /// <returns>For non-encapsulated dataset, create pixel data object from original pixel data. For encapsulated dataset, /// create "empty" pixel data object to subsequentially be filled with uncompressed data for each frame.</returns> private static DicomPixelData CreateDicomPixelData(DicomDataset dataset) { var inputTransferSyntax = dataset.InternalTransferSyntax; if (!inputTransferSyntax.IsEncapsulated) { return(DicomPixelData.Create(dataset)); } // Clone the encapsulated dataset because modifying the pixel data modifies the dataset var clone = dataset.Clone(); clone.InternalTransferSyntax = DicomTransferSyntax.ExplicitVRLittleEndian; var pixelData = DicomPixelData.Create(clone, true); // temporary fix for JPEG compressed YBR images, according to enforcement above if ((inputTransferSyntax == DicomTransferSyntax.JPEGProcess1 || inputTransferSyntax == DicomTransferSyntax.JPEGProcess2_4) && pixelData.SamplesPerPixel == 3) { // When converting to RGB in Dicom.Imaging.Codec.Jpeg.i, PlanarConfiguration is set to Interleaved pixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb; pixelData.PlanarConfiguration = PlanarConfiguration.Interleaved; } // temporary fix for JPEG 2000 Lossy images if ((inputTransferSyntax == DicomTransferSyntax.JPEG2000Lossy && pixelData.PhotometricInterpretation == PhotometricInterpretation.YbrIct) || (inputTransferSyntax == DicomTransferSyntax.JPEG2000Lossless && pixelData.PhotometricInterpretation == PhotometricInterpretation.YbrRct)) { // Converted to RGB in Dicom.Imaging.Codec.Jpeg2000.cpp pixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb; } // temporary fix for JPEG2000 compressed YBR images if ((inputTransferSyntax == DicomTransferSyntax.JPEG2000Lossless || inputTransferSyntax == DicomTransferSyntax.JPEG2000Lossy) && (pixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull || pixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull422 || pixelData.PhotometricInterpretation == PhotometricInterpretation.YbrPartial422)) { // For JPEG2000 YBR type images in Dicom.Imaging.Codec.Jpeg2000.cpp, // YBR_FULL is applied and PlanarConfiguration is set to Planar pixelData.PhotometricInterpretation = PhotometricInterpretation.YbrFull; pixelData.PlanarConfiguration = PlanarConfiguration.Planar; } return(pixelData); }
private IByteBuffer Load() { var tag = OverlayTag(DicomTag.OverlayData); if (Dataset.Contains(tag)) { var elem = Dataset.FirstOrDefault(x => x.Tag == tag) as DicomElement; return(elem.Buffer); } else { // overlay embedded in high bits of pixel data if (Dataset.InternalTransferSyntax.IsEncapsulated) { throw new DicomImagingException( "Attempted to extract embedded overlay from compressed pixel data. Decompress pixel data before attempting this operation."); } var pixels = DicomPixelData.Create(Dataset); // (1,1) indicates top left pixel of image int ox = Math.Max(0, OriginX - 1); int oy = Math.Max(0, OriginY - 1); int ow = Columns; int oh = Rows; var frame = pixels.GetFrame(0); var bits = new BitList(); bits.Capacity = Rows * Columns; int mask = 1 << BitPosition; // Sanity check: do not collect overlay data if Overlay Bit Position is within the used pixel range. (#110) if (this.BitPosition <= pixels.HighBit && this.BitPosition > pixels.HighBit - pixels.BitsStored) { // Do nothing } else if (pixels.BitsAllocated == 8) { var data = IO.ByteConverter.ToArray <byte>(frame); for (int y = 0; y < oh; y++) { int n = (y + oy) * pixels.Width + ox; int i = y * Columns; for (int x = 0; x < ow; x++) { if ((data[n] & mask) != 0) { bits[i] = true; } n++; i++; } } } else if (pixels.BitsAllocated == 16) { // we don't really care if the pixel data is signed or not var data = IO.ByteConverter.ToArray <ushort>(frame); for (int y = 0; y < oh; y++) { int n = (y + oy) * pixels.Width + ox; int i = y * Columns; for (int x = 0; x < ow; x++) { if ((data[n] & mask) != 0) { bits[i] = true; } n++; i++; } } } else { throw new DicomImagingException( "Unable to extract embedded overlay from pixel data with bits stored greater than 16."); } return(new MemoryByteBuffer(bits.Array)); } }
private void Load(DicomDataset ds) { _rows = ds.Get <ushort>(OverlayTag(DicomTag.OverlayRows)); _columns = ds.Get <ushort>(OverlayTag(DicomTag.OverlayColumns)); var type = ds.Get <string>(OverlayTag(DicomTag.OverlayType), "Unknown"); if (type.StartsWith("R")) { _type = DicomOverlayType.ROI; } else { _type = DicomOverlayType.Graphics; } DicomTag tag = OverlayTag(DicomTag.OverlayOrigin); if (ds.Contains(tag)) { _originX = ds.Get <short>(tag, 0, 1); _originY = ds.Get <short>(tag, 1, 1); } _bitsAllocated = ds.Get <ushort>(OverlayTag(DicomTag.OverlayBitsAllocated), 0, 1); _bitPosition = ds.Get <ushort>(OverlayTag(DicomTag.OverlayBitPosition), 0, 0); tag = OverlayTag(DicomTag.OverlayData); if (ds.Contains(tag)) { var elem = ds.FirstOrDefault(x => x.Tag == tag) as DicomElement; _data = elem.Buffer; } else { // overlay embedded in high bits of pixel data if (ds.InternalTransferSyntax.IsEncapsulated) { throw new DicomImagingException("Attempted to extract embedded overlay from compressed pixel data. Decompress pixel data before attempting this operation."); } var pixels = DicomPixelData.Create(ds); // (1,1) indicates top left pixel of image int ox = Math.Max(0, _originX - 1); int oy = Math.Max(0, _originY - 1); int ow = _rows - (pixels.Width - _rows - ox); int oh = _columns - (pixels.Height - _columns - oy); var frame = pixels.GetFrame(0); // calculate length of output buffer var count = (_rows * _columns) / 8; if (((_rows * _columns) % 8) != 0) { count++; } if ((count & 1) != 0) { count++; } var bytes = new byte[count]; var bits = new BitArray(bytes); int mask = 1 << _bitPosition; if (pixels.BitsAllocated == 8) { var data = ByteBufferEnumerator <byte> .Create(frame).ToArray(); for (int y = oy; y < oh; y++) { int n = (y * pixels.Width) + ox; int i = (y - oy) * _columns; for (int x = ox; x < ow; x++) { if ((data[n] & mask) != 0) { bits[i] = true; } n++; i++; } } } else if (pixels.BitsAllocated == 16) { // we don't really care if the pixel data is signed or not var data = ByteBufferEnumerator <ushort> .Create(frame).ToArray(); for (int y = oy; y < oh; y++) { int n = (y * pixels.Width) + ox; int i = (y - oy) * _columns; for (int x = ox; x < ow; x++) { if ((data[n] & mask) != 0) { bits[i] = true; } n++; i++; } } } else { throw new DicomImagingException("Unable to extract embedded overlay from pixel data with bits stored greater than 16."); } _data = new MemoryByteBuffer(bytes); } _description = ds.Get <string>(OverlayTag(DicomTag.OverlayDescription), String.Empty); _subtype = ds.Get <string>(OverlayTag(DicomTag.OverlaySubtype), String.Empty); _label = ds.Get <string>(OverlayTag(DicomTag.OverlayLabel), String.Empty); _frames = ds.Get <int>(OverlayTag(DicomTag.NumberOfFramesInOverlay), 0, 1); _frameOrigin = ds.Get <ushort>(OverlayTag(DicomTag.ImageFrameOrigin), 0, 1); //TODO: include ROI }
private IByteBuffer Load() { var tag = OverlayTag(DicomTag.OverlayData); if (Dataset.Contains(tag)) { var elem = Dataset.FirstOrDefault(x => x.Tag == tag) as DicomElement; return(elem.Buffer); } else { // overlay embedded in high bits of pixel data if (Dataset.InternalTransferSyntax.IsEncapsulated) { throw new DicomImagingException("Attempted to extract embedded overlay from compressed pixel data. Decompress pixel data before attempting this operation."); } var pixels = DicomPixelData.Create(Dataset); // (1,1) indicates top left pixel of image int ox = Math.Max(0, OriginX - 1); int oy = Math.Max(0, OriginY - 1); int ow = Rows - (pixels.Width - Rows - ox); int oh = Columns - (pixels.Height - Columns - oy); var frame = pixels.GetFrame(0); var bits = new BitList(); bits.Capacity = Rows * Columns; int mask = 1 << BitPosition; if (pixels.BitsAllocated == 8) { var data = ByteBufferEnumerator <byte> .Create(frame).ToArray(); for (int y = oy; y < oh; y++) { int n = (y * pixels.Width) + ox; int i = (y - oy) * Columns; for (int x = ox; x < ow; x++) { if ((data[n] & mask) != 0) { bits[i] = true; } n++; i++; } } } else if (pixels.BitsAllocated == 16) { // we don't really care if the pixel data is signed or not var data = ByteBufferEnumerator <ushort> .Create(frame).ToArray(); for (int y = oy; y < oh; y++) { int n = (y * pixels.Width) + ox; int i = (y - oy) * Columns; for (int x = ox; x < ow; x++) { if ((data[n] & mask) != 0) { bits[i] = true; } n++; i++; } } } else { throw new DicomImagingException("Unable to extract embedded overlay from pixel data with bits stored greater than 16."); } return(new MemoryByteBuffer(bits.Array)); } }