Skip to content

Commit

Permalink
ensure frame buffers created by codecs have even length
Browse files Browse the repository at this point in the history
  • Loading branch information
rcd committed Jan 11, 2013
1 parent 2c1e742 commit 116f930
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog.md
@@ -1,5 +1,6 @@
#### v1.0.31
* Improved portability and fewer compiler warnings (Anders Gustafsson)
* Ensure frame buffers created by codecs have even length
* Import DicomFileScanner from mDCM
* Miscellaneous improvements and fixes

Expand Down
2 changes: 2 additions & 0 deletions DICOM [Native]/Dicom.Imaging.Codec.Jpeg.i
Expand Up @@ -335,6 +335,7 @@ void JPEGCODEC::Encode(DicomPixelData^ oldPixelData, DicomPixelData^ newPixelDat
buffer = gcnew TempFileBuffer(MemoryBuffer->ToArray());
else
buffer = gcnew MemoryByteBuffer(MemoryBuffer->ToArray());
buffer = EvenLengthBuffer::Create(buffer);
newPixelData->AddFrame(buffer);
} finally {
MemoryBuffer = nullptr;
Expand Down Expand Up @@ -489,6 +490,7 @@ void JPEGCODEC::Decode(DicomPixelData^ oldPixelData, DicomPixelData^ newPixelDat
buffer = gcnew TempFileBuffer(frameArray->Data);
else
buffer = gcnew MemoryByteBuffer(frameArray->Data);
buffer = EvenLengthBuffer::Create(buffer);

if (newPixelData->PlanarConfiguration == PlanarConfiguration::Planar && newPixelData->SamplesPerPixel > 1) {
if (oldPixelData->SamplesPerPixel != 3 || oldPixelData->BitsStored > 8)
Expand Down
2 changes: 2 additions & 0 deletions DICOM [Native]/Dicom.Imaging.Codec.Jpeg2000.cpp
Expand Up @@ -210,6 +210,7 @@ void DicomJpeg2000NativeCodec::Encode(DicomPixelData^ oldPixelData, DicomPixelDa
buffer = gcnew TempFileBuffer(cbuf);
else
buffer = gcnew MemoryByteBuffer(cbuf);
buffer = EvenLengthBuffer::Create(buffer);
newPixelData->AddFrame(buffer);
} else
throw gcnew DicomCodecException("Unable to JPEG 2000 encode image");
Expand Down Expand Up @@ -351,6 +352,7 @@ void DicomJpeg2000NativeCodec::Decode(DicomPixelData^ oldPixelData, DicomPixelDa
buffer = gcnew TempFileBuffer(destArray->Data);
else
buffer = gcnew MemoryByteBuffer(destArray->Data);
buffer = EvenLengthBuffer::Create(buffer);
newPixelData->AddFrame(buffer);
}
finally {
Expand Down
2 changes: 2 additions & 0 deletions DICOM [Native]/Dicom.Imaging.Codec.JpegLS.cpp
Expand Up @@ -100,6 +100,7 @@ void DicomJpegLsNativeCodec::Encode(DicomPixelData^ oldPixelData, DicomPixelData
buffer = gcnew TempFileBuffer(jpegData);
else
buffer = gcnew MemoryByteBuffer(jpegData);
buffer = EvenLengthBuffer::Create(buffer);
newPixelData->AddFrame(buffer);
}
}
Expand All @@ -122,6 +123,7 @@ void DicomJpegLsNativeCodec::Decode(DicomPixelData^ oldPixelData, DicomPixelData
buffer = gcnew TempFileBuffer(frameData);
else
buffer = gcnew MemoryByteBuffer(frameData);
buffer = EvenLengthBuffer::Create(buffer);
newPixelData->AddFrame(buffer);
}
}
Expand Down
2 changes: 2 additions & 0 deletions DICOM [Native]/Dicom.Imaging.Codec.Rle.cpp
Expand Up @@ -213,6 +213,7 @@ void DicomRleNativeCodec::Encode(DicomPixelData^ oldPixelData, DicomPixelData^ n
buffer = gcnew TempFileBuffer(data);
else
buffer = gcnew MemoryByteBuffer(data);
buffer = EvenLengthBuffer::Create(buffer);
newPixelData->AddFrame(buffer);
}
}
Expand Down Expand Up @@ -363,6 +364,7 @@ void DicomRleNativeCodec::Decode(DicomPixelData^ oldPixelData, DicomPixelData^ n
buffer = gcnew TempFileBuffer(frameData);
else
buffer = gcnew MemoryByteBuffer(frameData);
buffer = EvenLengthBuffer::Create(buffer);
newPixelData->AddFrame(buffer);
}
}
Expand Down
4 changes: 3 additions & 1 deletion DICOM/DICOM.csproj
Expand Up @@ -67,9 +67,11 @@
<Compile Include="DicomDictionary.cs" />
<Compile Include="DicomDictionaryEntry.cs" />
<Compile Include="DicomDictionaryReader.cs" />
<Compile Include="DicomFileScanner.cs" />
<Compile Include="DicomMatchRules.cs" />
<Compile Include="DicomTransformRules.cs" />
<Compile Include="DicomUIDGenerator.cs" />
<Compile Include="IO\Buffer\EvenLengthBuffer.cs" />
<Compile Include="Media\DicomDirectory.cs" />
<Compile Include="Media\DicomDirectoryRecord.cs" />
<Compile Include="Media\DicomDirectoryRecordCollection.cs" />
Expand Down Expand Up @@ -264,7 +266,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_BuildVersioningStyle="None.None.Increment.YearDayOfYear" BuildVersion_UseGlobalSettings="True" />
<UserProperties BuildVersion_UseGlobalSettings="True" BuildVersion_BuildVersioningStyle="None.None.Increment.YearDayOfYear" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
45 changes: 45 additions & 0 deletions DICOM/IO/Buffer/EvenLengthBuffer.cs
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dicom.IO.Buffer {
public class EvenLengthBuffer : IByteBuffer {
private EvenLengthBuffer(IByteBuffer buffer) {
Buffer = buffer;
}

public IByteBuffer Buffer {
get;
private set;
}

public bool IsMemory {
get { return true; }
}

public uint Size {
get { return Buffer.Size + 1; }
}

public byte[] Data {
get {
byte[] data = new byte[Size];
System.Buffer.BlockCopy(Buffer.Data, 0, data, 0, (int)Buffer.Size);
return data;
}
}

public byte[] GetByteRange(int offset, int count) {
byte[] data = new byte[count];
System.Buffer.BlockCopy(Buffer.Data, offset, data, 0, Math.Min((int)Buffer.Size, count));
return data;
}

public static IByteBuffer Create(IByteBuffer buffer) {
if ((buffer.Size & 1) == 1)
return new EvenLengthBuffer(buffer);
return buffer;
}
}
}

0 comments on commit 116f930

Please sign in to comment.