public void SavePixels(string filename) { DicomPixelData pd = DicomPixelData.CreateFrom(_dicomFile); if (File.Exists(filename)) { File.Delete(filename); } using (FileStream fs = new FileStream(filename, FileMode.CreateNew)) { byte[] ba; DicomCompressedPixelData compressed = pd as DicomCompressedPixelData; if (compressed != null) { ba = compressed.GetFrameFragmentData(0); } else { ba = pd.GetFrame(0); } fs.Write(ba, 0, ba.Length); fs.Flush(); fs.Close(); } }
public void PartialFrameTest() { DicomFile file = new DicomFile("RlePartialFrameTest.dcm"); SetupMultiframeXA(file.DataSet, 511, 511, 7); file.ChangeTransferSyntax(TransferSyntax.RleLossless); file.Save(); DicomFile newFile = new DicomFile(file.Filename); newFile.Load(DicomReadOptions.StorePixelDataReferences); DicomPixelData pd; if (!newFile.TransferSyntax.Encapsulated) { pd = new DicomUncompressedPixelData(newFile); } else if (newFile.TransferSyntax.Equals(TransferSyntax.RleLossless)) { pd = new DicomCompressedPixelData(newFile); } else { throw new DicomCodecException("Unsupported transfer syntax: " + newFile.TransferSyntax); } for (int i = 0; i < pd.NumberOfFrames; i++) { pd.GetFrame(i); } }
public void Decode(DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) { DecodeFrame(n, oldPixelData, newPixelData, parameters); } }
public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters ?? new DicomRleCodecParameters(); int pixelCount = oldPixelData.ImageWidth * oldPixelData.ImageHeight; int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel; int segmentLength = (pixelCount & 1) == 1 ? pixelCount + 1 : pixelCount; byte[] segment = new byte[segmentLength]; byte[] frameData = new byte[oldPixelData.UncompressedFrameSize]; IList <DicomFragment> rleData = oldPixelData.GetFrameFragments(frame); RLEDecoder decoder = new RLEDecoder(rleData); if (decoder.NumberOfSegments != numberOfSegments) { throw new DicomCodecException("Unexpected number of RLE segments!"); } for (int s = 0; s < numberOfSegments; s++) { decoder.DecodeSegment(s, segment); int sample = s / oldPixelData.BytesAllocated; int sabyte = s % oldPixelData.BytesAllocated; int pos; int offset; if (newPixelData.PlanarConfiguration == 0) { pos = sample * oldPixelData.BytesAllocated; offset = oldPixelData.SamplesPerPixel * oldPixelData.BytesAllocated; } else { pos = sample * oldPixelData.BytesAllocated * pixelCount; offset = oldPixelData.BytesAllocated; } if (rleParams.ReverseByteOrder) { pos += sabyte; } else { pos += oldPixelData.BytesAllocated - sabyte - 1; } for (int p = 0; p < pixelCount; p++) { frameData[pos] = segment[p]; pos += offset; } } newPixelData.AppendFrame(frameData); }
public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { using (var input = new MemoryStream(oldPixelData.GetFrameFragmentData(frame))) using (var gzipStream = new GZipStream(input, CompressionMode.Decompress, false)) { var data = new byte[oldPixelData.UncompressedFrameSize]; gzipStream.Read(data, 0, data.Length); newPixelData.AppendFrame(data); } }
/// <summary> /// Called by the base class to create a new byte buffer containing normalized pixel data /// for this frame (8 or 16-bit grayscale, or 32-bit ARGB). /// </summary> /// <returns>A new byte buffer containing the normalized pixel data.</returns> protected override byte[] CreateNormalizedPixelData() { DicomMessageBase message = this.Parent.SourceMessage; CodeClock clock = new CodeClock(); clock.Start(); PhotometricInterpretation photometricInterpretation; byte[] rawPixelData = null; if (!message.TransferSyntax.Encapsulated) { DicomUncompressedPixelData pixelData = new DicomUncompressedPixelData(message); // DICOM library uses zero-based frame numbers MemoryManager.Execute(delegate { rawPixelData = pixelData.GetFrame(_frameIndex); }); ExtractOverlayFrames(rawPixelData, pixelData.BitsAllocated); photometricInterpretation = PhotometricInterpretation.FromCodeString(message.DataSet[DicomTags.PhotometricInterpretation]); } else if (DicomCodecRegistry.GetCodec(message.TransferSyntax) != null) { DicomCompressedPixelData pixelData = new DicomCompressedPixelData(message); string pi = null; MemoryManager.Execute(delegate { rawPixelData = pixelData.GetFrame(_frameIndex, out pi); }); photometricInterpretation = PhotometricInterpretation.FromCodeString(pi); } else { throw new DicomCodecException("Unsupported transfer syntax"); } if (photometricInterpretation.IsColor) { rawPixelData = ToArgb(message.DataSet, rawPixelData, photometricInterpretation); } else { NormalizeGrayscalePixels(message.DataSet, rawPixelData); } clock.Stop(); PerformanceReportBroker.PublishReport("DicomMessageSopDataSource", "CreateFrameNormalizedPixelData", clock.Seconds); return(rawPixelData); }
public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { if (oldPixelData.UncompressedFrameSize % 2 == 1) { using (var stream = new MemoryStream(oldPixelData.GetFrameFragmentData(frame))) { var data = new byte[oldPixelData.UncompressedFrameSize]; stream.Read(data, 0, data.Length); newPixelData.AppendFrame(data); } } else { newPixelData.AppendFrame(oldPixelData.GetFrameFragmentData(frame)); } }
public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { if (oldPixelData.UncompressedFrameSize%2 == 1) { using (var stream = new MemoryStream(oldPixelData.GetFrameFragmentData(frame))) { var data = new byte[oldPixelData.UncompressedFrameSize]; stream.Read(data, 0, data.Length); newPixelData.AppendFrame(data); } } else { newPixelData.AppendFrame(oldPixelData.GetFrameFragmentData(frame)); } }
protected override void Upload(DicomFile dicomObject, int frame, IStorageLocation storeLocation) { var frameIndex = frame - 1; if (dicomObject.TransferSyntax == TransferSyntax.JpegBaselineProcess1) { DicomCompressedPixelData pd = DicomPixelData.CreateFrom(dicomObject) as DicomCompressedPixelData; byte[] buffer = pd.GetFrameFragmentData(frameIndex); storeLocation.Upload(buffer); } else if (false) //TODO: handle compressed images properly! { DicomFile dcmJpeg = new DicomFile( ); DicomUncompressedPixelData unCompressed = DicomPixelData.CreateFrom(dicomObject) as DicomUncompressedPixelData; DicomCompressedPixelData compressed = new DicomCompressedPixelData(unCompressed); //compressed.ImageWidth = unCompressed.ImageWidth; //compressed.ImageHeight = unCompressed.HighBit; compressed.BitsStored = 8; compressed.BitsAllocated = 8; //compressed.HighBit = 7; compressed.SamplesPerPixel = 3; //compressed.PlanarConfiguration = 0; compressed.PhotometricInterpretation = "YBR_FULL_422"; compressed.TransferSyntax = TransferSyntax.JpegBaselineProcess1; byte[] imageBuffer = unCompressed.GetFrame(frameIndex); compressed.AddFrameFragment(imageBuffer); compressed.UpdateMessage(dcmJpeg); storeLocation.Upload(compressed.GetFrame(frameIndex)); //ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec codec = new ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec (ClearCanvas.Dicom.Codec.Jpeg.JpegMode.Baseline, 0, 0 ) ; //ClearCanvas.Dicom.Codec.Jpeg.DicomJpegParameters jparam = new ClearCanvas.Dicom.Codec.Jpeg.DicomJpegParameters ( ) ; //jparam. //codec. //codec.Encode ( ) } }
private void SetupEncapsulatedImageWithIconSequence(DicomFile file, bool encapsulateIconPixelData) { var codec = new NullDicomCodec(); DicomAttributeCollection dataSet = file.DataSet; SetupSecondaryCapture(dataSet); SetupMetaInfo(file); file.TransferSyntax = TransferSyntax.ImplicitVrLittleEndian; file.ChangeTransferSyntax(codec.CodecTransferSyntax); // explicitly create the icon sequence as either encapsulated or native, so that we are not depending on correct behaviour elsewhere... CreateIconImageSequence(dataSet); if (encapsulateIconPixelData) { var dataset = ((DicomAttributeSQ)dataSet[DicomTags.IconImageSequence])[0]; var pixelData = dataset[DicomTags.PixelData]; var pd = new DicomUncompressedPixelData(dataset); using (var pixelStream = ((DicomAttributeBinary)pixelData).AsStream()) { //Before compression, make the pixel data more "typical", so it's harder to mess up the codecs. //NOTE: Could combine mask and align into one method so we're not iterating twice, but I prefer having the methods separate. if (DicomUncompressedPixelData.RightAlign(pixelStream, pd.BitsAllocated, pd.BitsStored, pd.HighBit)) { var newHighBit = (ushort)(pd.HighBit - pd.LowBit); pd.HighBit = newHighBit; //correct high bit after right-aligning. dataset[DicomTags.HighBit].SetUInt16(0, newHighBit); } DicomUncompressedPixelData.ZeroUnusedBits(pixelStream, pd.BitsAllocated, pd.BitsStored, pd.HighBit); } // Set transfer syntax before compression, the codecs need it. var fragments = new DicomCompressedPixelData(pd) { TransferSyntax = codec.CodecTransferSyntax }; codec.Encode(pd, fragments, null); fragments.UpdateAttributeCollection(dataset); } }
public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters) { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) { using (var output = new MemoryStream()) { using (var gzipStream = new GZipStream(output, CompressionMode.Compress, true)) { var data = oldPixelData.GetFrame(n); gzipStream.Write(data, 0, data.Length); } // if the compressed stream is odd length, append an extra byte - gzip will know that it's padding during decompression if (output.Length%2 == 1) output.WriteByte(0); newPixelData.AddFrameFragment(output.ToArray()); } } }
public byte[] GetPixelData() { if (_compressedPixelData != null) { try { byte[] uncompressed = _compressedPixelData.GetFrame(0); _pixelData = uncompressed; _compressedPixelData = null; } catch (Exception ex) { throw new Exception(String.Format("Error occurred while decompressing the pixel data: {0}", ex.Message)); } } return(_pixelData); }
public byte[] GetPixelData() { if (_compressedPixelData != null) { try { byte[] uncompressed = _compressedPixelData.GetFrame(0); _pixelData = uncompressed; _compressedPixelData = null; } catch (Exception ex) { throw new Exception(String.Format("Error occurred while decompressing the pixel data: {0}", ex.Message)); } } return _pixelData; }
private static DicomCompressedPixelData CreateCompressedPixelData(HttpWebResponse response, byte[] pixelDataBuffer) { string transferSyntaxUid = response.Headers["TransferSyntaxUid"]; TransferSyntax transferSyntax = TransferSyntax.GetTransferSyntax(transferSyntaxUid); ushort bitsAllocated = ushort.Parse(response.Headers["BitsAllocated"], CultureInfo.InvariantCulture); ushort bitsStored = ushort.Parse(response.Headers["BitsStored"], CultureInfo.InvariantCulture); ushort height = ushort.Parse(response.Headers["ImageHeight"], CultureInfo.InvariantCulture); ushort width = ushort.Parse(response.Headers["ImageWidth"], CultureInfo.InvariantCulture); ushort samples = ushort.Parse(response.Headers["SamplesPerPixel"], CultureInfo.InvariantCulture); DicomAttributeCollection collection = new DicomAttributeCollection(); collection[DicomTags.BitsAllocated].SetUInt16(0, bitsAllocated); collection[DicomTags.BitsStored].SetUInt16(0, bitsStored); collection[DicomTags.HighBit].SetUInt16(0, ushort.Parse(response.Headers["HighBit"], CultureInfo.InvariantCulture)); collection[DicomTags.Rows].SetUInt16(0, height); collection[DicomTags.Columns].SetUInt16(0, width); collection[DicomTags.PhotometricInterpretation].SetStringValue(response.Headers["PhotometricInterpretation"]); collection[DicomTags.PixelRepresentation].SetUInt16(0, ushort.Parse(response.Headers["PixelRepresentation"], CultureInfo.InvariantCulture)); collection[DicomTags.SamplesPerPixel].SetUInt16(0, samples); collection[DicomTags.DerivationDescription].SetStringValue(response.Headers["DerivationDescription"]); collection[DicomTags.LossyImageCompression].SetStringValue(response.Headers["LossyImageCompression"]); collection[DicomTags.LossyImageCompressionMethod].SetStringValue(response.Headers["LossyImageCompressionMethod"]); collection[DicomTags.LossyImageCompressionRatio].SetFloat32(0, float.Parse(response.Headers["LossyImageCompressionRatio"], CultureInfo.InvariantCulture)); collection[DicomTags.PixelData] = new DicomFragmentSequence(DicomTags.PixelData); ushort planar; if (ushort.TryParse(response.Headers["PlanarConfiguration"], NumberStyles.Integer, CultureInfo.InvariantCulture, out planar)) { collection[DicomTags.PlanarConfiguration].SetUInt16(0, planar); } DicomCompressedPixelData cpd = new DicomCompressedPixelData(collection); cpd.TransferSyntax = transferSyntax; cpd.AddFrameFragment(pixelDataBuffer); return(cpd); }
public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters) { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) { using (var output = new MemoryStream()) { using (var gzipStream = new GZipStream(output, CompressionMode.Compress, true)) { var data = oldPixelData.GetFrame(n); gzipStream.Write(data, 0, data.Length); } // if the compressed stream is odd length, append an extra byte - gzip will know that it's padding during decompression if (output.Length % 2 == 1) { output.WriteByte(0); } newPixelData.AddFrameFragment(output.ToArray()); } } }
public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters) { if (oldPixelData.UncompressedFrameSize%2 == 1) { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) { using (var stream = new MemoryStream()) { var data = oldPixelData.GetFrame(n); stream.Write(data, 0, data.Length); stream.WriteByte(0); // must pad fragments to even length newPixelData.AddFrameFragment(stream.ToArray()); } } } else { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) { newPixelData.AddFrameFragment(oldPixelData.GetFrame(n)); } } }
public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters) { if (oldPixelData.UncompressedFrameSize % 2 == 1) { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) { using (var stream = new MemoryStream()) { var data = oldPixelData.GetFrame(n); stream.Write(data, 0, data.Length); stream.WriteByte(0); // must pad fragments to even length newPixelData.AddFrameFragment(stream.ToArray()); } } } else { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) { newPixelData.AddFrameFragment(oldPixelData.GetFrame(n)); } } }
internal RetrievePixelDataResult(DicomCompressedPixelData compressedPixelData, FrameStreamingResultMetaData resultMetaData) { _compressedPixelData = compressedPixelData; _metaData = resultMetaData; }
//TODO: I should be able to replace this with the media readers now protected override WadoResponse DoProcess(IWadoUriRequest request, string mimeType) { var dcmLocation = MediaStorage.GetLocation(new DicomMediaId(request, MimeMediaTypes.DICOM)); //var dcmLocation = RetrieveService.RetrieveSopInstances ( request, mimeType ).FirstOrDefault(); if (!dcmLocation.Exists( )) { throw new ApplicationException("Object Not Found - return proper wado error "); } //if (string.Compare(mimeType, MimeMediaTypes.DICOM, true) == 0) { return(new WadoResponse(Location, mimeType)); } DicomFile file = new DicomFile( ); var frameIndex = request.ImageRequestInfo.FrameNumber - 1 ?? 0; frameIndex = Math.Max(frameIndex, 0); file.Load(dcmLocation.GetReadStream()); if (string.Compare(mimeType, MimeMediaTypes.Jpeg, true) == 0) { WadoResponse response = new WadoResponse(); if (file.TransferSyntax == TransferSyntax.JpegBaselineProcess1) { //ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec codec = new ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec (ClearCanvas.Dicom.Codec.Jpeg.JpegMode.Baseline, 0, 0 ) //codec.Encode () DicomCompressedPixelData pd = DicomPixelData.CreateFrom(file) as DicomCompressedPixelData; byte[] buffer = pd.GetFrameFragmentData(frameIndex); response.Content = new MemoryStream(buffer); response.MimeType = mimeType; return(response); } else { } } if (string.Compare(mimeType, MimeMediaTypes.UncompressedData) == 0) { WadoResponse response = null; DicomPixelData pd = null; byte[] buffer = null; response = new WadoResponse( ); pd = DicomPixelData.CreateFrom(file); buffer = pd.GetFrame(frameIndex); //********* TEST CODE*************** //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (pd.ImageWidth, pd.ImageHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed ) ; //System.Drawing.Imaging.ColorPalette ncp = bitmap.Palette; // for (int i = 0; i < 256; i++) // ncp.Entries[i] = System.Drawing.Color.FromArgb(255, i, i, i); // bitmap.Palette = ncp; // System.Drawing.Imaging.BitmapData data = bitmap.LockBits (new System.Drawing.Rectangle ( 0,0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); //IntPtr ptr = data.Scan0 ; //System.Runtime.InteropServices.Marshal.Copy (buffer, 0, ptr, buffer.Length ) ; //string fileName = @"C:\Users\zalsafadi_p.SPX\Downloads\libwebp-master\Output\release-static\x86\bin\Samples\uncompressed.raw" ; //bitmap.UnlockBits (data); //bitmap.Save ( fileName); //File.WriteAllBytes(fileName, buffer) ; //********* TEST CODE*************** response.Content = new MemoryStream(buffer); response.MimeType = mimeType; return(response); } //if ( string.Compare(mimeType, MimeMediaTypes.WebP) == 0) //{ // WadoResponse response = new WadoResponse ( ) ; // byte[] buffer = File.ReadAllBytes(Location) ; // response.Content = new MemoryStream(buffer); // response.MimeType = mimeType ; // return response ; //} return(null); }
public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters) { DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters ?? new DicomRleCodecParameters(); // Convert to RGB if (oldPixelData.HasPaletteColorLut && parameters.ConvertPaletteToRGB) { oldPixelData.ConvertPaletteColorToRgb(); newPixelData.HasPaletteColorLut = false; newPixelData.SamplesPerPixel = oldPixelData.SamplesPerPixel; newPixelData.PlanarConfiguration = oldPixelData.PlanarConfiguration; newPixelData.PhotometricInterpretation = oldPixelData.PhotometricInterpretation; } int pixelCount = oldPixelData.ImageWidth * oldPixelData.ImageHeight; int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel; for (int i = 0; i < oldPixelData.NumberOfFrames; i++) { RLEEncoder encoder = new RLEEncoder(); byte[] frameData = oldPixelData.GetFrame(i); for (int s = 0; s < numberOfSegments; s++) { encoder.NextSegment(); int sample = s / oldPixelData.BytesAllocated; int sabyte = s % oldPixelData.BytesAllocated; int pos; int offset; if (newPixelData.PlanarConfiguration == 0) { pos = sample * oldPixelData.BytesAllocated; offset = numberOfSegments; } else { pos = sample * oldPixelData.BytesAllocated * pixelCount; offset = oldPixelData.BytesAllocated; } if (rleParams.ReverseByteOrder) { pos += sabyte; } else { pos += oldPixelData.BytesAllocated - sabyte - 1; } for (int p = 0; p < pixelCount; p++) { if (pos >= frameData.Length) { throw new DicomCodecException(""); } encoder.Encode(frameData[pos]); pos += offset; } encoder.Flush(); } encoder.MakeEvenLength(); newPixelData.AddFrameFragment(encoder.GetBuffer()); } }
public void PartialFrameTest() { DicomFile file = new DicomFile("RlePartialFrameTest.dcm"); SetupMultiframeXA(file.DataSet, 511, 511, 7); file.ChangeTransferSyntax(TransferSyntax.RleLossless); file.Save(); DicomFile newFile = new DicomFile(file.Filename); newFile.Load(DicomReadOptions.StorePixelDataReferences); DicomPixelData pd; if (!newFile.TransferSyntax.Encapsulated) pd = new DicomUncompressedPixelData(newFile); else if (newFile.TransferSyntax.Equals(TransferSyntax.RleLossless)) pd = new DicomCompressedPixelData(newFile); else throw new DicomCodecException("Unsupported transfer syntax: " + newFile.TransferSyntax); for (int i=0; i< pd.NumberOfFrames; i++) { pd.GetFrame(i); } }
private static DicomCompressedPixelData CreateCompressedPixelData(HttpWebResponse response, byte[] pixelDataBuffer) { string transferSyntaxUid = response.Headers["TransferSyntaxUid"]; TransferSyntax transferSyntax = TransferSyntax.GetTransferSyntax(transferSyntaxUid); ushort bitsAllocated = ushort.Parse(response.Headers["BitsAllocated"]); ushort bitsStored = ushort.Parse(response.Headers["BitsStored"]); ushort height = ushort.Parse(response.Headers["ImageHeight"]); ushort width = ushort.Parse(response.Headers["ImageWidth"]); ushort samples = ushort.Parse(response.Headers["SamplesPerPixel"]); DicomAttributeCollection collection = new DicomAttributeCollection(); collection[DicomTags.BitsAllocated].SetUInt16(0, bitsAllocated); collection[DicomTags.BitsStored].SetUInt16(0, bitsStored); collection[DicomTags.HighBit].SetUInt16(0, ushort.Parse(response.Headers["HighBit"])); collection[DicomTags.Rows].SetUInt16(0, height); collection[DicomTags.Columns].SetUInt16(0, width); collection[DicomTags.PhotometricInterpretation].SetStringValue(response.Headers["PhotometricInterpretation"]); collection[DicomTags.PixelRepresentation].SetUInt16(0, ushort.Parse(response.Headers["PixelRepresentation"])); collection[DicomTags.SamplesPerPixel].SetUInt16(0, samples); collection[DicomTags.DerivationDescription].SetStringValue(response.Headers["DerivationDescription"]); collection[DicomTags.LossyImageCompression].SetStringValue(response.Headers["LossyImageCompression"]); collection[DicomTags.LossyImageCompressionMethod].SetStringValue(response.Headers["LossyImageCompressionMethod"]); collection[DicomTags.LossyImageCompressionRatio].SetFloat32(0, float.Parse(response.Headers["LossyImageCompressionRatio"])); collection[DicomTags.PixelData] = new DicomFragmentSequence(DicomTags.PixelData); ushort planar; if (ushort.TryParse(response.Headers["PlanarConfiguration"], out planar)) collection[DicomTags.PlanarConfiguration].SetUInt16(0, planar); DicomCompressedPixelData cpd = new DicomCompressedPixelData(collection); cpd.TransferSyntax = transferSyntax; cpd.AddFrameFragment(pixelDataBuffer); return cpd; }
private void SetupEncapsulatedImageWithIconSequence(DicomFile file, bool encapsulateIconPixelData) { var codec = new NullDicomCodec(); DicomAttributeCollection dataSet = file.DataSet; SetupSecondaryCapture(dataSet); SetupMetaInfo(file); file.TransferSyntax = TransferSyntax.ImplicitVrLittleEndian; file.ChangeTransferSyntax(codec.CodecTransferSyntax); // explicitly create the icon sequence as either encapsulated or native, so that we are not depending on correct behaviour elsewhere... CreateIconImageSequence(dataSet); if (encapsulateIconPixelData) { var dataset = ((DicomAttributeSQ) dataSet[DicomTags.IconImageSequence])[0]; var pixelData = dataset[DicomTags.PixelData]; var pd = new DicomUncompressedPixelData(dataset); using (var pixelStream = ((DicomAttributeBinary) pixelData).AsStream()) { //Before compression, make the pixel data more "typical", so it's harder to mess up the codecs. //NOTE: Could combine mask and align into one method so we're not iterating twice, but I prefer having the methods separate. if (DicomUncompressedPixelData.RightAlign(pixelStream, pd.BitsAllocated, pd.BitsStored, pd.HighBit)) { var newHighBit = (ushort) (pd.HighBit - pd.LowBit); pd.HighBit = newHighBit; //correct high bit after right-aligning. dataset[DicomTags.HighBit].SetUInt16(0, newHighBit); } DicomUncompressedPixelData.ZeroUnusedBits(pixelStream, pd.BitsAllocated, pd.BitsStored, pd.HighBit); } // Set transfer syntax before compression, the codecs need it. var fragments = new DicomCompressedPixelData(pd) {TransferSyntax = codec.CodecTransferSyntax}; codec.Encode(pd, fragments, null); fragments.UpdateAttributeCollection(dataset); } }
public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters; if (rleParams == null) throw new DicomCodecException("Unexpected RLE Codec parameters"); int pixelCount = oldPixelData.ImageWidth * oldPixelData.ImageHeight; int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel; int segmentLength = (pixelCount & 1) == 1 ? pixelCount + 1 : pixelCount; byte[] segment = new byte[segmentLength]; byte[] frameData = new byte[oldPixelData.UncompressedFrameSize]; IList<DicomFragment> rleData = oldPixelData.GetFrameFragments(frame); RLEDecoder decoder = new RLEDecoder(rleData); if (decoder.NumberOfSegments != numberOfSegments) throw new DicomCodecException("Unexpected number of RLE segments!"); for (int s = 0; s < numberOfSegments; s++) { decoder.DecodeSegment(s, segment); int sample = s / oldPixelData.BytesAllocated; int sabyte = s % oldPixelData.BytesAllocated; int pos; int offset; if (newPixelData.PlanarConfiguration == 0) { pos = sample * oldPixelData.BytesAllocated; offset = oldPixelData.SamplesPerPixel * oldPixelData.BytesAllocated; } else { pos = sample * oldPixelData.BytesAllocated * pixelCount; offset = oldPixelData.BytesAllocated; } if (rleParams.ReverseByteOrder) pos += sabyte; else pos += oldPixelData.BytesAllocated - sabyte - 1; for (int p = 0; p < pixelCount; p++) { frameData[pos] = segment[p]; pos += offset; } } newPixelData.AppendFrame(frameData); }
public void Decode(DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { for (var n = 0; n < oldPixelData.NumberOfFrames; ++n) DecodeFrame(n, oldPixelData, newPixelData, parameters); }
public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters) { DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters; if (rleParams == null) throw new DicomCodecException("Unexpected RLE Codec parameters"); // Convert to RGB if (oldPixelData.HasPaletteColorLut && parameters.ConvertPaletteToRGB) { oldPixelData.ConvertPaletteColorToRgb(); newPixelData.HasPaletteColorLut = false; newPixelData.SamplesPerPixel = oldPixelData.SamplesPerPixel; newPixelData.PlanarConfiguration = oldPixelData.PlanarConfiguration; newPixelData.PhotometricInterpretation = oldPixelData.PhotometricInterpretation; } int pixelCount = oldPixelData.ImageWidth * oldPixelData.ImageHeight; int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel; for (int i = 0; i < oldPixelData.NumberOfFrames; i++) { RLEEncoder encoder = new RLEEncoder(); byte[] frameData = oldPixelData.GetFrame(i); for (int s = 0; s < numberOfSegments; s++) { encoder.NextSegment(); int sample = s / oldPixelData.BytesAllocated; int sabyte = s % oldPixelData.BytesAllocated; int pos; int offset; if (newPixelData.PlanarConfiguration == 0) { pos = sample * oldPixelData.BytesAllocated; offset = numberOfSegments; } else { pos = sample * oldPixelData.BytesAllocated * pixelCount; offset = oldPixelData.BytesAllocated; } if (rleParams.ReverseByteOrder) pos += sabyte; else pos += oldPixelData.BytesAllocated - sabyte - 1; for (int p = 0; p < pixelCount; p++) { if (pos >= frameData.Length) throw new DicomCodecException(""); encoder.Encode(frameData[pos]); pos += offset; } encoder.Flush(); } encoder.MakeEvenLength(); newPixelData.AddFrameFragment(encoder.GetBuffer()); } }