VidFile.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Dicom;
  2. using Dicom.Imaging;
  3. using FISLib.Vid;
  4. using FISLib.Vid.Visuals;
  5. using SkiaSharp;
  6. using System;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using Vinno.FIS.Sonopost.Managers;
  10. using Vinno.FIS.Sonopost.Managers.Interfaces;
  11. using Vinno.IUS.Common.Log;
  12. namespace Vinno.FIS.Sonopost.Features.Dicom
  13. {
  14. public class VidFile : IDisposable
  15. {
  16. private const int DefaultFrameRate = 1;
  17. private readonly FISVinnoImageData _vinnoImageData;
  18. private IVidService _fisVidService;
  19. public VidFile(string targetPath)
  20. {
  21. _fisVidService = AppManager.Instance.GetManager<IFISManager>().FISVidService;
  22. _vinnoImageData = _fisVidService.CreateFISVinnoImageData(targetPath);
  23. }
  24. /// <summary>
  25. /// Add images from dicom image.
  26. /// </summary>
  27. public void AddImages(DicomImage dicomImage, string id, string patientId)
  28. {
  29. var encoder = new FISVinnoImageEncoder();
  30. Logger.WriteLineInfo($"Begin Render image {id}, patientId:{patientId},total frames{dicomImage.NumberOfFrames}");
  31. for (var i = 0; i < dicomImage.NumberOfFrames; i++)
  32. {
  33. using (IImage image = dicomImage.RenderImage(i))
  34. {
  35. using (var bitmap = image.AsClonedBitmap())
  36. {
  37. if (bitmap == null)
  38. {
  39. continue;
  40. }
  41. int width = bitmap.Width;
  42. int height = bitmap.Height;
  43. if (width <= 0 || height <= 0)
  44. {
  45. continue;
  46. }
  47. var bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  48. using (var skiaBitmap = new SKBitmap(width, height))
  49. {
  50. skiaBitmap.SetPixels(bitmapData.Scan0);
  51. var vinnoImage = encoder.Encode(skiaBitmap);
  52. var visual = new FISVinno2DVisual();
  53. visual.Modes.Add(new FISVinnoMode("HAR", "HAR", FISVinnoModeType.Tissue));
  54. visual.ActiveModeType = FISVinnoModeType.Tissue;
  55. var physicalCoordinateInfo = new FISVinnoLinearTVTissuePhysicalCoordinate(1, 0, 1, 0, 0);
  56. var logicalCoordinateInfo = new FISVinnoLogicalCoordinate(false, false,
  57. new FISVinnoRect(0, 0, 1, 1), FISVinnoUnit.None, FISVinnoUnit.None);
  58. visual.PhysicalCoordinates.Add(FISVinnoVisualAreaType.Tissue, physicalCoordinateInfo);
  59. visual.LogicalCoordinates.Add(FISVinnoVisualAreaType.Tissue, logicalCoordinateInfo);
  60. vinnoImage.Visuals.Add(visual);
  61. _vinnoImageData.AddImage(vinnoImage);
  62. }
  63. bitmap.UnlockBits(bitmapData);
  64. }
  65. }
  66. }
  67. Logger.WriteLineInfo($"Finish Render image {id}, patientId:{patientId}");
  68. }
  69. /// <summary>
  70. /// Add probe.
  71. /// </summary>
  72. /// <param name="dataSet">Dicom dataset</param>
  73. public void AddProbe(DicomDataset dataSet, int imageCount)
  74. {
  75. var frame = GetFrameRate(dataSet, imageCount);
  76. var probe = new FISVinnoProbe("ThirdPart", FISVinnoProbeType.Linear, new FISVinnoApplication("ThirdPart", string.Empty, "ThirdPart", "ThirdPart", false), frame);
  77. _vinnoImageData.SetProbe(probe);
  78. }
  79. /// <summary>
  80. /// Add extended data.
  81. /// </summary>
  82. /// <param name="dataSet">Dicom dataset.</param>
  83. public void AddExtendedData(DicomDataset dataSet)
  84. {
  85. var extendedData = VinnoExtendedDataGenerator.Generate(dataSet);
  86. _vinnoImageData.SetExtendedData(extendedData.ToBytes());
  87. }
  88. private static double GetFrameRate(DicomDataset dataSet, int imageCount)
  89. {
  90. if (imageCount > 1)
  91. {
  92. if (dataSet.Contains(DicomTag.CineRate))
  93. {
  94. var cineRate = dataSet.GetSingleValue<int>(DicomTag.CineRate);
  95. if (cineRate > 0)
  96. {
  97. return cineRate;
  98. }
  99. }
  100. if (dataSet.Contains(DicomTag.RecommendedDisplayFrameRate))
  101. {
  102. var recommendedDisplayFrameRate = dataSet.GetSingleValue<int>(DicomTag.RecommendedDisplayFrameRate);
  103. if (recommendedDisplayFrameRate > 0)
  104. {
  105. return recommendedDisplayFrameRate;
  106. }
  107. }
  108. if (dataSet.Contains(DicomTag.FrameTime))
  109. {
  110. var frameRate = dataSet.GetSingleValue<double>(DicomTag.FrameTime);
  111. if (frameRate > 0)
  112. {
  113. return frameRate;
  114. }
  115. }
  116. }
  117. return DefaultFrameRate;
  118. }
  119. public void Dispose()
  120. {
  121. _vinnoImageData.Dispose();
  122. }
  123. }
  124. }