Example #1
0
        public void Render(int[] pixels, int width, int height)
        {
            byte[] data = null;

            if (_scaledData == null) _scaledData = _originalData.Rescale(_scale);

            data = (_scaledData as GrayscalePixelDataU8).Data;

            int ox = (int)(_offsetX * _scale);
            int oy = (int)(_offsetY * _scale);

#if NET35
            for (var y = 0; y < _scaledData.Height; ++y)
#else
                Parallel.For(0, _scaledData.Height, y =>
#endif
            {
                if ((oy + y) >= height) return;
                for (int i = _scaledData.Width * y, e = i + _scaledData.Width, x = 0; i < e; i++, x++)
                {
                    if (data[i] > 0)
                    {
                        //this is wrong, when scaling is active
                        //if ((ox + x) >= width) break;
                        int p = (oy * width) + ox + i;
                        pixels[p] = _color;
                    }
                }
            }
#if !NET35
            );
#endif
        }
Example #2
0
        public void Render(int[] pixels, int width, int height)
        {
            byte[] data = null;

            if (_scaledData == null) _scaledData = _originalData.Rescale(_scale);

            data = (_scaledData as GrayscalePixelDataU8).Data;

            int ox = (int)(_offsetX * _scale);
            int oy = (int)(_offsetY * _scale);

            Parallel.For(
                0,
                _scaledData.Height,
                y =>
                    {
                        if ((oy + y) >= height) return;
                        for (int i = _scaledData.Width * y, e = i + _scaledData.Width, x = 0; i < e; i++, x++)
                        {
                            if (data[i] > 0)
                            {
                                if ((ox + x) >= width) break;
                                int p = (oy * width) + ox + i;
                                pixels[p] = _color;
                            }
                        }
                    });
        }
Example #3
0
        public void Render(int[] pixels, int width, int height)
        {
            byte[] data = null;

            if (_scaledData == null) _scaledData = _originalData.Rescale(_scale);

            data = (_scaledData as GrayscalePixelDataU8).Data;

            int ox = (int)(_offsetX * _scale);
            int oy = (int)(_offsetY * _scale);

#if NET35
            for (var y = 0; y < _scaledData.Height; ++y)
#else
                Parallel.For(0, _scaledData.Height, y =>
#endif
            {
                if ((oy + y) >= height) return;
                for (int i = _scaledData.Width * y, e = i + _scaledData.Width, x = 0; i < e; i++, x++)
                {
                    if (data[i] > 0)
                    {
                        //this is wrong, when scaling is active
                        //if ((ox + x) >= width) break;
                        int p = (oy * width) + ox + i;
                        pixels[p] = _color;
                    }
                }
            }
#if !NET35
            );
#endif
        }
Example #4
0
        public override DICOMImage Parse()
        {
            // Read the pixel data from the image, frame 0
            IPixelData pixelData = PixelDataFactory.Create(image.PixelData, 0);

            // Get the pixels from the pixel data
            ushort[] pixels = ((GrayscalePixelDataU16)pixelData).Data;

            // Get the multiplier
            int min = image.PixelData.BitDepth.MinimumValue;
            int max = image.PixelData.BitDepth.MaximumValue;

            if (min != 0)
            {
                throw new FormatException("Error in the minimum value of the pixel data");
            }

            float multiplier = ushort.MaxValue / max;

            // Apply the multiplier to the pixels
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = (ushort)(pixels[i] * multiplier);
            }

            return(DICOMImage.FromData(image.Width, image.Height, pixels));
        }
        public void Render(int[] pixels, int width, int height)
        {
            byte[] data = null;

            if (_scaledData == null)
            {
                _scaledData = _originalData.Rescale(_scale);
            }

            data = (_scaledData as GrayscalePixelDataU8).Data;

            int ox = (int)(_offsetX * _scale);
            int oy = (int)(_offsetY * _scale);

            Parallel.For(0, _scaledData.Height, y => {
                if ((oy + y) >= height)
                {
                    return;
                }
                for (int i = _scaledData.Width * y, e = i + _scaledData.Width, x = 0; i < e; i++, x++)
                {
                    if (data[i] > 0)
                    {
                        if ((ox + x) >= width)
                        {
                            break;
                        }
                        int p     = (oy * width) + ox + i;
                        pixels[p] = _color;
                    }
                }
            });
        }
Example #6
0
 public ImageGraphic(IPixelData pixelData)
 {
     _originalData = pixelData;
     _zorder = 255;
     _applyLut = true;
     Scale(1.0);
 }
Example #7
0
 public ImageGraphic(IPixelData pixelData)
 {
     _originalData = pixelData;
     _zorder       = 255;
     _applyLut     = true;
     Scale(1.0);
 }
Example #8
0
        private static ImageData CreateImageData(IPixelData pixelData)
        {
            ImageData image = null;

            if (pixelData.Components != 1)
            {
                return(null);
            }

            if (pixelData is GrayscalePixelDataU16 grayscalePixelDataU16)
            {
                image = new ImageData(grayscalePixelDataU16.Data);
            }
            else if (pixelData is GrayscalePixelDataS16 grayscalePixelDataS16)
            {
                image = new ImageData(grayscalePixelDataS16.Data);
            }
            else if (pixelData is GrayscalePixelDataU8 grayscalePixelDataU8)
            {
                image = new ImageData(grayscalePixelDataU8.Data);
            }

            image.Width  = pixelData.Width;
            image.Height = pixelData.Height;

            var minMax = pixelData.GetMinMax();

            image.MinValue = minMax.Minimum;
            image.MaxValue = minMax.Maximum;

            return(image);
        }
Example #9
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scale) <= Double.Epsilon) return;

            _scale = scale;
            _scaledData = null;
        }
Example #10
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scale) <= Double.Epsilon) return;

            _scale = scale;
            _scaledData = null;
        }
Example #11
0
        /// <summary>Renders DICOM image to <see cref="IImage"/>.</summary>
        /// <param name="frame">Zero indexed frame number.</param>
        /// <returns>Rendered image</returns>
        public virtual IImage RenderImage(int frame = 0)
        {
            IPixelData pixels;

            lock (_lock)
            {
                var load = frame >= 0 && (frame != CurrentFrame || _rerender);
                CurrentFrame = frame;
                _rerender    = false;

                if (load)
                {
                    var frameIndex = GetFrameIndex(frame);
                    pixels  = PixelDataFactory.Create(_pixelData, frameIndex).Rescale(_scale);
                    _pixels = pixels;
                }
                else
                {
                    pixels = _pixels;
                }
            }

            if (ShowOverlays)
            {
                EstablishGraphicsOverlays();
            }

            IImage image;
            var    graphic = new ImageGraphic(pixels);

            if (ShowOverlays)
            {
                foreach (var overlay in _overlays)
                {
                    if (overlay.Data is EmptyBuffer)//fixed overlay.data is null, exception thrown
                    {
                        continue;
                    }

                    if (frame + 1 < overlay.OriginFrame ||
                        frame + 1 > overlay.OriginFrame + overlay.NumberOfFrames - 1)
                    {
                        continue;
                    }

                    var og = new OverlayGraphic(
                        PixelDataFactory.Create(overlay),
                        overlay.OriginX - 1,
                        overlay.OriginY - 1,
                        OverlayColor);
                    graphic.AddOverlay(og);
                    og.Scale(_scale);
                }
            }

            image = graphic.RenderImage(_pipeline.LUT);

            return(image);
        }
Example #12
0
		/// <summary>
		/// Initialize new instance of <seealso cref="OverlayGraphic"/>
		/// </summary>
		/// <param name="pixelData">Overlay pixel data</param>
		/// <param name="offsetx">X offset</param>
		/// <param name="offsety">Y offset</param>
		/// <param name="color">The color of the resulting overlay</param>
		public OverlayGraphic(SingleBitPixelData pixelData, int offsetx, int offsety, int color) {
			_originalData = pixelData;
			_scaledData = _originalData;
			_offsetX = offsetx;
			_offsetY = offsety;
			_color = color;
			_scale = 1.0;
		}
Example #13
0
        /// <summary>Renders DICOM image to <see cref="IImage"/>.</summary>
        /// <param name="frame">Zero indexed frame number.</param>
        /// <returns>Rendered image</returns>
        public virtual IImage RenderImage(int frame = 0)
        {
            bool load;

            lock (_lock)
            {
                load          = frame >= 0 && (frame != CurrentFrame || _rerender);
                _currentFrame = frame;
                _rerender     = false;
            }

            var frameIndex = GetFrameIndex(frame);

            if (load)
            {
                lock (_lock)
                {
                    _pixels = PixelDataFactory.Create(_pixelData, frameIndex).Rescale(_scale);
                }
            }

            if (ShowOverlays)
            {
                EstablishGraphicsOverlays();
            }

            IImage image;

            lock (_lock)
            {
                var graphic = new ImageGraphic(_pixels);

                if (ShowOverlays)
                {
                    foreach (var overlay in _overlays)
                    {
                        if (frame + 1 < overlay.OriginFrame ||
                            frame + 1 > overlay.OriginFrame + overlay.NumberOfFrames - 1)
                        {
                            continue;
                        }

                        var og = new OverlayGraphic(
                            PixelDataFactory.Create(overlay),
                            overlay.OriginX - 1,
                            overlay.OriginY - 1,
                            OverlayColor);
                        graphic.AddOverlay(og);
                        og.Scale(_scale);
                    }
                }

                image = graphic.RenderImage(_pipeline.LUT);
            }

            return(image);
        }
Example #14
0
 public OverlayGraphic(IPixelData pixelData, int offsetx, int offsety)
 {
     _originalData = pixelData;
     _offsetX = offsetx;
     _offsetY = offsety;
     _zorder = 128;
     _applyLut = false;
     Scale(1.0f);
 }
Example #15
0
 public OverlayGraphic(IPixelData pixelData, int offsetx, int offsety)
 {
     _originalData = pixelData;
     _offsetX      = offsetx;
     _offsetY      = offsety;
     _zorder       = 128;
     _applyLut     = false;
     Scale(1.0f);
 }
 /// <summary>
 /// Initialize new instance of <seealso cref="OverlayGraphic"/>
 /// </summary>
 /// <param name="pixelData">Overlay pixel data</param>
 /// <param name="offsetx">X offset</param>
 /// <param name="offsety">Y offset</param>
 /// <param name="color">The color of the resulting overlay</param>
 public OverlayGraphic(SingleBitPixelData pixelData, int offsetx, int offsety, int color)
 {
     _originalData = pixelData;
     _scaledData   = _originalData;
     _offsetX      = offsetx;
     _offsetY      = offsety;
     _color        = color;
     _scale        = 1.0;
 }
Example #17
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();
        }
Example #18
0
		private void Load(DcmDataset dataset) {
			Dataset = dataset;
			if (Dataset.InternalTransferSyntax.IsEncapsulated)
				Dataset.ChangeTransferSyntax(DicomTransferSyntax.ExplicitVRLittleEndian, null);
			DcmPixelData pixelData = new DcmPixelData(Dataset);
			_pixelData = PixelDataFactory.Create(pixelData, 0);
			_pipeline = PipelineFactory.Create(Dataset, pixelData);
			pixelData.Unload();

			_overlays = DcmOverlayData.FromDataset(Dataset);
		}
Example #19
0
        private static int[] GetGrayPixelData(DicomDataset dataset)
        {
            DicomPixelData pixelData = DicomPixelData.Create(dataset);
            IByteBuffer    buffer    = pixelData.GetFrame(0);

            IPixelData data          = PixelDataFactory.Create(pixelData, 0);
            var        renderOptions = GrayscaleRenderOptions.FromDataset(dataset);

            int[] output = new int[pixelData.Width * pixelData.Height];
            data.Render(new ModalityLUT(renderOptions), output);
            return(output);
        }
Example #20
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();
            }
        }
Example #21
0
 public ImageGraphic(IPixelData pixelData, Double scaleVal = 0)
     : this()
 {
     _originalData = pixelData;
     if (scaleVal > 0 && _originalData.Components == 1)
     {
         Scale(scaleVal);
     }
     else
     {
         Scale(1.0);
     }
 }
Example #22
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);
        }
Example #23
0
        /// <inheritdoc />
        public void Scale(double scale)
        {
            if ((scale - _scaleFactor).IsNearlyZero())
            {
                return;
            }

            _scaleFactor = scale;
            _scaledData  = null;

            foreach (var overlay in _overlays)
            {
                overlay.Scale(scale);
            }
        }
Example #24
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon)
            {
                return;
            }

            _scaleFactor = scale;
            _scaledData  = null;

            foreach (var overlay in _overlays)
            {
                overlay.Scale(scale);
            }
        }
Example #25
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon)
            {
                return;
            }

            _scaleFactor = scale;
            if (_bitmap != null)
            {
                _scaledData = null;
                _pixels.Dispose();
                _pixels = null;
                _bitmap = null;
            }
        }
Example #26
0
        private double?Interpolate(IPixelData pixels, Point2D imgSpace)
        {
            if ((imgSpace.X >= 0.0) && (imgSpace.X < pixels.Width - 1) && (imgSpace.Y >= 0.0) && (imgSpace.Y < pixels.Height - 1))
            {
                var    posX   = (int)Math.Floor(imgSpace.X);
                double alphaX = imgSpace.X - posX;
                var    posY   = (int)Math.Floor(imgSpace.Y);
                double alphaY = imgSpace.Y - posY;

                return((1 - alphaX) * ((1 - alphaY) * pixels.GetPixel(posX, posY)
                                       + alphaY * pixels.GetPixel(posX, posY + 1))
                       + alphaX * ((1 - alphaY) * pixels.GetPixel(posX + 1, posY)
                                   + alphaY * pixels.GetPixel(posX + 1, posY + 1)));
            }

            return(null);
        }
Example #27
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon)
            {
                return;
            }

            _scaleFactor = scale;
            if (_bitmap != null)
            {
                _scaledData = null;
                _pixels.Dispose();
                _pixels = null;
                _bitmap = null;
            }

            foreach (var overlay in _overlays)
            {
                overlay.Scale(scale);
            }
        }
Example #28
0
        public string DicomToGraphicalJSON(string filePathToDir)
        {
            string[] allFilePaths = System.IO.Directory.GetFiles(filePathToDir);

            // store all of the dicoms as a list of dicom files
            List <DicomFile> dicoms = new List <DicomFile>();

            // get the amount of data
            DicomFileData dicomFile = null;

            ArrayList data = new ArrayList();

            // loop though all of the files and open them
            foreach (string filePath in allFilePaths)
            {
                try
                {
                    dicoms.Add(DicomFile.Open(filePath));
                }
                catch (Exception)
                {
                    Console.WriteLine("file at \"" + filePath + "\" readable as a dicom file");
                }
            }

            // sort the order of all the dicom objects so they work in the correct order
            dicoms.Sort((d1, d2) => d1.Dataset.GetSingleValue <int>(DicomTag.InstanceNumber).CompareTo(d2.Dataset.GetSingleValue <int>(DicomTag.InstanceNumber)));

            // sort out the intial values based on the first dicom in the list
            // get the pixel data out as much as possible
            DicomPixelData header    = DicomPixelData.Create(dicoms[0].Dataset);
            IPixelData     pixelData = PixelDataFactory.Create(header, 0);

            // Build the correct file for this data depending on what is stored in the dicom files
            if (pixelData is GrayscalePixelDataU16)
            {
                data.AddRange(((GrayscalePixelDataU16)pixelData).Data);
                dicomFile = new U16DicomFileData();
            }
            else if (pixelData is GrayscalePixelDataS16)
            {
                data.AddRange(((GrayscalePixelDataS16)pixelData).Data);
                dicomFile = new S16DicomFileData();
            }
            else if (pixelData is GrayscalePixelDataU8)
            {
                data.AddRange(((GrayscalePixelDataU8)pixelData).Data);
                dicomFile = new U8bitDicomFileData();
            }
            else if (pixelData is GrayscalePixelDataS32)
            {
                data.AddRange(((GrayscalePixelDataS32)pixelData).Data);
                dicomFile = new S32DicomDataFile();
            }
            else if (pixelData is GrayscalePixelDataU32)
            {
                data.AddRange(((GrayscalePixelDataU32)pixelData).Data);
                dicomFile = new U32DicomDataFile();
            }
            else if (pixelData is ColorPixelData24)
            {
                // note that this one will take some work
                // 0 == red, 1 == Green, 2 == Blue and so on
                data.AddRange(((ColorPixelData24)pixelData).Data);
                dicomFile = new RGBColourDicomFileData();
            }
            else if (pixelData is SingleBitPixelData)
            {
                data.AddRange(((SingleBitPixelData)pixelData).Data);
                dicomFile = new U8bitDicomFileData();
            }

            // get the amount of rows the image data set has out of the dicom file.
            if (!dicoms[0].Dataset.TryGetSingleValue(DicomTag.Rows, out dicomFile.width))
            {
                if (!dicoms[0].Dataset.TryGetSingleValue(DicomTag.NumberOfVerticalPixels, out dicomFile.width))
                {
                    // if the value for rows dosn't exist mark the entry as -1
                    dicomFile.width = -1;
                }
            }

            // get the amount of coloumns in the image
            if (!dicoms[0].Dataset.TryGetSingleValue(DicomTag.Columns, out dicomFile.height))
            {
                // try getting this data  another way then
                if (!dicoms[0].Dataset.TryGetSingleValue(DicomTag.NumberOfHorizontalPixels, out dicomFile.height))
                {
                    // if the value for rows dosn't exist mark the entry as -1
                    dicomFile.height = -1;
                }
            }

            // Datastucture that will hold the final DS
            float[] aspectOfPixel = new float[2];


            // get the amount of coloumns in the image
            if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.PixelSpacing, out aspectOfPixel))
            {
                // all these methods will try to do it until one works or this feild will be made invalid
                if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.ImagerPixelSpacing, out aspectOfPixel))
                {
                    if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.NominalScannedPixelSpacing, out aspectOfPixel))
                    {
                        if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.CompensatorPixelSpacing, out aspectOfPixel))
                        {
                            if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.DetectorElementSpacing, out aspectOfPixel))
                            {
                                if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.PresentationPixelSpacing, out aspectOfPixel))
                                {
                                    if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.PrinterPixelSpacing, out aspectOfPixel))
                                    {
                                        if (!dicoms[0].Dataset.TryGetValues <float>(DicomTag.ObjectPixelSpacingInCenterOfBeam, out aspectOfPixel))
                                        {
                                            // if none of the above methods work mark the objects as invalid
                                            aspectOfPixel[0] = -1;
                                            aspectOfPixel[1] = -1;
                                            // This could be valid in a case were only one row of data was colected
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // TODO this dosn't take into account what will happen if the user only recorded one line of pixels since my work dosn't focus on it

            // Save the data from pixel spacing
            dicomFile.pixelSpacingX = aspectOfPixel[0];
            dicomFile.pixelSpacingY = aspectOfPixel[1];

            // try to get the thickness of the the slice or else set it to an invalid number

            if (dicoms[0].Dataset.TryGetSingleValue(DicomTag.SliceThickness, out dicomFile.sliceThickness))
            {
                dicomFile.sliceThickness = -1;
            }

            // try to get how much room is between each slice
            if (!dicoms[0].Dataset.TryGetSingleValue(DicomTag.SpacingBetweenSlices, out dicomFile.spacingBetweenSlices))
            {
                dicomFile.spacingBetweenSlices = -1;
            }

            dicomFile.breath = dicoms.Count;

            // loop though all of the dicoms in order now
            for (int index = 1; index < dicoms.Count; index++)
            {
                // get the pixel data out as much as possible
                header    = DicomPixelData.Create(dicoms[index].Dataset);
                pixelData = PixelDataFactory.Create(header, 0);

                // Build the correct file for this data depending on what is stored in the dicom files
                if (pixelData is GrayscalePixelDataU16)
                {
                    data.AddRange(((GrayscalePixelDataU16)pixelData).Data);
                }
                else if (pixelData is GrayscalePixelDataS16)
                {
                    data.AddRange(((GrayscalePixelDataS16)pixelData).Data);
                }
                else if (pixelData is GrayscalePixelDataU8)
                {
                    data.AddRange(((GrayscalePixelDataU8)pixelData).Data);
                }
                else if (pixelData is GrayscalePixelDataS32)
                {
                    data.AddRange(((GrayscalePixelDataS32)pixelData).Data);
                }
                else if (pixelData is GrayscalePixelDataU32)
                {
                    data.AddRange(((GrayscalePixelDataU32)pixelData).Data);
                }
                else if (pixelData is ColorPixelData24)
                {
                    // note that this one will take some work
                    // 0 == red, 1 == Green, 2 == Blue and so on
                    data.AddRange(((ColorPixelData24)pixelData).Data);
                }
                else if (pixelData is SingleBitPixelData)
                {
                    data.AddRange(((SingleBitPixelData)pixelData).Data);
                }

                int tempDicomRows = -1;
                // get the amount of rows the image data set has out of the dicom file.
                if (!dicoms[index].Dataset.TryGetSingleValue(DicomTag.Rows, out tempDicomRows))
                {
                    if (!dicoms[index].Dataset.TryGetSingleValue(DicomTag.NumberOfVerticalPixels, out tempDicomRows))
                    {
                        if (dicomFile.height != tempDicomRows)
                        {
                            throw new NonConsistantDicomDirectoryException("The number of rows at index" + index);
                        }
                    }
                }
                else if (dicomFile.width != tempDicomRows)
                {
                    throw new NonConsistantDicomDirectoryException("The number of rows at index" + index);
                }

                // get the amount of coloumns in the image and ensure it stays the same
                int tempDicomCols = -1;
                if (!dicoms[index].Dataset.TryGetSingleValue(DicomTag.Columns, out tempDicomCols))
                {
                    // try getting this data  another way then
                    if (dicoms[index].Dataset.TryGetSingleValue(DicomTag.NumberOfHorizontalPixels, out tempDicomCols))
                    {
                        if (dicomFile.height != tempDicomCols)
                        {
                            throw new NonConsistantDicomDirectoryException("The number of columns at index" + index);
                        }
                    }
                }
                else if (dicomFile.height != tempDicomCols)
                {
                    throw new NonConsistantDicomDirectoryException("The number of columns at index" + index);
                }


                // TODO Maybe change how this works going forward
                aspectOfPixel = new float[2];
                // get the amount of coloumns in the image
                if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.PixelSpacing, out aspectOfPixel))
                {
                    // all these methods will try to do it until one works or this feild will be made invalid
                    if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.ImagerPixelSpacing, out aspectOfPixel))
                    {
                        if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.NominalScannedPixelSpacing, out aspectOfPixel))
                        {
                            if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.CompensatorPixelSpacing, out aspectOfPixel))
                            {
                                if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.DetectorElementSpacing, out aspectOfPixel))
                                {
                                    if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.PresentationPixelSpacing, out aspectOfPixel))
                                    {
                                        if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.PrinterPixelSpacing, out aspectOfPixel))
                                        {
                                            if (!dicoms[index].Dataset.TryGetValues <float>(DicomTag.ObjectPixelSpacingInCenterOfBeam, out aspectOfPixel))
                                            {
                                                // if none of the above methods work mark the objects as invalid
                                                aspectOfPixel[0] = -1;
                                                aspectOfPixel[1] = -1;
                                                // This could be valid in a case were only one row of data was colected
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // check input data against the old data and throw an exception if the file isn't exact
                if (dicomFile.pixelSpacingX != aspectOfPixel[0] || dicomFile.pixelSpacingY != aspectOfPixel[1])
                {
                    throw new NonConsistantDicomDirectoryException("Pixel Spacing Dicom Tag at index " + index);
                }

                // check the slice thickness is constisant
                if (dicoms[index].Dataset.TryGetSingleValue(DicomTag.SliceThickness, out float tempSliceThickness))
                {
                    if (dicomFile.sliceThickness == -1)
                    {
                        dicomFile.sliceThickness = tempSliceThickness;
                    }
                    else if (tempSliceThickness != dicomFile.sliceThickness)
                    {
                        throw new NonConsistantDicomDirectoryException("Slice Thickness at index" + index);
                    }
                }

                // try to get how much room is between each slice
                if (!dicoms[index].Dataset.TryGetSingleValue(DicomTag.SpacingBetweenSlices, out float tempSliceSpacing))
                {
                    if (dicomFile.spacingBetweenSlices == -1)
                    {
                        dicomFile.spacingBetweenSlices = tempSliceSpacing;
                    }
                    else if (tempSliceThickness != dicomFile.sliceThickness)
                    {
                        throw new NonConsistantDicomDirectoryException("Spacing between slices at index" + index);
                    }
                }
            }

            dicomFile.SetPixelBuffer(data);

            // return the json strings here
            return(Newtonsoft.Json.JsonConvert.SerializeObject(dicomFile));
        }
Example #29
0

        
Example #30
0
 /// <summary>
 /// Initialize new instance of <seealso cref="ImageGraphic"/>
 /// </summary>
 /// <param name="pixelData">Pixel data</param>
 public ImageGraphic(IPixelData pixelData) : this()
 {
     _originalData = pixelData;
     Scale(1.0);
 }
Example #31
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;

                // temporary fix for JPEG 2000 Lossy images
                if (pixelData.PhotometricInterpretation == PhotometricInterpretation.YbrIct || pixelData.PhotometricInterpretation == PhotometricInterpretation.YbrRct)
                    pixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;

                _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;

            CreatePipeline();
        }
Example #32
0
 /// <summary>
 /// Initialize new instance of <seealso cref="ImageGraphic"/>
 /// </summary>
 /// <param name="pixelData">Pixel data</param>
 public ImageGraphic(IPixelData pixelData)
     : this()
 {
     _originalData = pixelData;
     Scale(1.0);
 }
Example #33
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon) return;

            _scaleFactor = scale;
            _scaledData = null;

            foreach (var overlay in _overlays)
            {
                overlay.Scale(scale);
            }
        }
Example #34
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon)
                return;

            _scaleFactor = scale;
            if (_bitmap != null) {
                _scaledData = null;
                _pixels.Dispose();
                _pixels = null;
                _bitmap = null;
            }
        }