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)); } } }
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 ( ) } }
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()); } } }
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) { 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()); } }
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()); } }
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; }