public void OnElement_ValidData_AddsCorrectTypeToDataset(DicomTag tag, DicomVR vr, string data, Type expected)
        {
            var dataset = new DicomDataset();
            var observer = new DicomDatasetReaderObserver(dataset);
            var buffer = new MemoryByteBuffer(Encoding.ASCII.GetBytes(data));

            observer.OnElement(null, tag, vr, buffer);
            Assert.IsType(expected, dataset.First());
        }
Exemple #2
0
        public void AddOrUpdatePixelData_InternalTransferSyntax_Succeeds()
        {
            var ds        = new DicomDataset( );
            var data      = new IO.Buffer.MemoryByteBuffer(new byte[] { 255 });    //dummy data
            var newSyntax = DicomTransferSyntax.DeflatedExplicitVRLittleEndian;

            ds.AddOrUpdatePixelData(DicomVR.OB, data);

            Assert.Equal(DicomTransferSyntax.ExplicitVRLittleEndian, ds.InternalTransferSyntax);

            ds.AddOrUpdatePixelData(DicomVR.OB, data, newSyntax);

            Assert.Equal(newSyntax, ds.InternalTransferSyntax);
        }
    public void All_VRs_should_have_one_and_only_one_matching_DicomElement_subclasses()
    {
      var codes = new Collection<string>();
      var types =
        Assembly.GetAssembly(typeof(DicomElement))
          .GetTypes()
          .Where(t => (t.IsSubclassOf(typeof(DicomElement)) || t == typeof(DicomSequence)) && !t.IsAbstract);

      var mbb = new MemoryByteBuffer(new byte[0]);

      foreach (var type in types)
      {
        DicomItem item;
        var ctor = type.GetConstructor(new[] { typeof(DicomTag), typeof(IByteBuffer) });
        if (ctor != null)
        {
          item = (DicomItem)ctor.Invoke(new object[] { new DicomTag(0, 0), mbb });
        }
        else
        {
          ctor = type.GetConstructor(new[] { typeof(DicomTag), typeof(Encoding), typeof(IByteBuffer) });
          if (ctor != null)
          {
            item = (DicomItem)ctor.Invoke(new object[] { new DicomTag(0, 0), Encoding.ASCII, mbb });
          }
          else
          {
            ctor = type.GetConstructor(new[] { typeof(DicomTag), typeof(DicomDataset[]) });

            Assert.NotNull(ctor);

            item = (DicomItem)ctor.Invoke(new object[] { new DicomTag(0, 0), new DicomDataset[0] });
          }
        }

        Assert.DoesNotContain(item.ValueRepresentation.Code, codes);
        codes.Add(item.ValueRepresentation.Code);
      }

      foreach (var vr in AllVRs)
      {
        Assert.Contains(vr, codes);
      }
    }
Exemple #4
0
        private void AddColorImage(Bitmap bitmap, int index)
        {
            if (_currentFilmBox == null)
            {
                throw new InvalidOperationException("Start film box first!");
            }

            if (index < 0 || index > _currentFilmBox.BasicImageBoxes.Count)
            {
                throw new ArgumentOutOfRangeException("Image box index out of range");
            }

            if (bitmap.PixelFormat != PixelFormat.Format24bppRgb && bitmap.PixelFormat != PixelFormat.Format32bppArgb &&
                bitmap.PixelFormat != PixelFormat.Format32bppRgb)
            {
                throw new ArgumentException("Not supported bitmap format");
            }

            var dataset = new DicomDataset();

            dataset.Add <ushort>(DicomTag.Columns, (ushort)bitmap.Width)
            .Add <ushort>(DicomTag.Rows, (ushort)bitmap.Height)
            .Add <ushort>(DicomTag.BitsAllocated, 8)
            .Add <ushort>(DicomTag.BitsStored, 8)
            .Add <ushort>(DicomTag.HighBit, 7)
            .Add(DicomTag.PixelRepresentation, (ushort)PixelRepresentation.Unsigned)
            .Add(DicomTag.PlanarConfiguration, (ushort)PlanarConfiguration.Interleaved)
            .Add <ushort>(DicomTag.SamplesPerPixel, 3)
            .Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);

            var pixelData = DicomPixelData.Create(dataset, true);
            var pixels    = GetColorbytes(bitmap);
            var buffer    = new Dicom.IO.Buffer.MemoryByteBuffer(pixels.Data);

            pixelData.AddFrame(buffer);
            var imageBox = _currentFilmBox.BasicImageBoxes[index];

            imageBox.ImageSequence = dataset;

            pixels.Dispose();
        }
 /// <summary>
 /// Gets a byte buffer of specified length from the current position and moves to subsequent position.
 /// </summary>
 /// <param name="count">Number of bytes to read.</param>
 /// <returns>Byte buffer containing the read bytes.</returns>
 public IByteBuffer GetBuffer(uint count)
 {
     IByteBuffer buffer = null;
     if (count == 0) buffer = EmptyBuffer.Value;
     else if (count >= this.LargeObjectSize)
     {
         buffer = new StreamByteBuffer(_stream, _stream.Position, count);
         _stream.Seek((int)count, SeekOrigin.Current);
     }
     else buffer = new MemoryByteBuffer(GetBytes((int)count));
     return buffer;
 }
Exemple #6
0
        private void AddGreyscaleImage(Bitmap bitmap, int index)
        {
            if (_currentFilmBox == null)
                throw new InvalidOperationException("Start film box first!");

            if (index < 0 || index > _currentFilmBox.BasicImageBoxes.Count)
                // ReSharper disable once NotResolvedInText
                throw new ArgumentOutOfRangeException("Image box index out of range");

            if (bitmap.PixelFormat != PixelFormat.Format24bppRgb &&
                bitmap.PixelFormat != PixelFormat.Format32bppArgb &&
                bitmap.PixelFormat != PixelFormat.Format32bppRgb
                )
                throw new ArgumentException("Not supported bitmap format");

            var dataset = new DicomDataset
            {
                {DicomTag.Columns, (ushort) bitmap.Width},
                {DicomTag.Rows, (ushort) bitmap.Height}
            };

            var pixelData = DicomPixelData.Create(dataset, true);
            pixelData.BitsStored = 8;
            pixelData.BitsAllocated = 8;
            pixelData.SamplesPerPixel = 1;
            pixelData.HighBit = 7;
            pixelData.PixelRepresentation = (ushort)PixelRepresentation.Unsigned;
            pixelData.PlanarConfiguration = (ushort)PlanarConfiguration.Interleaved;
            pixelData.PhotometricInterpretation = PhotometricInterpretation.Monochrome1;

            var pixels = GetGreyBytes(bitmap);
            var buffer = new MemoryByteBuffer(pixels.Data);

            pixelData.AddFrame(buffer);

            var imageBox = _currentFilmBox.BasicImageBoxes[index];
            imageBox.ImageSequence = dataset;
            imageBox.Polarity = Polarity;

            pixels.Dispose();
        }
        public override void Decode(
            DicomPixelData oldPixelData,
            DicomPixelData newPixelData,
            DicomCodecParams parameters)
        {
            for (var frame = 0; frame < oldPixelData.NumberOfFrames; frame++)
            {
                var rleData = oldPixelData.GetFrame(frame);

                // Create new frame data of even length
                var frameSize = newPixelData.UncompressedFrameSize;
                if ((frameSize & 1) == 1)
                {
                    ++frameSize;
                }

                var frameData = new MemoryByteBuffer(new byte[frameSize]);

                var pixelCount = oldPixelData.Width * oldPixelData.Height;
                var numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel;

                var decoder = new RLEDecoder(rleData);

                if (decoder.NumberOfSegments != numberOfSegments)
                {
                    throw new InvalidOperationException("Unexpected number of RLE segments!");
                }

                for (var s = 0; s < numberOfSegments; s++)
                {
                    var sample = s / newPixelData.BytesAllocated;
                    var sabyte = s % newPixelData.BytesAllocated;

                    int pos, offset;

                    if (newPixelData.PlanarConfiguration == PlanarConfiguration.Interleaved)
                    {
                        pos = sample * newPixelData.BytesAllocated;
                        offset = newPixelData.SamplesPerPixel * newPixelData.BytesAllocated;
                    }
                    else
                    {
                        pos = sample * newPixelData.BytesAllocated * pixelCount;
                        offset = newPixelData.BytesAllocated;
                    }

                    pos += newPixelData.BytesAllocated - sabyte - 1;
                    decoder.DecodeSegment(s, frameData, pos, offset);
                }

                newPixelData.AddFrame(frameData);
            }
        }
        private void CreateDicomFile(int[] frameIdArray, string directoryName, string fileName)
        {
            int frameId = frameIdArray[0];
            int fileId = ObjectContext.DicomFrame.Single(x => x.FrameId == frameId).FileId;
            var query1 = from dev in ObjectContext.DicomElementValue.Include("DicomElement")
                         where dev.FileId == fileId && (dev.GroupNumber != 40 && dev.GroupNumber != 32736) // Okrem GroupNumber 0x0028 a 0x7FE0
                         select new { dev.GroupNumber, dev.ElementNumber, dev.Value, dev.DicomElement.ValueRepresentation };

            DicomDataset dataset = new DicomDataset();

            foreach (var item in query1)
            {
                if (item.ValueRepresentation == "PN")
                {
                    var x = new DicomPersonName(new DicomTag((ushort) item.GroupNumber, (ushort) item.ElementNumber),
                                                Encoding.UTF7, item.Value);
                    dataset.Add(x);
                }
                else
                {
                    dataset.Add(new DicomTag((ushort)item.GroupNumber, (ushort)item.ElementNumber), item.Value);
                }
            }

            var query2 = from dev in ObjectContext.DicomElementValue
                         where dev.FileId == fileId && dev.GroupNumber == 40 // GroupNumber 0x0028
                         select new { dev.GroupNumber, dev.ElementNumber, dev.Value };

            DicomPixelData pixelData = DicomPixelData.Create(dataset, true);

            pixelData.BitsAllocated =
                ushort.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 256)).Value);
            pixelData.BitsStored =
                ushort.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 257)).Value);
            pixelData.HighBit =
                ushort.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 258)).Value);
            pixelData.PhotometricInterpretation =
                PhotometricInterpretation.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 4)).Value);
            pixelData.PixelRepresentation =
                (PixelRepresentation)int.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 259)).Value);
            pixelData.PlanarConfiguration =
                (PlanarConfiguration)int.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 6)).Value);
            pixelData.SamplesPerPixel =
                ushort.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 2)).Value);

            ushort width = ushort.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 17)).Value);
            ushort height = ushort.Parse(query2.Single(x => (x.GroupNumber == 40 && x.ElementNumber == 16)).Value);

            var query3 = from dfr in ObjectContext.DicomFrame
                         where frameIdArray.Contains(dfr.FrameId)
                         orderby dfr.FrameId ascending
                         select new { dfr.Frame };

            foreach (var item in query3)
            {
                byte[] pixels = item.Frame;
                MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
                pixelData.Width = width;
                pixelData.Height = height;
                pixelData.AddFrame(buffer);
            }

            string dirPath = HttpRuntime.AppDomainAppPath + @"\temp\" + directoryName;
            string filePath = dirPath + @"\" + fileName + ".dcm";
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);

            var file = new Dicom.DicomFile(dataset);
            file.Save(filePath);
        }