Esempio n. 1
0
        /// <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();
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the index for the supported color closest to a color
        /// </summary>
        /// <param name="color">Color to look up</param>
        /// <returns>Color index of closest supported color</returns>
        public int GetColorIndex(ByteColor color)
        {
            var minDistance = GetColorDistance(color, SupportedByteColors[0]);

            if (minDistance < 1)
            {
                return(0);
            }

            var bestIndex = 0;

            for (var i = 1; i < SupportedByteColors.Length; i++)
            {
                var monochrome  = (color.R == color.G && color.G == color.B);
                var deviceColor = SupportedByteColors[i];
                var distance    = GetColorDistance(color, deviceColor);
                if (distance <= minDistance && deviceColor.IsMonochrome == monochrome)
                {
                    minDistance = distance;
                    bestIndex   = i;
                    if (minDistance < 1)
                    {
                        break;
                    }
                }
            }

            return(bestIndex);
        }
Esempio n. 3
0
        /// <summary>
        /// Adjust RGB values on a color. Clamping the values between 0 and 255.
        /// </summary>
        /// <param name="color">Color to adjust</param>
        /// <param name="valueR">Red value to add</param>
        /// <param name="valueG">Green value to add</param>
        /// <param name="valueB">Blue value to add</param>
        protected static void AdjustRgb(ref ByteColor color, int valueR, int valueG, int valueB)
        {
            var r = ConvertColorIntToByte(color.R + valueR);
            var g = ConvertColorIntToByte(color.G + valueG);
            var b = ConvertColorIntToByte(color.B + valueB);

            color.SetBGR(b, g, r);
        }
Esempio n. 4
0
        /// <summary>
        /// Get a byte on the device with one Color for all pixels
        /// </summary>
        /// <param name="rgb"></param>
        /// <returns></returns>
        private byte GetMergedPixelDataInByte(ByteColor rgb)
        {
            var pixelData           = ColorToByte(rgb);
            var deviceBytesPerPixel = new byte[PixelPerByte];

            for (var i = 0; i < deviceBytesPerPixel.Length; i++)
            {
                deviceBytesPerPixel[i] = pixelData;
            }

            return(MergePixelDataInByte(deviceBytesPerPixel));
        }
Esempio n. 5
0
        /// <summary>
        /// Get a colored scan line on the device
        /// </summary>
        /// <param name="rgb"></param>
        /// <returns></returns>
        public byte[] GetColoredLineOnDevice(ByteColor rgb)
        {
            var devicePixel = GetMergedPixelDataInByte(rgb);

            var outputWidth = Width / PixelPerByte;
            var outputLine  = new byte[outputWidth];

            for (var x = 0; x < outputLine.Length; x++)
            {
                outputLine[x] = devicePixel;
            }

            return(outputLine);
        }
Esempio n. 6
0
        //########################################################################################

        #region Protected Methods

        /// <summary>
        /// Gets the Euclidean distance between two colors
        /// </summary>
        /// <param name="color1">First color to compare</param>
        /// <param name="color2">Second color to compare</param>
        /// <returns>Returns the distance between two colors</returns>
        protected double GetColorDistance(ByteColor color1, ByteColor color2)
        {
            if (IsPalletMonochrome)
            {
                return((color1.R - color2.R) * (color1.R - color2.R));
            }

            var(y1, u1, v1) = GetYuv(color1);
            var(y2, u2, v2) = GetYuv(color2);
            var diffY = y1 - y2;
            var diffU = u1 - u2;
            var diffV = v1 - v2;

            return(diffY * diffY + diffU * diffU + diffV * diffV);
        }
Esempio n. 7
0
 /// <summary>
 /// Calculate YUV color space
 /// </summary>
 /// <param name="color"></param>
 /// <returns></returns>
 private static (double Y, double U, double V) GetYuv(ByteColor color)
 {
     return(color.R * .299000 + color.G * .587000 + color.B * .114000,
            color.R * -.168736 + color.G * -.331264 + color.B * .500000 + 128,
            color.R *  .500000 + color.G * -.418688 + color.B * -.081312 + 128);
 }
Esempio n. 8
0
        /// <summary>
        /// Send a Dithered 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 SendDitheredBitmapToDevice(IntPtr scanLine, int stride, int maxX, int maxY)
        {
            var data         = new ByteColor[Width, 2];
            var currentLine  = 0;
            var previousLine = 1;

            var inputLine = new byte[stride];
            var pixel     = new ByteColor(0, 0, 0);
            var odd       = false;
            var dither    = false;

            for (var y = 0; y < maxY; y++, scanLine += stride)
            {
                if (odd)
                {
                    previousLine = 0;
                    currentLine  = 1;
                    odd          = false;
                    dither       = true;
                }
                else
                {
                    previousLine = 1;
                    currentLine  = 0;
                    odd          = true;
                }

                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;
                    }
                    data[x, currentLine] = pixel;
                }

                for (var x = maxX; x < Width; x++)
                {
                    data[x, currentLine] = ByteColors.White;
                }

                if (dither)
                {
                    DitherAndWrite(data, previousLine, currentLine, false);
                }
            }

            // Finish last line
            DitherAndWrite(data, currentLine, previousLine, true);

            // Write blank lines if image is smaller than display.
            for (var y = maxY; y < Height; y++)
            {
                DisplayWriter.WriteBlankLine();
            }

            DisplayWriter.Finish();
        }
Esempio n. 9
0
 /// <summary>
 /// Convert a pixel to a DataByte
 /// </summary>
 /// <param name="rgb">color byte</param>
 /// <returns>Pixel converted to specific byte value for the hardware</returns>
 protected abstract byte ColorToByte(ByteColor rgb);