SaturateCast.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. namespace OpenCvSharp.Internal.Util;
  2. #pragma warning disable 1591
  3. public static class SaturateCast
  4. {
  5. // ReSharper disable UnusedMember.Global
  6. public static byte ToByte(sbyte v) => (byte)Math.Max((int)v, 0);
  7. public static byte ToByte(ushort v) => (byte)Math.Min(v, (uint)byte.MaxValue);
  8. public static byte ToByte(int v) => (byte)((uint)v <= byte.MaxValue ? v : v > 0 ? byte.MaxValue : 0);
  9. public static byte ToByte(short v) => ToByte((int)v);
  10. public static byte ToByte(uint v) => (byte)Math.Min(v, byte.MaxValue);
  11. public static byte ToByte(float v) { var iv = (int)Math.Round(v); return ToByte(iv); }
  12. public static byte ToByte(double v) { var iv = (long)Math.Round(v); return ToByte(iv); }
  13. public static byte ToByte(long v) => (byte)((ulong)v <= byte.MaxValue ? v : v > 0 ? byte.MaxValue : 0);
  14. public static byte ToByte(ulong v) => (byte)Math.Min(v, byte.MaxValue);
  15. public static sbyte ToSByte(byte v) => (sbyte)Math.Min((int)v, sbyte.MaxValue);
  16. public static sbyte ToSByte(ushort v) => (sbyte)Math.Min(v, (uint)sbyte.MaxValue);
  17. public static sbyte ToSByte(int v) => (sbyte)((uint)(v - sbyte.MinValue) <= byte.MaxValue ? v : v > 0 ? sbyte.MaxValue : sbyte.MinValue);
  18. public static sbyte ToSByte(short v) => ToSByte((int)v);
  19. public static sbyte ToSByte(uint v) => (sbyte)Math.Min(v, sbyte.MaxValue);
  20. public static sbyte ToSByte(float v) { var iv = (int)Math.Round(v); return ToSByte(iv); }
  21. public static sbyte ToSByte(double v) { var iv = (int)Math.Round(v); return ToSByte(iv); }
  22. public static sbyte ToSByte(long v) => (sbyte)((ulong)(v - sbyte.MinValue) <= byte.MaxValue ? v : v > 0 ? sbyte.MaxValue : sbyte.MinValue);
  23. public static sbyte ToSByte(ulong v) => (sbyte)Math.Min(v, (int)sbyte.MaxValue);
  24. public static ushort ToUInt16(sbyte v) => (ushort)Math.Max((int)v, 0);
  25. public static ushort ToUInt16(short v) => (ushort)Math.Max((int)v, 0);
  26. public static ushort ToUInt16(int v) => (ushort)((uint)v <= ushort.MaxValue ? v : v > 0 ? ushort.MaxValue : 0);
  27. public static ushort ToUInt16(uint v) => (ushort)Math.Min(v, ushort.MaxValue);
  28. public static ushort ToUInt16(float v) { var iv = (int)Math.Round(v); return ToUInt16(iv); }
  29. public static ushort ToUInt16(double v) { var iv = (int)Math.Round(v); return ToUInt16(iv); }
  30. public static ushort ToUInt16(long v) => (ushort)((ulong)v <= ushort.MaxValue ? v : v > 0 ? ushort.MaxValue : 0);
  31. public static ushort ToUInt16(ulong v) => (ushort)Math.Min(v, ushort.MaxValue);
  32. public static short ToInt16(ushort v) => (short)Math.Min(v, short.MaxValue);
  33. public static short ToInt16(int v) => (short)((uint)(v - short.MinValue) <= ushort.MaxValue ? v : v > 0 ? short.MaxValue : short.MinValue);
  34. public static short ToInt16(uint v) => (short)Math.Min(v, short.MaxValue);
  35. public static short ToInt16(float v) { var iv = (int)Math.Round(v); return ToInt16(iv); }
  36. public static short ToInt16(double v) { var iv = (int)Math.Round(v); return ToInt16(iv); }
  37. public static short ToInt16(long v) => (short)((ulong)(v - short.MinValue) <= ushort.MaxValue ? v : v > 0 ? short.MaxValue : short.MinValue);
  38. public static short ToInt16(ulong v) => (short)Math.Min(v, (int)short.MaxValue);
  39. public static int ToInt32(uint v) => (int)Math.Min(v, int.MaxValue);
  40. public static int ToInt32(long v) => (int)((ulong)(v - int.MinValue) <= uint.MaxValue ? v : v > 0 ? int.MaxValue : int.MinValue);
  41. public static int ToInt32(ulong v) => (int)Math.Min(v, int.MaxValue);
  42. public static int ToInt32(float v) => (int)Math.Round(v);
  43. public static int ToInt32(double v) => (int)Math.Round(v);
  44. public static uint ToUInt32(sbyte v) => (uint)Math.Max(v, (sbyte)0);
  45. public static uint ToUInt32(short v) => (uint)Math.Max(v, (short)0);
  46. public static uint ToUInt32(int v) => (uint)Math.Max(v, 0);
  47. public static uint ToUInt32(long v) => (uint)((ulong)v <= uint.MaxValue ? v : v > 0 ? uint.MaxValue : 0);
  48. public static uint ToUInt32(ulong v) => (uint)Math.Min(v, uint.MaxValue);
  49. // we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc.
  50. public static uint ToUInt32(float v) => (uint)Math.Round(v);
  51. public static uint ToUInt32(double v) => (uint)Math.Round(v);
  52. public static ulong ToUInt64(sbyte v) => (ulong)Math.Max(v, (sbyte)0);
  53. public static ulong ToUInt64(short v) => (ulong)Math.Max(v, (short)0);
  54. public static ulong ToUInt64(int v) => (ulong)Math.Max(v, 0);
  55. public static ulong ToUInt64(long v) => (ulong)Math.Max(v, 0);
  56. public static long ToInt64(ulong v) => (long)Math.Min(v, long.MaxValue);
  57. }