⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecgconverter.cs

📁 ecg tool kit for medical image retrieval system
💻 CS
📖 第 1 页 / 共 3 页
字号:
/***************************************************************************
Copyright 2004-2008, Thoraxcentrum, Erasmus MC, Rotterdam, The Netherlands

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Written by Maarten JB van Ettinger.

****************************************************************************/
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Threading;

using ECGConversion.ECGDiagnostic;
using ECGConversion.ECGDemographics;
using ECGConversion.ECGGlobalMeasurements;
using ECGConversion.ECGSignals;
using ECGConversion.ECGLeadMeasurements;

namespace ECGConversion
{
	/// <summary>
	/// Class containing all converter functions for ECG files.
	/// </summary>
	public class ECGConverter
	{
		public const string SoftwareName = "ECGConversion";
		public static bool LoadAvailablePlugins = true;
		private static int _DemographicsError = 0;
		private static int _DiagnosticError = 0;
		private static int _GlobalMeasurementsError = 0;
		private static int _SignalError = 0;
		private static int _LeadMeasurementsError = 0;

		private static ECGConverter _Instance = null;
		private static Mutex _Mutex = new Mutex();
		private SortedList _SupportedFormats;
		private SortedList _SupportedECGMS;

		/// <summary>
		/// Get the one instance of the converter object.
		/// </summary>
		/// <returns></returns>
		public static ECGConverter Instance
		{
			get
			{
				try
				{
					_Mutex.WaitOne();

					if (_Instance == null)
					{
						_Instance = new ECGConverter();

						if (LoadAvailablePlugins)
						{
							AddPlugins(
								_Instance,
								Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
						}
					}
				}
				finally
				{
					_Mutex.ReleaseMutex();
				}

				return _Instance;
			}
		}

		/// <summary>
		/// constructor for ECGConverter
		/// </summary>
		private ECGConverter()
		{
			_SupportedFormats = new SortedList();
			_SupportedECGMS = new SortedList();

			_SupportedFormats.Add("SCP-ECG", new ECGPlugin("SCP-ECG", "scp", typeof(SCP.SCPFormat), typeof(SCPReader), true, "ToSCP"));
			_SupportedFormats.Add("RAW", new ECGPlugin("RAW", "raw", typeof(RawFormat.RawECGFormat), typeof(RawECGReader), false));
			_SupportedFormats.Add("CSV", new ECGPlugin("CSV", "csv", typeof(CSV.CSVFormat), null, false));
		}

		/// <summary>
		/// destructor for ECGConverter
		/// </summary>
		~ECGConverter()
		{}

		/// <summary>
		/// Function to get number of supported formats.
		/// </summary>
		/// <returns>nr of supported formats</returns>
		public int getNrSupportedFormats()
		{
			return _SupportedFormats.Count;
		}	

		/// <summary>
		/// Function to get a list of all supported formats.
		/// </summary>
		/// <returns>array containing the names of all the supported formats.</returns>
		public string[] getSupportedFormatsList()
		{
			string[] ret = new string[_SupportedFormats.Count];

			for (int i=0;i < _SupportedFormats.Count;i++)
				ret[i] = ((ECGPlugin) _SupportedFormats.GetByIndex(i)).Name;

			return ret;
		}

		/// <summary>
		/// Function to get number of supported ECG Management Systems.
		/// </summary>
		/// <returns>nr of supported formats</returns>
		public int getNrSupportedECGManagementSystems()
		{
			return _SupportedECGMS.Count;
		}

		/// <summary>
		/// Function to get a list of all supported ECG Management Systems.
		/// </summary>
		/// <returns>array containing the names of all the supported formats.</returns>
		public string[] getSupportedManagementSystemsList()
		{
			string[] ret = new string[_SupportedECGMS.Count];

			for (int i=0;i < _SupportedECGMS.Count;i++)
				ret[i] = ((ECGManagementSystem.IECGManagementSystem) _SupportedECGMS.GetByIndex(i)).Name;

			return ret;
		}

		/// <summary>
		/// Function to convert ECG file.
		/// </summary>
		/// <param name="src">source format</param>
		/// <param name="toType">convert to</param>
		/// <param name="dst">returns destination format</param>
		/// <returns>
		/// 0 on succes
		/// 1 on unsupported format (probably missing plugin)
		/// </returns>
		public int Convert(IECGFormat src, string toType, out IECGFormat dst)
		{
			return Convert(src, toType, null, out dst);
		}

		/// <summary>
		/// Function to convert ECG file.
		/// </summary>
		/// <param name="src">source format</param>
		/// <param name="toType">convert to</param>
		/// <param name="cfg">configuration to set format to</param>
		/// <param name="dst">returns destination format</param>
		/// <returns>
		/// 0 on succes
		/// 1 on unsupported or bad format (probably missing plugin)
		/// 2 on bad configuration
		/// </returns>
		public int Convert(IECGFormat src, string toType, ECGConfig cfg, out IECGFormat dst)
		{
			dst = null;

			if (toType != null)
			{
				int index = _SupportedFormats.IndexOfKey(toType.ToUpper());

				return Convert(src, index, cfg, out dst);
			}

			return 1;
		}

		/// <summary>
		/// Function to check whether format is supported.
		/// </summary>
		/// <param name="type">type of format</param>
		/// <returns>the format</returns>
		public bool hasFormatSupport(string type)
		{
			return type != null && _SupportedFormats.Contains(type.ToUpper());
		}

		/// <summary>
		/// Function to check whether ECG Management Systems is supported.
		/// </summary>
		/// <param name="type">type of format</param>
		/// <returns>the format</returns>
		public bool hasECGManagementSystemSupport(string type)
		{
			return type != null && _SupportedECGMS.Contains(type.ToUpper());
		}

		/// <summary>
		/// Function to check whether ECG Management Systems is supported.
		/// </summary>
		/// <param name="type">type of format</param>
		/// <returns>the format</returns>
		public bool hasECGManagementSystemSaveSupport(string type)
		{
			return type != null && hasECGManagementSystemSaveSupport(_SupportedECGMS.IndexOfKey(type.ToUpper()));
		}

		/// <summary>
		/// Function to get a formats.
		/// </summary>
		/// <param name="type">type of format</param>
		/// <returns>the format</returns>
		public IECGFormat getFormat(string type)
		{
			if (type != null)
			{
				int index = _SupportedFormats.IndexOfKey(type.ToUpper());

				return getFormat(index);
			}

			return null;
		}

		/// <summary>
		/// Function to get the extension of a format.
		/// </summary>
		/// <param name="type">type of format</param>
		/// <returns>extension of format</returns>
		public string getExtension(string type)
		{
			if (type != null)
			{
				int index = _SupportedFormats.IndexOfKey(type.ToUpper());

				return getExtension(index);
			}

			return null;
		}

		/// <summary>
		/// Function to get a type.
		/// </summary>
		/// <param name="type">type of format</param>
		/// <returns>type</returns>
		public Type getType(string type)
		{
			if (type != null)
			{
				int index = _SupportedFormats.IndexOfKey(type.ToUpper());

				return getType(index);
			}

			return null;
		}

		/// <summary>
		/// Function to get a configuration.
		/// </summary>
		/// <param name="type">type of format</param>
		/// <returns>configuration</returns>
		public ECGConfig getConfig(string type)
		{
			if (type != null)
			{
				int index = _SupportedFormats.IndexOfKey(type.ToUpper());

				return getConfig(index);
			}

			return null;
		}

		/// <summary>
		/// Function to get a reader.
		/// </summary>
		/// <param name="type">type of reader</param>
		/// <returns>the reader</returns>
		public IECGReader getReader(string type)
		{
			if (type != null)
			{
				int index = _SupportedFormats.IndexOfKey(type.ToUpper());

				return getReader(index);
			}

			return null;
		}

		/// <summary>
		/// Function to get an ECG Management System.
		/// </summary>
		/// <param name="type">type of ECG Management System</param>
		/// <returns>ECG Management System</returns>
		public ECGManagementSystem.IECGManagementSystem getECGManagementSystem(string type)
		{
			if (type != null)
			{
				int index = _SupportedECGMS.IndexOfKey(type.ToUpper());

				return getECGManagementSystem(index);
			}

			return null;
		}

		/// <summary>
		/// Convert by supported list.
		/// </summary>
		/// <param name="src">source format</param>
		/// <param name="i">position in list</param>
		/// <param name="cfg">configuration to set format to</param>
		/// <param name="dst">returns destination format</param>
		/// <returns>0 if successful</returns>
		public int Convert(IECGFormat src, int i, out IECGFormat dst)
		{
			return Convert(src, i, null, out dst);
		}

		/// <summary>
		/// Convert by supported list.
		/// </summary>
		/// <param name="src">source format</param>
		/// <param name="i">position in list</param>
		/// <param name="cfg">configuration to set format to</param>
		/// <param name="dst">returns destination format</param>
		/// <returns>0 if successful</returns>
		public int Convert(IECGFormat src, int i, ECGConfig cfg, out IECGFormat dst)
		{
			dst = null;

			if ((i < 0)
			||	(i >= _SupportedFormats.Count))
				return 1;

			return ((ECGPlugin)_SupportedFormats.GetByIndex(i)).Convert(src, cfg, out dst);
		}

		/// <summary>
		/// Get format from supported list.
		/// </summary>
		/// <param name="i">position in list</param>
		/// <returns>format</returns>
		public IECGFormat getFormat(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedFormats.Count))
				return null;

			return ((ECGPlugin)_SupportedFormats.GetByIndex(i)).getFormat();
		}

		/// <summary>
		/// Function to get the extension of a format.
		/// </summary>
		/// <param name="i">position in list</param>
		/// <returns>extension of format</returns>
		public string getExtension(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedFormats.Count))
				return null;

			return ((ECGPlugin)_SupportedFormats.GetByIndex(i)).Extension;
		}

		/// <summary>
		/// Get format from supported list.
		/// </summary>
		/// <param name="i">position in list</param>
		/// <returns>type</returns>
		public Type getType(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedFormats.Count))
				return null;

			return ((ECGPlugin)_SupportedFormats.GetByIndex(i)).getType();
		}

		/// <summary>
		/// Get configuration from supported list.
		/// </summary>
		/// <param name="i">position in list</param>
		/// <returns>configuration</returns>
		public ECGConfig getConfig(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedFormats.Count))
				return null;

			IECGFormat format = ((ECGPlugin)_SupportedFormats.GetByIndex(i)).getFormat();

			if (format == null)
				return null;

			return format.Config;
		}

		/// <summary>
		/// Get reader from supported list.
		/// </summary>
		/// <param name="i">position in list</param>
		/// <returns>reader</returns>
		public IECGReader getReader(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedFormats.Count))
				return null;

			return ((ECGPlugin)_SupportedFormats.GetByIndex(i)).getReader();
		}

		/// <summary>
		/// Get ECG Management System from supported list.
		/// </summary>
		/// <param name="i">position in list</param>
		/// <returns>ECG Management System</returns>
		public ECGManagementSystem.IECGManagementSystem getECGManagementSystem(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedECGMS.Count))
				return null;

			return ((ECGManagementSystem.IECGManagementSystem)_SupportedECGMS.GetByIndex(i));
		}

		/// <summary>
		/// Function to check whether plugin entry has got support for unkownreader.
		/// </summary>
		/// <param name="i">position in list</param>
		/// <returns>true if supported</returns>
		public bool hasUnknownReaderSupport(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedFormats.Count))
				return false;

			return ((ECGPlugin)_SupportedFormats.GetByIndex(i)).hasUnknownReaderSupport;
		}

		/// <summary>
		/// Function to check whether ECG Management Systems is supported.
		/// </summary>
		/// <param name="i">type of format</param>
		/// <returns>the format</returns>
		public bool hasECGManagementSystemSaveSupport(int i)
		{
			if ((i < 0)
			||	(i >= _SupportedECGMS.Count))
				return false;
			
			return ((ECGManagementSystem.IECGManagementSystem) _SupportedECGMS.GetByIndex(i)).CanSave();
		}

		/// <summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -