예제 #1
0
        private byte[] GetNullImage()
        {
            if (cachedNullImage is null)
            {
                var rawNullImg = KeyBitmap.FromBgr24Array(1, 1, new byte[] { 0, 0, 0 }).GetScaledVersion(imgSize, imgSize);
                cachedNullImage = EncodeImageToJpg(rawNullImg);
            }

            return(cachedNullImage);
        }
        internal async Task SetBitmapResultsInExpectedOutput8PxTiles(IHardwareInternalInfos hardware)
        {
            // Arrange
            Verifier.Initialize();

            Verifier
            .UseFileNameAsDirectory()
            .UseFileName(hardware.DeviceName)
            ;

            using var context = new StreamDeckHidTestContext(hardware);
            context.Hid.BytesPerLineOutput = 1024;

            // create an image that probably survives JPEG compression
            // by using 8x8 tiles that have the same color and hopefully results in similar JPEGs
            // even when using different JPEG encoders.
            // In a best case scenario I hope this test is resistant against swapping JPEG encoders.

            // we have to create a key bitmap for the exact key size to prevent automatic resizing.

            var keySize = hardware.Keys.KeySize;

            const int tileSize     = 8;
            const int channelCount = 3;

            // make sure the key size is divisible by 8
            (keySize % tileSize).Should().Be(0);

            var tileCnt     = keySize / tileSize;
            var rnd         = new Random(42);
            var colorBuffer = new byte[3];

            (byte R, byte G, byte B) GetNextRndColor()
            {
                rnd.NextBytes(colorBuffer);
                return(colorBuffer[0], colorBuffer[1], colorBuffer[2]);
            }

            var pixelData = new byte[keySize * keySize * channelCount];

            for (int y = 0; y < tileCnt; y++)
            {
                for (int x = 0; x < tileCnt; x++)
                {
                    var(r, g, b) = GetNextRndColor();

                    for (int dy = 0; dy < tileSize; dy++)
                    {
                        for (int dx = 0; dx < tileSize; dx++)
                        {
                            var yOffset = (y * tileSize + dy) * keySize;
                            var xOffset = x * tileSize + dx;
                            var index   = (yOffset + xOffset) * channelCount;

                            pixelData[index]     = b;
                            pixelData[index + 1] = g;
                            pixelData[index + 2] = r;
                        }
                    }
                }
            }

            var colorFullKeyBitmap = KeyBitmap.FromBgr24Array(keySize, keySize, pixelData);

            // Act
            context.Board.SetKeyBitmap(0, colorFullKeyBitmap);

            // Assert
            await Verifier.VerifyAsync(context.Log.ToString());
        }