Exemple #1
0
        public GrayscalePixelDataS16(int width, int height, BitDepth bitDepth, IByteBuffer data)
        {
            _bits   = bitDepth;
            _width  = width;
            _height = height;
            _data   = ByteBufferEnumerator <short> .Create(data).ToArray();

            if (bitDepth.BitsStored != 16)
            {
                int sign = 1 << bitDepth.HighBit;
                int mask = (UInt16.MaxValue >> (bitDepth.BitsAllocated - bitDepth.BitsStored));

                Parallel.For(0, _data.Length, (int i) => {
                    short d = _data[i];
                    if ((d & sign) != 0)
                    {
                        _data[i] = (short)-(((-d) & mask) + 1);
                    }
                    else
                    {
                        _data[i] = (short)(d & mask);
                    }
                });
            }
        }
        internal void Add(IByteBuffer fragment)
        {
            if (_offsetTable == null)
            {
                var en = ByteBufferEnumerator <uint> .Create(fragment);

                _offsetTable = new List <uint>(en);
                return;
            }
            _fragments.Add(fragment);
        }
Exemple #3
0
        public GrayscalePixelDataU16(int width, int height, BitDepth bitDepth, IByteBuffer data)
        {
            _width  = width;
            _height = height;
            _data   = ByteBufferEnumerator <ushort> .Create(data).ToArray();

            if (bitDepth.BitsStored != 16)
            {
                int mask = (1 << (bitDepth.HighBit + 1)) - 1;

                Parallel.For(0, _data.Length, (int i) => {
                    _data[i] = (ushort)(_data[i] & mask);
                });
            }
        }
Exemple #4
0
        public override T Get <T>(int item = -1)
        {
            if (!typeof(T).IsArray && item == -1)
            {
                item = 0;
            }

            if (typeof(T) == typeof(short))
            {
                return((T)(object)ByteBufferEnumerator <short> .Create(Buffer).ToArray().GetValue(item));
            }

            if (typeof(T) == typeof(short[]))
            {
                return((T)(object)ByteBufferEnumerator <short> .Create(Buffer).ToArray());
            }

            if (typeof(T) == typeof(ushort))
            {
                return((T)(object)ByteBufferEnumerator <ushort> .Create(Buffer).ToArray().GetValue(item));
            }

            if (typeof(T) == typeof(ushort[]))
            {
                return((T)(object)ByteBufferEnumerator <ushort> .Create(Buffer).ToArray());
            }

            if (typeof(T) == typeof(int))
            {
                return((T)(object)ByteBufferEnumerator <int> .Create(Buffer).ToArray().GetValue(item));
            }

            if (typeof(T) == typeof(int[]))
            {
                return((T)(object)ByteBufferEnumerator <int> .Create(Buffer).ToArray());
            }

            if (typeof(T) == typeof(uint))
            {
                return((T)(object)ByteBufferEnumerator <uint> .Create(Buffer).ToArray().GetValue(item));
            }

            if (typeof(T) == typeof(uint[]))
            {
                return((T)(object)ByteBufferEnumerator <uint> .Create(Buffer).ToArray());
            }

            if (typeof(T) == typeof(float))
            {
                return((T)(object)ByteBufferEnumerator <float> .Create(Buffer).ToArray().GetValue(item));
            }

            if (typeof(T) == typeof(float[]))
            {
                return((T)(object)ByteBufferEnumerator <float> .Create(Buffer).ToArray());
            }

            if (typeof(T) == typeof(double))
            {
                return((T)(object)ByteBufferEnumerator <double> .Create(Buffer).ToArray().GetValue(item));
            }

            if (typeof(T) == typeof(double[]))
            {
                return((T)(object)ByteBufferEnumerator <double> .Create(Buffer).ToArray());
            }

            return(base.Get <T>(item));
        }
Exemple #5
0
        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
        }
Exemple #6
0
        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));
            }
        }