public static void RoundTripAndRender() { foreach (FileInfo fi in new DirectoryInfo("..\\CompressionSamples").GetFiles("*.dcm")) { Console.WriteLine("Compression Test -- Filename: " + fi.Name); DICOMData data = new DICOMData(); data.ParseFile(fi.FullName, true, new NullLogger()); TransferSyntax syntax = data.TransferSyntax; int compressedLength = (int)data[DICOMTags.PixelData].GetDataLength(false); data.Uncompress(); Image testImage = DICOMQuickRenderer.QuickRender(data, 0); Assert.IsNotNull(testImage); int uncompressedLength = (int)data[DICOMTags.PixelData].GetDataLength(false); Assert.IsTrue(uncompressedLength > compressedLength); if (CompressionWorker.SupportsCompression(syntax.Compression)) { data.ChangeTransferSyntax(syntax); int recompressedLength = (int)data[DICOMTags.PixelData].GetDataLength(false); Assert.IsTrue(recompressedLength < uncompressedLength); } //data.WriteFile(fi.FullName.Replace(".dcm", "_un.dcm")); } }
/// <summary> /// Checks to see if we support both the decompression (if any) and compression (if any) to perform the transfer syntax change requested. /// If either step is not available, then this will return false, but you may still be able to do a decompression. You can check that with /// the static methods on <see cref="DICOMSharp.Data.Compression.CompressionWorker"/>. /// </summary> /// <param name="newSyntax">The new transfer syntax you'd like to change to</param> /// <returns>Whether or not the complete change can be performed.</returns> public bool CanChangeTransferSyntax(TransferSyntax newSyntax) { if (!CompressionWorker.SupportsDecompression(this.TransferSyntax.Compression)) { return(false); } if (!CompressionWorker.SupportsCompression(newSyntax.Compression)) { return(false); } return(true); }
public virtual void Compress() { if (Compressable && (_unCompressedStream != null)) { var stream = _unCompressedStream; stream.Position = 0; if (StreamWrapper != null) { StreamWrapper.Stream = CompressionWorker.Compress(stream, Compression); } } }
public virtual void DeCompress() { if (Compressable && (_unCompressedStream == null) && (this.Data != null)) { this._unCompressedStream = null; try { _unCompressedStream = CompressionWorker.DeCompress(StreamWrapper.Stream, Compression); _streamWrapper.Clear(); _streamWrapper = null; _unCompressedStream.Position = 0; } catch (Exception e) { _unCompressedStream = null; Trace.TraceError("StreamThing.DeCompress Error {0}", e.Message); } } }
/// <summary> /// Changes the transfer syntax used by the DICOMData structure (compressed/uncompressed, endianness, implicit/explicit VR, etc.) to a new syntax. /// </summary> /// <param name="newSyntax">The new transfer syntax to change to.</param> /// <returns>Returns <c>true</c> if the change was successful, or <c>false</c> if an error was encountered.</returns> public bool ChangeTransferSyntax(TransferSyntax newSyntax) { if (!CanChangeTransferSyntax(newSyntax)) { return(false); } CompressionInfo oldCompression = TransferSyntax.Compression; CompressionInfo newCompression = newSyntax.Compression; if (oldCompression != newCompression) { //Is it compressed? if (oldCompression != CompressionInfo.None) { //Attempt to uncompress if (!this.Uncompress()) { return(false); } } //Need to compress it? if (newCompression != CompressionInfo.None) { //Attempt to compress if (!CompressionWorker.Compress(this, newSyntax)) { return(false); } } } //Compressionworker will do this... //TransferSyntax = newSyntax; return(true); }
/// <summary> /// Attempts to remove any compression from the image data and changes the Transfer Syntax to Explicit VR Little Endian. /// </summary> /// <returns>Returns <c>true</c> if the change was successful, or <c>false</c> if an error was encountered.</returns> public bool Uncompress() { return(CompressionWorker.Uncompress(this)); }