Skip to content

Commit

Permalink
Replaced use of FileStream with IFileReference. Issue fo-dicom#69
Browse files Browse the repository at this point in the history
  • Loading branch information
anders9ustafsson committed Sep 1, 2015
1 parent 0922954 commit f0319a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions DICOM [Unit Tests]/DICOM [Unit Tests].csproj
Expand Up @@ -68,6 +68,7 @@
</Compile>
<Compile Include="DicomDatasetTest.cs" />
<Compile Include="DicomDictionaryTest.cs" />
<Compile Include="Imaging\ColorTableTest.cs" />
<Compile Include="IO\DesktopPathTest.cs" />
<Compile Include="IO\TemporaryFileTest.cs" />
<Compile Include="Media\DicomDirectoryTest.cs" />
Expand Down
22 changes: 22 additions & 0 deletions DICOM [Unit Tests]/Imaging/ColorTableTest.cs
@@ -0,0 +1,22 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom.Imaging
{
using System.IO;

using Xunit;

public class ColorTableTest
{
[Fact]
public void SaveLut_ValidTable_Succeeds()
{
var path = @".\Test Data\monochrome1.lut";
if (File.Exists(path)) File.Delete(path);

ColorTable.SaveLUT(path, ColorTable.Monochrome1);
Assert.True(File.Exists(path));
}
}
}
19 changes: 13 additions & 6 deletions DICOM/Imaging/ColorTable.cs
Expand Up @@ -6,6 +6,8 @@

namespace Dicom.Imaging
{
using Dicom.IO;

public static class ColorTable
{
public static readonly Color32[] Monochrome1 = InitGrayscaleLUT(true);
Expand Down Expand Up @@ -62,14 +64,19 @@ public static Color32[] LoadLUT(string file)
}
}

public static void SaveLUT(string file, Color32[] lut)
public static void SaveLUT(string path, Color32[] lut)
{
if (lut.Length != 256) return;
FileStream fs = new FileStream(file, FileMode.Create);
for (int i = 0; i < 256; i++) fs.WriteByte(lut[i].R);
for (int i = 0; i < 256; i++) fs.WriteByte(lut[i].G);
for (int i = 0; i < 256; i++) fs.WriteByte(lut[i].B);
fs.Close();

var file = IOManager.CreateFileReference(path);
file.Delete();

using (var fs = file.OpenWrite())
{
for (var i = 0; i < 256; i++) fs.WriteByte(lut[i].R);
for (var i = 0; i < 256; i++) fs.WriteByte(lut[i].G);
for (var i = 0; i < 256; i++) fs.WriteByte(lut[i].B);
}
}
}
}
Expand Down

0 comments on commit f0319a8

Please sign in to comment.