FileCreator.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using AI.Reconstruction;
  5. using AI.Common;
  6. namespace Tools
  7. {
  8. public class FileCreator
  9. {
  10. /// <summary>
  11. /// 注意:颈动脉三维重建的代码中extendedData是由左右颈的Enum值、VinnoImageData里读到的Probe和Visual组成的VinnoCarotid3DPhysicalData
  12. /// 考虑到要兼容甲状腺三维重建(以及其他部位),这里未给extendedData赋值(extendedData应该由界面上希望呈现哪些额外的内容来决定要写入什么信息),
  13. /// 因此在写入surface文件里时,extendedData的长度为0
  14. /// 另,physicalLength 的单位由调用者决定,重建时不关心单位是mm还是cm还是m,(如果在界面上呈现病灶尺寸时需要单位,可以作为extendedData写入)
  15. /// </summary>
  16. /// <param name="vidFilePath"></param>
  17. /// <param name="scanType"></param>
  18. /// <param name="physicalLength"></param>
  19. /// <param name="extenedData"></param>
  20. /// <returns></returns>
  21. public static RawVolumeData VidToRawVolumeData(string vidFilePath, EnumVolumeDataScanType scanType, int physicalLength)
  22. {
  23. var vinnoImgData = new VinnoImageData(vidFilePath, OperationMode.Open);
  24. int imgWidth = 0;
  25. int imgDepth = 0;
  26. float physicalWidth = 0;
  27. float physicalDepth = 0;
  28. RectF logicalRegion = RectF.Empty;
  29. List<byte[]> dataBuffers = new List<byte[]>();
  30. for (int ni = 0; ni < vinnoImgData.ImageCount; ni++)
  31. {
  32. var vinnoImg = vinnoImgData.GetImage(ni);
  33. var imgBuffer = vinnoImg.ImageData;
  34. dataBuffers.Add(imgBuffer);
  35. if (ni == 0)
  36. {
  37. imgWidth = vinnoImg.Width;
  38. imgDepth = vinnoImg.Height;
  39. if (vinnoImg.Visuals.Count != 0)
  40. {
  41. if (vinnoImg.Visuals[0] is Vinno2DVisual visual)
  42. {
  43. if (visual.PhysicalCoordinates.ContainsKey(VinnoVisualAreaType.Tissue))
  44. {
  45. var physicalCoordinate = visual.PhysicalCoordinates[VinnoVisualAreaType.Tissue];
  46. if (physicalCoordinate is VinnoTissuePhysicalCoordinate pCoordinate)
  47. {
  48. physicalDepth = (float)(pCoordinate.DepthEnd - pCoordinate.DepthStart);
  49. physicalWidth = (float)pCoordinate.Width;
  50. }
  51. var logicalCoordinate = visual.LogicalCoordinates[VinnoVisualAreaType.Tissue];
  52. var region = logicalCoordinate.Region;
  53. logicalRegion = new RectF((float)region.Left, (float)region.Top, (float)region.Width,
  54. (float)region.Height);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. // 从vinnoImgData.ExtendedData中好像可以解析出 scanDistance、carotidType、carotidDirection
  61. // scanDistance 即扫查距离,等同于这里的physicalLength
  62. // carotidType 可以区分左右颈
  63. // carotidDirection 是扫查方向,从上到下,还是从下到上
  64. var extendedData = vinnoImgData.ExtendedData;
  65. if (extendedData.Length != 0)
  66. {
  67. using (var stream = new MemoryStream(extendedData))
  68. {
  69. stream.Position = 0;
  70. var reader = new AI.Common.Tools.AIStreamReader(stream);
  71. var count = reader.ReadInt();
  72. if (count > 0 && count <= extendedData.Length)
  73. {
  74. // ToDo 补充完整(用读出的值替换现在的physicalLength和scanType么?)
  75. }
  76. }
  77. }
  78. var volumeData = new RawVolumeData();
  79. volumeData.ReadFromVinnoImageDatas(dataBuffers, imgWidth, imgDepth, logicalRegion,
  80. physicalWidth, physicalDepth, physicalLength, scanType,extendedData);
  81. return volumeData;
  82. }
  83. }
  84. }