Exemplo n.º 1
0
        public void AsciiUsedIfEncodingIsNotInCharacterSet()
        {
            var ds = new DicomDataset
            {
                new DicomCodeString(DicomTag.SpecificCharacterSet, @"\ISO 2022 IR 100")
            };
            var logCollector = NewLogCollector();

            // Buc^Jérôme encoded, but with the escape sequence  for the second component
            // pointing to shift_jis instead of Latin-1
            var name = new byte[]
            { 0x1b, 0x2d, 0x41, 0x42, 0x75, 0x63, 0x5e, 0x1b, 0x28, 0x4a, 0x4a, 0xe9, 0x72, 0xf4, 0x6d, 0x65 };
            IByteBuffer buffer      = new MemoryByteBuffer(name);
            var         patientName = new DicomPersonName(DicomTag.PatientName, ds.GetEncodingsForSerialization(), buffer);

            ds.Add(patientName);
            Assert.Equal("Buc^J?r?me", ds.GetString(DicomTag.PatientName));
            Assert.Equal(2, logCollector.NumberOfWarnings);
            var expectedMessage =
                "Found escape sequence for 'shift_jis', which is not defined";

            Assert.StartsWith(expectedMessage, logCollector.WarningAt(0));
            expectedMessage =
                "Could not decode string 'J?r?me' with given encoding, using replacement characters.";
            Assert.Equal(expectedMessage, logCollector.WarningAt(1));
        }
Exemplo n.º 2
0
        public void AsciiUsedForUnknownEscapeSequence()
        {
            var ds = new DicomDataset
            {
                new DicomCodeString(DicomTag.SpecificCharacterSet, @"\ISO 2022 IR 100")
            };
            var logCollector = NewLogCollector();

            // Buc^Jérôme encoded, but with an invalid escape sequence instead of Latin-1 for the second component
            var name = new byte[]
            { 0x1b, 0x2d, 0x41, 0x42, 0x75, 0x63, 0x5e, 0x1b, 0x28, 0x4b, 0x4a, 0xe9, 0x72, 0xf4, 0x6d, 0x65 };
            IByteBuffer buffer      = new MemoryByteBuffer(name);
            var         patientName = new DicomPersonName(DicomTag.PatientName, ds.GetEncodingsForSerialization(), buffer);

            ds.Add(patientName);
            Assert.Equal("Buc^J?r?me", ds.GetString(DicomTag.PatientName));
            Assert.Equal(2, logCollector.NumberOfWarnings);
            var expectedMessage =
                "Unknown escape sequence found in string, using ASCII encoding.";

            Assert.Equal(expectedMessage, logCollector.WarningAt(0));
            expectedMessage =
                "Could not decode string 'J?r?me' with given encoding, using replacement characters.";
            Assert.Equal(expectedMessage, logCollector.WarningAt(1));
        }
Exemplo n.º 3
0
        public void OverwriteRegisteredEncoding()
        {
            var ds = new DicomDataset
            {
                new DicomCodeString(DicomTag.SpecificCharacterSet, "ISO IR 144"),
            };

            try
            {
                // patch the encoding - without this, the name would display as gibberish
                DicomEncoding.RegisterEncoding("ISO IR 144", "koi8-r");

                // Грозный^Иван encoded in KOI-8 instead of iso-8859-5
                var         koi8Name    = new byte[] { 0xe7, 0xd2, 0xcf, 0xda, 0xce, 0xd9, 0xca, 0x5e, 0xe9, 0xd7, 0xc1, 0xce };
                IByteBuffer buffer      = new MemoryByteBuffer(koi8Name);
                var         patientName = new DicomPersonName(DicomTag.PatientName, ds.GetEncodingsForSerialization(), buffer);
                ds.Add(patientName); // patient name would show gibberish
                Assert.Equal("Грозный^Иван", ds.GetString(DicomTag.PatientName));
            }
            finally
            {
                // set back the correct encoding to avoid breaking other tests
                DicomEncoding.RegisterEncoding("ISO IR 144", "iso-8859-5");
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Populate the <paramref name="items"/> queue.
 /// </summary>
 /// <param name="dataset">Source of population.</param>
 /// <param name="items">Destination of population.</param>
 private static void BuildWalkQueue(DicomDataset dataset, Queue <DicomItem> items)
 {
     foreach (var item in dataset)
     {
         if (item is DicomElement)
         {
             items.Enqueue(item);
         }
         else if (item is DicomFragmentSequence)
         {
             var sq = item as DicomFragmentSequence;
             items.Enqueue(item);
             foreach (var fragment in sq)
             {
                 items.Enqueue(new DicomFragmentItem(fragment));
             }
             items.Enqueue(new EndDicomFragment());
         }
         else if (item is DicomSequence)
         {
             var sq = item as DicomSequence;
             items.Enqueue(item);
             foreach (var sqi in sq)
             {
                 sqi.SetFallbackEncodings(dataset.GetEncodingsForSerialization());
                 sqi.OnBeforeSerializing();
                 items.Enqueue(new BeginDicomSequenceItem(sqi));
                 BuildWalkQueue(sqi, items);
                 items.Enqueue(new EndDicomSequenceItem());
             }
             items.Enqueue(new EndDicomSequence());
         }
     }
 }
Exemplo n.º 5
0
        public void RegisterNewEncoding()
        {
            DicomEncoding.RegisterEncoding("KOI 8", "koi8-r");
            var ds = new DicomDataset
            {
                new DicomCodeString(DicomTag.SpecificCharacterSet, "KOI 8"),
            };
            // Грозный^Иван encoded in KOI-8
            var         koi8Name    = new byte[] { 0xe7, 0xd2, 0xcf, 0xda, 0xce, 0xd9, 0xca, 0x5e, 0xe9, 0xd7, 0xc1, 0xce };
            IByteBuffer buffer      = new MemoryByteBuffer(koi8Name);
            var         patientName = new DicomPersonName(DicomTag.PatientName, ds.GetEncodingsForSerialization(), buffer);

            ds.Add(patientName);

            Assert.Equal("Грозный^Иван", ds.GetString(DicomTag.PatientName));
        }
Exemplo n.º 6
0
        public void SingleByteCodeExtensions(string encodingName, string expectedName, byte[] rawData)
        {
            var ds = new DicomDataset
            {
                // empty first encoding defaults to ASCII
                new DicomCodeString(DicomTag.SpecificCharacterSet, $"\\{encodingName}"),
            };

            // combine ASCII text with text encoded with another single byte encoding
            // and use the respective code extension
            var         asciiPart = new byte[] { 0x41, 0x53, 0x43, 0x49, 0x49 }; // "ASCII"
            IByteBuffer buffer    = new MemoryByteBuffer(asciiPart.Concat(rawData).ToArray());

            var patientName = new DicomPersonName(DicomTag.PatientName, ds.GetEncodingsForSerialization(), buffer);

            ds.Add(patientName);
            Assert.Equal("ASCII" + expectedName, ds.GetString(DicomTag.PatientName));
        }
Exemplo n.º 7
0
        public void ReplacementCharactersUsedForBadEncoding()
        {
            var ds = new DicomDataset
            {
                new DicomCodeString(DicomTag.SpecificCharacterSet, "ISO IR 192"),
            };
            var logCollector = NewLogCollector();
            // not a valid UTF-8 encoding
            var         badName     = new byte[] { 0xc4, 0xe9, 0xef, 0xed, 0xf5, 0xf3, 0xe9, 0xef, 0xf2 };
            IByteBuffer buffer      = new MemoryByteBuffer(badName);
            var         patientName = new DicomPersonName(DicomTag.PatientName, ds.GetEncodingsForSerialization(), buffer);

            ds.Add(patientName);
            Assert.Equal("���������", ds.GetString(DicomTag.PatientName));
            Assert.Equal(1, logCollector.NumberOfWarnings);
            var expectedMessage =
                "Could not decode string '���������' with given encoding, using replacement characters.";

            Assert.Equal(expectedMessage, logCollector.WarningAt(0));
        }