1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace Tools
- {
- internal enum OperationMode
- {
- Open,
- Create
- }
- public class VinnoStreamReader
- {
- private readonly Stream _stream;
- public VinnoStreamReader(Stream stream)
- {
- _stream = stream;
- }
- /// <summary>
- /// Read string from stream.
- /// </summary>
- /// <returns></returns>
- public string ReadString()
- {
- var dataLength = ReadInt();
- var data = new byte[dataLength];
- _stream.Read(data, 0, dataLength);
- return Encoding.Unicode.GetString(data, 0, data.Length);
- }
- /// <summary>
- /// Read a int32 value from the stream.
- /// </summary>
- /// <returns></returns>
- public int ReadInt()
- {
- var data = new byte[sizeof(int)];
- _stream.Read(data, 0, sizeof(int));
- return BitConverter.ToInt32(data, 0);
- }
- /// <summary>
- /// Read a short value from the stream.
- /// </summary>
- /// <returns></returns>
- public short ReadShort()
- {
- var data = new byte[sizeof(short)];
- _stream.Read(data, 0, sizeof(short));
- return BitConverter.ToInt16(data, 0);
- }
- /// <summary>
- /// Read a Int64 value from the stream.
- /// </summary>
- /// <returns></returns>
- public long ReadLong()
- {
- var data = new byte[sizeof(long)];
- _stream.Read(data, 0, sizeof(long));
- return BitConverter.ToInt64(data, 0);
- }
- /// <summary>
- /// Read a float value from the stream.
- /// </summary>
- /// <returns></returns>
- public float ReadFloat()
- {
- var data = new byte[sizeof(float)];
- _stream.Read(data, 0, sizeof(float));
- return BitConverter.ToSingle(data, 0);
- }
- /// <summary>
- /// Read a double value from the stream.
- /// </summary>
- /// <returns></returns>
- public double ReadDouble()
- {
- var data = new byte[sizeof(double)];
- _stream.Read(data, 0, sizeof(double));
- return BitConverter.ToDouble(data, 0);
- }
- /// <summary>
- /// Read a bool value from the stream.
- /// </summary>
- /// <returns></returns>
- public bool ReadBool()
- {
- var data = new byte[sizeof(bool)];
- _stream.Read(data, 0, sizeof(bool));
- return BitConverter.ToBoolean(data, 0);
- }
- /// <summary>
- /// Read a byte value from the stream.
- /// </summary>
- /// <returns></returns>
- public byte ReadByte()
- {
- return (byte)(_stream.ReadByte());
- }
- /// <summary>
- /// Read a byte array from the stream.
- /// </summary>
- /// <returns></returns>
- public byte[] ReadBytes()
- {
- var size = ReadInt();
- var data = new byte[size];
- _stream.Read(data, 0, size);
- return data;
- }
- public long[] ReadLongs()
- {
- var data = ReadBytes();
- var result = new long[data.Length / sizeof(long)];
- Buffer.BlockCopy(data, 0, result, 0, data.Length);
- return result;
- }
- }
- public class VinnoStreamWriter
- {
- private readonly Stream _stream;
- public VinnoStreamWriter(Stream stream)
- {
- _stream = stream;
- }
- /// <summary>
- /// Write a string to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteString(string value)
- {
- var data = Encoding.Unicode.GetBytes(value);
- WriteInt(data.Length);
- _stream.Write(data, 0, data.Length);
- }
- /// <summary>
- /// Write a int32 value to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteInt(int value)
- {
- _stream.Write(BitConverter.GetBytes(value), 0, sizeof(int));
- }
- /// <summary>
- /// Write a short value to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteShort(short value)
- {
- _stream.Write(BitConverter.GetBytes(value), 0, sizeof(short));
- }
- /// <summary>
- /// Write a int64 value to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteLong(long value)
- {
- _stream.Write(BitConverter.GetBytes(value), 0, sizeof(long));
- }
- /// <summary>
- /// Write a float value to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteFloat(float value)
- {
- _stream.Write(BitConverter.GetBytes(value), 0, sizeof(float));
- }
- /// <summary>
- /// Write a double value to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteDouble(double value)
- {
- _stream.Write(BitConverter.GetBytes(value), 0, sizeof(double));
- }
- /// <summary>
- /// Write a bool value to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteBool(bool value)
- {
- _stream.Write(BitConverter.GetBytes(value), 0, sizeof(bool));
- }
- /// <summary>
- /// Write a byte value to stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteByte(byte value)
- {
- _stream.WriteByte(value);
- }
- /// <summary>
- /// Write a array in to stream.
- /// </summary>
- public void WriteBytes(byte[] value)
- {
- WriteInt(value.Length);
- _stream.Write(value, 0, value.Length);
- }
- /// <summary>
- /// Write long array into stream.
- /// </summary>
- /// <param name="value"></param>
- public void WriteLongs(long[] value)
- {
- var data = new byte[value.Length * sizeof(long)];
- Buffer.BlockCopy(value, 0, data, 0, data.Length);
- WriteBytes(data);
- }
- }
- public enum VinnoProbeType
- {
- Undefined,
- Linear,
- Convex,
- Sector,
- };
- public class VinnoApplication
- {
- /// <summary>
- /// Gets the application's ID
- /// </summary>
- public string ApplicationId { get; }
- /// <summary>
- /// Gets the OriginalId of the applciation.
- /// </summary>
- public string ApplicationOriginalId { get; }
- /// <summary>
- /// Gets the application's name.
- /// </summary>
- public string ApplicationName { get; }
- /// <summary>
- /// Gets the applciation's category name.
- /// </summary>
- public string ApplicationCategoryName { get; }
- /// <summary>
- /// Gets if is user defined.
- /// </summary>
- public bool IsUserDefined { get; }
- public VinnoApplication(string applicationId, string applicationOriginalId, string applicationName, string applicationCategoryName, bool isUserDefined)
- {
- ApplicationId = applicationId;
- ApplicationOriginalId = applicationOriginalId;
- ApplicationName = applicationName;
- ApplicationCategoryName = applicationCategoryName;
- IsUserDefined = isUserDefined;
- }
- public byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteString(ApplicationId);
- writer.WriteString(ApplicationOriginalId);
- writer.WriteString(ApplicationName);
- writer.WriteString(ApplicationCategoryName);
- writer.WriteBool(IsUserDefined);
- result = stream.ToArray();
- }
- return result;
- }
- public static VinnoApplication FromBytes(byte[] bytes)
- {
- VinnoApplication result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var applicationId = reader.ReadString();
- var applicationOriginalId = reader.ReadString();
- var applicationName = reader.ReadString();
- var applicationCategoryName = reader.ReadString();
- var isUserDefined = reader.ReadBool();
- result = new VinnoApplication(applicationId, applicationOriginalId, applicationName, applicationCategoryName, isUserDefined);
- }
- return result;
- }
- }
- public class VinnoProbe
- {
- public string Name { get; }
- public VinnoProbeType Type { get; }
- public VinnoApplication Application { get; }
- public double FrameRate { get; }
- public VinnoProbe(string name, VinnoProbeType type, VinnoApplication application, double frameRate)
- {
- Name = name; //探头的型号名
- Type = type; //探头的类型
- Application = application;
- FrameRate = frameRate;
- }
- public byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteString(Name);
- writer.WriteByte((byte)Type);
- writer.WriteBytes(Application.ToBytes());
- writer.WriteDouble(FrameRate);
- result = stream.ToArray();
- }
- return result;
- }
- public static VinnoProbe FromBytes(byte[] bytes)
- {
- if (bytes == null)
- {
- return null;
- }
- VinnoProbe result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var name = reader.ReadString();
- var type = (VinnoProbeType)reader.ReadByte();
- var application = VinnoApplication.FromBytes(reader.ReadBytes());
- var frameRate = reader.ReadDouble();
- if (frameRate == 0)
- {
- frameRate = 15;
- }
- if (frameRate < 10 && frameRate >= 1)
- {
- frameRate = 10;
- }
- result = new VinnoProbe(name, type, application, frameRate);
- }
- return result;
- }
- }
- internal enum VidImageFormat
- {
- Jpeg = 0,
- Png = 1,
- H264 = 2,
- Zip = 3,
- Diff = 4,
- }
- internal interface IImageDataContainer
- {
- byte[] ImageData { get; set; }
- }
- internal class VinnoImageUpdater
- {
- private readonly IImageDataContainer _imageDataContainer;
- public VinnoImageUpdater(VinnoImage image)
- {
- _imageDataContainer = image;
- }
- /// <summary>
- /// Update VinnoImage's image data
- /// </summary>
- /// <param name="imageData">The image data to be updated.</param>
- public void Update(byte[] imageData)
- {
- _imageDataContainer.ImageData = imageData;
- }
- }
- public enum VinnoVisualType
- {
- V2D = 0,
- V3D,
- }
- public enum VinnoVisualIndicator
- {
- A = 0,
- B = 1,
- C = 2,
- D = 3
- }
- public enum VinnoDisplayMode
- {
- /// <summary>
- /// Normal B mode
- /// </summary>
- Normal = 0,
- /// <summary>
- /// Up/Down, B holds 1/3, Other hodes 2/3
- /// </summary>
- UpDown13B = 1,
- /// <summary>
- /// Up/Down, half, half
- /// </summary>
- UpDownHalfHalf = 2,
- /// <summary>
- /// Up/Down, B holds2/3, Other hodes1/3
- /// </summary>
- UpDown23B = 3,
- ///// <summary>
- ///// Left/Right, B holds 1/3, other hodes 2/3
- ///// </summary>
- //SideBySide13B = 3,
- /// <summary>
- /// Left/Right, half, half
- /// </summary>
- SideBySideHalfHalf = 4,
- /// <summary>
- /// Left/Right, B holds2/3, Other hodes1/3
- /// </summary>
- SideBySide14BOther = 5,
- /// <summary>
- /// All Image area are Other image instead of B image
- /// </summary>
- FullOther = 6,
- FullTissue = 7,
- }
- public enum VinnoModeType
- {
- Undefined = 0,
- Tissue,
- Flow,
- Doppler,
- TissueTM,
- Tissue3D,
- FlowM,
- };
- public class VinnoMode
- {
- public string Name { get; }
- public string DisplayName { get; }
- public VinnoModeType Type { get; }
- public VinnoMode(string name, string displayName, VinnoModeType type)
- {
- Name = name;
- DisplayName = displayName;
- Type = type;
- }
- public byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteString(Name);
- writer.WriteString(DisplayName);
- writer.WriteByte((byte)Type);
- result = stream.ToArray();
- }
- return result;
- }
- public static VinnoMode FromBytes(byte[] bytes)
- {
- VinnoMode result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var name = reader.ReadString();
- var displayName = reader.ReadString();
- var type = (VinnoModeType)reader.ReadByte();
- result = new VinnoMode(name, displayName, type);
- }
- return result;
- }
- }
- internal enum VinnoVisualAreaType
- {
- Tissue = 0,
- Flow,
- TimeMotion,
- Doppler,
- TissueTimeMotion,
- Trace,
- Colorbar,
- Tissue3D,
- ZoomTissue,
- ZoomFlow,
- AssociateTissue,
- AssociateFlow,
- }
- internal enum VinnoUnit
- {
- None = 0,
- percent = 1,
- cm = 10,
- mm = 11,
- inch = 12,
- ft = 13,
- s = 20,
- minute = 21,
- hour = 22,
- day = 23,
- week = 24,
- week_day = 25,
- Tick = 26,
- msec = 27,
- degree = 30,
- radian = 31,
- g = 40,
- mg = 41,
- ng = 42,
- kg = 43,
- oz = 44,
- lb = 45,
- cm2 = 50,
- mm2 = 51,
- m2 = 52,
- cm3 = 60,
- mm3 = 61,
- ml = 62,
- L = 63,
- cms = 70,
- mms = 71,
- ms = 72,
- cms2 = 80,
- mms2 = 81,
- cm3s = 90,
- mls = 91,
- mlmin = 92,
- Lmin = 93,
- gcm3 = 100,
- gml = 101,
- ngml = 102,
- mmHg = 110,
- mV = 120,
- Hz = 130,
- KHz = 131,
- /// <summary>
- /// beats per minute
- /// </summary>
- HR = 132,
- //SI
- cm3m2 = 140,
- mlm2 = 141,
- //CI
- cm3sm2 = 150,
- mlsm2 = 151,
- cm3minm2 = 153,
- mlminm2 = 154,
- Lminm2 = 155,
- /// <summary>
- /// MVCF:mean velocity of circumferential fiber shortening
- /// </summary>
- circs = 160,
- //CO
- mlbeat = 170,
- mm2pa = 180,
- d1mpa = 181,
- kpa = 182,
- mmHgs = 190,
- gm2 = 200,
- /// AVA Index
- cm2m2 = 210
- }
- internal class VinnoPoint
- {
- public double X { get; }
- public double Y { get; }
- public VinnoPoint(double x, double y)
- {
- X = x;
- Y = y;
- }
- public override string ToString()
- {
- return $"X:{X},Y:{Y}";
- }
- protected bool Equals(VinnoPoint other)
- {
- return X.Equals(other.X) && Y.Equals(other.Y);
- }
- public override bool Equals(object obj)
- {
- if (ReferenceEquals(null, obj)) return false;
- if (ReferenceEquals(this, obj)) return true;
- if (obj.GetType() != this.GetType()) return false;
- return Equals((VinnoPoint)obj);
- }
- public override int GetHashCode()
- {
- unchecked
- {
- return (X.GetHashCode() * 397) ^ Y.GetHashCode();
- }
- }
- public static bool operator ==(VinnoPoint left, VinnoPoint right)
- {
- return Equals(left, right);
- }
- public static bool operator !=(VinnoPoint left, VinnoPoint right)
- {
- return !Equals(left, right);
- }
- }
- internal class VinnoRect
- {
- public double Left { get; }
- public double Right { get; }
- public double Top { get; }
- public double Bottom { get; }
- public double Width => Math.Abs(Right - Left);
- public double Height => Math.Abs(Bottom - Top);
- public VinnoPoint TopLeft => new VinnoPoint(Left, Top);
- public VinnoPoint TopRight => new VinnoPoint(Right, Top);
- public VinnoPoint BottomLeft => new VinnoPoint(Left, Bottom);
- public VinnoPoint BottomRight => new VinnoPoint(Right, Bottom);
- public VinnoRect(double x, double y, double width, double height)
- {
- if ((width < 0.0) || (height < 0.0))
- {
- throw new ArgumentException("width and height can not less than 0.");
- }
- Left = x;
- Top = y;
- Right = Left + width;
- Bottom = Top + height;
- }
- public VinnoRect(VinnoPoint topLeft, VinnoPoint bottomRight)
- {
- Left = topLeft.X;
- Top = topLeft.Y;
- Right = bottomRight.X;
- Bottom = bottomRight.Y;
- }
- public static bool operator ==(VinnoRect rect1, VinnoRect rect2)
- {
- return ((((rect1.Left == rect2.Left) && (rect1.Right == rect2.Right)) && (rect1.Bottom == rect2.Bottom)) &&
- (rect1.Top == rect2.Top));
- }
- public static bool operator !=(VinnoRect rect1, VinnoRect rect2)
- {
- return !(rect1 == rect2);
- }
- public static bool Equals(VinnoRect rect1, VinnoRect rect2)
- {
- return ((((rect1.Left == rect2.Left) && (rect1.Right == rect2.Right)) && (rect1.Bottom == rect2.Bottom)) &&
- (rect1.Top == rect2.Top));
- }
- public override bool Equals(object o)
- {
- if ((o == null) || !(o is VinnoRect))
- {
- return false;
- }
- VinnoRect rect = (VinnoRect)o;
- return Equals(this, rect);
- }
- public bool Equals(VinnoRect value)
- {
- return Equals(this, value);
- }
- public override int GetHashCode()
- {
- return (((Left.GetHashCode() ^ Right.GetHashCode()) ^ Top.GetHashCode()) ^ Bottom.GetHashCode());
- }
- public override string ToString()
- {
- return $"L:{Left},R:{Right},T:{Top},B:{Bottom}";
- }
- }
- internal class VinnoLogicalCoordinate
- {
- public bool IsFlipHorizontal { get; }
- public bool IsFlipVertical { get; }
- public VinnoRect Region { get; }
- public VinnoUnit XUnit { get; }
- public VinnoUnit YUnit { get; }
- public VinnoLogicalCoordinate(bool isFlipHorizontal, bool isFlipVertical, VinnoRect region, VinnoUnit xUnit, VinnoUnit yUnit)
- {
- YUnit = yUnit;
- XUnit = xUnit;
- Region = region;
- IsFlipVertical = isFlipVertical;
- IsFlipHorizontal = isFlipHorizontal;
- }
- public byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteBool(IsFlipHorizontal);
- writer.WriteBool(IsFlipVertical);
- writer.WriteByte((byte)XUnit);
- writer.WriteByte((byte)YUnit);
- writer.WriteDouble(Region.Left);
- writer.WriteDouble(Region.Top);
- writer.WriteDouble(Region.Right);
- writer.WriteDouble(Region.Bottom);
- result = stream.ToArray();
- }
- return result;
- }
- public static VinnoLogicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoLogicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var isFlipHorizontal = reader.ReadBool();
- var isFlipVertical = reader.ReadBool();
- var xUnit = (VinnoUnit)reader.ReadByte();
- var yUnit = (VinnoUnit)reader.ReadByte();
- var left = reader.ReadDouble();
- var top = reader.ReadDouble();
- var right = reader.ReadDouble();
- var bottom = reader.ReadDouble();
- var region = new VinnoRect(new VinnoPoint(left, top), new VinnoPoint(right, bottom));
- result = new VinnoLogicalCoordinate(isFlipHorizontal, isFlipVertical, region, xUnit, yUnit);
- }
- return result;
- }
- }
- internal enum PhysicalCoordinateType
- {
- Tissue = 0,
- TimeMotion,
- ConvexTissue,
- LinearTissue,
- ConvexTVTissue,
- LinearTVTissue,
- Doppler,
- TissueTimeMotion,
- MAM,
- PWV
- }
- internal abstract class VinnoPhysicalCoordinate
- {
- public PhysicalCoordinateType Type { get; protected set; }
- public virtual byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteByte((byte)Type);
- result = stream.ToArray();
- }
- return result;
- }
- public static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate physicalCoordinate = null;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- switch (type)
- {
- case PhysicalCoordinateType.ConvexTissue:
- physicalCoordinate = VinnoConvexTissuePhysicalCoordinate.FromBytes(bytes);
- break;
- case PhysicalCoordinateType.LinearTissue:
- physicalCoordinate = VinnoLinearTissuePhysicalCoordinate.FromBytes(bytes);
- break;
- case PhysicalCoordinateType.ConvexTVTissue:
- physicalCoordinate = VinnoConvexTVTissuePhysicalCoordinate.FromBytes(bytes);
- break;
- case PhysicalCoordinateType.LinearTVTissue:
- physicalCoordinate = VinnoLinearTVTissuePhysicalCoordinate.FromBytes(bytes);
- break;
- case PhysicalCoordinateType.Doppler:
- physicalCoordinate = VinnoDopplerPhysicalCoordinate.FromBytes(bytes);
- break;
- case PhysicalCoordinateType.TissueTimeMotion:
- physicalCoordinate = VinnoTissueTimeMotionPhysicalCoordinate.FromBytes(bytes);
- break;
- case PhysicalCoordinateType.MAM:
- physicalCoordinate = VinnoMAMPhysicalCoordinate.FromBytes(bytes);
- break;
- case PhysicalCoordinateType.PWV:
- physicalCoordinate = VinnoPWVPhysicalCoordinate.FromBytes(bytes);
- break;
- }
- }
- return physicalCoordinate;
- }
- }
- internal abstract class VinnoTissuePhysicalCoordinate : VinnoPhysicalCoordinate
- {
- public double DepthEnd { get; }
- public double DepthStart { get; }
- public double Width { get; }
- public double BeamPosition { get; }
- protected VinnoTissuePhysicalCoordinate(double depthEnd, double depthStart, double width, double beamPosition)
- {
- Type = PhysicalCoordinateType.Tissue;
- BeamPosition = beamPosition;
- Width = width;
- DepthStart = depthStart;
- DepthEnd = depthEnd;
- }
- public override byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var baseData = base.ToBytes();
- stream.Write(baseData, 0, baseData.Length);
- stream.Position = stream.Length;
- var writer = new VinnoStreamWriter(stream);
- writer.WriteDouble(DepthStart);
- writer.WriteDouble(DepthEnd);
- writer.WriteDouble(Width);
- writer.WriteDouble(BeamPosition);
- result = stream.ToArray();
- }
- return result;
- }
- }
- internal abstract class VinnoTimeMotionPhysicalCoordinate : VinnoPhysicalCoordinate
- {
- public double SweepSpeed { get; }
- public double Max { get; }
- public double Min { get; }
- protected VinnoTimeMotionPhysicalCoordinate(double sweepSpeed, double max, double min)
- {
- Type = PhysicalCoordinateType.TimeMotion;
- Min = min;
- Max = max;
- SweepSpeed = sweepSpeed;
- }
- public override byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var baseData = base.ToBytes();
- stream.Write(baseData, 0, baseData.Length);
- stream.Position = stream.Length;
- var writer = new VinnoStreamWriter(stream);
- writer.WriteDouble(Min);
- writer.WriteDouble(Max);
- writer.WriteDouble(SweepSpeed);
- result = stream.ToArray();
- }
- return result;
- }
- }
- internal class VinnoConvexTissuePhysicalCoordinate : VinnoTissuePhysicalCoordinate
- {
- public double ZeroRadius { get; private set; }
- public VinnoConvexTissuePhysicalCoordinate(double depthEnd, double depthStart, double width, double beamPosition, double zeroRadius)
- : base(depthEnd, depthStart, width, beamPosition)
- {
- Type = PhysicalCoordinateType.ConvexTissue;
- ZeroRadius = zeroRadius;
- }
- public override byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var baseData = base.ToBytes();
- stream.Write(baseData, 0, baseData.Length);
- stream.Position = stream.Length;
- var writer = new VinnoStreamWriter(stream);
- writer.WriteDouble(ZeroRadius);
- result = stream.ToArray();
- }
- return result;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.ConvexTissue)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.ConvexTissue}, source type:{type}");
- }
- var depthStart = reader.ReadDouble();
- var depthEnd = reader.ReadDouble();
- var width = reader.ReadDouble();
- var beamPosition = reader.ReadDouble();
- var zeroRadius = reader.ReadDouble();
- result = new VinnoConvexTissuePhysicalCoordinate(depthEnd, depthStart, width, beamPosition, zeroRadius);
- }
- return result;
- }
- }
- internal class VinnoLinearTissuePhysicalCoordinate : VinnoTissuePhysicalCoordinate
- {
- public double Steer { get; }
- public VinnoLinearTissuePhysicalCoordinate(double depthEnd, double depthStart, double width, double beamPosition, double steer)
- : base(depthEnd, depthStart, width, beamPosition)
- {
- Type = PhysicalCoordinateType.LinearTissue;
- Steer = steer;
- }
- public override byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var baseData = base.ToBytes();
- stream.Write(baseData, 0, baseData.Length);
- stream.Position = stream.Length;
- var writer = new VinnoStreamWriter(stream);
- writer.WriteDouble(Steer);
- result = stream.ToArray();
- }
- return result;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.LinearTissue)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.LinearTissue}, source type:{type}");
- }
- var depthStart = reader.ReadDouble();
- var depthEnd = reader.ReadDouble();
- var width = reader.ReadDouble();
- var beamPosition = reader.ReadDouble();
- var steer = reader.ReadDouble();
- result = new VinnoLinearTissuePhysicalCoordinate(depthEnd, depthStart, width, beamPosition, steer);
- }
- return result;
- }
- }
- internal class VinnoConvexTVTissuePhysicalCoordinate : VinnoConvexTissuePhysicalCoordinate
- {
- public double OriginalZeroRadius { get; private set; }
- public double OriginalRocx { get; private set; }
- public VinnoConvexTVTissuePhysicalCoordinate(double depthEnd, double depthStart, double width, double beamPosition, double zeroRadius, double originalZeroRadius, double originalRocx)
- : base(depthEnd, depthStart, width, beamPosition, zeroRadius)
- {
- Type = PhysicalCoordinateType.ConvexTVTissue;
- OriginalRocx = originalRocx;
- OriginalZeroRadius = originalZeroRadius;
- }
- public override byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var baseData = base.ToBytes();
- stream.Write(baseData, 0, baseData.Length);
- stream.Position = stream.Length;
- var writer = new VinnoStreamWriter(stream);
- writer.WriteDouble(OriginalZeroRadius);
- writer.WriteDouble(OriginalRocx);
- result = stream.ToArray();
- }
- return result;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.ConvexTVTissue)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.ConvexTVTissue}, source type:{type}");
- }
- var depthStart = reader.ReadDouble();
- var depthEnd = reader.ReadDouble();
- var width = reader.ReadDouble();
- var beamPosition = reader.ReadDouble();
- var zeroRadius = reader.ReadDouble();
- var originalZeroRadius = reader.ReadDouble();
- var originalRocx = reader.ReadDouble();
- result = new VinnoConvexTVTissuePhysicalCoordinate(depthEnd, depthStart, width, beamPosition, zeroRadius, originalZeroRadius, originalRocx);
- }
- return result;
- }
- }
- internal class VinnoLinearTVTissuePhysicalCoordinate : VinnoConvexTissuePhysicalCoordinate
- {
- public VinnoLinearTVTissuePhysicalCoordinate(double depthEnd, double depthStart, double width, double beamPosition, double zeroRadius)
- : base(depthEnd, depthStart, width, beamPosition, zeroRadius)
- {
- Type = PhysicalCoordinateType.LinearTVTissue;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.LinearTVTissue)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.LinearTVTissue}, source type:{type}");
- }
- var depthStart = reader.ReadDouble();
- var depthEnd = reader.ReadDouble();
- var width = reader.ReadDouble();
- var beamPosition = reader.ReadDouble();
- var zeroRadius = reader.ReadDouble();
- result = new VinnoLinearTVTissuePhysicalCoordinate(depthEnd, depthStart, width, beamPosition, zeroRadius);
- }
- return result;
- }
- }
- internal class VinnoDopplerPhysicalCoordinate : VinnoTimeMotionPhysicalCoordinate
- {
- public double BaseLine { get; }
- public VinnoDopplerPhysicalCoordinate(double sweepSpeed, double max, double min, double baseLine)
- : base(sweepSpeed, max, min)
- {
- Type = PhysicalCoordinateType.Doppler;
- BaseLine = baseLine;
- }
- public override byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var baseData = base.ToBytes();
- stream.Write(baseData, 0, baseData.Length);
- stream.Position = stream.Length;
- var writer = new VinnoStreamWriter(stream);
- writer.WriteDouble(BaseLine);
- result = stream.ToArray();
- }
- return result;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.Doppler)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.Doppler}, source type:{type}");
- }
- var min = reader.ReadDouble();
- var max = reader.ReadDouble();
- var sweepSpeed = reader.ReadDouble();
- var baseLine = reader.ReadDouble();
- result = new VinnoDopplerPhysicalCoordinate(sweepSpeed, max, min, baseLine);
- }
- return result;
- }
- }
- internal class VinnoTissueTimeMotionPhysicalCoordinate : VinnoTimeMotionPhysicalCoordinate
- {
- public double DepthStart { get; }
- public double DepthEnd { get; }
- public VinnoTissueTimeMotionPhysicalCoordinate(double sweepSpeed, double max, double min, double depthStart, double depthEnd)
- : base(sweepSpeed, max, min)
- {
- Type = PhysicalCoordinateType.TissueTimeMotion;
- DepthEnd = depthEnd;
- DepthStart = depthStart;
- }
- public override byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var baseData = base.ToBytes();
- stream.Write(baseData, 0, baseData.Length);
- stream.Position = stream.Length;
- var writer = new VinnoStreamWriter(stream);
- writer.WriteDouble(DepthStart);
- writer.WriteDouble(DepthEnd);
- result = stream.ToArray();
- }
- return result;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.TissueTimeMotion)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.TissueTimeMotion}, source type:{type}");
- }
- var min = reader.ReadDouble();
- var max = reader.ReadDouble();
- var sweepSpeed = reader.ReadDouble();
- var depthStart = reader.ReadDouble();
- var depthEnd = reader.ReadDouble();
- result = new VinnoTissueTimeMotionPhysicalCoordinate(sweepSpeed, max, min, depthStart, depthEnd);
- }
- return result;
- }
- }
- internal class VinnoMAMPhysicalCoordinate : VinnoTissueTimeMotionPhysicalCoordinate
- {
- public VinnoMAMPhysicalCoordinate(double sweepSpeed, double max, double min, double depthStart, double depthEnd)
- : base(sweepSpeed, max, min, depthStart, depthEnd)
- {
- Type = PhysicalCoordinateType.MAM;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.MAM)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.MAM}, source type:{type}");
- }
- var min = reader.ReadDouble();
- var max = reader.ReadDouble();
- var sweepSpeed = reader.ReadDouble();
- var depthStart = reader.ReadDouble();
- var depthEnd = reader.ReadDouble();
- result = new VinnoMAMPhysicalCoordinate(sweepSpeed, max, min, depthStart, depthEnd);
- }
- return result;
- }
- }
- internal class VinnoPWVPhysicalCoordinate : VinnoTissueTimeMotionPhysicalCoordinate
- {
- public VinnoPWVPhysicalCoordinate(double sweepSpeed, double max, double min, double depthStart, double depthEnd)
- : base(sweepSpeed, max, min, depthStart, depthEnd)
- {
- Type = PhysicalCoordinateType.PWV;
- }
- public new static VinnoPhysicalCoordinate FromBytes(byte[] bytes)
- {
- VinnoPhysicalCoordinate result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var type = (PhysicalCoordinateType)reader.ReadByte();
- if (type != PhysicalCoordinateType.PWV)
- {
- throw new InvalidCastException($"Type not matched, target type:{PhysicalCoordinateType.PWV}, source type:{type}");
- }
- var min = reader.ReadDouble();
- var max = reader.ReadDouble();
- var sweepSpeed = reader.ReadDouble();
- var depthStart = reader.ReadDouble();
- var depthEnd = reader.ReadDouble();
- result = new VinnoPWVPhysicalCoordinate(sweepSpeed, max, min, depthStart, depthEnd);
- }
- return result;
- }
- }
- internal class Vinno2DVisual : VinnoVisual
- {
- /// <summary>
- /// Gets all LogicalCoordinates of this visual.
- /// </summary>
- public Dictionary<VinnoVisualAreaType, VinnoLogicalCoordinate> LogicalCoordinates { get; }
- /// <summary>
- /// Gets all PhysicalCoordinates of this visual.
- /// </summary>
- public Dictionary<VinnoVisualAreaType, VinnoPhysicalCoordinate> PhysicalCoordinates { get; }
- public Vinno2DVisual()
- {
- VisualType = VinnoVisualType.V2D;
- LogicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoLogicalCoordinate>();
- PhysicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoPhysicalCoordinate>();
- }
- public override byte[] ToBytes()
- {
- var baseData = base.ToBytes();
- byte[] result;
- using (var stream = new MemoryStream())
- {
- stream.Write(baseData, 0, baseData.Length);
- var writer = new VinnoStreamWriter(stream);
- writer.WriteByte((byte)PhysicalCoordinates.Count);
- foreach (var key in PhysicalCoordinates.Keys)
- {
- writer.WriteByte((byte)key);
- writer.WriteBytes(PhysicalCoordinates[key].ToBytes());
- }
- writer.WriteByte((byte)LogicalCoordinates.Count);
- foreach (var key in LogicalCoordinates.Keys)
- {
- writer.WriteByte((byte)key);
- writer.WriteBytes(LogicalCoordinates[key].ToBytes());
- }
- result = stream.ToArray();
- }
- return result;
- }
- public new static VinnoVisual FromBytes(byte[] bytes)
- {
- Vinno2DVisual result = null;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var visualType = (VinnoVisualType)reader.ReadByte();
- var displayMode = (VinnoDisplayMode)reader.ReadByte();
- if (visualType == VinnoVisualType.V2D)
- {
- var indicator = (VinnoVisualIndicator)reader.ReadByte();
- var activeModeType = (VinnoModeType)reader.ReadByte();
- var modeCount = reader.ReadByte();
- var modes = new List<VinnoMode>();
- for (var i = 0; i < modeCount; i++)
- {
- var mode = VinnoMode.FromBytes(reader.ReadBytes());
- modes.Add(mode);
- }
- result = new Vinno2DVisual { Indicator = indicator };
- foreach (var mode in modes)
- {
- result.Modes.Add(mode);
- }
- result.ActiveModeType = activeModeType;
- var physicalCoordinateCount = reader.ReadByte();
- var physicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoPhysicalCoordinate>();
- for (var i = 0; i < physicalCoordinateCount; i++)
- {
- var visualAreaType = (VinnoVisualAreaType)reader.ReadByte();
- var physicalCoordinate = VinnoPhysicalCoordinate.FromBytes(reader.ReadBytes());
- physicalCoordinates.Add(visualAreaType, physicalCoordinate);
- }
- var logicalCoordinateCount = reader.ReadByte();
- var logicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoLogicalCoordinate>();
- for (var i = 0; i < logicalCoordinateCount; i++)
- {
- var visualAreaType = (VinnoVisualAreaType)reader.ReadByte();
- var logicalCoordinate = VinnoLogicalCoordinate.FromBytes(reader.ReadBytes());
- logicalCoordinates.Add(visualAreaType, logicalCoordinate);
- }
- foreach (var key in physicalCoordinates.Keys)
- {
- result.PhysicalCoordinates.Add(key, physicalCoordinates[key]);
- }
- foreach (var key in logicalCoordinates.Keys)
- {
- result.LogicalCoordinates.Add(key, logicalCoordinates[key]);
- }
- result.DisplayMode = displayMode;
- }
- }
- return result;
- }
- }
- internal enum VinnoAreaIndicator
- {
- Global = 0,
- A,
- B,
- C,
- ThreeD
- }
- internal class VinnoTissue3DArea
- {
- public VinnoRect Bounds { get; }
- public double CmPerPixel { get; }
- public VinnoPoint GlobalOffset { get; }
- public VinnoAreaIndicator Indicator { get; }
- public VinnoTissue3DArea(VinnoRect bounds, double cmPerPixel, VinnoPoint globalOffset, VinnoAreaIndicator indicator)
- {
- Indicator = indicator;
- GlobalOffset = globalOffset;
- CmPerPixel = cmPerPixel;
- Bounds = bounds;
- }
- public byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteByte((byte)Indicator);
- writer.WriteDouble(CmPerPixel);
- writer.WriteDouble(GlobalOffset.X);
- writer.WriteDouble(GlobalOffset.Y);
- writer.WriteDouble(Bounds.Left);
- writer.WriteDouble(Bounds.Top);
- writer.WriteDouble(Bounds.Right);
- writer.WriteDouble(Bounds.Bottom);
- result = stream.ToArray();
- }
- return result;
- }
- public static VinnoTissue3DArea FromBytes(byte[] tissue3DAreaData)
- {
- VinnoTissue3DArea result;
- using (var stream = new MemoryStream(tissue3DAreaData))
- {
- var reader = new VinnoStreamReader(stream);
- var indicator = (VinnoAreaIndicator)reader.ReadByte();
- var cmPerPixel = reader.ReadDouble();
- var x = reader.ReadDouble();
- var y = reader.ReadDouble();
- var globalOffset = new VinnoPoint(x, y);
- var left = reader.ReadDouble();
- var top = reader.ReadDouble();
- var right = reader.ReadDouble();
- var bottom = reader.ReadDouble();
- var bounds = new VinnoRect(new VinnoPoint(left, top), new VinnoPoint(right, bottom));
- result = new VinnoTissue3DArea(bounds, cmPerPixel, globalOffset, indicator);
- }
- return result;
- }
- }
- internal class Vinno3DVisual : VinnoVisual
- {
- /// <summary>
- /// Gets the tissue 3d areas for this visual.
- /// </summary>
- public IList<VinnoTissue3DArea> Tissue3DAreas { get; }
- public Vinno3DVisual()
- {
- VisualType = VinnoVisualType.V3D;
- Tissue3DAreas = new List<VinnoTissue3DArea>();
- }
- public override byte[] ToBytes()
- {
- var baseData = base.ToBytes();
- byte[] result;
- using (var stream = new MemoryStream())
- {
- stream.Write(baseData, 0, baseData.Length);
- var writer = new VinnoStreamWriter(stream);
- writer.WriteByte((byte)Tissue3DAreas.Count);
- foreach (var tissue3DArea in Tissue3DAreas)
- {
- writer.WriteBytes(tissue3DArea.ToBytes());
- }
- result = stream.ToArray();
- }
- return result;
- }
- public new static VinnoVisual FromBytes(byte[] bytes)
- {
- Vinno3DVisual result = null;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var visualType = (VinnoVisualType)reader.ReadByte();
- var displayMode = (VinnoDisplayMode)reader.ReadByte();
- if (visualType == VinnoVisualType.V3D)
- {
- var indicator = (VinnoVisualIndicator)reader.ReadByte();
- result = new Vinno3DVisual { Indicator = indicator };
- var activeModeType = (VinnoModeType)reader.ReadByte();
- var modeCount = reader.ReadByte();
- var modes = new List<VinnoMode>();
- for (int i = 0; i < modeCount; i++)
- {
- var mode = VinnoMode.FromBytes(reader.ReadBytes());
- modes.Add(mode);
- }
- foreach (var mode in modes)
- {
- result.Modes.Add(mode);
- }
- result.ActiveModeType = activeModeType;
- var tissue3DAreaCount = reader.ReadByte();
- var tissue3DAreas = new List<VinnoTissue3DArea>();
- for (int i = 0; i < tissue3DAreaCount; i++)
- {
- var tissue3DArea = VinnoTissue3DArea.FromBytes(reader.ReadBytes());
- tissue3DAreas.Add(tissue3DArea);
- }
- foreach (var tissue3DArea in tissue3DAreas)
- {
- result.Tissue3DAreas.Add(tissue3DArea);
- }
- result.DisplayMode = displayMode;
- }
- }
- return result;
- }
- }
- public class VinnoVisual
- {
- /// <summary>
- /// Gets or sets the Display mode of this image.
- /// </summary>
- public VinnoDisplayMode DisplayMode { get; set; }
- /// <summary>
- /// Gets the visual type of this Visual.
- /// </summary>
- protected VinnoVisualType VisualType;
- /// <summary>
- /// Gets or sets the Indicator of this Visual.
- /// </summary>
- public VinnoVisualIndicator Indicator { get; set; }
- /// <summary>
- /// Gets or set the active mode type of this visual.
- /// </summary>
- public VinnoModeType ActiveModeType { get; set; }
- /// <summary>
- /// Gets all modse of this visual.
- /// </summary>
- public IList<VinnoMode> Modes { get; }
- public VinnoVisual()
- {
- ActiveModeType = VinnoModeType.Undefined;
- Modes = new List<VinnoMode>();
- //Default is A
- Indicator = VinnoVisualIndicator.A;
- //Default is Normal
- DisplayMode = VinnoDisplayMode.Normal;
- }
- public virtual byte[] ToBytes()
- {
- byte[] result;
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteByte((byte)VisualType);
- writer.WriteByte((byte)DisplayMode);
- writer.WriteByte((byte)Indicator);
- writer.WriteByte((byte)ActiveModeType);
- writer.WriteByte((byte)Modes.Count);
- foreach (var mode in Modes)
- {
- writer.WriteBytes(mode.ToBytes());
- }
- result = stream.ToArray();
- }
- return result;
- }
- public static VinnoVisual FromBytes(byte[] bytes)
- {
- VinnoVisual result = null;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var visualType = (VinnoVisualType)reader.ReadByte();
- if (visualType == VinnoVisualType.V2D)
- {
- result = Vinno2DVisual.FromBytes(bytes);
- }
- if (visualType == VinnoVisualType.V3D)
- {
- result = Vinno3DVisual.FromBytes(bytes);
- }
- }
- return result;
- }
- }
- internal class VinnoImage : IImageDataContainer
- {
- /// <summary>
- /// Gets the index of this image.
- /// </summary>
- public int Index { get; }
- /// <summary>
- /// Gets the width of this image data.
- /// </summary>
- public int Width { get; }
- /// <summary>
- /// Gets the height of this image data.
- /// </summary>
- public int Height { get; }
- /// <summary>
- /// Gets the image data of this image.
- /// </summary>
- public byte[] ImageData { get; private set; }
- /// <summary>
- /// Gets all visuals of this image.
- /// </summary>
- public IList<VinnoVisual> Visuals { get; }
- /// <summary>
- /// Implement the interface for update the image data.
- /// </summary>
- byte[] IImageDataContainer.ImageData
- {
- get => ImageData;
- set => ImageData = value;
- }
- public VinnoImage(int index, int width, int height, byte[] imageData)
- {
- Visuals = new List<VinnoVisual>();
- Index = index;
- Width = width;
- Height = height;
- ImageData = imageData;
- }
- /// <summary>
- /// Convert image to bytes.
- /// </summary>
- /// <returns>The converted bytes.</returns>
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- if (writer != null)
- {
- writer.WriteInt(Index);
- writer.WriteByte((byte)Visuals.Count);
- foreach (var visual in Visuals)
- {
- var buffer = visual?.ToBytes();
- if (buffer != null)
- {
- writer.WriteBytes(buffer);
- }
- }
- writer.WriteShort((short)Width);
- writer.WriteShort((short)Height);
- writer.WriteBytes(ImageData);
- result = stream.ToArray();
- }
- }
- return result;
- }
- /// <summary>
- /// Convert bytes to a <see cref="VinnoImage"/>
- /// </summary>
- /// <param name="bytes">The bytes to be converted.</param>
- /// <returns>The converted <see cref="VinnoImage"/></returns>
- public static VinnoImage FromBytes(byte[] bytes)
- {
- VinnoImage result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var index = reader.ReadInt();
- var visualCount = reader.ReadByte();
- var visuals = new List<VinnoVisual>();
- for (int i = 0; i < visualCount; i++)
- {
- var visual = VinnoVisual.FromBytes(reader.ReadBytes());
- visuals.Add(visual);
- }
- var widht = reader.ReadShort();
- var height = reader.ReadShort();
- var imageData = reader.ReadBytes();
- result = new VinnoImage(index, widht, height, imageData);
- foreach (var visual in visuals)
- {
- result.Visuals.Add(visual);
- }
- }
- return result;
- }
- }
- internal class VinnoImageData : IDisposable
- {
- private readonly OperationMode _operationMode;
- private readonly Stream _stream;
- private readonly VinnoStreamReader _reader;
- private readonly VinnoStreamWriter _writer;
- private readonly string _filePath;
- private readonly List<long> _imagePositionList;
- private bool _disposed;
- private bool _closed;
- private readonly object _addGetLocker = new object();
- private const string Header = "VINNO IMAGE DATA";
- private const string CacheHeader = "VINNOIMGCACHE";
- /// <summary>
- /// Gets the version of this image data.
- /// </summary>
- public int Version { get; }
- /// <summary>
- /// Gets the image count of this image data.
- /// </summary>
- public int ImageCount { get; private set; }
- /// <summary>
- /// Gets the probe information.
- /// </summary>
- public VinnoProbe Probe { get; private set; }
- /// <summary>
- /// Gets the image format of this image data.
- /// </summary>
- public VidImageFormat ImageFormat { get; }
- /// <summary>
- /// Gets or sets the extended data.
- /// </summary>
- public byte[] ExtendedData { get; set; }
- /// <summary>
- /// Create a VINNO Image Data.
- /// </summary>
- /// <param name="filePath">The file path to read or create.</param>
- /// <param name="mode">The operation mode, create to create a vid file, open to open a vid file.</param>
- public VinnoImageData(string filePath, OperationMode mode)
- {
- try
- {
- ImageFormat = VidImageFormat.Jpeg;
- _filePath = filePath;
- _operationMode = mode;
- ExtendedData = new byte[0];
- if (mode == OperationMode.Create)
- {
- //Create temp file.
- var tempFile = _filePath + ".tmp";
- _stream = new FileStream(tempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
- _writer = new VinnoStreamWriter(_stream);
- Version = 3;
- Probe = null;
- _imagePositionList = new List<long>();
- ImageCount = 0;
- }
- else
- {
- if (!File.Exists(_filePath) && File.Exists(_filePath + ".tmp"))
- {
- _filePath = _filePath + ".tmp";
- }
- _stream = new FileStream(_filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
- _reader = new VinnoStreamReader(_stream);
- var header = _reader.ReadString();
- if (header != Header && header != CacheHeader)
- {
- _stream.Dispose();
- throw new InvalidDataException("File is not a VID file.");
- }
- Version = _reader.ReadInt();
- //Get probe info
- var probeData = _reader.ReadBytes();
- Probe = VinnoProbe.FromBytes(probeData);
- ImageFormat = (VidImageFormat)_reader.ReadInt();
- ExtendedData = _reader.ReadBytes();
- //Get the index list.
- _imagePositionList = new List<long>(_reader.ReadLongs());
- ImageCount = _imagePositionList.Count;
- }
- }
- catch (Exception ex)
- {
- _stream?.Dispose();
- }
- }
- /// <summary>
- /// Create a VINNO Image Data.
- /// </summary>
- /// <param name="stream"></param>
- public VinnoImageData(Stream stream)
- {
- try
- {
- ImageFormat = VidImageFormat.Jpeg;
- ExtendedData = new byte[0];
- _stream = stream;
- _reader = new VinnoStreamReader(_stream);
- var header = _reader.ReadString();
- if (header != Header)
- {
- _stream.Dispose();
- throw new InvalidDataException("File is not a VID file.");
- }
- Version = _reader.ReadInt();
- //Get probe info
- var probeData = _reader.ReadBytes();
- Probe = VinnoProbe.FromBytes(probeData);
- ImageFormat = (VidImageFormat)_reader.ReadInt();
- ExtendedData = _reader.ReadBytes();
- //Get the index list.
- _imagePositionList = new List<long>(_reader.ReadLongs());
- ImageCount = _imagePositionList.Count;
- }
- catch (Exception ex)
- {
- _stream?.Dispose();
- }
- }
- /// <summary>
- /// Add probe info into vid.
- /// </summary>
- /// <param name="probe"></param>
- public void AddProbe(VinnoProbe probe)
- {
- Probe = probe;
- }
- /// <summary>
- /// Add dicom info into vid
- /// </summary>
- /// <param name="extendedData"></param>
- public void AddExtendedData(byte[] extendedData)
- {
- ExtendedData = extendedData;
- }
- /// <summary>
- /// Add one image into vid.
- /// </summary>
- /// <param name="image"></param>
- public void AddImage(VinnoImage image)
- {
- if (_closed)
- {
- throw new InvalidOperationException("ImageData closed.");
- }
- if (_operationMode != OperationMode.Create)
- {
- throw new InvalidOperationException("Can not add image under open mode.");
- }
- lock (_addGetLocker)
- {
- var postion = _stream.Position;
- _writer.WriteBytes(image.ToBytes());
- _imagePositionList.Add(postion);
- ImageCount++;
- }
- }
- /// <summary>
- /// Gets one image from the vid.
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public VinnoImage GetImage(int index)
- {
- if (_closed)
- {
- throw new InvalidOperationException("ImageData closed.");
- }
- if (_operationMode != OperationMode.Open)
- {
- throw new InvalidOperationException("Can not open image under create mode.");
- }
- if (index >= ImageCount || index < 0)
- {
- throw new IndexOutOfRangeException("Can not find image Data");
- }
- lock (_addGetLocker)
- {
- //Jump to image.
- _stream.Position = _imagePositionList[index];
- var imageData = _reader.ReadBytes();
- return VinnoImage.FromBytes(imageData);
- }
- }
- public void Close()
- {
- if (!_closed)
- {
- lock (_addGetLocker)
- {
- if (_operationMode == OperationMode.Create && !string.IsNullOrWhiteSpace(_filePath) && ImageCount > 0)
- {
- using (var stream = File.Create(_filePath))
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteString(Header);
- writer.WriteInt(Version);
- writer.WriteBytes(Probe.ToBytes());
- writer.WriteInt((int)ImageFormat);
- writer.WriteBytes(ExtendedData);
- //Position data length = imagecount(int 4bytes) + imagecount * positionSize(long 8bytes)
- var positionsDataLength = _imagePositionList.Count * sizeof(long) + 4;
- var offset = stream.Length + positionsDataLength;
- for (var i = 0; i < _imagePositionList.Count; i++)
- {
- _imagePositionList[i] = _imagePositionList[i] + offset;
- }
- writer.WriteLongs(_imagePositionList.ToArray());
- _stream.Position = 0;
- _stream.CopyTo(stream);
- }
- }
- _stream?.Dispose();
- if (_operationMode == OperationMode.Create && !string.IsNullOrWhiteSpace(_filePath))
- {
- var tempFile = _filePath + ".tmp";
- File.Delete(tempFile);
- }
- }
- _closed = true;
- }
- }
- protected void Dispose(bool disposing)
- {
- if (!_disposed)
- {
- Close();
- }
- _disposed = true;
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- }
- /// <summary>
- /// 扫描方位 左侧或右侧
- /// </summary>
- public enum ScanPosition
- {
- Left,
- Right,
- Fusion
- }
- public class Vinno3DPhysicalData
- {
- /// <summary>
- /// Get thyroid scan position.
- /// </summary>
- public ScanPosition ScanPosition { get; }
- /// <summary>
- /// Get vinno visual.
- /// </summary>
- public IList<VinnoVisual> VinnoVisuals { get; }
- /// <summary>
- /// Get vinno visual.
- /// </summary>
- public VinnoProbe VinnoProbe { get; }
- public Vinno3DPhysicalData(ScanPosition scanPosition, VinnoProbe vinnoProbe, IList<VinnoVisual> vinnoVisuals)
- {
- ScanPosition = scanPosition;
- VinnoProbe = vinnoProbe;
- VinnoVisuals = vinnoVisuals;
- }
- /// <summary>
- /// From bytes.
- /// </summary>
- /// <param name="bytes">bytes.</param>
- /// <returns>Instance of vinno carotid 3d physical data.</returns>
- public static Vinno3DPhysicalData FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var thyroidType = (ScanPosition)reader.ReadByte();
- var probe = VinnoProbe.FromBytes(reader.ReadBytes());
- var visualCount = reader.ReadByte();
- var visuals = new List<VinnoVisual>();
- for (int i = 0; i < visualCount; i++)
- {
- var visual = VinnoVisual.FromBytes(reader.ReadBytes()); // 全部为null
- visuals.Add(visual);
- }
- return new Vinno3DPhysicalData(thyroidType, probe, visuals);
- }
- }
- }
- }
|