Skip to content

Commit

Permalink
Merge pull request #73 from anders9ustafsson/GH-72
Browse files Browse the repository at this point in the history
Replace ByteBufferEnumerator calls with ByteConverter. Connected to #72
  • Loading branch information
anders9ustafsson committed Aug 13, 2015
2 parents 47d337f + ef6a2fc commit 3a32d97
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 35 deletions.
1 change: 1 addition & 0 deletions DICOM [Unit Tests]/DICOM [Unit Tests].csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="DicomElementTest.cs" />
<Compile Include="DicomEncodingTest.cs" />
<Compile Include="DicomFileTest.cs" />
<Compile Include="DicomOtherByteTest.cs" />
<Compile Include="DicomPersonNameTest.cs" />
<Compile Include="DicomTagTest.cs" />
<Compile Include="DicomUIDTest.cs" />
Expand Down
47 changes: 27 additions & 20 deletions DICOM [Unit Tests]/DicomDatasetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,78 @@ public class DicomDatasetTest
[Fact]
public void Add_OtherDoubleElement_Succeeds()
{
var tag = DicomTag.DoubleFloatPixelData;
var dataset = new DicomDataset();
dataset.Add(DicomTag.DoubleFloatPixelData, 3.45);
Assert.IsType(typeof(DicomOtherDouble), dataset.First());
dataset.Add(tag, 3.45);
Assert.IsType<DicomOtherDouble>(dataset.First(item => item.Tag.Equals(tag)));
}

[Fact]
public void Add_OtherDoubleElementWithMultipleDoubles_Succeeds()
{
var tag = DicomTag.DoubleFloatPixelData;
var dataset = new DicomDataset();
dataset.Add(DicomTag.DoubleFloatPixelData, 3.45, 6.78, 9.01);
Assert.IsType(typeof(DicomOtherDouble), dataset.First());
Assert.Equal(3, dataset.Get<double[]>(DicomTag.DoubleFloatPixelData).Length);
dataset.Add(tag, 3.45, 6.78, 9.01);
Assert.IsType<DicomOtherDouble>(dataset.First(item => item.Tag.Equals(tag)));
Assert.Equal(3, dataset.Get<double[]>(tag).Length);
}

[Fact]
public void Add_UnlimitedCharactersElement_Succeeds()
{
var tag = DicomTag.LongCodeValue;
var dataset = new DicomDataset();
dataset.Add(DicomTag.LongCodeValue, "abc");
Assert.IsType(typeof(DicomUnlimitedCharacters), dataset.First());
Assert.Equal("abc", dataset.Get<string>(DicomTag.LongCodeValue));
dataset.Add(tag, "abc");
Assert.IsType<DicomUnlimitedCharacters>(dataset.First(item => item.Tag.Equals(tag)));
Assert.Equal("abc", dataset.Get<string>(tag));
}

[Fact]
public void Add_UnlimitedCharactersElementWithMultipleStrings_Succeeds()
{
var tag = DicomTag.LongCodeValue;
var dataset = new DicomDataset();
dataset.Add(DicomTag.LongCodeValue, "a", "b", "c");
Assert.IsType(typeof(DicomUnlimitedCharacters), dataset.First());
Assert.Equal("c", dataset.Get<string>(DicomTag.LongCodeValue, 2));
dataset.Add(tag, "a", "b", "c");
Assert.IsType<DicomUnlimitedCharacters>(dataset.First(item => item.Tag.Equals(tag)));
Assert.Equal("c", dataset.Get<string>(tag, 2));
}

[Fact]
public void Add_UniversalResourceElement_Succeeds()
{
var tag = DicomTag.URNCodeValue;
var dataset = new DicomDataset();
dataset.Add(DicomTag.URNCodeValue, "abc");
Assert.IsType(typeof(DicomUniversalResource), dataset.First());
Assert.Equal("abc", dataset.Get<string>(DicomTag.URNCodeValue));
dataset.Add(tag, "abc");
Assert.IsType<DicomUniversalResource>(dataset.First(item => item.Tag.Equals(tag)));
Assert.Equal("abc", dataset.Get<string>(tag));
}

[Fact]
public void Add_UniversalResourceElementWithMultipleStrings_OnlyFirstValueIsUsed()
{
var tag = DicomTag.URNCodeValue;
var dataset = new DicomDataset();
dataset.Add(DicomTag.URNCodeValue, "a", "b", "c");
Assert.IsType(typeof(DicomUniversalResource), dataset.First());
dataset.Add(tag, "a", "b", "c");
Assert.IsType<DicomUniversalResource>(dataset.First(item => item.Tag.Equals(tag)));

var data = dataset.Get<string[]>(DicomTag.URNCodeValue);
var data = dataset.Get<string[]>(tag);
Assert.Equal(1, data.Length);
Assert.Equal("a", data.First());
}

[Fact]
public void Add_PersonName_MultipleNames_YieldsMultipleValues()
{
var tag = DicomTag.PerformingPhysicianName;
var dataset = new DicomDataset();
dataset.Add(
DicomTag.PerformingPhysicianName,
tag,
"Gustafsson^Anders^L",
"Yates^Ian",
"Desouky^Hesham",
"Horn^Chris");

var data = dataset.Get<string[]>(DicomTag.PerformingPhysicianName);
var data = dataset.Get<string[]>(tag);
Assert.Equal(4, data.Length);
Assert.Equal("Desouky^Hesham", data[2]);
}
Expand All @@ -92,7 +99,7 @@ public void Add_MultiVMStringTags_YieldsMultipleValues(DicomTag tag, string[] va
var dataset = new DicomDataset();
dataset.Add(tag, values);

Assert.IsType(expectedType, dataset.First());
Assert.IsType(expectedType, dataset.First(item => item.Tag.Equals(tag)));

var data = dataset.Get<string[]>(tag);
Assert.Equal(values.Length, data.Length);
Expand Down
105 changes: 105 additions & 0 deletions DICOM [Unit Tests]/DicomOtherByteTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom
{
using System;
using System.Linq;

using Xunit;

public class DicomOtherByteTest
{
#region Unit tests

[Fact]
public void Get_Short_ReturnsCorrectValue()
{
Get_SingleItem_ReturnsCorrectValue(63, (short)0x7f7e);
}

[Fact]
public void Get_ShortArray_ReturnsCorrectValue()
{
Get_Array_ReturnsCorrectValue(31, (short)0x3f3e, 128);
}

[Fact]
public void Get_UShort_ReturnsCorrectValue()
{
Get_SingleItem_ReturnsCorrectValue(63, (ushort)0x7f7e);
}

[Fact]
public void Get_UShortArray_ReturnsCorrectValue()
{
Get_Array_ReturnsCorrectValue(31, (ushort)0x3f3e, 128);
}

[Fact]
public void Get_Byte_ReturnsCorrectValue()
{
Get_SingleItem_ReturnsCorrectValue(63, (byte)0x3f);
}

[Fact]
public void Get_ByteArray_ReturnsCorrectValue()
{
Get_Array_ReturnsCorrectValue(31, (byte)0x1f, 256);
}

[Fact]
public void Get_UInt_ReturnsCorrectValue()
{
Get_SingleItem_ReturnsCorrectValue(63, (uint)0xfffefdfc);
}

[Fact]
public void Get_UIntArray_ReturnsCorrectValue()
{
Get_Array_ReturnsCorrectValue(31, (uint)0x7f7e7d7c, 64);
}

[Fact]
public void Get_Double_ReturnsCorrectValue()
{
var doubles = new double[1];
Buffer.BlockCopy(new byte[] { 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f }, 0, doubles, 0, 8);
Get_SingleItem_ReturnsCorrectValue(7, doubles[0]);
}

[Fact]
public void Get_DoubleArray_ReturnsCorrectValue()
{
var doubles = new double[1];
Buffer.BlockCopy(new byte[] { 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f }, 0, doubles, 0, 8);
Get_Array_ReturnsCorrectValue(15, doubles[0], 32);
}

#endregion

#region Helper methods

public static void Get_SingleItem_ReturnsCorrectValue<T>(int index, T expected)
{
var element = new DicomOtherByte(
DicomTag.PixelData,
Enumerable.Range(0, 256).Select(i => (byte)i).ToArray());
var actual = element.Get<T>(index);
Assert.Equal(expected, actual);
}

public static void Get_Array_ReturnsCorrectValue<T>(int index, T expected, int expectedLength)
{
var element = new DicomOtherByte(
DicomTag.PixelData,
Enumerable.Range(0, 256).Select(i => (byte)i).ToArray());
var actual = element.Get<T[]>();

Assert.Equal(expectedLength, actual.Length);
Assert.Equal(expected, actual[index]);
}

#endregion
}
}
26 changes: 13 additions & 13 deletions DICOM/DicomElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public IEnumerable<DicomTag> Values
if (_values == null)
{
var values = new List<DicomTag>();
var parts = ByteBufferEnumerator<ushort>.Create(Buffer).ToArray();
var parts = ByteConverter.ToArray<ushort>(Buffer);
for (int i = 0; i < parts.Length; i += 2)
{
var group = parts[i + 0];
Expand Down Expand Up @@ -965,29 +965,29 @@ 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)ByteConverter.Get<short>(Buffer, item);

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

if (typeof(T) == typeof(ushort)) return (T)(object)ByteBufferEnumerator<ushort>.Create(Buffer).ToArray().GetValue(item);
if (typeof(T) == typeof(ushort)) return (T)(object)ByteConverter.Get<ushort>(Buffer, item);

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

if (typeof(T) == typeof(int)) return (T)(object)ByteBufferEnumerator<int>.Create(Buffer).ToArray().GetValue(item);
if (typeof(T) == typeof(int)) return (T)(object)ByteConverter.Get<int>(Buffer, item);

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

if (typeof(T) == typeof(uint)) return (T)(object)ByteBufferEnumerator<uint>.Create(Buffer).ToArray().GetValue(item);
if (typeof(T) == typeof(uint)) return (T)(object)ByteConverter.Get<uint>(Buffer, item);

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

if (typeof(T) == typeof(float)) return (T)(object)ByteBufferEnumerator<float>.Create(Buffer).ToArray().GetValue(item);
if (typeof(T) == typeof(float)) return (T)(object)ByteConverter.Get<float>(Buffer, item);

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

if (typeof(T) == typeof(double)) return (T)(object)ByteBufferEnumerator<double>.Create(Buffer).ToArray().GetValue(item);
if (typeof(T) == typeof(double)) return (T)(object)ByteConverter.Get<double>(Buffer, item);

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

return base.Get<T>(item);
}
Expand Down
4 changes: 2 additions & 2 deletions DICOM/Imaging/DicomOverlayData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private IByteBuffer Load()

if (pixels.BitsAllocated == 8)
{
var data = ByteBufferEnumerator<byte>.Create(frame).ToArray();
var data = IO.ByteConverter.ToArray<byte>(frame);

for (int y = oy; y < oh; y++)
{
Expand All @@ -416,7 +416,7 @@ private IByteBuffer Load()
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();
var data = IO.ByteConverter.ToArray<ushort>(frame);

for (int y = oy; y < oh; y++)
{
Expand Down

0 comments on commit 3a32d97

Please sign in to comment.