/// <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 = Parent.SourceMessage; #if DEBUG CodeClock clock = new CodeClock(); clock.Start(); #endif 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); #if DEBUG clock.Stop(); PerformanceReportBroker.PublishReport("DicomMessageSopDataSource", "CreateFrameNormalizedPixelData", clock.Seconds); #endif return rawPixelData; }
//private static void WriteLog(string s) //{ // File.AppendAllText("C:\\_store.txt", string.Format("{0}\n",s)); //} /// <summary> /// Hàm xử lý khi nhận xong dữ liệu Dicom /// </summary> /// <param name="server"></param> /// <param name="association"></param> /// <param name="presentationId"></param> /// <param name="message"></param> void IDicomServerHandler.OnReceiveRequestMessage(DicomServer server, ServerAssociationParameters association, byte presentationId, DicomMessage message) { //wc.Stop(); //WriteLog(wc.ElapsedMilliseconds.ToString()); if (message.CommandField == DicomCommandField.CEchoRequest) { server.SendCEchoResponse(presentationId, message.MessageId, DicomStatuses.Success); return; } String studyInstanceUid = null; String seriesInstanceUid = null; DicomUid sopInstanceUid; //String patientName = null; String sex = null; sex = message.DataSet[DicomTags.PatientsSex].GetString(0, "O"); bool ok = message.DataSet[DicomTags.SopInstanceUid].TryGetUid(0, out sopInstanceUid); if (ok) ok = message.DataSet[DicomTags.SeriesInstanceUid].TryGetString(0, out seriesInstanceUid); if (ok) ok = message.DataSet[DicomTags.StudyInstanceUid].TryGetString(0, out studyInstanceUid); //if (ok) ok = message.DataSet[DicomTags.PatientsName].TryGetString(0, out patientName); if (!ok) { VBLogger.LogError("Unable to retrieve UIDs from request message, sending failure status."); server.SendCStoreResponse(presentationId, message.MessageId, sopInstanceUid.UID, DicomStatuses.ProcessingFailure); return; } TransferSyntax syntax = association.GetPresentationContext(presentationId).AcceptedTransferSyntax; server.SendCStoreResponse(presentationId, message.MessageId, sopInstanceUid.UID, DicomStatuses.Success); string pathImage = ""; var path = new StringBuilder(); path.AppendFormat("{0}{1}{2}{3}{4}", StorageLocation, Path.DirectorySeparatorChar, studyInstanceUid, Path.DirectorySeparatorChar, seriesInstanceUid); try { // Save File if (!Directory.Exists(StorageLocation)) Directory.CreateDirectory(StorageLocation); if (!Directory.Exists(path.ToString())) Directory.CreateDirectory(path.ToString()); path.AppendFormat("{0}{1}.dcm", Path.DirectorySeparatorChar, sopInstanceUid.UID); var dicomFile = new DicomFile(message, path.ToString()) { TransferSyntaxUid = syntax.UidString, MediaStorageSopInstanceUid = sopInstanceUid.UID, ImplementationClassUid = DicomImplementation.ClassUID.UID, ImplementationVersionName = DicomImplementation.Version, SourceApplicationEntityTitle = association.CallingAE, MediaStorageSopClassUid = message.SopClass.Uid }; dicomFile.Save(DicomWriteOptions.None); //WriteLog(string.Format("Save File OK!: {0}", wc.ElapsedMilliseconds)); var pd = new DicomUncompressedPixelData(message.DataSet); pathImage = path.ToString(); byte[] thePixels = pd.GetFrame(0); var pixData = new UInt16[thePixels.Length/2]; int h = dicomFile.DataSet[DicomTags.Rows].GetUInt16(0, 0); int w = dicomFile.DataSet[DicomTags.Columns].GetUInt16(0, 0); UInt16 max = 0, min = UInt16.MaxValue; //min = pd.ge unsafe { fixed (byte* pixPointer = thePixels) { var pP = (UInt16*) pixPointer; for (int i = 0; i < w*h; ++i) { pixData[i] = *pP; if (min > pixData[i]) min = pixData[i]; if (max < pixData[i]) max = pixData[i]; pP++; } } } int indexOf = pathImage.LastIndexOf(".dcm"); pathImage = pathImage.Substring(0, indexOf); pathImage = string.Concat(pathImage, "_thumb.jpg"); string photometricInterpretation; message.DataSet[DicomTags.PhotometricInterpretation].TryGetString(0, out photometricInterpretation); Bitmap bmp = CreateBitmap(pixData, min, max, w, h, photometricInterpretation); bmp.Save(pathImage, ImageFormat.Jpeg); bmp.Dispose(); } catch (Exception ex) { // File.WriteAllText(@"C:\log.txt", ex.ToString()); } //WriteLog(string.Format(" Create Thumbnail OK: {0}", wc.ElapsedMilliseconds)); String patientName = message.DataSet[DicomTags.PatientsName].GetString(0, ""); //Xóa các ký tự thừa trong Patient Name patientName = patientName.Replace('^', ' '); VBLogger.LogInfo("Received SOP Instance: {0} for patient {1}", sopInstanceUid, patientName); //Insert Install To DB //InsertInstanceToImageServer(association, message.DataSet); // FeedBack to Rislink string patientId = message.DataSet[DicomTags.PatientId].GetString(0, ""); string bodyPart = message.DataSet[DicomTags.BodyPartExamined].GetString(0, ""); string viewPosition = message.DataSet[DicomTags.ViewPosition].GetString(0, ""); string accessionNumber = message.DataSet[DicomTags.AccessionNumber].GetString(0, "").Trim(); accessionNumber = accessionNumber == "" ? patientId : accessionNumber; //var t = message.DataSet[DicomTags.SourceSerialNumber].GetString(0, "").Trim(); string requestingAe = association.CallingAE; if (viewPosition.Trim() == "") viewPosition = message.DataSet[DicomTags.PatientOrientation].GetString(0, ""); //WriteLog(string.Format("Finish time: {0}",wc.ElapsedMilliseconds)); var updateOk = SpMergeData(bodyPart, patientId, patientName, path.ToString(), sex, studyInstanceUid, seriesInstanceUid, sopInstanceUid.ToString(), viewPosition, accessionNumber, requestingAe); Console.WriteLine(updateOk.ToString(CultureInfo.InvariantCulture)); }