/// <summary>
        /// Creates a DICOM overlay from a GDI+ Bitmap.
        /// </summary>
        /// <param name="ds">Dataset</param>
        /// <param name="bitmap">Bitmap</param>
        /// <param name="mask">Color mask for overlay</param>
        /// <returns>DICOM overlay</returns>
        public static DicomOverlayData FromBitmap(DicomDataset ds, Bitmap bitmap, Color mask)
        {
            ushort group = 0x6000;
            while (ds.Contains(new DicomTag(group, DicomTag.OverlayBitPosition.Element))) group += 2;

            var overlay = new DicomOverlayData(ds, group)
                              {
                                  Type = DicomOverlayType.Graphics,
                                  Rows = bitmap.Height,
                                  Columns = bitmap.Width,
                                  OriginX = 1,
                                  OriginY = 1,
                                  BitsAllocated = 1,
                                  BitPosition = 1
                              };

            var array = new BitList { Capacity = overlay.Rows * overlay.Columns };

            int p = 0;
            for (var y = 0; y < bitmap.Height; y++)
            {
                for (var x = 0; x < bitmap.Width; x++, p++)
                {
                    if (bitmap.GetPixel(x, y).ToArgb() == mask.ToArgb()) array[p] = true;
                }
            }

            overlay.Data = EvenLengthBuffer.Create(new MemoryByteBuffer(array.Array));

            return overlay;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets all overlays in a DICOM dataset.
        /// </summary>
        /// <param name="ds">Dataset</param>
        /// <returns>Array of overlays</returns>
        public static DicomOverlayData[] FromDataset(DicomDataset ds)
        {
            var groups = new List <ushort>();

            groups.AddRange(
                ds.Where(x => x.Tag.Group >= 0x6000 && x.Tag.Group <= 0x60FF && x.Tag.Element == 0x0010)
                .Select(x => x.Tag.Group));
            var overlays = new List <DicomOverlayData>();

            foreach (var group in groups)
            {
                // ensure that 6000 group is actually an overlay group
                if (ds.Get <DicomElement>(new DicomTag(group, 0x0010)).ValueRepresentation != DicomVR.US)
                {
                    continue;
                }

                try
                {
                    DicomOverlayData overlay = new DicomOverlayData(ds, group);
                    overlays.Add(overlay);
                }
                catch
                {
                    // bail out if not an overlay group
                }
            }
            return(overlays.ToArray());
        }
Ejemplo n.º 3
0
        /// <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();
        }
Ejemplo n.º 4
0
        public void OverlayTypeGetter_TypeNotSet_ShouldThrow()
        {
            const ushort group = 0x6008;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group);

            var exception = Record.Exception(() => od.Type);

            Assert.NotNull(exception);
        }
Ejemplo n.º 5
0
        /// <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();
            }
        }
Ejemplo n.º 6
0
        public void OverlayTypeGetter_TypeSetToOtherThanRAndG_ShouldThrow()
        {
            const ushort group = 0x6001;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group);

            dataset.AddOrUpdate(new DicomTag(group, 0x0040), "O");

            var exception = Record.Exception(() => od.Type);

            Assert.NotNull(exception);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        public void OverlayTypeGetter_TypeSetToR_ReturnsROI()
        {
            const DicomOverlayType expected = DicomOverlayType.ROI;
            const ushort           group    = 0x6012;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group);

            dataset.AddOrUpdate(new DicomTag(group, 0x0040), "R");

            var actual = od.Type;

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 9
0
        public void OriginYGetter_ReturnsValueAtIndex0()
        {
            const int    expected = 42;
            const ushort group    = 0x6012;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group);

            dataset.AddOrUpdate(new DicomTag(group, 0x0050), "42", "0");

            var actual = od.OriginY;

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 10
0
        public void OriginYSetter_ReturnsValueAtIndex0()
        {
            const int    expected = 96;
            const ushort group    = 0x6012;

            DicomDataset     dataset     = new DicomDataset();
            DicomOverlayData overlayData = new DicomOverlayData(dataset, group);

            overlayData.OriginY = expected;

            int originY = dataset.GetValueOrDefault <short>(new DicomTag(group, 0x0050), 0, 1);

            Assert.Equal(expected, originY);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets all overlays in a DICOM dataset.
        /// </summary>
        /// <param name="ds">Dataset</param>
        /// <returns>Array of overlays</returns>
        public static DicomOverlayData[] FromDataset(DicomDataset ds)
        {
            var groups = new List <ushort>();

            groups.AddRange(ds.Where(x => x.Tag.Group >= 0x6000 && x.Tag.Group <= 0x60FF && x.Tag.Element == 0x0010).Select(x => x.Tag.Group));
            var overlays = new List <DicomOverlayData>();

            foreach (var group in groups)
            {
                DicomOverlayData overlay = new DicomOverlayData(ds, group);
                overlays.Add(overlay);
            }
            return(overlays.ToArray());
        }
Ejemplo n.º 12
0
        public void OverlayTypeSetter_SetToROI_ReturnsR()
        {
            const string expected = "R";
            const ushort group    = 0x6011;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group)
            {
                Type = DicomOverlayType.ROI
            };

            var actual = dataset.Get <string>(new DicomTag(group, 0x0040));

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 13
0
        public void Constructor_GroupSpecified_GroupTransferredToDescription()
        {
            const string expected = "Description 6002";
            const ushort group    = 0x6002;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group)
            {
                Description = expected
            };

            var actual = dataset.Get <string>(new DicomTag(group, 0x0022));

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 14
0
        public void Constructor_GroupSpecified_GroupTransferredToSubtype()
        {
            const string expected = "Subtype 6005";
            const ushort group    = 0x6005;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group)
            {
                Subtype = expected
            };

            var actual = dataset.Get <string>(new DicomTag(group, 0x0045));

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 15
0
        public void Constructor_GroupSpecified_GroupTransferredToLabel()
        {
            const string expected = "Label 6003";
            const ushort group    = 0x6003;

            var dataset = new DicomDataset();
            var od      = new DicomOverlayData(dataset, group)
            {
                Label = expected
            };

            var actual = dataset.Get <string>(new DicomTag(group, 0x1500));

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Creates a DICOM overlay from a GDI+ Bitmap.
        /// </summary>
        /// <param name="ds">Dataset</param>
        /// <param name="bitmap">Bitmap</param>
        /// <param name="mask">Color mask for overlay</param>
        /// <returns>DICOM overlay</returns>
        public static DicomOverlayData FromBitmap(DicomDataset ds, Bitmap bitmap, Color mask)
        {
            ushort group = 0x6000;

            while (ds.Contains(new DicomTag(group, DicomTag.OverlayBitPosition.Element)))
            {
                group += 2;
            }

            var overlay = new DicomOverlayData(ds, group);

            overlay.Type          = DicomOverlayType.Graphics;
            overlay.Rows          = bitmap.Height;
            overlay.Columns       = bitmap.Width;
            overlay.OriginX       = 1;
            overlay.OriginY       = 1;
            overlay.BitsAllocated = 1;
            overlay.BitPosition   = 1;

            var count = overlay.Rows * overlay.Columns / 8;

            if ((overlay.Rows * overlay.Columns) % 8 > 0)
            {
                count++;
            }

            var array = new BitList();

            array.Capacity = overlay.Rows * overlay.Columns;

            int p = 0;

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++, p++)
                {
                    if (bitmap.GetPixel(x, y).ToArgb() == mask.ToArgb())
                    {
                        array[p] = true;
                    }
                }
            }

            overlay.Data = EvenLengthBuffer.Create(
                new MemoryByteBuffer(array.Array));

            return(overlay);
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Create array of graphics overlays from dataset.
 /// </summary>
 /// <param name="dataset">Dataset potentially containing overlays.</param>
 /// <returns>Array of overlays of type <see cref="DicomOverlayType.Graphics"/>.</returns>
 private static DicomOverlayData[] CreateGraphicsOverlays(DicomDataset dataset)
 {
     return(DicomOverlayData.FromDataset(dataset)
            .Where(x => x.Type == DicomOverlayType.Graphics && x.Data != null)
            .ToArray());
 }
Ejemplo n.º 18
0
		/// <summary>
		/// Gets all overlays in a DICOM dataset.
		/// </summary>
		/// <param name="ds">Dataset</param>
		/// <returns>Array of overlays</returns>
		public static DicomOverlayData[] FromDataset(DicomDataset ds) {
			var groups = new List<ushort>();
			groups.AddRange(ds.Where(x => x.Tag.Group >= 0x6000 && x.Tag.Group <= 0x60FF && x.Tag.Element == 0x0010).Select(x => x.Tag.Group));
			var overlays = new List<DicomOverlayData>();
			foreach (var group in groups) {
				// ensure that 6000 group is actually an overlay group
				if (ds.Get<DicomElement>(new DicomTag(group, 0x0010)).ValueRepresentation != DicomVR.US)
					continue;

				try {
					DicomOverlayData overlay = new DicomOverlayData(ds, group);
					overlays.Add(overlay);
				} catch {
					// bail out if not an overlay group
				}
			}
			return overlays.ToArray();
		}
Ejemplo n.º 19
0
 /// <summary>
 /// Gets all overlays in a DICOM dataset.
 /// </summary>
 /// <param name="ds">Dataset</param>
 /// <returns>Array of overlays</returns>
 public static DicomOverlayData[] FromDataset(DicomDataset ds)
 {
     var groups = new List<ushort>();
     groups.AddRange(ds.Where(x => x.Tag.Group >= 0x6000 && x.Tag.Group <= 0x60FF && x.Tag.Element == 0x0010).Select(x => x.Tag.Group));
     var overlays = new List<DicomOverlayData>();
     foreach (var group in groups) {
         DicomOverlayData overlay = new DicomOverlayData(ds, group);
         overlays.Add(overlay);
     }
     return overlays.ToArray();
 }
		public static DicomOverlayData FromBitmap(DicomDataset ds, Bitmap bitmap, Color mask) {
#endif
			ushort group = 0x6000;
			while (ds.Contains(new DicomTag(group, DicomTag.OverlayBitPosition.Element)))
				group += 2;

			var overlay = new DicomOverlayData(ds, group);
			overlay.Type = DicomOverlayType.Graphics;
#if NETFX_CORE || SILVERLIGHT
			overlay.Rows = bitmap.PixelHeight;
			overlay.Columns = bitmap.PixelWidth;
#else
			overlay.Rows = bitmap.Height;
			overlay.Columns = bitmap.Width;
#endif
			overlay.OriginX = 1;
			overlay.OriginY = 1;
			overlay.BitsAllocated = 1;
			overlay.BitPosition = 1;

			var count = overlay.Rows * overlay.Columns / 8;
			if ((overlay.Rows * overlay.Columns) % 8 > 0)
				count++;

			var array = new BitList();
			array.Capacity = overlay.Rows * overlay.Columns;

#if NETFX_CORE || SILVERLIGHT
			int p = 0;
			for (int y = 0; y < bitmap.PixelHeight; y++)
			{
				for (int x = 0; x < bitmap.PixelWidth; x++, p++)
				{
					if (bitmap.GetPixel(x, y).ToArgb() == mask.ToArgb())
						array[p] = true;
#else
			int p = 0;
			for (int y = 0; y < bitmap.Height; y++) {
				for (int x = 0; x < bitmap.Width; x++, p++) {
					if (bitmap.GetPixel(x, y).ToArgb() == mask.ToArgb())
						array[p] = true;
#endif
				}
			}

			overlay.Data = EvenLengthBuffer.Create(
									new MemoryByteBuffer(array.Array));

			return overlay;
		}