public void AllEqualityMethodsBehaveTheSameWay()
        {
            var key1     = new KeyBitmap(1, 1, new byte[3]);
            var key2Data = new byte[3] {
                1, 2, 3
            };
            var key2 = new KeyBitmap(1, 1, key2Data);
            var key3 = new KeyBitmap(1, 1, new byte[3]);

            var equalityMethods = new List <Func <KeyBitmap, KeyBitmap, bool> >()
            {
                KeyBitmap.Equals,
                (a, b) => a == b,
                (a, b) => !(a != b),
                (a, b) => a.Equals(b),
            };

            foreach (var eq in equalityMethods)
            {
                eq(key1, key3).Should().BeTrue();
                eq(key3, key1).Should().BeTrue();
                eq(key1, key2).Should().BeFalse();
                eq(key2, key1).Should().BeFalse();
            }
        }
Ejemplo n.º 2
0
        public byte[] GeneratePayload(KeyBitmap keyBitmap)
        {
            var rawData = keyBitmap.GetScaledVersion(imgWidth, imgWidth);
            var bmp     = new byte[imgWidth * imgWidth * colorChannels + bmpHeader.Length];

            Array.Copy(bmpHeader, 0, bmp, 0, bmpHeader.Length);

            if (rawData != null)
            {
                for (int y = 0; y < imgWidth; y++)
                {
                    for (int x = 0; x < imgWidth; x++)
                    {
                        var src = (y * imgWidth + x) * colorChannels;
                        var tar = ((imgWidth - x - 1) * imgWidth + y) * colorChannels + bmpHeader.Length;

                        bmp[tar + 0] = rawData[src + 0];
                        bmp[tar + 1] = rawData[src + 1];
                        bmp[tar + 2] = rawData[src + 2];
                    }
                }
            }

            return(bmp);
        }
 /// <summary>
 /// Sets a background image for all keys
 /// </summary>
 /// <param name="board"></param>
 /// <param name="bitmap"></param>
 public static void SetKeyBitmap(this IMacroBoard board, KeyBitmap bitmap)
 {
     for (int i = 0; i < board.Keys.Count; i++)
     {
         board.SetKeyBitmap(i, bitmap);
     }
 }
Ejemplo n.º 4
0
        static void ExampleWithSystemDrawing(IStreamDeck deck)
        {
            //Create a key with lambda graphics
            var key = KeyBitmap.FromGraphics(g =>
            {
                //See https://stackoverflow.com/questions/6311545/c-sharp-write-text-on-bitmap for details
                g.SmoothingMode     = SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                //Fill background black
                g.FillRectangle(Brushes.Black, 0, 0, deck.IconSize, deck.IconSize);

                //Write text to graphics
                var f = new Font("Arial", 13);
                g.DrawString("Drawing", f, Brushes.White, new PointF(5, 20));

                //Draw other stuff to image
                //g.DrawImage
                //g.DrawLine
            });

            deck.SetKeyBitmap(7, key);
        }
 /// <summary>
 /// Sets a background image for all keys
 /// </summary>
 /// <param name="deck"></param>
 /// <param name="bitmap"></param>
 public static void SetKeyBitmap(this IStreamDeck deck, KeyBitmap bitmap)
 {
     for (int i = 0; i < deck.KeyCount; i++)
     {
         deck.SetKeyBitmap(i, bitmap);
     }
 }
        public byte[] GeneratePayload(KeyBitmap keyBitmap)
        {
            var rawData = keyBitmap.GetScaledVersion(ImgWidth, ImgWidth);
            var bmp     = new byte[ImgWidth * ImgWidth * ColorChannels + BmpHeader.Length];

            Array.Copy(BmpHeader, 0, bmp, 0, BmpHeader.Length);

            if (rawData != null)
            {
                for (var y = 0; y < ImgWidth; y++)
                {
                    for (var x = 0; x < ImgWidth; x++)
                    {
                        var src = (y * ImgWidth + x) * ColorChannels;
                        var tar = ((ImgWidth - x - 1) * ImgWidth + y) * ColorChannels + BmpHeader.Length;

                        bmp[tar + 0] = rawData[src + 0];
                        bmp[tar + 1] = rawData[src + 1];
                        bmp[tar + 2] = rawData[src + 2];
                    }
                }
            }

            return(bmp);
        }
        public static ReadOnlySpan <byte> GetScaledVersion(this KeyBitmap keyBitmap, int width, int height)
        {
            IKeyBitmapDataAccess keyDataAccess = keyBitmap;

            if (keyDataAccess.IsEmpty)
            {
                // default span is of length 0 (the caller has to check this special case)
                return(default);
        public void EqualsReturnsFalseIfOnlyOneElementIsNull()
        {
            var key = new KeyBitmap(4, 4, null);

            KeyBitmap.Equals(null, null).Should().BeTrue();
            KeyBitmap.Equals(null, key).Should().BeFalse();
            KeyBitmap.Equals(key, null).Should().BeFalse();
        }
        public void EqualsReturnsFalseIfDataDoesNotMatch()
        {
            var key1 = new KeyBitmap(1, 1, null);
            var key2 = new KeyBitmap(1, 1, new byte[3]);

            key1.Should().NotBe(key2);
            key2.Should().NotBe(key1);
        }
Ejemplo n.º 10
0
        public override void SetKeyBitmap(int keyId, KeyBitmap bitmapData)
        {
            VerifyNotDisposed();
            keyId = hardwareInformation.ExtKeyIdToHardwareKeyId(keyId);

            var payload = cacheKeyBitmaps.GetValue(bitmapData, hardwareInformation.GeneratePayload);

            imageQueue.Add(keyId, payload);
        }
 public async Task ShowImage(int keyID, string imageFilePath)
 {
     await this.ConnectionWrapper((deck) =>
     {
         KeyBitmap bitmap = KeyBitmap.FromFile(imageFilePath);
         deck.SetKeyBitmap(keyID, bitmap);
         return(Task.FromResult(0));
     });
 }
 public async Task ShowColor(int keyID, byte r, byte g, byte b)
 {
     await this.ConnectionWrapper((deck) =>
     {
         KeyBitmap bitmap = KeyBitmap.FromRGBColor(r, g, b);
         deck.SetKeyBitmap(keyID, bitmap);
         return(Task.FromResult(0));
     });
 }
 private static void initializeIconBitmaps()
 {
     restartIcon = IconLoader.LoadIconByName("restart.png", true);
     for (int i = 0; i < iconsActive.Length; i++)
     {
         var name = $"card{i}.png";
         iconsActive[i]   = IconLoader.LoadIconByName(name, true);
         iconsInactive[i] = IconLoader.LoadIconByName(name, false);
     }
 }
        public void RgbFactoryShouldCreateANullDataElementForBlack()
        {
            var expectation = new KeyBitmap(1, 1, null);
            var wrongResult = new KeyBitmap(1, 1, new byte[] { 0, 0, 0 });

            var key = KeyBitmap.Create.FromRgb(0, 0, 0);

            key.Should().Be(expectation);
            key.Should().NotBe(wrongResult);
        }
        private byte[] GetNullImage()
        {
            if (cachedNullImage is null)
            {
                var rawNullImg = new KeyBitmap(1, 1, new byte[] { 0, 0, 0 }).GetScaledVersion(imgSize, imgSize);
                cachedNullImage = EncodeImageToJpg(rawNullImg);
            }

            return(cachedNullImage);
        }
        public void RgbFactoryShouldCreateASinglePixelKeyBitmap()
        {
            byte red   = 100;
            byte green = 200;
            byte blue  = 0;

            var expectation = new KeyBitmap(1, 1, new byte[] { blue, green, red });
            var key         = KeyBitmap.Create.FromRgb(red, green, blue);

            key.Should().Be(expectation);
        }
Ejemplo n.º 17
0
        private static KeyBitmap GetGrayImage(byte b)
        {
            var raw = new byte[72 * 72 * 3];

            for (int i = 0; i < raw.Length; i++)
            {
                raw[i] = b;
            }

            return(KeyBitmap.FromRawBitmap(raw));
        }
Ejemplo n.º 18
0
        public virtual void SetKeyBitmap(int keyId, KeyBitmap bitmapData)
        {
            keyId = HardwareInfo.ExtKeyIdToHardwareKeyId(keyId);

            var payload = HardwareInfo.GeneratePayload(bitmapData);

            foreach (var report in OutputReportSplitter.Split(payload, Buffer, HardwareInfo.ReportSize, HardwareInfo.HeaderSize, keyId, HardwareInfo.PrepareDataForTransmittion))
            {
                DeckHid.WriteReport(report);
            }
        }
        public byte[] GeneratePayload(KeyBitmap keyBitmap)
        {
            var rawData = keyBitmap.GetScaledVersion(imgSize, imgSize);

            if (rawData is null)
            {
                return(GetNullImage());
            }

            return(EncodeImageToJpg(rawData));
        }
Ejemplo n.º 20
0
        public void KeyBitmapsWithDifferentBgrValuesAreNotEqual()
        {
            var key1 = new KeyBitmap(1, 1, new byte[3]);

            var key2Data = new byte[3] {
                1, 2, 3
            };
            var key2 = new KeyBitmap(1, 1, key2Data);

            key1.Should().NotBe(key2);
            key2.Should().NotBe(key1);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Sets a background image for all keys
        /// </summary>
        /// <param name="board"></param>
        /// <param name="bitmap"></param>
        public static void SetKeyBitmap(this IMacroBoard board, KeyBitmap bitmap)
        {
            if (board is null)
            {
                throw new System.ArgumentNullException(nameof(board));
            }

            for (var i = 0; i < board.Keys.Count; i++)
            {
                board.SetKeyBitmap(i, bitmap);
            }
        }
Ejemplo n.º 22
0
        protected override void DrawBitmap()
        {
            if (_bitmap == null || RefreshBitmap)
            {
                _bitmap       = StreamDeckPanel.Validate(_imageFile);
                RefreshBitmap = false;
            }

            if (_keyBitmap == null)
            {
                _keyBitmap = KeyBitmap.Create.FromBitmap(_bitmap);
            }
        }
Ejemplo n.º 23
0
        public virtual void SetKeyBitmap(int keyId, KeyBitmap bitmapData)
        {
            keyId = hardwareInformation.ExtKeyIdToHardwareKeyId(keyId);

            var payload = hardwareInformation.GeneratePayload(bitmapData);

            reportGenerator.Initialize(payload, keyId);

            while (reportGenerator.HasNextReport)
            {
                deckHid.WriteReport(reportGenerator.GetNextReport());
            }
        }
Ejemplo n.º 24
0
        private static KeyBitmap GetStripeImage(int pos)
        {
            var raw = new byte[72 * 72 * 3];

            for (int y = 0; y < 72; y++)
            {
                var p = (y * 72 + pos) * 3;
                raw[p + 0] = 255;
                raw[p + 1] = 255;
                raw[p + 2] = 255;
            }
            return(KeyBitmap.FromRawBitmap(raw));
        }
Ejemplo n.º 25
0
        protected ScreenBase(ScreenManager screenManager, KeyBitmap defaultKeyBitmap)
        {
            this.ScreenManager = screenManager ?? throw new ArgumentNullException(nameof(screenManager));

            for (var column = 0; column < 5; column++)
            {
                for (var row = 0; row < 3; row++)
                {
                    this._keyBitmaps[column, row] = defaultKeyBitmap ?? throw new ArgumentNullException(nameof(defaultKeyBitmap));
                }
            }
            ;
        }
Ejemplo n.º 26
0
        public void EqualsReturnsFalseIfHeightIsDifferent()
        {
            var key1 = new KeyBitmap(4, 4, null);
            var key2 = new KeyBitmap(4, 3, null);
            var key3 = new KeyBitmap(4, 4, null);

            key1.Should().NotBe(key2);
            key1.Should().NotBeSameAs(key2);

            key1.Should().Be(key3);
            key1.Should().NotBeSameAs(key3);

            KeyBitmap.Equals(key1, key3).Should().BeTrue();
        }
Ejemplo n.º 27
0
        private void InitDeck(IStreamDeck deck)
        {
            deck.SetBrightness(100);
            deck.ClearKeys();
            var bitmap = KeyBitmap.FromFile(@"Resources\drum.png");

            deck.SetKeyBitmap(0, bitmap);
            var bitmapSad = KeyBitmap.FromFile(@"Resources\trumpet.png");

            deck.SetKeyBitmap(1, bitmapSad);
            var bitmapSnip = KeyBitmap.FromFile(@"Resources\snipping.png");

            deck.SetKeyBitmap(4, bitmapSnip);
        }
Ejemplo n.º 28
0
        private static void Deck_KeyPressed(object sender, KeyEventArgs e)
        {
            var d = sender as Client;

            if (d == null)
            {
                return;
            }

            if (e.IsDown)
            {
                rnd.NextBytes(rgbBuffer);
                var randomColor = KeyBitmap.FromRGBColor(rgbBuffer[0], rgbBuffer[1], rgbBuffer[2]);
                d.SetKeyBitmap(e.Key, randomColor);
            }
        }
Ejemplo n.º 29
0
        public static byte[] GetScaledVersion(this KeyBitmap keyBitmap, int width, int height)
        {
            IKeyBitmapDataAccess keyDataAccess = keyBitmap;

            if (keyDataAccess.IsNull)
            {
                return(null);
            }

            if (keyBitmap.Width == width && keyBitmap.Height == height)
            {
                return(keyDataAccess.CopyData());
            }

            var destRect = new Rectangle(0, 0, width, height);

            using (var scaledBmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
                using (var sourceBmp = keyDataAccess.GetBitmap())
                {
                    using (var g = Graphics.FromImage(scaledBmp))
                    {
                        g.CompositingMode    = CompositingMode.SourceCopy;
                        g.CompositingQuality = CompositingQuality.HighQuality;
                        g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                        g.SmoothingMode      = SmoothingMode.HighQuality;
                        g.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                        using (var wrapMode = new ImageAttributes())
                        {
                            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                            g.DrawImage(sourceBmp, destRect, 0, 0, sourceBmp.Width, sourceBmp.Height, GraphicsUnit.Pixel, wrapMode);
                        }
                    }

                    var bmpData = scaledBmp.LockBits(destRect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                    try
                    {
                        var dataArray = new byte[width * height * 3];
                        Marshal.Copy(bmpData.Scan0, dataArray, 0, dataArray.Length);
                        return(dataArray);
                    }
                    finally
                    {
                        scaledBmp.UnlockBits(bmpData);
                    }
                }
        }
Ejemplo n.º 30
0
        public void HashCodesDontMatchEasilyForDifferentObjects()
        {
            var key1     = new KeyBitmap(1, 1, new byte[3]);
            var key2Data = new byte[3] {
                1, 2, 3
            };
            var key2 = new KeyBitmap(1, 1, key2Data);
            var key3 = new KeyBitmap(1, 1, null);
            var key4 = new KeyBitmap(100, 100, new byte[100 * 100 * 3]);

            var hash1 = key1.GetHashCode();
            var hash2 = key2.GetHashCode();
            var hash3 = key3.GetHashCode();
            var hash4 = key4.GetHashCode();

            hash1.Should().NotBe(hash2);
            hash1.Should().NotBe(hash3);
            hash1.Should().NotBe(hash4);
        }