private void UpdateImageBox(DcmImageBox imageBox, String filename, int index) { //try //{ var ff = new DicomFileFormat(); ff.Load(filename, DicomReadOptions.DefaultWithoutDeferredLoading); if (ff.Dataset == null) { return; } ff.Dataset.ChangeTransferSyntax(DicomTransferSyntax.ImplicitVRLittleEndian, null); var pixelData = new DcmPixelData(ff.Dataset); var pi = PhotometricInterpretation.Lookup(pixelData.PhotometricInterpretation); // Grayscale only printer? if (pi.IsColor && _supportsColorPrinting == false) { pixelData.Unload(); return; } // Color only printer? if (pi.IsColor == false && _supportsGrayscalePrinting == false) { pixelData.Unload(); return; } DicomUID imageBoxSOPClassUID; DcmItemSequence seq; var item = new DcmItemSequenceItem(); pixelData.UpdateDataset(item.Dataset); if (pi.IsColor) { imageBoxSOPClassUID = DicomUID.BasicColorImageBoxSOPClass; seq = new DcmItemSequence(DicomTags.BasicColorImageSequence); } else { imageBoxSOPClassUID = DicomUID.BasicGrayscaleImageBoxSOPClass; seq = new DcmItemSequence(DicomTags.BasicGrayscaleImageSequence); } seq.AddSequenceItem(item); imageBox.Dataset.AddItem(seq); pixelData.Unload(); imageBox.UpdateImageBox(imageBoxSOPClassUID); imageBox.ImageBoxPosition = (ushort)index; //} //catch (Exception) //{ //} }
protected override void AdditionalMembers(DcmDataset dataset) { dataset.AddElement(DicomTags.PatientsBirthDate); dataset.AddElement(DicomTags.PatientsSex); dataset.AddElement(DicomTags.PatientsAge); dataset.AddElement(DicomTags.PatientsSize); dataset.AddElement(DicomTags.PatientsWeight); dataset.AddElement(DicomTags.MedicalAlerts); dataset.AddElement(DicomTags.PregnancyStatus); dataset.AddElement(DicomTags.Allergies); //*Contrast allergies?? dataset.AddElement(DicomTags.PatientComments); dataset.AddElement(DicomTags.SpecialNeeds); //* dataset.AddElement(DicomTags.PatientState); //* dataset.AddElement(DicomTags.CurrentPatientLocation); //* dataset.AddElement(DicomTags.InstitutionName); dataset.AddElement(DicomTags.AdmissionID); dataset.AddElement(DicomTags.AccessionNumber); dataset.AddElement(DicomTags.ReferringPhysiciansName); dataset.AddElement(DicomTags.AdmittingDiagnosesDescription); dataset.AddElement(DicomTags.RequestingPhysician); dataset.AddElement(DicomTags.StudyInstanceUID); dataset.AddElement(DicomTags.RequestedProcedureDescription); dataset.AddElement(DicomTags.RequestedProcedureID); dataset.AddElement(DicomTags.ReasonForTheRequestedProcedure); dataset.AddElement(DicomTags.RequestedProcedurePriority); dataset.AddElement(DicomTags.StudyDate); //* dataset.AddElement(DicomTags.StudyTime); //* //DicomTags.RequestedProcedureCodeSequence //DicomTags.ScheduledProtocolCodeSequence DcmItemSequenceItem sps = new DcmItemSequenceItem(); sps.Dataset.AddElementWithValue(DicomTags.ScheduledStationAETitle, ScheduledStationAE); sps.Dataset.AddElement(DicomTags.ScheduledProcedureStepStartDate); sps.Dataset.GetDA(DicomTags.ScheduledProcedureStepStartDate).SetDateTimeRange(ScheduledProcedureStepStartDate); sps.Dataset.AddElement(DicomTags.ScheduledProcedureStepStartTime); sps.Dataset.GetTM(DicomTags.ScheduledProcedureStepStartTime).SetDateTimeRange(ScheduledProcedureStepStartTime); sps.Dataset.AddElementWithValue(DicomTags.Modality, Modality); sps.Dataset.AddElement(DicomTags.ScheduledPerformingPhysiciansName); sps.Dataset.AddElement(DicomTags.ScheduledProcedureStepDescription); sps.Dataset.AddElement(DicomTags.ScheduledProcedureStepLocation); sps.Dataset.AddElement(DicomTags.ScheduledProcedureStepID); DcmItemSequence sq = new DcmItemSequence(DicomTags.ScheduledProcedureStepSequence); sq.AddSequenceItem(sps); dataset.AddItem(sq); }
private void LoadDataset(DcmDataset ds, Collection <Node> parent) { if (ds == null) { return; } foreach (DcmItem di in ds.Elements) { Image icon = LoadTreeViewAdvResourceImage("Leaf", di.VR.VR, Color.Blue); DicomNode dn = new DicomNode(icon, di); parent.Add(dn); if (di is DcmItemSequence) { dn.Icon = LoadTreeViewAdvResourceImage("FolderClosed", "SQ", Color.Blue); DcmItemSequence sq = di as DcmItemSequence; foreach (DcmItemSequenceItem item in sq.SequenceItems) { icon = LoadTreeViewAdvResourceImage("Folder", "", Color.Black); DicomTagNode din = new DicomTagNode(icon, DicomTags.Item, item.StreamLength); dn.Nodes.Add(din); LoadDataset(item.Dataset, din.Nodes); if (item.StreamLength == 0xffffffff) { icon = LoadTreeViewAdvResourceImage("FolderClosed", "", Color.Black); din.Nodes.Add(new DicomTagNode(icon, DicomTags.ItemDelimitationItem, 0)); } } if (sq.StreamLength == 0xffffffff) { icon = LoadTreeViewAdvResourceImage("FolderClosed", "", Color.Black); dn.Nodes.Add(new DicomTagNode(icon, DicomTags.SequenceDelimitationItem, 0)); } } } }
public override void FromDataset(DcmDataset dataset) { //dataset.Dump(); _dataset = dataset; dataset.LoadDicomFields(this); if (dataset.Contains(DicomTags.ScheduledProcedureStepSequence)) { DcmItemSequence sq = dataset.GetSQ(DicomTags.ScheduledProcedureStepSequence); if (sq.SequenceItems.Count > 0) { DcmItemSequenceItem sps = sq.SequenceItems[0]; Modality = sps.Dataset.GetString(DicomTags.Modality, String.Empty); ScheduledStationAETitle = sps.Dataset.GetString(DicomTags.ScheduledStationAETitle, String.Empty); ScheduledProcedureStartDate = sps.Dataset.GetDateTime(DicomTags.ScheduledProcedureStepStartDate, 0, DateTime.MinValue); ScheduledProcedureStartTime = sps.Dataset.GetDateTime(DicomTags.ScheduledProcedureStepStartTime, 0, DateTime.MinValue); ScheduledPerformingPhysicianName = sps.Dataset.GetString(DicomTags.ScheduledPerformingPhysiciansName, String.Empty); ScheduledProcedureStepDescription = sps.Dataset.GetString(DicomTags.ScheduledProcedureStepDescription, String.Empty); ScheduledProcedureStepID = sps.Dataset.GetString(DicomTags.ScheduledProcedureStepID, String.Empty); ScheduledProcedureStepLocation = sps.Dataset.GetString(DicomTags.ScheduledProcedureStepLocation, String.Empty); } } }
/// <summary> /// Read dataset from stream /// </summary> /// <param name="stopAtTag">End parsing at this tag</param> /// <param name="options">DICOM read options</param> /// <returns>Status code</returns> public DicomReadStatus Read(DicomTag stopAtTag, DicomReadOptions options) { // Counters: // _remain - bytes remaining in stream // _bytes - estimates bytes to end of dataset // _read - number of bytes read from stream try { _need = 0; _remain = _stream.Length - _stream.Position; while (_remain > 0) { DicomReadStatus status = ParseTag(stopAtTag, options); if (status == DicomReadStatus.SuccessEndRead) { return(DicomReadStatus.Success); } if (status != DicomReadStatus.Success) { return(status); } status = ParseVR(options); if (status != DicomReadStatus.Success) { return(status); } status = ParseLength(options); if (status != DicomReadStatus.Success) { return(status); } if (_tag.IsPrivate) { if (_tag.Element != 0x0000 && _tag.Element <= 0x00ff) { // handle UN private creator id if (_vr != DicomVR.LO && Flags.IsSet(options, DicomReadOptions.ForcePrivateCreatorToLO)) { Dicom.Debug.Log.Warn("Converting Private Creator VR from '{0}' to 'LO'", _vr.VR); _vr = DicomVR.LO; } } } if (_vr == DicomVR.UN && _syntax.IsExplicitVR && Flags.IsSet(options, DicomReadOptions.UseDictionaryForExplicitUN)) { _vr = _tag.Entry.DefaultVR; } if (_fragment != null) { status = InsertFragmentItem(options); if (status != DicomReadStatus.Success) { return(status); } } else if (_sqs.Count > 0 && (_tag == DicomTags.Item || _tag == DicomTags.ItemDelimitationItem || _tag == DicomTags.SequenceDelimitationItem)) { status = InsertSequenceItem(options); if (status != DicomReadStatus.Success) { return(status); } } else { if (_sqs.Count > 0) { DcmItemSequence sq = _sqs.Peek(); if (sq.StreamLength != UndefinedLength) { long end = sq.StreamPosition + 8 + sq.StreamLength; if (_syntax.IsExplicitVR) { end += 2 + 2; } if ((_stream.Position - _offset) >= end) { if (_sds.Count == _sqs.Count) { _sds.Pop(); } _sqs.Pop(); } } } if (_len == UndefinedLength) { if (_vr == DicomVR.SQ) { DcmItemSequence sq = new DcmItemSequence(_tag, _pos, _len, _endian); InsertDatasetItem(sq, options); _sqs.Push(sq); } else { _fragment = new DcmFragmentSequence(_tag, _vr, _pos, _endian); InsertDatasetItem(_fragment, options); } } else { if (_vr == DicomVR.SQ) { DcmItemSequence sq = new DcmItemSequence(_tag, _pos, _len, _endian); InsertDatasetItem(sq, options); _sqs.Push(sq); } else { if (_len > _remain) { return(NeedMoreData(_len)); } DcmElement elem = DcmElement.Create(_tag, _vr, _pos, _endian, CurrentBuffer(options)); _remain -= _len; _read += _len; InsertDatasetItem(elem, options); } } } _tag = null; _vr = null; _len = UndefinedLength; } return(DicomReadStatus.Success); } catch (EndOfStreamException) { // should never happen return(DicomReadStatus.UnknownError); } }
/// <summary> /// Write dataset to stream /// </summary> /// <param name="dataset">Dataset</param> /// <param name="options">DICOM write options</param> /// <returns>Status code</returns> public DicomWriteStatus Write(DcmDataset dataset, DicomWriteOptions options) { TransferSyntax = dataset.InternalTransferSyntax; dataset.SelectByteOrder(_syntax.Endian); foreach (DcmItem item in dataset.Elements) { if (item.Tag.Element == 0x0000) { continue; } if (Flags.IsSet(options, DicomWriteOptions.CalculateGroupLengths) && item.Tag.Group != _group && item.Tag.Group <= 0x7fe0) { _group = item.Tag.Group; _writer.Write((ushort)_group); _writer.Write((ushort)0x0000); if (_syntax.IsExplicitVR) { _writer.Write((byte)'U'); _writer.Write((byte)'L'); _writer.Write((ushort)4); } else { _writer.Write((uint)4); } _writer.Write((uint)dataset.CalculateGroupWriteLength(_group, _syntax, options)); } _writer.Write((ushort)item.Tag.Group); _writer.Write((ushort)item.Tag.Element); if (_syntax.IsExplicitVR) { _writer.Write((byte)item.VR.VR[0]); _writer.Write((byte)item.VR.VR[1]); } if (item is DcmItemSequence) { DcmItemSequence sq = item as DcmItemSequence; if (_syntax.IsExplicitVR) { _writer.Write((ushort)0x0000); } if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence) || (item.Tag.IsPrivate && !_syntax.IsExplicitVR)) { int hl = _syntax.IsExplicitVR ? 12 : 8; _writer.Write((uint)sq.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths) - (uint)hl); } else { _writer.Write((uint)UndefinedLength); } foreach (DcmItemSequenceItem ids in sq.SequenceItems) { ids.Dataset.ChangeTransferSyntax(dataset.InternalTransferSyntax, null); _writer.Write((ushort)DicomTags.Item.Group); _writer.Write((ushort)DicomTags.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) { _writer.Write((uint)ids.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths) - (uint)8); } else { _writer.Write((uint)UndefinedLength); } Write(ids.Dataset, options & ~DicomWriteOptions.CalculateGroupLengths); if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) { _writer.Write((ushort)DicomTags.ItemDelimitationItem.Group); _writer.Write((ushort)DicomTags.ItemDelimitationItem.Element); _writer.Write((uint)0x00000000); } } if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence) && !(item.Tag.IsPrivate && !_syntax.IsExplicitVR)) { _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Group); _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Element); _writer.Write((uint)0x00000000); } } else if (item is DcmFragmentSequence) { DcmFragmentSequence fs = item as DcmFragmentSequence; if (_syntax.IsExplicitVR) { _writer.Write((ushort)0x0000); } _writer.Write((uint)UndefinedLength); _writer.Write((ushort)DicomTags.Item.Group); _writer.Write((ushort)DicomTags.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.WriteFragmentOffsetTable) && fs.HasOffsetTable) { _writer.Write((uint)fs.OffsetTableBuffer.Length); fs.OffsetTableBuffer.CopyTo(_writer.BaseStream); } else { _writer.Write((uint)0x00000000); } foreach (ByteBuffer bb in fs.Fragments) { _writer.Write((ushort)DicomTags.Item.Group); _writer.Write((ushort)DicomTags.Item.Element); _writer.Write((uint)bb.Length); bb.CopyTo(_writer.BaseStream); } _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Group); _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Element); _writer.Write((uint)0x00000000); } else { DcmElement de = item as DcmElement; if (_syntax.IsExplicitVR) { if (de.VR.Is16BitLengthField) { _writer.Write((ushort)de.Length); } else { _writer.Write((ushort)0x0000); _writer.Write((uint)de.Length); } } else { _writer.Write((uint)de.Length); } de.ByteBuffer.CopyTo(_writer.BaseStream); } } return(DicomWriteStatus.Success); }
protected override void OnReceiveNGetRequest(byte presentationID, ushort messageID, DicomUID requestedClass, DicomUID requestedInstance, DicomTag[] attributes) { if (requestedClass == DicomUID.PrinterSOPClass && requestedInstance == DicomUID.PrinterSOPInstance) { DcmDataset ds = new DcmDataset(DicomTransferSyntax.ImplicitVRLittleEndian); ds.AddElementWithValue(DicomTags.PrinterStatus, "NORMAL"); ds.AddElementWithValue(DicomTags.PrinterStatus, "NORMAL"); ds.AddElementWithValue(DicomTags.PrinterName, _config.PrinterName); ds.AddElementWithValue(DicomTags.Manufacturer, "N/A"); ds.AddElementWithValue(DicomTags.ManufacturersModelName, "N/A"); ds.AddElementWithValue(DicomTags.DeviceSerialNumber, "N/A"); ds.AddElementWithValue(DicomTags.SoftwareVersions, "N/A"); ds.SetDateTime(DicomTags.DateOfLastCalibration, DicomTags.TimeOfLastCalibration, DateTime.Now); SendNGetResponse(presentationID, messageID, requestedClass, requestedInstance, ds, DcmStatus.Success); return; } if (requestedClass == DicomUID.PrintJobSOPClass) { DcmPrintJob job = null; foreach (DcmPrintJob pj in _jobs) { if (pj.SOPInstanceUID == requestedInstance) { job = pj; break; } } if (job == null) { job = new DcmPrintJob(requestedInstance); job.ExecutionStatus = "DONE"; job.CreationDateTime = DateTime.Today; job.PrintPriority = _session.PrintPriority; job.PrinterName = _config.PrinterName; job.Originator = Associate.CallingAE; } SendNGetResponse(presentationID, messageID, requestedClass, requestedInstance, job.Dataset, DcmStatus.Success); return; } if (requestedClass == DicomUID.PrinterConfigurationRetrievalSOPClass && requestedInstance == DicomUID.PrinterConfigurationRetrievalSOPInstance) { DcmDataset ds = new DcmDataset(DicomTransferSyntax.ImplicitVRLittleEndian); DcmDataset config = new DcmDataset(DicomTransferSyntax.ImplicitVRLittleEndian); DcmItemSequence sq = new DcmItemSequence(DicomTags.PrinterConfigurationSequence); sq.AddSequenceItem(config); ds.AddItem(sq); SendNGetResponse(presentationID, messageID, requestedClass, requestedInstance, ds, DcmStatus.Success); return; } SendAbort(DcmAbortSource.ServiceProvider, DcmAbortReason.NotSpecified); }
protected override void OnReceiveNCreateResponse(byte presentationID, ushort messageIdRespondedTo, DicomUID affectedClass, DicomUID affectedInstance, DcmDataset dataset, DcmStatus status) { if (_filmSession != null) { if (affectedClass == DicomUID.BasicFilmSessionSOPClass) { if (status == DcmStatus.Success) { int filmBoxesCount = CalculateRequiredImageBoxes(); if (filmBoxesCount == 0) { SendReleaseRequest(); return; } for (int i = 0; i < filmBoxesCount; i++) { DicomUID uid = DicomUID.Generate(); DcmDataset filmBoxDataset = new DcmDataset(DicomTransferSyntax.ImplicitVRLittleEndian); DcmFilmBox filmBox = _filmSession.CreateFilmBox(uid, filmBoxDataset.Clone()); filmBox.AnnotationDisplayFormatID = _annotationDisplayFormatID; filmBox.BorderDensity = _borderDensity; filmBox.ConfigurationInformation = _configurationInformation; filmBox.EmptyImageDensity = _emptyImageDensity; filmBox.FilmOrientation = _filmOrientation; filmBox.FilmSizeID = _filmSizeID; filmBox.Illumination = _illumination; filmBox.ImageDisplayFormat = _imageDisplayFormat; filmBox.MagnificationType = _magnificationType; filmBox.MaxDensity = _maxDensity; filmBox.MinDensity = _minDensity; filmBox.ReflectedAmbientLight = _reflectedAmbientLight; filmBox.RequestedResolutionID = _requestedResolutionID; filmBox.SmoothingType = _smoothingType; filmBox.Trim = _trim; byte pcid = Associate.FindAbstractSyntax(DicomUID.BasicGrayscalePrintManagementMetaSOPClass); SendNCreateRequest(pcid, NextMessageID(), DicomUID.BasicFilmBoxSOPClass, filmBox.SOPInstanceUID, filmBox.Dataset); } return; } } if (affectedClass == DicomUID.BasicFilmBoxSOPClass) { if (status == DcmStatus.Success) { DcmFilmBox filmBox = _filmSession.FindFilmBox(affectedInstance); int filmBoxIndex = _filmSession.BasicFilmBoxes.IndexOf(filmBox); if (filmBox != null) { DcmItemSequence referencedImageBoxSequenceList = null; referencedImageBoxSequenceList = dataset.GetSQ(DicomTags.ReferencedImageBoxSequence); if (referencedImageBoxSequenceList != null) { foreach (DcmItemSequenceItem item in referencedImageBoxSequenceList.SequenceItems) { DicomUID referencedSOPInstanceUID = item.Dataset.GetUID(DicomTags.ReferencedSOPInstanceUID); if (referencedSOPInstanceUID != null) { DcmImageBox imageBox = new DcmImageBox(filmBox, DcmImageBox.GraySOPClassUID, referencedSOPInstanceUID); filmBox.BasicImageBoxes.Add(imageBox); } } } _pendingImageBoxResponses.Clear(); if (filmBox.BasicImageBoxes.Count > 0) { int imageBoxIndex = 0; int imagesPerFilmbox = CalculateImagesPreFilmBox(); foreach (DcmImageBox imageBox in filmBox.BasicImageBoxes) { if (imagesPerFilmbox * filmBoxIndex + imageBoxIndex < _files.Count) { UpdateImageBox(imageBox, _files[imagesPerFilmbox * filmBoxIndex + imageBoxIndex], imageBoxIndex); } _pendingImageBoxResponses.Add(imageBox); imageBoxIndex++; byte pcid = Associate.FindAbstractSyntax(DicomUID.PrinterSOPClass); SendNSetRequest(pcid, NextMessageID(), imageBox.SOPClassUID, imageBox.SOPInstanceUID, imageBox.Dataset); } } return; } } } } SendAbort(DcmAbortSource.ServiceUser, DcmAbortReason.NotSpecified); }