Пример #1
0
 public static CanvasBitmap ToWin2D(this Bitmap bmp, ICanvasResourceCreator resourceCreator)
 {
     return(CanvasBitmap.CreateFromBytes(resourceCreator, bmp.Bytes, bmp.Width, bmp.Height, DirectXPixelFormat.B8G8R8A8UIntNormalized));
 }
Пример #2
0
 public Task <object> Create(byte[] imageData, int imageWidth, int imageHeight, int pixelByteCount)
 {
     return(Task.Run(() => {
         return (object)CanvasBitmap.CreateFromBytes(CanvasDevice.GetSharedDevice(), imageData, imageWidth, imageHeight, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, 96);
     }));
 }
Пример #3
0
        public void DrawToSession([NotNull] CanvasControl sender, [NotNull] CanvasDrawingSession g, bool drawGrid = false)
        {
            switch (State)
            {
            case TileState.Locked:
                if (RawImageData != null)
                {
                    g.Clear(Colors.DarkKhaki);     // wrong state
                }
                else
                {
                    g.Clear(Colors.DarkGray);     // which is lighter than 'Gray'
                }
                return;

            case TileState.Corrupted:
                g.Clear(Colors.Red);
                return;

            case TileState.Empty:
                g.Clear(Colors.White);
                if (drawGrid)
                {
                    DrawGrid(g, Color.FromArgb(100, 155, 155, 155));
                }
                if (IsSelected)
                {
                    DrawSelection(g);
                }
                return;

            case TileState.Ready:

                if (RawImageData == null)
                {
                    g.Clear(Colors.Fuchsia);     // Should not happen!
                    return;
                }

                g.Clear(Colors.White);
                try
                {
                    using (var bmp = CanvasBitmap.CreateFromBytes(sender, RawImageData, 256, 256,     // these two should be doubled if we interpolate
                                                                  DirectXPixelFormat.B8G8R8A8UIntNormalized, 96, CanvasAlphaMode.Premultiplied))
                    {
                        g.DrawImage(bmp, new Rect(0, 0, 256, 256));
                        g.Flush();
                        // you'll get "Exception thrown at 0x12F9AF43 (Microsoft.Graphics.Canvas.dll) in SlickUWP.exe: 0xC0000005: Access violation reading location 0x1B2EEF78. occurred"
                        // if you fail to flush before disposing of the bmp
                    }
                }
                catch
                {
                    g.Clear(Colors.DarkOrange);
                }

                if (drawGrid)
                {
                    DrawGrid(g, Color.FromArgb(100, 155, 155, 155));
                }
                if (IsSelected)
                {
                    DrawSelection(g);
                }
                g.Flush();

                return;

            default:
                throw new Exception("Non exhaustive switch in Tile_Draw");
            }
        }
Пример #4
0
        public void Invalidate()
        {
            var animations = _animations;

            if (animations == null || _canvas == null || _bitmaps == null)
            {
                return;
            }

            var index           = _index;
            var framesPerUpdate = _limitFps ? _animationFrameRate < 60 ? 1 : 2 : 1;

            var enqueue = false;

            if (_animationFrameRate < 60 && !_limitFps)
            {
                if (_skipFrame)
                {
                    _skipFrame = false;
                    return;
                }

                _skipFrame = true;
            }

            for (int i = 0; i < animations.Length; i++)
            {
                if (_bitmaps[i] == null || animations[i] == null)
                {
                    if (animations[i] != null)
                    {
                        var buffer = ArrayPool <byte> .Shared.Rent(256 * 256 * 4);

                        _bitmaps[i] = CanvasBitmap.CreateFromBytes(_canvas, buffer, _frameSize.Width, _frameSize.Height, DirectXPixelFormat.B8G8R8A8UIntNormalized);
                        ArrayPool <byte> .Shared.Return(buffer);
                    }
                    else
                    {
                        continue;
                    }
                }

                animations[i].RenderSync(_bitmaps[i], index[i]);

                if (i == 1 && !_isLoopingEnabled[i])
                {
                    IndexChanged?.Invoke(this, index[i]);
                }

                if (_startIndex[i] <= index[1] && index[i] + framesPerUpdate < animations[i].TotalFrame)
                {
                    _index[i] += framesPerUpdate;
                }
                else
                {
                    if (_isLoopingEnabled[i])
                    {
                        _index[i] = 0;

                        if (i == 1 && _value == 0 && _enqueued != 0 && _enqueuedState != null)
                        {
                            enqueue = true;
                        }
                    }
                    else if (i == 1)
                    {
                        lock (_subscribeLock)
                        {
                            _subscribed   = false;
                            _unsubscribe  = true;
                            _thread.Tick -= OnTick;
                        }
                    }
                }
            }

            if (enqueue)
            {
                _subscribed = false;
                _           = Dispatcher.RunIdleAsync(idle => SetValue(_enqueuedState, _enqueued));
            }
        }
Пример #5
0
        public async void ProcessFrame(ProcessVideoFrameContext context)
        {
            // When using SupportedMemoryTypes => MediaMemoryTypes.GpuAndCpu we need to check if we're using GPU or CPU for the frame

            // If we're on GPU, use InputFrame.Direct3DSurface
            if (context.InputFrame.SoftwareBitmap == null)
            {
                using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
                    using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var chromaKeyEffect = new ChromaKeyEffect
                            {
                                Color     = ChromaColor,
                                Source    = inputBitmap,
                                Tolerance = Tolerance,
                                Feather   = true
                            };

                            //only load bg image if it hasnt already been loaded or if image is different
                            if (_backgroundBitmap == null || _lastUsedImageFilePath != _imageFileName)
                            {
                                _backgroundBitmap = await CanvasBitmap.LoadAsync(ds, new Uri($"ms-appdata:///Local/{ImageFileName}"));
                            }

                            _lastUsedImageFilePath = _imageFileName; //keep track of any incoming image changes

                            var compositeEffect = new CompositeEffect
                            {
                                Sources = { _backgroundBitmap, chromaKeyEffect }
                            };

                            ds.DrawImage(compositeEffect);
                        }

                return;
            }

            // If we're on CPU, use InputFrame.SoftwareBitmap
            if (context.InputFrame.Direct3DSurface == null)
            {
                // InputFrame's raw pixels
                byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight];
                context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer());

                using (var inputBitmap = CanvasBitmap.CreateFromBytes(
                           _canvasDevice,
                           inputFrameBytes,
                           context.InputFrame.SoftwareBitmap.PixelWidth,
                           context.InputFrame.SoftwareBitmap.PixelHeight,
                           context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat()))

                    using (var renderTarget = new CanvasRenderTarget(
                               _canvasDevice,
                               context.OutputFrame.SoftwareBitmap.PixelWidth,
                               context.OutputFrame.SoftwareBitmap.PixelHeight,
                               (float)context.OutputFrame.SoftwareBitmap.DpiX,
                               context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(),
                               CanvasAlphaMode.Ignore))
                    {
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var chroma = new ChromaKeyEffect
                            {
                                Color     = ChromaColor,
                                Source    = inputBitmap,
                                Tolerance = Tolerance
                            };

                            //only load bg image if it hasnt already been loaded or if image is different
                            if (_backgroundBitmap == null || _lastUsedImageFilePath != _imageFileName)
                            {
                                _backgroundBitmap = await CanvasBitmap.LoadAsync(ds, new Uri($"ms-appdata:///Local/{ImageFileName}"));
                            }

                            //keep track of any incoming image changes
                            _lastUsedImageFilePath = _imageFileName;

                            var compositeEffect = new CompositeEffect {
                                Mode = CanvasComposite.Add
                            };
                            compositeEffect.Sources.Add(_backgroundBitmap);
                            compositeEffect.Sources.Add(chroma);

                            ds.DrawImage(compositeEffect);
                        }
                    }
            }
        }
        // ** Methods ** //

        // This is run for every video frame passed in the media pipleine (MediaPlayer, MediaCapture, etc)
        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            evaluatableVideoFrame = VideoFrame.CreateWithDirect3D11Surface(context.InputFrame.Direct3DSurface);

            // ********** Draw Bounding Boxes with Win2D ********** //

            // Use Direct3DSurface if using GPU memory
            if (context.InputFrame.Direct3DSurface != null)
            {
                if (modelBindingComplete && options.PreferredDeviceKind != LearningModelDeviceKindPreview.LearningDeviceGpu)
                {
                    options.PreferredDeviceKind = LearningModelDeviceKindPreview.LearningDeviceGpu;
                }

                using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, context.InputFrame.Direct3DSurface))
                    using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(canvasDevice, context.OutputFrame.Direct3DSurface))
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            ds.DrawImage(inputBitmap);

                            foreach (var box in filteredBoxes)
                            {
                                var x = (uint)Math.Max(box.X, 0);
                                var y = (uint)Math.Max(box.Y, 0);
                                var w = (uint)Math.Min(renderTarget.Bounds.Width - x, box.Width);
                                var h = (uint)Math.Min(renderTarget.Bounds.Height - y, box.Height);

                                // Draw the Text 10px above the top of the bounding box
                                ds.DrawText(box.Label, x, y - 10, Colors.Yellow);
                                ds.DrawRectangle(new Rect(x, y, w, h), new CanvasSolidColorBrush(canvasDevice, Colors.Yellow), 2f);
                            }
                        }

                return;
            }

            // Use SoftwareBitmap if using CPU memory
            if (context.InputFrame.SoftwareBitmap != null)
            {
                if (modelBindingComplete && options.PreferredDeviceKind != LearningModelDeviceKindPreview.LearningDeviceCpu)
                {
                    options.PreferredDeviceKind = LearningModelDeviceKindPreview.LearningDeviceCpu;
                }

                // InputFrame's pixels
                byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight];
                context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer());

                using (var inputBitmap = CanvasBitmap.CreateFromBytes(canvasDevice, inputFrameBytes, context.InputFrame.SoftwareBitmap.PixelWidth, context.InputFrame.SoftwareBitmap.PixelHeight, context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat()))
                    using (var renderTarget = new CanvasRenderTarget(canvasDevice, context.OutputFrame.SoftwareBitmap.PixelWidth, context.InputFrame.SoftwareBitmap.PixelHeight, (float)context.OutputFrame.SoftwareBitmap.DpiX, context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(), CanvasAlphaMode.Premultiplied))
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            ds.DrawImage(inputBitmap);

                            foreach (var box in filteredBoxes)
                            {
                                var x = (uint)Math.Max(box.X, 0);
                                var y = (uint)Math.Max(box.Y, 0);
                                var w = (uint)Math.Min(context.OutputFrame.SoftwareBitmap.PixelWidth - x, box.Width);
                                var h = (uint)Math.Min(context.OutputFrame.SoftwareBitmap.PixelHeight - y, box.Height);

                                // Draw the Text 10px above the top of the bounding box
                                ds.DrawText(box.Label, x, y - 10, Colors.Yellow);
                                ds.DrawRectangle(new Rect(x, y, w, h), new CanvasSolidColorBrush(canvasDevice, Colors.Yellow), 2f);
                            }
                        }
            }
        }
        public void TestBitmapPixelFormats()
        {
            using (new DisableDebugLayer())
            {
                var device = new CanvasDevice();

                foreach (var format in AllPixelFormats())
                {
                    // Unknown formats should not be supported.
                    if (!formatFlags.ContainsKey(format))
                    {
                        Assert.IsFalse(device.IsPixelFormatSupported(format));
                        ValidateCannotCreateBitmap(device, format, FirstSupportedAlphaMode(format));
                        continue;
                    }

                    // Optional formats may be legitimately not supported, depending on the device.
                    if (!device.IsPixelFormatSupported(format))
                    {
                        Assert.IsTrue((formatFlags[format] & FormatFlags.Optional) != 0);
                        ValidateCannotCreateBitmap(device, format, FirstSupportedAlphaMode(format));
                        continue;
                    }

                    // We should be able to create this format using all its supported alpha modes.
                    var lotsOfZeroes = new byte[1024];

                    foreach (var alphaMode in SupportedAlphaModes(format))
                    {
                        CanvasBitmap.CreateFromBytes(device, lotsOfZeroes, 4, 4, format, 96, alphaMode);
                    }

                    // Other alpha modes should not be supported.
                    foreach (var alphaMode in UnsupportedAlphaModes(format))
                    {
                        ValidateCannotCreateBitmap(device, format, alphaMode);
                    }

                    // We should also be able to create this format without explicitly specifying an alpha mode.
                    var bitmap = CanvasBitmap.CreateFromBytes(device, lotsOfZeroes, 4, 4, format);

                    Assert.AreEqual(FirstSupportedAlphaMode(format), bitmap.AlphaMode);

                    // Some formats can be drawn directly, while others cannot.
                    if ((formatFlags[format] & FormatFlags.CannotDraw) == 0)
                    {
                        ValidateCanDrawImage(device, bitmap);
                    }
                    else
                    {
                        ValidateCannotDrawImage(device, bitmap);
                    }

                    // But all formats should be drawable when used as effect inputs.
                    ValidateCanDrawImage(device, new ColorMatrixEffect {
                        Source = bitmap
                    });

                    // Make sure we can get and set pixels of this format.
                    var bytes = bitmap.GetPixelBytes();

                    Assert.IsTrue(bytes.All(b => b == 0));

                    var sequence = Enumerable.Range(0, bytes.Length)
                                   .Select(value => (byte)value)
                                   .ToArray();

                    bitmap.SetPixelBytes(sequence);

                    var otherBytes = bitmap.GetPixelBytes();

                    CollectionAssert.AreEqual(sequence, otherBytes);
                }
            }
        }
Пример #8
0
        private void CanvasAnimatedDraw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
        {
            DrawEventCount();

            _nIteration++;
            const float usedDpi = 96.0f;

            if (_restart || _nIteration % 2048 == 0)
            {
                _restart = false;
                CanvasResourcesCreate(null, null);
                _g++;
            }

            bool oneState = true;
            byte theState = 0;

            for (int x = 0, bufferIndex = 0; x < _width; x++)
            {
                for (int y = 0; y < _height; y++, bufferIndex += 4)
                {
                    byte state = (byte)((_next[x, y] = NextStateValue(_current, x, y)) & 0xff);
                    if (x == 0 && y == 0)
                    {
                        theState = state;
                    }
                    else if (theState != state)
                    {
                        oneState = false;
                    }

                    switch (state % 16)
                    {
                    case 0:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 255;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 1:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 255;
                        _buffer[bufferIndex + 2] = 180;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 2:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 255;
                        _buffer[bufferIndex + 2] = 100;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 3:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 255;
                        _buffer[bufferIndex + 2] = 0;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 4:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 180;
                        _buffer[bufferIndex + 2] = 0;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 5:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 100;
                        _buffer[bufferIndex + 2] = 0;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 6:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 0;
                        _buffer[bufferIndex + 2] = 0;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 7:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 0;
                        _buffer[bufferIndex + 2] = 100;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 8:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 0;
                        _buffer[bufferIndex + 2] = 180;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 9:
                        _buffer[bufferIndex + 0] = 255;
                        _buffer[bufferIndex + 1] = 0;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 10:
                        _buffer[bufferIndex + 0] = 180;
                        _buffer[bufferIndex + 1] = 0;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 11:
                        _buffer[bufferIndex + 0] = 100;
                        _buffer[bufferIndex + 1] = 0;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 12:
                        _buffer[bufferIndex + 0] = 0;
                        _buffer[bufferIndex + 1] = 0;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 13:
                        _buffer[bufferIndex + 0] = 0;
                        _buffer[bufferIndex + 1] = 100;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    case 14:
                        _buffer[bufferIndex + 0] = 0;
                        _buffer[bufferIndex + 1] = 180;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;

                    default:
                        _buffer[bufferIndex + 0] = 0;
                        _buffer[bufferIndex + 1] = 255;
                        _buffer[bufferIndex + 2] = 255;
                        _buffer[bufferIndex + 3] = 255;
                        break;
                    }
                }
            }

            if (oneState)
            {
                _restart = true;
            }

            using (var bitmap = CanvasBitmap.CreateFromBytes(sender, _buffer, _width, _height, DirectXPixelFormat.B8G8R8A8UIntNormalized, usedDpi))
            {
                args.DrawingSession.DrawImage(bitmap, new Rect(0, 0, _width, _height), new Rect(0, 0, _width, _height), 1, CanvasImageInterpolation.NearestNeighbor);
            }

            args.DrawingSession.DrawText($"FPS {FramesPerSecond:F2}", new Vector2(10, 10), Colors.Black);
            args.DrawingSession.DrawText("Step " + _nIteration, new Vector2(10, 30), Colors.Black);
            args.DrawingSession.DrawText("G " + _g, new Vector2(10, 50), Colors.Black);

            var tmp = _next;

            _next    = _current;
            _current = tmp;
        }
Пример #9
0
        public async Task OpenFileAsync(StorageFile file)
        {
            CanvasBitmap fileBitmap = null;
            var          extension  = file.FileType;

            switch (extension)
            {
            case ".bin":
                var buffer = await FileIO.ReadBufferAsync(file);

                var width        = 0;
                var height       = 0;
                var format       = DirectXPixelFormat.B8G8R8A8UIntNormalized;
                var dialogResult = await BinaryDetailsInputDialog.ShowAsync();

                if (dialogResult == ContentDialogResult.Primary &&
                    ParseBinaryDetailsSizeBoxes(out width, out height))
                {
                    fileBitmap = CanvasBitmap.CreateFromBytes(_canvasDevice, buffer, width, height, format);
                }
                break;

            default:
                // open it with win2d
                using (var stream = await file.OpenReadAsync())
                {
                    fileBitmap = await CanvasBitmap.LoadAsync(_canvasDevice, stream);
                }
                break;
            }

            if (fileBitmap != null)
            {
                var size = fileBitmap.SizeInPixels;
                var backgroundSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(backgroundSurface))
                {
                    drawingSession.FillRectangle(0, 0, size.Width, size.Height, _backgroundCavnasBrush);
                }

                var imageSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(imageSurface))
                {
                    drawingSession.Clear(Colors.Transparent);
                    drawingSession.DrawImage(fileBitmap);
                }

                _backgroundBrush.Surface = backgroundSurface;
                _imageBrush.Surface      = imageSurface;
                ImageGrid.Width          = size.Width;
                ImageGrid.Height         = size.Height;
                ImageScrollViewer.ChangeView(0, 0, 1, true);
                if (_borderEnabled)
                {
                    ImageBorderBrush.Color = Colors.Black;
                }
                _currentFile = file;
            }
        }
Пример #10
0
        //private bool correctionFlag = false;

        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
                using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                    using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
                        using (var scaleEffect = new ScaleEffect())
                            using (CanvasSolidColorBrush solidColorBrush = new CanvasSolidColorBrush(_canvasDevice, _backgroundColor))
                            {
                                solidColorBrush.Opacity = _backgroundOpacity;
                                double rel = context.InputFrame.RelativeTime.Value.Ticks / (double)TimeSpan.TicksPerMillisecond;

                                //context.OutputFrame.Duration = new TimeSpan( (long)(frameLength * TimeSpan.TicksPerMillisecond));



                                int frameTimeCounter = (int)Math.Round(rel / _frameLength, 0);

                                int[] pitch = new int[_count];
                                int[] yaw   = new int[_count];
                                int[] fov   = new int[_count];

                                for (int i = 0; i < _count; i++)
                                {
                                    try
                                    {
                                        //pitch[i] = this.pitch[ (frameTimeCounter + (int)Math.Round(offset, 0)) * (count) + i];
                                        //fov[i] = this.fov[ (frameTimeCounter + (int)Math.Round(offset, 0)) * (count) + i];
                                        //yaw[i] = this.yaw[ (frameTimeCounter + (int)Math.Round(offset, 0)) * (count) + i];

                                        pitch[i] = this._pitch[(frameTimeCounter + (int)_offset) * (_count) + i];
                                        fov[i]   = this._fov[(frameTimeCounter + (int)_offset) * (_count) + i];
                                        yaw[i]   = this._yaw[(frameTimeCounter + (int)_offset) * (_count) + i];
                                    }
                                    catch (ArgumentOutOfRangeException ex)
                                    {
                                        Debug.WriteLine(ex.Message);
                                        pitch[i] = 0;
                                        fov[i]   = 0;
                                        yaw[i]   = 0;
                                    }
                                }

                                byte[]       tab = Heatmap.GenerateHeatmap(pitch, yaw, fov);
                                CanvasBitmap cb  = CanvasBitmap.CreateFromBytes(_canvasDevice, tab, 64, 64, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, 96, CanvasAlphaMode.Premultiplied);
                                scaleEffect.Source            = cb;
                                scaleEffect.Scale             = new System.Numerics.Vector2((float)_width / 64, (float)_height / 64);
                                scaleEffect.InterpolationMode = CanvasImageInterpolation.Cubic;
                                scaleEffect.BorderMode        = EffectBorderMode.Hard;


                                if (_graysclaleVideoFlag)
                                {
                                    var grayScaleEffect = new GrayscaleEffect
                                    {
                                        BufferPrecision = CanvasBufferPrecision.Precision8UIntNormalized,
                                        CacheOutput     = false,
                                        Source          = inputBitmap
                                    };
                                    ds.DrawImage(grayScaleEffect);
                                }
                                else
                                {
                                    ds.DrawImage(inputBitmap);
                                }

                                ds.DrawImage(scaleEffect, 0, 0, new Windows.Foundation.Rect {
                                    Height = _height, Width = _width
                                }, _heatmapOpacity);



                                if (_generateDots)
                                {
                                    for (int i = 0; i < _count; i++)
                                    {
                                        ds.FillCircle(yaw[i] * _width / 64, pitch[i] * _height / 64, _dotsRadius, _colors[i % 5]);
                                    }
                                }



                                ds.FillRectangle(new Windows.Foundation.Rect {
                                    Height = _height, Width = _width
                                }, solidColorBrush);

                                ds.Flush();
                            }
        }
        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            // If memory type is CPU, the frame is in  InputFrame.SoftwareBitmap.
            // For GPU, the frame is in InputFrame.Direct3DSurface

            if (context.InputFrame.SoftwareBitmap == null)
            {
                using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
                    using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var effect = new EdgeDetectionEffect
                            {
                                Source = inputBitmap,
                                Amount = this.Amount
                            };

                            ds.DrawImage(effect);
                        }

                return;
            }

            // Testing issue with GPU
            if (context.InputFrame.Direct3DSurface == null)
            {
#if DEBUG
                if (!debugOutputFlag)
                {
                    Debug.WriteLine($"PixelFormat: {context.InputFrame.SoftwareBitmap.BitmapPixelFormat}");
                    debugOutputFlag = true;
                }
#endif

                // ********** Test for using SoftwareBitmap.Convert ********* //
                //using (var inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(
                //    _canvasDevice,
                //    SoftwareBitmap.Convert(context.InputFrame.SoftwareBitmap, context.InputFrame.SoftwareBitmap.BitmapPixelFormat))) //PixelFormat: Bgra8

                //using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                //using (var ds = renderTarget.CreateDrawingSession())
                //{
                //    var effect = new EdgeDetectionEffect
                //    {
                //        Source = inputBitmap,
                //        Amount = this.Amount
                //    };

                //    ds.DrawImage(effect);
                //}

                // ********************************************************** //

                // InputFrame's raw pixels
                byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight];
                context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer());

                using (var inputBitmap = CanvasBitmap.CreateFromBytes(
                           _canvasDevice,
                           inputFrameBytes,
                           context.InputFrame.SoftwareBitmap.PixelWidth,
                           context.InputFrame.SoftwareBitmap.PixelHeight,
                           context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat()))

                    using (var renderTarget = new CanvasRenderTarget(
                               _canvasDevice,
                               context.OutputFrame.SoftwareBitmap.PixelWidth,
                               context.OutputFrame.SoftwareBitmap.PixelHeight,
                               (float)context.OutputFrame.SoftwareBitmap.DpiX,
                               context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(),
                               CanvasAlphaMode.Premultiplied))
                    {
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var effect = new EdgeDetectionEffect
                            {
                                Source = inputBitmap,
                                Amount = this.Amount
                            };

                            ds.DrawImage(effect);
                        }
                    }
            }
        }
Пример #12
0
        private async void GenerateButtonClicked(object sender, RoutedEventArgs e)
        {
            if (AutoGradientCheckBox.IsChecked.GetValueOrDefault())
            {
                _perlinNoiseEngine.InitializeGradients();
            }

            if (AutoPermutationsCheckBox.IsChecked.GetValueOrDefault())
            {
                _perlinNoiseEngine.InitializePermutation();
            }

            var actualWidth  = (int)NoiseCanvasControl.ActualWidth;
            var actualHeight = (int)NoiseCanvasControl.ActualHeight;

            var octaves   = (int)OctavesSlider.Value;
            var frequency = (float)(FrequencySlider.Value / 1000);
            var amplitude = (float)(AmplitudeSlider.Value / 1000);

            var noisemap = await Task.Run(() => _perlinNoiseEngine.GenerateNoiseMap(actualWidth, actualHeight, octaves, frequency, amplitude));

            var totalSize  = actualWidth * actualHeight;
            var noisebytes = new byte[totalSize * 4];

            for (int i = 0; i < totalSize; i++)
            {
                noisebytes[i * 4]     = noisemap[i];
                noisebytes[i * 4 + 1] = noisemap[i];
                noisebytes[i * 4 + 2] = noisemap[i];
                noisebytes[i * 4 + 3] = 255;
            }

            _noisebitmap = CanvasBitmap.CreateFromBytes(NoiseCanvasControl, noisebytes, actualWidth, actualHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized);

            var shapemap = new byte[totalSize];
            int centerX  = actualWidth / 2;
            int centerY  = actualHeight / 2;

            int radius = (int)((actualWidth / 2) * .8);

            for (int y = 0; y < actualHeight; y++)
            {
                for (int x = 0; x < actualWidth; x++)
                {
                    float distance = (float)Math.Sqrt(Math.Pow(x - centerX, 2) + Math.Pow(y - centerY, 2));
                    if (distance > radius)
                    {
                        continue;
                    }

                    float part  = distance / radius;
                    byte  value = (byte)(255 * (1 - part));
                    shapemap[y * actualWidth + x] = value;
                }
            }

            var shapebytes = new byte[totalSize * 4];

            for (int i = 0; i < totalSize; i++)
            {
                shapebytes[i * 4]     = shapemap[i];
                shapebytes[i * 4 + 1] = shapemap[i];
                shapebytes[i * 4 + 2] = shapemap[i];
                shapebytes[i * 4 + 3] = 255;
            }

            _shapebitmap = CanvasBitmap.CreateFromBytes(ShapeCanvasControl, shapebytes, actualWidth, actualHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized);

            var heightbytes = new byte[totalSize * 4];

            for (int i = 0; i < totalSize; i++)
            {
                byte first  = noisemap[i];
                byte second = shapemap[i];
                int  temp   = (first + second) / 2;

                byte value = (byte)temp;

                heightbytes[i * 4]     = value;
                heightbytes[i * 4 + 1] = value;
                heightbytes[i * 4 + 2] = value;
                heightbytes[i * 4 + 3] = 255;
            }

            _heightbitmap = CanvasBitmap.CreateFromBytes(HeightCanvasControl, heightbytes, actualWidth, actualHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized);

            var bytes = new byte[totalSize * 4];

            for (int i = 0; i < totalSize; i++)
            {
                int value = (noisemap[i] + shapemap[i]) / 2;
                int index = i * 4;

                if (value < 100)
                {
                    // dark blue - ocean
                    bytes[index]     = 104;
                    bytes[index + 1] = 34;
                    bytes[index + 2] = 26;
                    bytes[index + 3] = 255;
                }
                else if (value < 128)
                {
                    // lighter blue - shallow
                    bytes[index]     = 212;
                    bytes[index + 1] = 118;
                    bytes[index + 2] = 58;
                    bytes[index + 3] = 255;
                }
                else if (value < 140)
                {
                    // yellowish - coast
                    bytes[index]     = 145;
                    bytes[index + 1] = 231;
                    bytes[index + 2] = 255;
                    bytes[index + 3] = 255;
                }
                else if (value < 180)
                {
                    // green - grass/forest
                    bytes[index]     = 43;
                    bytes[index + 1] = 149;
                    bytes[index + 2] = 63;
                    bytes[index + 3] = 255;
                }
                else if (value < 210)
                {
                    // gray - mountain
                    bytes[index]     = 100;
                    bytes[index + 1] = 100;
                    bytes[index + 2] = 100;
                    bytes[index + 3] = 255;
                }
                else
                {
                    // white - snow
                    bytes[index]     = 255;
                    bytes[index + 1] = 255;
                    bytes[index + 2] = 255;
                    bytes[index + 3] = 255;
                }
            }

            _finalbitmap = CanvasBitmap.CreateFromBytes(FinalCanvasControl, bytes, actualWidth, actualHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized);

            NoiseCanvasControl.Invalidate();
            ShapeCanvasControl.Invalidate();
            HeightCanvasControl.Invalidate();
            FinalCanvasControl.Invalidate();
        }
Пример #13
0
        public override void Init(WriteableBitmap bmp, IGrap9Attr attr = null)
        {
            var newbg = attr.bgurl != bgurl || attr.fgurl != fgurl || attr.hard != hard;

            base.Init(bmp, attr);
            b      = bmp;
            source = new CanvasRenderTarget(
                device, b.PixelWidth, b.PixelHeight, 96,
                Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized,
                CanvasAlphaMode.Premultiplied
                );
            Canvas = new CanvasRenderTarget(
                device, b.PixelWidth, b.PixelHeight, 96,
                Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized,
                CanvasAlphaMode.Premultiplied
                );
            Invalidate(true);

            try
            {
                if (newbg)
                {
                    if (fgurl != "")
                    {
                        var img = LayerPaint.Img.Create(fgurl);
                        Shape = CanvasBitmap.CreateFromBytes(
                            device, img.PixelBuffer, img.PixelWidth, img.PixelHeight,
                            Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized
                            );
                    }
                    else
                    {
                        var px     = 200;
                        var csize  = Canvas.Size;
                        var sample = new CanvasRenderTarget(device, 2 * px, 2 * px, 96,
                                                            Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, CanvasAlphaMode.Premultiplied
                                                            );
                        var tb = new CanvasRadialGradientBrush(device, new[]  {
                            new CanvasGradientStop()
                            {
                                Color = Color.FromArgb(255, 255, 255, 255), Position = hard
                            },
                            new CanvasGradientStop()
                            {
                                Color = Color.FromArgb(255, 0, 0, 0), Position = 1f
                            },
                        }, CanvasEdgeBehavior.Clamp, CanvasAlphaMode.Straight);
                        using (var s = sample.CreateDrawingSession())
                        {
                            tb.RadiusX = px; tb.RadiusY = px; tb.Center = new Vector2(px, px);
                            s.FillRectangle(0, 0, px * 2, px * 2, tb);
                        }
                        Shape = sample;
                    }
                    if (bgurl != "")
                    {
                        var img = LayerPaint.Img.Create(bgurl);
                        Texture = CanvasBitmap.CreateFromBytes(
                            device, img.PixelBuffer, img.PixelWidth, img.PixelHeight,
                            Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized
                            );
                    }
                    else
                    {
                        Texture = null;
                    }
                    //fgurl = bgurl= "";
                }
            }
            catch (Exception e)
            {
                new Windows.UI.Popups.MessageDialog(e.ToString()).ShowMux();
            }

            Loadd();
        }
Пример #14
0
        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            try
            {
                // When using SupportedMemoryTypes => MediaMemoryTypes.GpuAndCpu we need to check if we're using GPU or CPU for the frame

                // If we're on GPU, use InputFrame.Direct3DSurface
                if (context.InputFrame.SoftwareBitmap == null)
                {
                    using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
                        using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                            using (var ds = renderTarget.CreateDrawingSession())
                            {
                                ds.DrawImage(inputBitmap);
                                ds.DrawImage(this._overlay, inputBitmap.Bounds);
                            }

                    return;
                }

                // If we're on CPU, use InputFrame.SoftwareBitmap
                if (context.InputFrame.Direct3DSurface == null)
                {
                    // InputFrame's raw pixels
                    byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight];
                    context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer());

                    using (var inputBitmap = CanvasBitmap.CreateFromBytes(
                               _canvasDevice,
                               inputFrameBytes,
                               context.InputFrame.SoftwareBitmap.PixelWidth,
                               context.InputFrame.SoftwareBitmap.PixelHeight,
                               context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat()))

                        using (var renderTarget = new CanvasRenderTarget(
                                   _canvasDevice,
                                   context.OutputFrame.SoftwareBitmap.PixelWidth,
                                   context.OutputFrame.SoftwareBitmap.PixelHeight,
                                   (float)context.OutputFrame.SoftwareBitmap.DpiX,
                                   context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(),
                                   CanvasAlphaMode.Premultiplied))
                        {
                            using (var ds = renderTarget.CreateDrawingSession())
                            {
                                ds.DrawImage(inputBitmap);
                                ds.DrawImage(this._overlay, inputBitmap.Bounds);
                            }
                        }
                }
            }
            catch (Exception)
            {
                if (crashCount < 20)
                {
                    crashCount++;
                    Debug.WriteLine($"ProcessFrame Exception: #{crashCount}");
                }
                else
                {
                    //System.Exception HResult = 0x88990012
                    //Message = Objects used together must be created from the same factory instance. (Exception from HRESULT: 0x88990012)
                    //Source = System.Private.CoreLib
                    //StackTrace:
                    //at System.Runtime.InteropServices.WindowsRuntime.IClosable.Close()
                    //at System.Runtime.InteropServices.WindowsRuntime.IClosableToIDisposableAdapter.Dispose()
                    //at VideoEffects.Win2D.OverlayVideoEffect.ProcessFrame(ProcessVideoFrameContext context) in D:\GitHub\VideoDiary\src\VideoDiary.EffectsLibrary\Win2dEffects\OverlayVideoEffect.cs:line 66

                    throw;
                }
            }
        }
Пример #15
0
        private async Task ChatPhotoAsync(UpdateFileGenerationStart update, string[] args)
        {
            try
            {
                var conversion = JsonConvert.DeserializeObject <ChatPhotoConversion>(args[2]);

                var sticker = await _protoService.SendAsync(new GetFile(conversion.StickerFileId)) as Telegram.Td.Api.File;

                if (sticker == null || !sticker.Local.IsFileExisting())
                {
                    _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID No sticker found")));
                    return;
                }

                Background background = null;

                var backgroundLink = await _protoService.SendAsync(new GetInternalLinkType(conversion.BackgroundUrl ?? string.Empty)) as InternalLinkTypeBackground;

                if (backgroundLink != null)
                {
                    background = await _protoService.SendAsync(new SearchBackground(backgroundLink.BackgroundName)) as Background;
                }
                else
                {
                    var freeform = new[] { 0xDBDDBB, 0x6BA587, 0xD5D88D, 0x88B884 };
                    background = new Background(0, true, false, string.Empty,
                                                new Document(string.Empty, "application/x-tgwallpattern", null, null, TdExtensions.GetLocalFile("Assets\\Background.tgv", "Background")),
                                                new BackgroundTypePattern(new BackgroundFillFreeformGradient(freeform), 50, false, false));
                }

                if (background == null || (background.Document != null && !background.Document.DocumentValue.Local.IsFileExisting()))
                {
                    _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID No background found")));
                    return;
                }

                var device  = CanvasDevice.GetSharedDevice();
                var bitmaps = new List <CanvasBitmap>();

                var sfondo = new CanvasRenderTarget(device, 640, 640, 96, DirectXPixelFormat.B8G8R8A8UIntNormalized, CanvasAlphaMode.Premultiplied);

                using (var session = sfondo.CreateDrawingSession())
                {
                    if (background.Type is BackgroundTypePattern pattern)
                    {
                        if (pattern.Fill is BackgroundFillFreeformGradient freeform)
                        {
                            var colors    = freeform.GetColors();
                            var positions = new Vector2[]
                            {
                                new Vector2(0.80f, 0.10f),
                                new Vector2(0.35f, 0.25f),
                                new Vector2(0.20f, 0.90f),
                                new Vector2(0.65f, 0.75f),
                            };

                            using (var gradient = CanvasBitmap.CreateFromBytes(device, ChatBackgroundFreeform.GenerateGradientData(50, 50, colors, positions), 50, 50, DirectXPixelFormat.B8G8R8A8UIntNormalized))
                                using (var cache = await PlaceholderHelper.GetPatternBitmapAsync(device, null, background.Document.DocumentValue))
                                {
                                    using (var scale = new ScaleEffect {
                                        Source = gradient, BorderMode = EffectBorderMode.Hard, Scale = new Vector2(640f / 50f, 640f / 50f)
                                    })
                                        using (var colorize = new TintEffect {
                                            Source = cache, Color = Color.FromArgb(0x76, 00, 00, 00)
                                        })
                                            using (var tile = new BorderEffect {
                                                Source = colorize, ExtendX = CanvasEdgeBehavior.Wrap, ExtendY = CanvasEdgeBehavior.Wrap
                                            })
                                                using (var effect = new BlendEffect {
                                                    Foreground = tile, Background = scale, Mode = BlendEffectMode.Overlay
                                                })
                                                {
                                                    session.DrawImage(effect, new Rect(0, 0, 640, 640), new Rect(0, 0, 640, 640));
                                                }
                                }
                        }
                    }
                }

                bitmaps.Add(sfondo);

                var width  = (int)(512d * conversion.Scale);
                var height = (int)(512d * conversion.Scale);

                var animation = await Task.Run(() => LottieAnimation.LoadFromFile(sticker.Local.Path, new Windows.Graphics.SizeInt32 {
                    Width = width, Height = height
                }, false, null));

                if (animation == null)
                {
                    _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID Can't load Lottie animation")));
                    return;
                }

                var composition = new MediaComposition();
                var layer       = new MediaOverlayLayer();

                var buffer = ArrayPool <byte> .Shared.Rent(width *height * 4);

                var framesPerUpdate = animation.FrameRate < 60 ? 1 : 2;
                var duration        = TimeSpan.Zero;

                for (int i = 0; i < animation.TotalFrame; i += framesPerUpdate)
                {
                    var bitmap = CanvasBitmap.CreateFromBytes(device, buffer, width, height, DirectXPixelFormat.B8G8R8A8UIntNormalized);
                    animation.RenderSync(bitmap, i);

                    var clip    = MediaClip.CreateFromSurface(bitmap, TimeSpan.FromMilliseconds(1000d / 30d));
                    var overlay = new MediaOverlay(clip, new Rect(320 - (width / 2d), 320 - (height / 2d), width, height), 1);

                    overlay.Delay = duration;

                    layer.Overlays.Add(overlay);
                    duration += clip.OriginalDuration;

                    bitmaps.Add(bitmap);
                }

                composition.OverlayLayers.Add(layer);
                composition.Clips.Add(MediaClip.CreateFromSurface(sfondo, duration));

                var temp = await _protoService.GetFileAsync(update.DestinationPath);

                var profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
                profile.Audio                       = null;
                profile.Video.Bitrate               = 1800000;
                profile.Video.Width                 = 640;
                profile.Video.Height                = 640;
                profile.Video.FrameRate.Numerator   = 30;
                profile.Video.FrameRate.Denominator = 1;

                var progress = composition.RenderToFileAsync(temp, MediaTrimmingPreference.Precise, profile);
                progress.Progress = (result, delta) =>
                {
                    _protoService.Send(new SetFileGenerationProgress(update.GenerationId, 100, (int)delta));
                };

                var result = await progress;
                if (result == TranscodeFailureReason.None)
                {
                    _protoService.Send(new FinishFileGeneration(update.GenerationId, null));
                }
                else
                {
                    _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, result.ToString())));
                }

                ArrayPool <byte> .Shared.Return(buffer);

                foreach (var bitmap in bitmaps)
                {
                    bitmap.Dispose();
                }
            }
            catch (Exception ex)
            {
                _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID " + ex.ToString())));
            }
        }
Пример #16
0
        /// <summary>
        /// Captures the ink from an <see cref="InkCanvas"/> control.
        /// </summary>
        /// <param name="canvas">
        /// The <see cref="InkCanvas"/> control.
        /// </param>
        /// <param name="rootRenderElement">
        /// A <see cref="FrameworkElement"/> which wraps the canvas.
        /// </param>
        /// <param name="encoderId">
        /// A <see cref="BitmapEncoder"/> ID to use to render the image.
        /// </param>
        /// <returns>
        /// Returns an awaitable task.
        /// </returns>
        public static async Task <StorageFile> CaptureInkAsImageAsync(
            this InkCanvas canvas,
            FrameworkElement rootRenderElement,
            Guid encoderId)
        {
            var targetFile =
                await
                ApplicationData.Current.TemporaryFolder.CreateFileAsync(
                    $"{Guid.NewGuid()}.png",
                    CreationCollisionOption.ReplaceExisting);

            if (targetFile != null)
            {
                var renderBitmap = new RenderTargetBitmap();
                await renderBitmap.RenderAsync(rootRenderElement);

                var bitmapSizeAt96Dpi = new Size(renderBitmap.PixelWidth, renderBitmap.PixelHeight);

                var pixels = await renderBitmap.GetPixelsAsync();

                var win2DDevice = CanvasDevice.GetSharedDevice();

                using (
                    var target = new CanvasRenderTarget(
                        win2DDevice,
                        (float)rootRenderElement.ActualWidth,
                        (float)rootRenderElement.ActualHeight,
                        96.0f))
                {
                    using (var drawingSession = target.CreateDrawingSession())
                    {
                        using (
                            var canvasBitmap = CanvasBitmap.CreateFromBytes(
                                win2DDevice,
                                pixels,
                                (int)bitmapSizeAt96Dpi.Width,
                                (int)bitmapSizeAt96Dpi.Height,
                                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                                96.0f))
                        {
                            drawingSession.DrawImage(
                                canvasBitmap,
                                new Rect(0, 0, target.SizeInPixels.Width, target.SizeInPixels.Height),
                                new Rect(0, 0, bitmapSizeAt96Dpi.Width, bitmapSizeAt96Dpi.Height));
                        }
                        drawingSession.Units = CanvasUnits.Pixels;
                        drawingSession.DrawInk(canvas.InkPresenter.StrokeContainer.GetStrokes());
                    }

                    using (var stream = await targetFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
                        var encoder    = await BitmapEncoder.CreateAsync(encoderId, stream);

                        encoder.SetPixelData(
                            BitmapPixelFormat.Bgra8,
                            BitmapAlphaMode.Ignore,
                            (uint)renderBitmap.PixelWidth,
                            (uint)renderBitmap.PixelHeight,
                            logicalDpi,
                            logicalDpi,
                            target.GetPixelBytes());

                        await encoder.FlushAsync();
                    }
                }
            }

            return(targetFile);
        }
Пример #17
0
        public async Task OpenFileAsync(StorageFile file)
        {
            CanvasBitmap fileBitmap = null;
            var          extension  = file.FileType;

            switch (extension)
            {
            case ".bin":
                var buffer = await FileIO.ReadBufferAsync(file);

                var width  = 0;
                var height = 0;
                var format = DirectXPixelFormat.B8G8R8A8UIntNormalized;

                // If the image name ends in (width)x(height), then use that in the dialog
                var fileName = file.Name;
                var fileStem = fileName.Substring(0, fileName.LastIndexOf('.'));
                var pattern  = @".*[A-z](?<width>[0-9]+)x(?<height>[0-9]+)";
                var match    = Regex.Match(fileStem, pattern);
                if (match.Success)
                {
                    ResetBinaryDetailsInputDialog(int.Parse(match.Groups["width"].Value), int.Parse(match.Groups["height"].Value));
                }
                else
                {
                    ResetBinaryDetailsInputDialog();
                }

                var dialogResult = await BinaryDetailsInputDialog.ShowAsync();

                if (dialogResult == ContentDialogResult.Primary &&
                    ParseBinaryDetailsSizeBoxes(out width, out height))
                {
                    fileBitmap = CanvasBitmap.CreateFromBytes(_canvasDevice, buffer, width, height, format);
                }
                break;

            default:
                // open it with win2d
                using (var stream = await file.OpenReadAsync())
                {
                    fileBitmap = await CanvasBitmap.LoadAsync(_canvasDevice, stream);
                }
                break;
            }

            if (fileBitmap != null)
            {
                var size = fileBitmap.SizeInPixels;
                var backgroundSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(backgroundSurface))
                {
                    drawingSession.FillRectangle(0, 0, size.Width, size.Height, _backgroundCavnasBrush);
                }

                var imageSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(imageSurface))
                {
                    drawingSession.Clear(Colors.Transparent);
                    drawingSession.DrawImage(fileBitmap);
                }

                _backgroundBrush.Surface = backgroundSurface;
                _imageBrush.Surface      = imageSurface;
                ImageGrid.Width          = size.Width;
                ImageGrid.Height         = size.Height;
                ImageScrollViewer.ChangeView(0, 0, 1, true);
                if (_borderEnabled)
                {
                    ImageBorderBrush.Color = Colors.Black;
                }
                _currentFile   = file;
                _currentBitmap = fileBitmap;
            }
        }
        protected override com.codename1.ui.Image generatePeerImage()
        {
            int width  = getWidth();
            int height = getHeight();

            if (width <= 0 || height <= 0)
            {
                width  = getPreferredW();
                height = getPreferredH();
            }
            if (element.Parent == null)
            {
                if (peerImage != null)
                {
                    return((com.codename1.ui.Image)peerImage);
                }
                return(com.codename1.ui.Image.createImage(width, height));
            }
            CodenameOneImage img = new CodenameOneImage();

            //img.@this();
            img.name = "PeerImage: " + element.ToString();
            IRandomAccessStream stream = new InMemoryRandomAccessStream();
            CanvasBitmap        cb     = null;

            using (AutoResetEvent are = new AutoResetEvent(false))
            {
                SilverlightImplementation.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    if (element is WebView)
                    {
                        try
                        {
                            Task.Delay(TimeSpan.FromTicks(4).Duration()).GetAwaiter().GetResult();
                            ((WebView)element).CapturePreviewToStreamAsync(stream).AsTask().GetAwaiter().GetResult();
                            stream.FlushAsync().AsTask().GetAwaiter().GetResult();
                            stream.Seek(0);
                            Task.Delay(TimeSpan.FromMilliseconds(10)).GetAwaiter().GetResult();
                            cb = CanvasBitmap.LoadAsync(SilverlightImplementation.screen, stream).AsTask().GetAwaiter().GetResult();
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                    else
                    {
                        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                        renderTargetBitmap.RenderAsync(element).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
                        byte[] buf = renderTargetBitmap.GetPixelsAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult().ToArray();
                        cb         = CanvasBitmap.CreateFromBytes(SilverlightImplementation.screen, buf, width, height, SilverlightImplementation.pixelFormat, SilverlightImplementation.screen.Dpi);
                    }
                    img.image = new CanvasRenderTarget(SilverlightImplementation.screen, cb.SizeInPixels.Width, cb.SizeInPixels.Height, cb.Dpi);
                    img.graphics.destination.drawImage(cb, 0, 0);
                    img.graphics.destination.dispose();
                    are.Set();
                }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
                are.WaitOne();
            }
            return(com.codename1.ui.Image.createImage(img));

            // return com.codename1.ui.Image.createImage(width, height);
        }
Пример #19
0
        private void StartBtn_Click(object sender, RoutedEventArgs e)
        {
            source             = new CancellationTokenSource();
            token              = source.Token;
            StopBtn.IsEnabled  = true;
            StartBtn.IsEnabled = false;
            Task.Run(async() =>
            {
                Debug.WriteLine("Start Screen Cast");
                while (token.IsCancellationRequested == false)
                {
                    ValueSet valueSet = new ValueSet
                    {
                        { "screen", isCompressData }
                    };

                    if (App.Connection != null)
                    {
                        stopwatch.Start();
                        AppServiceResponse response = await App.Connection.SendMessageAsync(valueSet);
                        var responseTime            = stopwatch.Elapsed.Milliseconds;

                        var screenBytes = response.Message["screen"] as byte[];
                        var width       = (int)response.Message["screenWidth"];
                        var height      = (int)response.Message["screenHeight"];

                        double len = Math.Round((double)screenBytes.Length / 1024, 2);

                        if (swapChain == null)
                        {
                            await InitShapChain(width, height);
                        }

                        stopwatch.Restart();

                        var decompressTime = 0;
                        if (isCompressData == true)
                        {
                            screenBytes    = Decompress(screenBytes);
                            decompressTime = stopwatch.Elapsed.Milliseconds;
                            stopwatch.Restart();
                        }

                        using (CanvasDrawingSession ds = SwapChainPanel.SwapChain.CreateDrawingSession(Colors.Transparent))
                        {
                            using (CanvasBitmap screen = CanvasBitmap.CreateFromBytes(device, screenBytes, width, height, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized))
                            {
                                ds.DrawImage(screen, new Windows.Foundation.Rect(0, 0, 800, 600));
                            }
                        }
                        SwapChainPanel.SwapChain.Present();
                        var drawTime = stopwatch.Elapsed.Milliseconds;
                        stopwatch.Stop();

                        Debug.WriteLine($"Response: {responseTime} ms Draw:{drawTime} ms Decompress: {decompressTime} ms Length: {len} Kb");
                    }
                    await Task.Delay(TimeSpan.FromMilliseconds(25));
                }
                Debug.WriteLine("Stop Screen Cast");
            }, token);
        }
Пример #20
0
        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            //using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
            //using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
            //using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
            //{
            //    var invert = new InvertEffect()
            //    {
            //        Source = inputBitmap
            //    };
            //    ds.DrawImage(invert);

            //}

            // When using SupportedMemoryTypes => MediaMemoryTypes.GpuAndCpu we need to check if we're using GPU or CPU for the frame

            // If we're on GPU, use InputFrame.Direct3DSurface
            if (context.InputFrame.SoftwareBitmap == null)
            {
                using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
                    using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var invert = new InvertEffect()
                            {
                                Source = inputBitmap
                            };
                            ds.DrawImage(invert);
                        }

                return;
            }

            // If we're on CPU, use InputFrame.SoftwareBitmap
            if (context.InputFrame.Direct3DSurface == null)
            {
                // InputFrame's raw pixels
                byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight];
                context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer());

                using (var inputBitmap = CanvasBitmap.CreateFromBytes(
                           _canvasDevice,
                           inputFrameBytes,
                           context.InputFrame.SoftwareBitmap.PixelWidth,
                           context.InputFrame.SoftwareBitmap.PixelHeight,
                           context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat()))

                    using (var renderTarget = new CanvasRenderTarget(
                               _canvasDevice,
                               context.OutputFrame.SoftwareBitmap.PixelWidth,
                               context.OutputFrame.SoftwareBitmap.PixelHeight,
                               (float)context.OutputFrame.SoftwareBitmap.DpiX,
                               context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(),
                               CanvasAlphaMode.Premultiplied))
                    {
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var invert = new InvertEffect()
                            {
                                Source = inputBitmap
                            };
                            ds.DrawImage(invert);
                        }
                    }
            }
        }