From 7d8dd3be2b826dd621fc7a08f50d40f8e93a19c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81=20=D0=9A=D1=83=D0=B7=D0=BD?= =?UTF-8?q?=D0=B5=D1=86=D0=BE=D0=B2?= Date: Tue, 5 Mar 2024 10:03:07 +0300 Subject: [PATCH] 20240305 --- AboutBox1.cs | 82 ---------- UnsafeBitmap.cs | 97 ------------ util.cs | 412 +++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 387 insertions(+), 204 deletions(-) delete mode 100644 UnsafeBitmap.cs diff --git a/AboutBox1.cs b/AboutBox1.cs index 6179df4..1959fa8 100644 --- a/AboutBox1.cs +++ b/AboutBox1.cs @@ -68,88 +68,6 @@ namespace AB // this.textBoxDescription.Text = comment; this.textBoxDescription.Text = comm; } - /* - #region Методы доступа к атрибутам сборки - - public string AssemblyTitle - { - get - { - object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); - if (attributes.Length > 0) - { - AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; - if (titleAttribute.Title != "") - { - return titleAttribute.Title; - } - } - return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase); - } - } - - public string AssemblyVersion - { - get - { - return Assembly.GetExecutingAssembly().GetName().Version.ToString(); - } - } - - public string AssemblyDescription - { - get - { - object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); - if (attributes.Length == 0) - { - return ""; - } - return ((AssemblyDescriptionAttribute)attributes[0]).Description; - } - } - - public string AssemblyProduct - { - get - { - object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false); - if (attributes.Length == 0) - { - return ""; - } - return ((AssemblyProductAttribute)attributes[0]).Product; - } - } - - public string AssemblyCopyright - { - get - { - object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); - if (attributes.Length == 0) - { - return ""; - } - return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; - } - } - - public string AssemblyCompany - { - get - { - object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); - if (attributes.Length == 0) - { - return ""; - } - return ((AssemblyCompanyAttribute)attributes[0]).Company; - } - } - #endregion - - */ private void AboutBox1_Load(object sender, EventArgs e) { Util.SetTabWidth(textBoxDescription, 1); diff --git a/UnsafeBitmap.cs b/UnsafeBitmap.cs deleted file mode 100644 index a4e316f..0000000 --- a/UnsafeBitmap.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Drawing; -using System.Drawing.Imaging; - -namespace Grav01 -{ - public unsafe class UnsafeBitmap - { - Bitmap bitmap; - // three elements used for MakeGreyUnsafe - int width; - BitmapData bitmapData = null; - Byte* pBase = null; - public UnsafeBitmap(Bitmap bitmap) - { - this.bitmap = new Bitmap(bitmap); - } - public UnsafeBitmap(int width, int height) - { - this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb); - } - public void Dispose() - { - bitmap.Dispose(); - } - public Bitmap Bitmap - { - get - { - return (bitmap); - } - } - private Point PixelSize - { - get - { - GraphicsUnit unit = GraphicsUnit.Pixel; - RectangleF bounds = bitmap.GetBounds(ref unit); - - return new Point((int)bounds.Width, (int)bounds.Height); - } - } - public void LockBitmap() - { - GraphicsUnit unit = GraphicsUnit.Pixel; - RectangleF boundsF = bitmap.GetBounds(ref unit); - Rectangle bounds = new Rectangle((int)boundsF.X, - (int)boundsF.Y, - (int)boundsF.Width, - (int)boundsF.Height); - - // Figure out the number of bytes in a row - // This is rounded up to be a multiple of 4 - // bytes, since a scan line in an image must always be a multiple of 4 bytes - // in length. - width = (int)boundsF.Width * sizeof(PixelData); - if (width % 4 != 0) - { - width = 4 * (width / 4 + 1); - } - bitmapData = - bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); - pBase = (Byte*)bitmapData.Scan0.ToPointer(); - } - public PixelData GetPixel(int x, int y) - { - PixelData returnValue = *PixelAt(x, y); - return returnValue; - } - public void SetPixel(int x, int y, PixelData colour) - { - PixelData* pixel = PixelAt(x, y); - *pixel = colour; - } - public void UnlockBitmap() - { - bitmap.UnlockBits(bitmapData); - bitmapData = null; - pBase = null; - } - public PixelData* PixelAt(int x, int y) - { - return (PixelData*)(pBase + y * width + x * sizeof(PixelData)); - } - } - - public struct PixelData - { - public byte blue; - public byte green; - public byte red; - } -} diff --git a/util.cs b/util.cs index aa81055..afd4c25 100644 --- a/util.cs +++ b/util.cs @@ -474,7 +474,6 @@ namespace UTIL return (PixelData*)(pBase + y * width + x * sizeof(PixelData)); } } - public struct PixelData { public byte blue; @@ -609,6 +608,68 @@ namespace UTIL , new int[] { tabWidth * characterWidth } ); } + public static String Format(T num, UInt16 chars, Boolean sign) + { + String tmp = num.GetType().ToString().Split(new Char[] { '.' })[1]; + String a = ""; + switch (tmp) + { + case "Double": + case "Single": + case "Decimal": + if (!sign) + a = Math.Round(Convert.ToDouble(num), 0).ToString().PadLeft(chars, ' '); + else + a = Math.Round(Convert.ToDouble(num), 0).ToString("+0;-#").PadLeft(chars, ' '); + return a; + case "UInt64": + case "UInt32": + case "UInt16": + case "Byte": + case "Int64": + case "Int32": + case "Int16": + case "SByte": + if (!sign) + a = Convert.ToInt64(num).ToString().PadLeft(chars, ' '); + else + a = Convert.ToInt64(num).ToString("+0;-#").PadLeft(chars, ' '); + return a; + default: + return num.ToString(); + } + } + public static String Format(T num, UInt16 chars, Boolean sign, UInt16 round) + { + String tmp = num.GetType().ToString().Split(new Char[] { '.' })[1]; + String a = ""; + switch (tmp) + { + case "Double": + case "Single": + case "Decimal": + if (!sign) + a = Math.Round(Convert.ToDouble(num), round).ToString().PadLeft(chars, ' '); + else + a = Math.Round(Convert.ToDouble(num), round).ToString("+0;-#").PadLeft(chars, ' '); + return a; + case "UInt64": + case "UInt32": + case "UInt16": + case "Byte": + case "Int64": + case "Int32": + case "Int16": + case "SByte": + if (!sign) + a = Convert.ToInt64(num).ToString().PadLeft(chars, ' '); + else + a = Convert.ToInt64(num).ToString("+0;-#").PadLeft(chars, ' '); + return a; + default: + return num.ToString(); + } + } } public class CircularBuffer { @@ -624,13 +685,15 @@ namespace UTIL _buffer = new T[bufferSize]; _bufferSize = bufferSize; _head = bufferSize - 1; + _tail = 0; + _length = 0; } public Int32 Count - { + { get { return _length; } } public Int32 Head - { + { get { return _head; } } public Int32 Tail @@ -651,27 +714,28 @@ namespace UTIL } public T Dequeue() { - lock (_lock) - { - if (IsEmpty) throw new InvalidOperationException("Queue exhausted"); + if (IsEmpty) throw new InvalidOperationException("Queue exhausted"); - T dequeued = _buffer[_tail]; - _tail = NextPosition(_tail); - _length--; - return dequeued; - } + T dequeued = _buffer[_tail]; + _tail = NextPosition(_tail); + _length--; + return dequeued; } public T Peek(int pos) { _pos = pos; - lock (_lock) - { - if (IsEmpty) throw new InvalidOperationException("Queue exhausted"); - if (_tail + pos > _head) throw new InvalidOperationException("End data"); + if (IsEmpty) throw new InvalidOperationException("Queue exhausted"); + if (_tail + pos > _head) throw new InvalidOperationException("End data"); - T dequeued = _buffer[_tail + pos]; - return dequeued; - } + T dequeued = _buffer[_tail + pos]; + return dequeued; + } + public T Peek() + { + if (IsEmpty) throw new InvalidOperationException("Queue exhausted"); + if (_tail > _head) throw new InvalidOperationException("End data"); + T dequeued = _buffer[_tail]; + return dequeued; } private int NextPosition(int position) { @@ -679,16 +743,205 @@ namespace UTIL } public void Enqueue(T toAdd) { - lock (_lock) + _head = NextPosition(_head); + _buffer[_head] = toAdd; + if (IsFull) + _tail = NextPosition(_tail); + else + _length++; + } + } + public class CircularBuffer2 + { + T[] _buffer; + Int32 _head; + Int32 _tail; + Int32 _length; + Int32 _bufferSize; + Int32 _pos; + Object _lock = new object(); + public CircularBuffer2(Int32 bufferSize) + { + _buffer = new T[bufferSize]; + _bufferSize = bufferSize; + _head = bufferSize - 1; + _tail = 0; + _length = 0; + } + public T Dequeue() + { + T dequeued = _buffer[_tail]; + _tail = (_tail + 1) % _bufferSize; + _length--; + return dequeued; + } + public T Peek() + { + if (_length == 0) throw new InvalidOperationException("No data"); + return _buffer[_tail]; + } + private int NextPosition(int position) + { + return (position + 1) % _bufferSize; + } + public void Enqueue(T toAdd) + { + _head = (_head + 1) % _bufferSize; + _buffer[_head] = toAdd; + if (_length == _bufferSize) + _tail = (_tail + 1) % _bufferSize; + else + _length++; + } + } + public class GMedian + { + UInt16[] bufUI16; + Int16[] bufI16; + UInt32[] bufUI32; + Int32[] bufI32; + Double[] bufd; + Single[] buff; + Byte _counter = 0; + + public GMedian(UInt16 num) + { + bufUI16 = new UInt16[num]; + bufI16 = new Int16[num]; + bufUI32 = new UInt32[num]; + bufI32 = new Int32[num]; + bufd = new Double[num]; + buff = new Single[num]; + _counter = 0; + } + /* + #define SIZE 5 + int32_t bufferaz[SIZE]; + int32_t bufferum[SIZE]; + uint8_t _countaz = 0; + uint8_t _countum = 0; + int32_t filteredAZ(int32_t newVal) + { + bufferaz[_countaz] = newVal; + if ((_countaz < SIZE - 1) && (bufferaz[_countaz] > bufferaz[_countaz + 1])) + { + for (int i = _countaz; i < SIZE - 1; i++) + { + if (bufferaz[i] > bufferaz[i + 1]) + { + int32_t buff = bufferaz[i]; + bufferaz[i] = bufferaz[i + 1]; + bufferaz[i + 1] = buff; + } + } + } + else + { + if ((_countaz > 0) && (bufferaz[_countaz - 1] > bufferaz[_countaz])) + { + for (int i = _countaz; i > 0; i--) + { + if (bufferaz[i] < bufferaz[i - 1]) + { + int32_t buff = bufferaz[i]; + bufferaz[i] = bufferaz[i - 1]; + bufferaz[i - 1] = buff; + } + } + } + } + if (++_countaz >= SIZE) _countaz = 0; + return bufferaz[SIZE / 2]; + } + */ + } + public class GMedian where T : IComparable + + { + private T[] buf; + byte _count = 0; + UInt16 _num = 0; + + public GMedian(UInt16 num) + { + buf = new T[num]; + _count = 0; + _num = num; + } + public T filtered(T newVal) + { + buf[_count] = newVal; + if ((_count < _num - 1) && (buf[_count].CompareTo(buf[_count + 1]) > 0)) { - _head = NextPosition(_head); - _buffer[_head] = toAdd; - if (IsFull) - _tail = NextPosition(_tail); - else - _length++; + for (int i = _count; i < _num - 1; i++) + { + if (buf[i].CompareTo(buf[i + 1]) > 0) + { + T buff = buf[i]; + buf[i] = buf[i + 1]; + buf[i + 1] = buff; + } + } } + else + { + if ((_count > 0) && (buf[_count - 1].CompareTo(buf[_count])) > 0) + { + for (int i = _count; i > 0; i--) + { + if (buf[i].CompareTo(buf[i - 1]) < 0) + { + T buff = buf[i]; + buf[i] = buf[i - 1]; + buf[i - 1] = buff; + } + } + } + } + if (++_count >= _num) _count = 0; + return buf[_num / 2]; } + + /* + #define SIZE 5 + int32_t bufferaz[SIZE]; + int32_t bufferum[SIZE]; + uint8_t _countaz = 0; + uint8_t _countum = 0; + int32_t filteredAZ(int32_t newVal) + { + bufferaz[_countaz] = newVal; + if ((_countaz < SIZE - 1) && (bufferaz[_countaz] > bufferaz[_countaz + 1])) + { + for (int i = _countaz; i < SIZE - 1; i++) + { + if (bufferaz[i] > bufferaz[i + 1]) + { + int32_t buff = bufferaz[i]; + bufferaz[i] = bufferaz[i + 1]; + bufferaz[i + 1] = buff; + } + } + } + else + { + if ((_countaz > 0) && (bufferaz[_countaz - 1] > bufferaz[_countaz])) + { + for (int i = _countaz; i > 0; i--) + { + if (bufferaz[i] < bufferaz[i - 1]) + { + int32_t buff = bufferaz[i]; + bufferaz[i] = bufferaz[i - 1]; + bufferaz[i - 1] = buff; + } + } + } + } + if (++_countaz >= SIZE) _countaz = 0; + return bufferaz[SIZE / 2]; + } + */ } public class SetNet { @@ -804,5 +1057,114 @@ namespace UTIL return adapters; } } + public class GMedian3 + { + public UInt16 filtered(UInt16 value) + { // возвращает фильтрованное значение + bufUI16[_counter] = value; + if (++_counter > 2) + _counter = 0; + return (Math.Max(bufUI16[0], bufUI16[1]) == Math.Max(bufUI16[1], bufUI16[2])) ? Math.Max(bufUI16[0], bufUI16[2]) : Math.Max(bufUI16[1], Math.Min(bufUI16[0], bufUI16[2])); + } + public Int16 filtered(Int16 value) + { // возвращает фильтрованное значение + bufI16[_counter] = value; + if (++_counter > 2) + _counter = 0; + return (Math.Max(bufI16[0], bufI16[1]) == Math.Max(bufI16[1], bufI16[2])) ? Math.Max(bufI16[0], bufI16[2]) : Math.Max(bufI16[1], Math.Min(bufI16[0], bufI16[2])); + } + public UInt32 filtered(UInt32 value) + { // возвращает фильтрованное значение + bufUI32[_counter] = value; + if (++_counter > 2) + _counter = 0; + return (Math.Max(bufUI32[0], bufUI32[1]) == Math.Max(bufUI32[1], bufUI32[2])) ? Math.Max(bufUI32[0], bufUI32[2]) : Math.Max(bufUI32[1], Math.Min(bufUI32[0], bufUI32[2])); + } + public Int32 filtered(Int32 value) + { // возвращает фильтрованное значение + bufI32[_counter] = value; + if (++_counter > 2) + _counter = 0; + return (Math.Max(bufI32[0], bufI32[1]) == Math.Max(bufI32[1], bufI32[2])) ? Math.Max(bufI32[0], bufI32[2]) : Math.Max(bufI32[1], Math.Min(bufI32[0], bufI32[2])); + } + public float filtered(float value) + { // возвращает фильтрованное значение + buff[_counter] = value; + if (++_counter > 2) + _counter = 0; + return (Math.Max(buff[0], buff[1]) == Math.Max(buff[1], buff[2])) ? Math.Max(buff[0], buff[2]) : Math.Max(buff[1], Math.Min(buff[0], buff[2])); + } + public Double filtered(Double value) + { // возвращает фильтрованное значение + bufd[_counter] = value; + if (++_counter > 2) + _counter = 0; + return (Math.Max(bufd[0], bufd[1]) == Math.Max(bufd[1], bufd[2])) ? Math.Max(bufd[0], bufd[2]) : Math.Max(bufd[1], Math.Min(bufd[0], bufd[2])); + } + + UInt16[] bufUI16 = new UInt16[3]; + Int16[] bufI16 = new Int16[3]; + UInt32[] bufUI32 = new UInt32[3]; + Int32[] bufI32 = new Int32[3]; + Double[] bufd = new Double[3]; + Single[] buff = new Single[3]; + Byte _counter = 0; + }; + public class ParParse + { + private List<(bool, String, String, bool, String)> Pars = new List<(bool, string, string, bool, string)>(); + private int _num = 0; + private (bool, string, string, bool, string) nullitem = (false, null, null, false, null); + public int NumofPar { get; } + public (bool, string, string, bool, string) GetPar(UInt16 num) + { + if (Pars[num].Item1) + return Pars[num]; + else + return nullitem; + } + public (bool, string, string, bool, string) GetPar(String par) + { + for (int j = 0; j < _num; j++) + { + if ((par == Pars[j].Item2 || par == Pars[j].Item3) && Pars[j].Item1) + return Pars[j]; + } + return nullitem; + } + public void AddPar(String shrt, String lng, Boolean key) + { + Pars.Add((false, shrt, lng, key, "")); + _num = Pars.Count; + } + public int Parse(String[] arg) + { + int rez = 0; + if (_num == 0) + return -1; + + if (arg.Length == 0) + return -1; + + for (int i = 0; i < arg.Length; i++) + { + for (int j = 0; j < _num; j++) + { + if (arg[i] == Pars[j].Item2 || arg[i] == Pars[j].Item3) + { + (bool, string, string, bool, string) tmp = Pars[j]; + if (Pars[j].Item4) + { + tmp.Item5 = arg[i + 1]; + } + tmp.Item1 = true; + Pars[j] = tmp; + rez++; + } + } + } + return rez; + } + } }