//orderDither操作 public bool orderDither(int dim) { if (opratedBitmap == null || currBitmap == null) { return(false); } if (dim > 16) { return(false); } if (powerOf2(dim) == false) { return(false); } if (opratedBitmap != null) { opratedBitmap.Dispose(); } opratedBitmap = new Bitmap(currBitmap.Width * dim, currBitmap.Height * dim); setPalette(BitModel.BW); currBitmodel = BitModel.BW; setSize(opratedBitmap.Width, opratedBitmap.Height); bitmapDrawer.setBitmap(opratedBitmap); bitmapDrawer.turnBWImageX(currBitmap, dim); return(true); }
//dither操作 public bool dither(int dim) { if (currBitmap == null) { return(false); } if (dim > 256) { return(false); } if (powerOf2(dim) == false) { return(false); } if (opratedBitmap != null) { opratedBitmap.Dispose(); } opratedBitmap = new Bitmap(currBitmap); setPalette(BitModel.BW); currBitmodel = BitModel.BW; bitmapDrawer.setBitmap(opratedBitmap); bitmapDrawer.turnBWImage1_x(dim); return(true); }
private void setPalette(BitModel bitModel) { bytePalette = null; if (bitModel == BitModel.COLOR_24_32) { setHeadOffset(54); bitInfoHead[14] = 32; bitInfoHead[15] = 0; } else if (bitModel == BitModel.GRAY) { bytePalette = new byte[256 * 4]; int currIndex = 0; for (int i = 0; i < 256; i++) { bytePalette[currIndex] = (byte)i; bytePalette[currIndex + 1] = (byte)i; bytePalette[currIndex + 2] = (byte)i; bytePalette[currIndex + 3] = 0; currIndex += 4; } setHeadOffset(1078); bitInfoHead[14] = 8; bitInfoHead[15] = 0; } else if (bitModel == BitModel.BW) { bytePalette = new byte[8]; for (int i = 0; i < 4; i++) { bytePalette[i] = 0; } for (int i = 4; i < 7; i++) { bytePalette[i] = 255; } bytePalette[7] = 0; setHeadOffset(70); bitInfoHead[14] = 1; bitInfoHead[15] = 0; } }
//增强灰度图像对比度 public bool enhanceGrayImage() { if (currBitmap == null) { return(false); } if (opratedBitmap != null) { opratedBitmap.Dispose(); } opratedBitmap = new Bitmap(currBitmap); setPalette(BitModel.GRAY); currBitmodel = BitModel.GRAY; bitmapDrawer.setBitmap(opratedBitmap); bitmapDrawer.enhanceImage(); return(true); }
//增强彩色对比度 public bool enhanceColorImage() { if (currBitmap == null) { return(false); } if (opratedBitmap != null) { opratedBitmap.Dispose(); } opratedBitmap = new Bitmap(currBitmap); setPalette(BitModel.COLOR_24_32); currBitmodel = BitModel.COLOR_24_32; bitmapDrawer.setBitmap(opratedBitmap); bitmapDrawer.enhanceColorImageYcbcr(); return(true); }
//转为黑白图像 public bool turnBWImage(int threshold) { if (currBitmap == null) { return(false); } if (opratedBitmap != null) { opratedBitmap.Dispose(); } opratedBitmap = new Bitmap(currBitmap); setPalette(BitModel.BW); currBitmodel = BitModel.BW; bitmapDrawer.setBitmap(opratedBitmap); bitmapDrawer.turnBWImage1_1(threshold); return(true); }
public void OnBitModified([NotNull] BitModel obj) { ComputeErrorStatistics(DataModel); }
static void Main(string[] args) { Console.WriteLine("Legen sie die Paket-Konfigurationsdatei (.txt) in das gleiche Verzeichnis wie die EXE und geben sie den Dateinamen an:"); var path = Console.ReadLine(); if (File.Exists(path)) { var text = File.ReadAllText(path); var toRead = text.Replace('\n', ' ').Replace('\t', ' ').Replace('\r', ' '); #region Analog // read Analog Config var config = new List <NetworkPacketModel>(); var startedReading = false; AnalogNetworkPacketModel currentAnalog = null; try { while (true) { if (toRead == "") { throw new ReadParameterException("no ending </ANALOG> tag found"); } else { toRead = toRead.TrimStart(); if (startedReading) { if (toRead.StartsWith("id=")) { int id; var tuple = ReadApastrophContent(toRead.Substring(("id=").Length, toRead.Length - ("id=").Length)); if (int.TryParse(tuple.Item1, out id)) { currentAnalog = new AnalogNetworkPacketModel() { Id = id }; config.Add(currentAnalog); } else { throw new ReadParameterException($"Id has to be integer: {toRead}"); } toRead = tuple.Item2; } else if (toRead.StartsWith("name=")) { var tuple = ReadApastrophContent(toRead.Substring(("name=").Length, toRead.Length - ("name=").Length)); currentAnalog.Name = tuple.Item1; toRead = tuple.Item2; } else if (toRead.StartsWith("unit=")) { var tuple = ReadApastrophContent(toRead.Substring(("unit=").Length, toRead.Length - ("unit=").Length)); currentAnalog.Unit = tuple.Item1; toRead = tuple.Item2; } else if (toRead.StartsWith("</ANALOG>")) { toRead = toRead.Substring("</ANALOG>".Length, toRead.Length - "</ANALOG>".Length); break; } else { throw new ReadParameterException($"unexcepted character: {toRead}"); } } else if (toRead.StartsWith("<ANALOG>")) { startedReading = true; toRead = toRead.Substring("<ANALOG>".Length, toRead.Length - "<ANALOG>".Length); } else { throw new ReadParameterException($"unexcepted character: {toRead}"); } } } } catch (ReadParameterException e) { Console.WriteLine("Fehler bei der Konvertierung:\n" + e); } #endregion #region Digital // read Digital Config startedReading = false; DigitalNetworkPacketModel currentDigital = null; BitModel currentBit = null; var lastAnalogId = config[config.Count - 1].Id; try { if (toRead != "") { while (true) { if (toRead == "") { throw new ReadParameterException("no ending </DIGITAL> tag found"); } else { toRead = toRead.TrimStart(); if (startedReading) { if (toRead.StartsWith("id=")) { int id; var tuple = ReadApastrophContent(toRead.Substring(("id=").Length, toRead.Length - ("id=").Length)); if (int.TryParse(tuple.Item1, out id)) { id += lastAnalogId + 1; if (currentDigital != null && currentDigital.Id == id) { currentBit = new BitModel(); currentDigital.Bits.Add(currentBit); } else { currentDigital = new DigitalNetworkPacketModel() { Id = id }; config.Add(currentDigital); currentBit = new BitModel(); currentDigital.Bits.Add(currentBit); } } else { throw new ReadParameterException($"Id has to be integer: {toRead}"); } toRead = tuple.Item2; } else if (toRead.StartsWith("name=")) { var tuple = ReadApastrophContent(toRead.Substring(("name=").Length, toRead.Length - ("name=").Length)); currentBit.Name = tuple.Item1; toRead = tuple.Item2; } else if (toRead.StartsWith("bit=")) { int bit; var tuple = ReadApastrophContent(toRead.Substring(("bit=").Length, toRead.Length - ("bit=").Length)); if (int.TryParse(tuple.Item1, out bit)) { currentBit.Bit = bit; } else { throw new ReadParameterException($"Bit has to be integer: {toRead}"); } toRead = tuple.Item2; } else if (toRead.StartsWith("</DIGITAL>")) { toRead = toRead.Substring("</DIGITAL>".Length, toRead.Length - "</DIGITAL>".Length); break; } else { throw new ReadParameterException($"unexcepted character: {toRead}"); } } else if (toRead.StartsWith("<DIGITAL>")) { startedReading = true; toRead = toRead.Substring("<DIGITAL>".Length, toRead.Length - "<DIGITAL>".Length); } else { throw new ReadParameterException($"unexcepted character: {toRead}"); } } } } } catch (ReadParameterException e) { Console.WriteLine("Fehler bei der Konvertierung:\n" + e); } #endregion // sort and remove duplicates config.Sort(new IdComparer()); var lastId = -1; var i = 0; while (i < config.Count) { if (config[i].Id != lastId) { if (config[i].GetType() == typeof(DigitalNetworkPacketModel)) { var digital = config[i] as DigitalNetworkPacketModel; digital.Bits.Sort(new BitComparer()); var lastBit = -1; var f = 0; while (f < digital.Bits.Count) { if (digital.Bits[f].Bit != lastBit) { lastBit = digital.Bits[f].Bit; f++; } else { digital.Bits.RemoveAt(f); } } } lastId = config[i].Id; i++; } else { config.RemoveAt(i); } } // write back to file var json = JsonConvert.SerializeObject(config, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, Formatting = Formatting.Indented }); File.WriteAllText(OUTPUT_FILE_NAME, json); Console.WriteLine($"Erfolgreich. Konfiguration in {OUTPUT_FILE_NAME} gespeichert"); } else { Console.WriteLine("Datei nicht vorhanden"); } Console.WriteLine("Beliebige Taste drücken um Anwendung zu beenden"); Console.ReadKey(); }
//读取bmp文件函数 public bool getBitmap(string path) { //判断文件存在 if (File.Exists(path) == false) { return(false); } using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { int readBytesNum = 0; //读取文件头 int readNum = 0; readNum = fileStream.Read(fileHead, 0, fileHead.Length); if (readNum != 14) { return(false); } readBytesNum += 14; //读取位图信息头 readNum = fileStream.Read(bitInfoHead, 0, bitInfoHead.Length); if (readNum != 40) { return(false); } readBytesNum += 40; //读取文件类型 if (fileHead[0] != 'B' || fileHead[1] != 'M') { return(false); } //读取到图像数据的偏移 int headOffset = 0; for (int i = 10; i < 14; i++) { headOffset += fileHead[i] << ((i - 10) * 8); } //读取宽度 int width = 0; for (int i = 4; i < 8; i++) { width += (bitInfoHead[i]) << ((i - 4) * 8); } //读取高度 int height = 0; for (int i = 8; i < 12; i++) { height += (bitInfoHead[i]) << ((i - 12) * 8); } //读取位深 int bitCount = 0; bitCount += (bitInfoHead[15] << 8) + bitInfoHead[14]; //行容量 int rawByteCount = 0; //申请调色板 if (bitCount == 1 || bitCount == 4 || bitCount == 8) { palette = new Color[1 << bitCount]; bytePalette = new byte[palette.Length * 4]; readNum = fileStream.Read(bytePalette, 0, bytePalette.Length); if (readNum != bytePalette.Length) { return(false); } readBytesNum += bytePalette.Length; for (int i = 0; i < palette.Length; i++) { int cellOffset = i * 4; palette[i] = Color.FromArgb(bytePalette[cellOffset + 2] , bytePalette[cellOffset + 1] , bytePalette[cellOffset]); } //计算行容量 int byteCount = (bitCount * width + (8 - bitCount)) / 8; rawByteCount = ((byteCount + 3) / 4) * 4; } else if (bitCount == 24) { int byteCount = 3 * width; rawByteCount = ((byteCount + 3) / 4) * 4; } else if (bitCount == 32) { int byteCount = width * 4; rawByteCount = byteCount; } else { return(false); } if (currBitmap != null) { currBitmap.Dispose(); } currBitmap = new Bitmap(width, height); //读取图像数据 byte[] rawByte = new byte[rawByteCount]; //位深为1 if (bitCount == 1) { int cellCount = (width) / 8; int bitLeft = width % 8; for (int j = height - 1; j >= 0; j--) { //读取一行 readNum = fileStream.Read(rawByte, 0, rawByteCount); readBytesNum += readNum; if (rawByteCount != readNum) { return(false); } //当前x int i = 0; for (int cellIndex = 0; cellIndex < cellCount; cellIndex++) { byte currByte = rawByte[cellIndex]; for (int n = 7; n >= 0 && i < width; n--) { currBitmap.SetPixel(i, j, palette[(currByte >> n) & 1]); i++; } } //剩余bit if (i < width) { byte currByte = rawByte[cellCount]; for (int n = 7; n >= (8 - bitLeft); n--) { currBitmap.SetPixel(i, j, palette[(currByte >> n) & 1]); i++; } } } currBitmodel = BitModel.BW; } //位深为4 else if (bitCount == 4) { int cellCount = (width) / 2; int bitLeft = width % 2; for (int j = height - 1; j >= 0; j--) { //读取一行 readNum = fileStream.Read(rawByte, 0, rawByteCount); readBytesNum += readNum; if (rawByteCount != readNum) { return(false); } //当前x int i = 0; for (int cellIndex = 0; cellIndex < cellCount; cellCount++) { byte currByte = rawByte[cellIndex]; for (int n = 4; n >= 0; n -= 4) { currBitmap.SetPixel(i, j, palette[(currByte >> n) & 0xF]); i++; } } //剩余bit if (i < width) { byte currByte = rawByte[cellCount]; for (int n = 4; n >= (2 - bitLeft); n -= 4) { currBitmap.SetPixel(i, j, palette[(currByte >> n) & 0xF]); i++; } } } currBitmodel = BitModel.COLOR_4; } //位深为8 else if (bitCount == 8) { for (int j = height - 1; j >= 0; j--) { //读取一行 readNum = fileStream.Read(rawByte, 0, rawByteCount); readBytesNum += readNum; if (rawByteCount != readNum) { return(false); } for (int i = 0; i < width; i++) { currBitmap.SetPixel(i, j, palette[rawByte[i]]); } } currBitmodel = BitModel.COLOR_8; } //位深为24或32 else if (bitCount == 32 || bitCount == 24) { int step = bitCount / 8; for (int j = height - 1; j >= 0; j--) { //读取一行 readNum = fileStream.Read(rawByte, 0, rawByteCount); readBytesNum += readNum; if (rawByteCount != readNum) { return(false); } int i = 0; int currIndex = 0; for (; i < width; i++) { currBitmap.SetPixel(i, j, Color.FromArgb(rawByte[currIndex + 2], rawByte[currIndex + 1], rawByte[currIndex])); currIndex += step; } } } currBitmodel = BitModel.COLOR_24_32; } if (opratedBitmap != null) { opratedBitmap.Dispose(); } opratedBitmap = new Bitmap(currBitmap); return(true); }