/// <summary> /// Send a Bitmap as Byte Array to the Device /// </summary> /// <param name="scanLine">Int Pointer to the start of the Bytearray</param> /// <param name="stride">Length of a ScanLine</param> /// <param name="maxX">Max Pixels horizontal</param> /// <param name="maxY">Max Pixels Vertical</param> internal void SendBitmapToDevice(IntPtr scanLine, int stride, int maxX, int maxY) { var inputLine = new byte[stride]; var pixel = new ByteColor(0, 0, 0); for (var y = 0; y < maxY; y++, scanLine += stride) { Marshal.Copy(scanLine, inputLine, 0, inputLine.Length); var xPos = 0; for (var x = 0; x < maxX; x++) { pixel.SetBGR(inputLine[xPos++], inputLine[xPos++], inputLine[xPos++], IsPalletMonochrome); if (ColorBytesPerPixel > 3) { xPos += ColorBytesPerPixel - 3; } DisplayWriter.Write(GetColorIndex(pixel)); } for (var x = maxX; x < Width; x++) { DisplayWriter.WriteBlankPixel(); } } // Write blank lines if image is smaller than display. for (var y = maxY; y < Height; y++) { DisplayWriter.WriteBlankLine(); } DisplayWriter.Finish(); }
/// <summary> /// Floyd-Steinberg Dithering /// </summary> /// <param name="data"></param> /// <param name="previousLine"></param> /// <param name="currentLine"></param> /// <param name="isLastLine"></param> protected void DitherAndWrite(ByteColor[,] data, int previousLine, int currentLine, bool isLastLine) { for (var x = 0; x < Width; x++) { var currentPixel = data[x, previousLine]; var colorNdx = GetColorIndex(currentPixel); var bestColor = SupportedByteColors[colorNdx]; var errorR = currentPixel.R - bestColor.R; var errorG = currentPixel.G - bestColor.G; var errorB = currentPixel.B - bestColor.B; // Add 7/16 of the color difference to the pixel on the right if (x < Width - 1) { AdjustRgb(ref data[x + 1, previousLine], errorR * 7 / 16, errorG * 7 / 16, errorB * 7 / 16); } if (!isLastLine) { // Add 3/16 of the color difference to the pixel below and to the left if (x > 0) { AdjustRgb(ref data[x - 1, currentLine], errorR * 3 / 16, errorG * 3 / 16, errorB * 3 / 16); } // Add 5/16 of the color difference to the pixel directly below AdjustRgb(ref data[x, currentLine], errorR * 5 / 16, errorG * 5 / 16, errorB * 5 / 16); // Add 1/16 of the color difference to the pixel below and to the right if (x < Width - 1) { AdjustRgb(ref data[x + 1, currentLine], errorR / 16, errorG / 16, errorB / 16); } } DisplayWriter.Write(colorNdx); } }