using System.Text; using System.Text.RegularExpressions; namespace Inspectron.Epson.SOAP { public static class Utils { public static string GetEnumAttr(string name, string value, Regex regex) { if (!regex.IsMatch(value)) { throw new ArgumentException($"Parameter \"{name}\" is invalid"); } return $" {name}=\"{value}\""; } public static string GetBoolAttr(string name, string value) { return $" {name}=\"{!string.IsNullOrEmpty(value)}\""; } public static string GetIntAttr(string name, int value, int min, int max) { if (value < min || value > max) { throw new ArgumentException($"Parameter \"{name}\" is invalid"); } return $" {name}=\"{value}\""; } public static string GetUByteAttr(string name, int value) { return GetIntAttr(name, value, 0, 255); } public static string GetUShortAttr(string name, int value) { return GetIntAttr(name, value, 0, 65535); } public static string GetShortAttr(string name, int value) { return GetIntAttr(name, value, -32768, 32767); } public static string ToHexBinary(string s) { var result = new StringBuilder(s.Length * 2); foreach (char c in s) { result.Append(((int)c).ToString("x2")); } return result.ToString(); } public static string EscapeMarkup(string s) { if (string.IsNullOrEmpty(s)) return s; var markup = new Regex(@"[<>&'""\t\n\r]"); if (markup.IsMatch(s)) { s = markup.Replace(s, match => { return match.Value switch { "<" => "<", ">" => ">", "&" => "&", "'" => "'", "\"" => """, "\t" => " ", "\n" => " ", "\r" => " ", _ => match.Value }; }); } return s; } public static string EscapeControl(string s) { if (string.IsNullOrEmpty(s)) return s; var control = new Regex(@"[\\\x00-\x1f\x7f-\xff]"); if (control.IsMatch(s)) { s = control.Replace(s, match => { char c = match.Value[0]; if (c == '\\') return "\\\\"; else return $"\\x{((int)c):x2}"; }); } return s; } public static string ToMonoImage(ImageData imgdata, int s, double g) { var x = new Func(code => (char)code); var m8 = new int[8][] { new int[] { 2, 130, 34, 162, 10, 138, 42, 170 }, new int[] { 194, 66, 226, 98, 202, 74, 234, 106 }, new int[] { 50, 178, 18, 146, 58, 186, 26, 154 }, new int[] { 242, 114, 210, 82, 250, 122, 218, 90 }, new int[] { 14, 142, 46, 174, 6, 134, 38, 166 }, new int[] { 206, 78, 238, 110, 198, 70, 230, 102 }, new int[] { 62, 190, 30, 158, 54, 182, 22, 150 }, new int[] { 254, 126, 222, 94, 246, 118, 214, 86 } }; var d = imgdata.Data; var w = imgdata.Width; var h = imgdata.Height; var r = new StringBuilder(((w + 7) >> 3) * h); int n = 0; int p = 0; int t = 128; var e = new int[w]; for (int j = 0; j < h; j++) { int e1 = 0; int e2 = 0; int i = 0; while (i < w) { int b = i & 7; if (s == 0) { t = m8[j & 7][b]; } int v = (int)(Math.Pow( (((d[p++] * 0.29891 + d[p++] * 0.58661 + d[p++] * 0.11448) * d[p]) / 255 + 255 - d[p++]) / 255, 1 / g) * 255); if (s == 1) { v += (e[i] + e1) >> 4; int f = v - (v < t ? 0 : 255); if (i > 0) { e[i - 1] += f; } e[i] = f * 7 + e2; e1 = f * 5; e2 = f * 3; } if (v < t) { n |= 128 >> b; } i++; if (b == 7 || i == w) { r.Append(x(n == 16 ? 32 : n)); n = 0; } } } return r.ToString(); } public static string ToGrayImage(int[] data, int width, int height, double g) { var x = new Func(code => (char)code); var m4 = new int[4][] { new int[] { 0, 9, 2, 11 }, new int[] { 13, 4, 15, 6 }, new int[] { 3, 12, 1, 10 }, new int[] { 16, 7, 14, 5 } }; var thermal = new int[] { 0, 7, 13, 19, 23, 27, 31, 35, 40, 44, 49, 52, 54, 55, 57, 59, 61, 62, 64, 66, 67, 69, 70, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 83, 84, 85, 86, 86, 87, 88, 88, 89, 90, 90, 91, 91, 92, 93, 93, 94, 94, 95, 96, 96, 97, 98, 98, 99, 99, 100, 101, 101, 102, 102, 103, 103, 104, 104, 105, 105, 106, 106, 107, 107, 108, 108, 109, 109, 110, 110, 111, 111, 112, 112, 112, 113, 113, 114, 114, 115, 115, 116, 116, 117, 117, 118, 118, 119, 119, 120, 120, 120, 121, 121, 122, 122, 123, 123, 123, 124, 124, 125, 125, 125, 126, 126, 127, 127, 127, 128, 128, 129, 129, 130, 130, 130, 131, 131, 132, 132, 132, 133, 133, 134, 134, 135, 135, 135, 136, 136, 137, 137, 137, 138, 138, 139, 139, 139, 140, 140, 141, 141, 141, 142, 142, 143, 143, 143, 144, 144, 145, 145, 146, 146, 146, 147, 147, 148, 148, 148, 149, 149, 150, 150, 150, 151, 151, 152, 152, 152, 153, 153, 154, 154, 155, 155, 155, 156, 156, 157, 157, 158, 158, 159, 159, 160, 160, 161, 161, 161, 162, 162, 163, 163, 164, 164, 165, 165, 166, 166, 166, 167, 167, 168, 168, 169, 169, 170, 170, 171, 171, 172, 173, 173, 174, 175, 175, 176, 177, 178, 178, 179, 180, 180, 181, 182, 182, 183, 184, 184, 185, 186, 186, 187, 189, 191, 193, 195, 198, 200, 202, 255 }; var r = new StringBuilder(((width + 1) >> 1) * height); int n = 0; int p = 0; for (int j = 0; j < height; j++) { int i = 0; while (i < width) { int b = i & 1; int v = thermal[(int)(Math.Pow( (((data[p++] * 0.29891 + data[p++] * 0.58661 + data[p++] * 0.11448) * data[p]) / 255 + 255 - data[p++]) / 255, 1 / g) * 255)]; int v1 = v / 17; if (m4[j & 3][i & 3] < v % 17) { v1++; } n |= v1 << ((1 - b) << 2); i++; if (b == 1 || i == width) { r.Append(x(n)); n = 0; } } } return r.ToString(); } } public class ImageData { public int[] Data { get; set; } public int Width { get; set; } public int Height { get; set; } } }