123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using Dicom;
- using Dicom.Imaging;
- using FISLib.Vid;
- using FISLib.Vid.Visuals;
- using SkiaSharp;
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using Vinno.FIS.Sonopost.Managers;
- using Vinno.FIS.Sonopost.Managers.Interfaces;
- using Vinno.IUS.Common.Log;
- namespace Vinno.FIS.Sonopost.Features.Dicom
- {
- public class VidFile : IDisposable
- {
- private const int DefaultFrameRate = 1;
- private readonly FISVinnoImageData _vinnoImageData;
- private IVidService _fisVidService;
- public VidFile(string targetPath)
- {
- _fisVidService = AppManager.Instance.GetManager<IFISManager>().FISVidService;
- _vinnoImageData = _fisVidService.CreateFISVinnoImageData(targetPath);
- }
- /// <summary>
- /// Add images from dicom image.
- /// </summary>
- public void AddImages(DicomImage dicomImage, string id, string patientId)
- {
- var encoder = new FISVinnoImageEncoder();
- Logger.WriteLineInfo($"Begin Render image {id}, patientId:{patientId},total frames{dicomImage.NumberOfFrames}");
- for (var i = 0; i < dicomImage.NumberOfFrames; i++)
- {
- using (IImage image = dicomImage.RenderImage(i))
- {
- using (var bitmap = image.AsClonedBitmap())
- {
- if (bitmap == null)
- {
- continue;
- }
- int width = bitmap.Width;
- int height = bitmap.Height;
- if (width <= 0 || height <= 0)
- {
- continue;
- }
- var bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
- using (var skiaBitmap = new SKBitmap(width, height))
- {
- skiaBitmap.SetPixels(bitmapData.Scan0);
- var vinnoImage = encoder.Encode(skiaBitmap);
- var visual = new FISVinno2DVisual();
- visual.Modes.Add(new FISVinnoMode("HAR", "HAR", FISVinnoModeType.Tissue));
- visual.ActiveModeType = FISVinnoModeType.Tissue;
- var physicalCoordinateInfo = new FISVinnoLinearTVTissuePhysicalCoordinate(1, 0, 1, 0, 0);
- var logicalCoordinateInfo = new FISVinnoLogicalCoordinate(false, false,
- new FISVinnoRect(0, 0, 1, 1), FISVinnoUnit.None, FISVinnoUnit.None);
- visual.PhysicalCoordinates.Add(FISVinnoVisualAreaType.Tissue, physicalCoordinateInfo);
- visual.LogicalCoordinates.Add(FISVinnoVisualAreaType.Tissue, logicalCoordinateInfo);
- vinnoImage.Visuals.Add(visual);
- _vinnoImageData.AddImage(vinnoImage);
- }
- bitmap.UnlockBits(bitmapData);
- }
- }
- }
- Logger.WriteLineInfo($"Finish Render image {id}, patientId:{patientId}");
- }
- /// <summary>
- /// Add probe.
- /// </summary>
- /// <param name="dataSet">Dicom dataset</param>
- public void AddProbe(DicomDataset dataSet, int imageCount)
- {
- var frame = GetFrameRate(dataSet, imageCount);
- var probe = new FISVinnoProbe("ThirdPart", FISVinnoProbeType.Linear, new FISVinnoApplication("ThirdPart", string.Empty, "ThirdPart", "ThirdPart", false), frame);
- _vinnoImageData.SetProbe(probe);
- }
- /// <summary>
- /// Add extended data.
- /// </summary>
- /// <param name="dataSet">Dicom dataset.</param>
- public void AddExtendedData(DicomDataset dataSet)
- {
- var extendedData = VinnoExtendedDataGenerator.Generate(dataSet);
- _vinnoImageData.SetExtendedData(extendedData.ToBytes());
- }
- private static double GetFrameRate(DicomDataset dataSet, int imageCount)
- {
- if (imageCount > 1)
- {
- if (dataSet.Contains(DicomTag.CineRate))
- {
- var cineRate = dataSet.GetSingleValue<int>(DicomTag.CineRate);
- if (cineRate > 0)
- {
- return cineRate;
- }
- }
- if (dataSet.Contains(DicomTag.RecommendedDisplayFrameRate))
- {
- var recommendedDisplayFrameRate = dataSet.GetSingleValue<int>(DicomTag.RecommendedDisplayFrameRate);
- if (recommendedDisplayFrameRate > 0)
- {
- return recommendedDisplayFrameRate;
- }
- }
- if (dataSet.Contains(DicomTag.FrameTime))
- {
- var frameRate = dataSet.GetSingleValue<double>(DicomTag.FrameTime);
- if (frameRate > 0)
- {
- return frameRate;
- }
- }
- }
- return DefaultFrameRate;
- }
- public void Dispose()
- {
- _vinnoImageData.Dispose();
- }
- }
- }
|