/// <summary> /// Read explicit VR little endian up to transfer syntax element and determines transfer syntax for rest of elements /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <param name="syntax">the transfer syntax of the DICOM file</param> /// <returns>elements preceeding and including transfer syntax element</returns> public static List <IDICOMElement> ReadFileMetadata(DICOMBinaryReader dr, ref TransferSyntax syntax) { var elements = new List <IDICOMElement>(); syntax = syntax != TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN ? syntax : TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN; while (dr.StreamPosition < dr.StreamLength) { long position = dr.StreamPosition; if (TagReader.ReadLittleEndian(dr).Group == _metaGroup) { dr.StreamPosition = position; IDICOMElement el = DICOMElementReader.ReadElementExplicitLittleEndian(dr); Tag uid = TagHelper.TRANSFER_SYNTAX_UID; if (el.Tag == uid) { syntax = TransferSyntaxHelper.GetSyntax(el); } elements.Add(el); } else { dr.StreamPosition = position; break; } } return(elements); }
public static void Write(DICOMBinaryWriter dw, DICOMIOSettings settings, DICOMObject d, bool isSequenceItem = false) { if (!isSequenceItem) { TransferSyntaxHelper.SetSyntax(d, settings.TransferSyntax); } for (var i = 0; i < d.Elements.Count; i++) { var el = d.Elements[i]; var currentSettings = IsFileMetaGroup(el) ? settings.GetFileMetaSettings() : settings; if (GroupWriter.IsGroupHeader(el)) { var skip = GroupWriter.WriteGroup(dw, currentSettings, d, el); i += skip; } else { _logger.LogInformation($"Writing element ${el.Tag.CompleteID}"); try { DICOMElementWriter.Write(dw, currentSettings, el); } catch (Exception e) { _logger.LogError($"Error writing : ${el.Tag.CompleteID}\n{e}"); throw e; } } } }
/// <summary> /// Splits the DICOM object into chunks that are within the max PDU size /// </summary> /// <param name="dicomObject"> the DICOM objec to be split</param> /// <param name="maxPduSize">the max length (in bytes) for a PDU</param> /// <param name="asc">the association that the file will be sent</param> /// <returns></returns> public static List <byte[]> GetChunks(DICOMObject dicomObject, int maxPduSize, PresentationContext pc) { byte[] dicomBytes; using (var stream = new MemoryStream()) { using (var dw = new DICOMBinaryWriter(stream)) { var tx = TransferSyntaxHelper.GetSyntax(pc.TransferSyntaxes.First()); DICOMObjectWriter.WriteSameSyntax(dw, new DICOMIOSettings { TransferSyntax = tx, DoWriteIndefiniteSequences = false }, dicomObject); dicomBytes = stream.ToArray(); } } var split = new List <byte[]>(); var i = 0; while (i < dicomBytes.Length) { var toTake = dicomBytes.Length >= maxPduSize - 6 ? maxPduSize - 6 : dicomBytes.Length; var fragment = dicomBytes.Skip(i).Take(toTake).ToArray(); i += fragment.Length; split.Add(fragment); } return(split); }
public DICOMWriteSettings ToDICOMWriteSettings() { var settings = DICOMWriteSettings.Default(); settings.TransferSyntax = TransferSyntaxHelper.GetSyntax(TransferSyntaxes.FirstOrDefault()); return(settings); }
public PDataTF(DICOMObject dicom, bool isLastItem, bool isCommandObject, PresentationContext context) : this() { byte[] data; using (var stream = new MemoryStream()) { using (var dw = new DICOMBinaryWriter(stream)) { var settings = new DICOMWriteSettings(); settings.TransferSyntax = isCommandObject ? TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN : TransferSyntaxHelper.GetSyntax(context.TransferSyntaxes[0]); DICOMObjectWriter.Write(dw, settings, dicom); data = stream.ToArray(); } } var frag = new PDVItemFragment(); frag.Data = data; frag.IsLastItem = isLastItem; frag.IsCommandObject = isCommandObject; var item = new PDVItem(); item.Fragment = frag; item.PresentationContextID = context.Id; Items.Add(item); }
/// <summary> /// Splits the DICOM object into chunks that are within the max PDU size /// </summary> /// <param name="dicomObject"> the DICOM objec to be split</param> /// <param name="maxPduSize">the max length (in bytes) for a PDU</param> /// <param name="asc">the association that the file will be sent</param> /// <returns></returns> private static List <byte[]> GetChunks(DICOMObject dicomObject, int maxPduSize, Association asc) { byte[] dicomBytes; using (var stream = new MemoryStream()) { using (var dw = new DICOMBinaryWriter(stream)) { DICOMObjectWriter.Write(dw, new DICOMWriteSettings { TransferSyntax = TransferSyntaxHelper.GetSyntax(asc.PresentationContexts.First().TransferSyntaxes.First()), DoWriteIndefiniteSequences = false }, dicomObject); dicomBytes = stream.ToArray(); } } var split = new List <byte[]>(); int i = 0; while (i < dicomBytes.Length) { int toTake = dicomBytes.Length >= (maxPduSize - 6) + i ? maxPduSize - 6 : dicomBytes.Length - i; byte[] fragment = dicomBytes.Skip(i).Take(toTake).ToArray(); i += fragment.Length; split.Add(fragment); } return(split); }
/// <summary> /// Read explicit VR little endian up to transfer syntax element and determines transfer syntax for rest of elements /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <param name="syntax">the transfer syntax of the DICOM file</param> /// <returns>elements preceeding and including transfer syntax element</returns> private static List <IDICOMElement> ReadFileMetadata(DICOMBinaryReader dr, out TransferSyntax syntax) { List <IDICOMElement> elements = new List <IDICOMElement>(); syntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN; while (dr.StreamPosition < dr.StreamLength) { long position = dr.StreamPosition; if (TagReader.ReadLittleEndian(dr).Group == _MetaGroup) { dr.StreamPosition = position; IDICOMElement el = DICOMElementReader.ReadElementExplicitLittleEndian(dr); if (el.Tag.CompleteID == TagHelper.TRANSFER_SYNTAX_UID.CompleteID) { syntax = TransferSyntaxHelper.GetSyntax(el); } elements.Add(el); } else { dr.StreamPosition = position; break; } } return(elements); }
/// <summary> /// Read explicit VR little endian up to transfer syntax element and determines transfer syntax for rest of elements /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <param name="syntax">the transfer syntax of the DICOM file</param> /// <returns>elements preceeding and including transfer syntax element</returns> public static List <IDICOMElement> ReadFileMetadata(DICOMBinaryReader dr, ref TransferSyntax syntax, ref StringEncoding enc) { var elements = new List <IDICOMElement>(); while (dr.StreamPosition < dr.StreamLength) { var position = dr.StreamPosition; if (TagReader.ReadLittleEndian(dr).Group == _metaGroup) { dr.StreamPosition = position; var el = DICOMElementReader.ReadElementExplicitLittleEndian(dr, enc); if (el.Tag == TagHelper.TransferSyntaxUID) { syntax = TransferSyntaxHelper.GetSyntax(el); } elements.Add(el); } else { dr.StreamPosition = position; break; } } return(elements); }
private static TransferSyntax GetTransferSyntax(Association asc, List <PDataTF> dataPds, out int presCtxId) { var ctx = asc.PresentationContexts.FirstOrDefault(p => p.Id == dataPds.First().Items.First().PresentationContextID); presCtxId = ctx.Id; return(ctx != null?TransferSyntaxHelper.GetSyntax(ctx.TransferSyntaxes.First()) : TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN); }
public static void Write(string filePath, DICOMWriteSettings settings, DICOMObject toWrite) { using (DICOMBinaryWriter dw = new DICOMBinaryWriter(filePath)) { DICOMPreambleWriter.Write(dw); TransferSyntaxHelper.SetSyntax(toWrite, settings.TransferSyntax); DICOMObjectWriter.WriteObjectLittleEndian(dw, settings, toWrite); } }
/// <summary> /// Write DICOM file out (bytes) to a specified stream /// </summary> /// <param name="stream">the stream to which to write the file</param> /// <param name="settings">the write settings</param> /// <param name="toWrite">the object to write</param> public static void Write(Stream stream, DICOMWriteSettings settings, DICOMObject toWrite) { settings = settings ?? DICOMWriteSettings.Default(); using (var dw = new DICOMBinaryWriter(stream)) { DICOMPreambleWriter.Write(dw); TransferSyntaxHelper.SetSyntax(toWrite, settings.TransferSyntax); DICOMObjectWriter.Write(dw, settings, toWrite); } }
/// <summary> /// Writes DICOM object to a file /// </summary> /// <param name="file">the path to write</param> /// <param name="settings">the DICOM settings to write (endianness, and indefinite sequences)</param> public void Write(string file, DICOMWriteSettings settings = null) { settings = settings ?? DICOMWriteSettings.Default(); //If image is compressed, lets not change the transfer syntax UID (so image will not be read incorrectly) var setSyntax = GetSelector().TransferSyntaxUID?.Data; if (setSyntax != null && TransferSyntaxHelper.IsCompressedImage(setSyntax)) { settings.TransferSyntax = TransferSyntaxHelper.GetSyntax(setSyntax); } DICOMFileWriter.Write(file, settings, this); }
public static void WriteLittleEndian(string filePath, DICOMObject toWrite) { DICOMWriteSettings settings = DICOMWriteSettings.Default(); IDICOMElement currentUID = toWrite.FindFirst(TagHelper.TRANSFER_SYNTAX_UID); if (currentUID != null) { settings.TransferSyntax = TransferSyntaxHelper.GetSyntax(currentUID); } //TODO Currently don't support BigEndian writing : switch syntax to supported if (settings.TransferSyntax == TransferSyntax.EXPLICIT_VR_BIG_ENDIAN) { settings.TransferSyntax = TransferSyntax.EXPLICIT_VR_LITTLE_ENDIAN; } Write(filePath, settings, toWrite); }
/// <summary> /// Ignores the rule of writing metadata in explicit VR little endian and instead writes all elements with the passed in syntax /// </summary> /// <param name="dw"></param> /// <param name="settings"></param> /// <param name="d"></param> /// <param name="isSequenceItem"></param> public static void WriteSameSyntax(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d, bool isSequenceItem = false) { for (int i = 0; i < d.Elements.Count; i++) { IDICOMElement el = d.Elements[i]; if (!isSequenceItem) { TransferSyntaxHelper.SetSyntax(d, settings.TransferSyntax); } if (GroupWriter.IsGroupHeader(el)) { int skip = GroupWriter.WriteGroup(dw, settings, d, el); i += skip; } else { DICOMElementWriter.Write(dw, settings, el); } } }
public static void Write(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d, bool isSequenceItem = false) { for (var i = 0; i < d.Elements.Count; i++) { var el = d.Elements[i]; if (!isSequenceItem) { TransferSyntaxHelper.SetSyntax(d, settings.TransferSyntax); } var currentSettings = IsFileMetaGroup(el) ? settings.GetFileMetaSettings() : settings; if (GroupWriter.IsGroupHeader(el)) { var skip = GroupWriter.WriteGroup(dw, currentSettings, d, el); i += skip; } else { DICOMElementWriter.Write(dw, currentSettings, el); } } }