Exemplo n.º 1
0
        public void PixelBufferResize()
        {
            tlog.Debug(tag, $"PixelBufferResize START");

            var testingTarget = new PixelBuffer(100, 50, PixelFormat.BGR8888);

            Assert.IsNotNull(testingTarget, "Can't create success object PixelData");
            Assert.IsInstanceOf <PixelBuffer>(testingTarget, "Should be an instance of PixelData type.");

            Assert.AreEqual(100, testingTarget.GetWidth(), "Shoule be equal!");
            Assert.AreEqual(50, testingTarget.GetHeight(), "Shoule be equal!");

            testingTarget.Resize(50, 100);
            Assert.AreEqual(50, testingTarget.GetWidth(), "Shoule be equal!");
            Assert.AreEqual(100, testingTarget.GetHeight(), "Shoule be equal!");

            testingTarget.Dispose();
            tlog.Debug(tag, $"PixelBufferResize END (OK)");
        }
Exemplo n.º 2
0
        TextureSet CreateTextureSet(RendererParameters textParameters, List <string> embeddedItems)
        {
            EmbeddedItemInfo[] embeddedItemLayout = new EmbeddedItemInfo[0];
            PixelBuffer        pixelBuffer        = TextUtils.Render(textParameters, ref embeddedItemLayout);

            uint dstWidth  = pixelBuffer.GetWidth();
            uint dstHeight = pixelBuffer.GetHeight();

            int index  = 0;
            int length = embeddedItemLayout.Length;

            for (int i = 0; i < length; i++)
            {
                EmbeddedItemInfo itemLayout = embeddedItemLayout[i];
                int width  = (int)itemLayout.Size.Width;
                int height = (int)itemLayout.Size.Height;

                int x = (int)itemLayout.Position.X;
                int y = (int)itemLayout.Position.Y;

                PixelBuffer itemPixelBuffer = ImageLoading.LoadImageFromFile(embeddedItems[index++]);

                if (itemPixelBuffer == null)
                {
                    continue;
                }

                itemPixelBuffer.Resize((ushort)width, (ushort)height);
                itemPixelBuffer.Rotate(itemLayout.Angle);


                width  = (int)itemPixelBuffer.GetWidth();
                height = (int)itemPixelBuffer.GetHeight();

                PixelFormat itemPixelFormat = itemPixelBuffer.GetPixelFormat();

                // Check if the item is out of the buffer.
                if ((x + width < 0) ||
                    (x > dstWidth) ||
                    (y < 0) ||
                    (y - height > dstHeight))
                {
                    // The embedded item is completely out of the buffer.
                    continue;
                }

                // Crop if it exceeds the boundaries of the destination buffer.
                int layoutX   = 0;
                int layoutY   = 0;
                int cropX     = 0;
                int cropY     = 0;
                int newWidth  = width;
                int newHeight = height;

                bool crop = false;

                if (0 > x)
                {
                    newWidth += x;
                    cropX     = Math.Abs(x);
                    crop      = true;
                }
                else
                {
                    layoutX = x;
                }

                if (cropX + newWidth > dstWidth)
                {
                    crop      = true;
                    newWidth -= (int)((cropX + newWidth) - dstWidth);
                }

                layoutY = y;
                if (0 > layoutY)
                {
                    newHeight += layoutY;
                    cropY      = Math.Abs(layoutY);
                    crop       = true;
                }

                if (cropY + newHeight > dstHeight)
                {
                    crop       = true;
                    newHeight -= (int)((cropY + newHeight) - dstHeight);
                }

                int uiCropX     = cropX;
                int uiCropY     = cropY;
                int uiNewWidth  = newWidth;
                int uiNewHeight = newHeight;

                if (crop)
                {
                    itemPixelBuffer.Crop((ushort)uiCropX, (ushort)uiCropY, (ushort)uiNewWidth, (ushort)uiNewHeight);
                }

                // Blend the item pixel buffer with the text's color according its blending mode.
                if (ColorBlendingMode.Multiply == itemLayout.ColorBlendingMode)
                {
                    PixelBuffer buffer = new PixelBuffer((uint)uiNewWidth, (uint)uiNewHeight, itemPixelFormat);

                    IntPtr bufferIntPtr     = buffer.GetBuffer();
                    IntPtr itemBufferIntPtr = itemPixelBuffer.GetBuffer();

                    uint bytesPerPixel = GetBytesPerPixel(itemPixelFormat);
                    uint size          = (uint)(uiNewWidth * uiNewHeight * bytesPerPixel);


                    unsafe {
                        byte *bufferPtr     = (byte *)bufferIntPtr.ToPointer();
                        byte *itemBufferPtr = (byte *)itemBufferIntPtr.ToPointer();

                        for (uint j = 0; j < size; j += bytesPerPixel)
                        {
                            *(bufferPtr + 0) = (byte)((*(itemBufferPtr + 0u)) * textParameters.TextColor.R);
                            *(bufferPtr + 1) = (byte)((*(itemBufferPtr + 1u)) * textParameters.TextColor.G);
                            *(bufferPtr + 2) = (byte)((*(itemBufferPtr + 2u)) * textParameters.TextColor.B);
                            *(bufferPtr + 3) = (byte)((*(itemBufferPtr + 3u)) * textParameters.TextColor.A);

                            itemBufferPtr += bytesPerPixel;
                            bufferPtr     += bytesPerPixel;
                        }
                    }

                    itemPixelBuffer = buffer;
                }

                TextUtils.UpdateBuffer(itemPixelBuffer, pixelBuffer, (uint)layoutX, (uint)layoutY, true);
            }

            PixelData pixelData = PixelBuffer.Convert(pixelBuffer);

            Texture texture = new Texture(TextureType.TEXTURE_2D,
                                          pixelData.GetPixelFormat(),
                                          pixelData.GetWidth(),
                                          pixelData.GetHeight());

            texture.Upload(pixelData);

            TextureSet textureSet = new TextureSet();

            textureSet.SetTexture(0u, texture);

            return(textureSet);
        }