private async void OnJpegPacket(JpegPacket packet) { if (IsDecoding) { PendingPakcet = packet; return; } IsDecoding = true; await DecodeLiveviewFrame(packet); IsDecoding = false; if (Context.HistogramCreator != null && ApplicationSettings.GetInstance().IsHistogramDisplayed&& !Context.HistogramCreator.IsRunning) { rwLock.EnterReadLock(); try { Context.HistogramCreator.CreateHistogram(LiveviewImageBitmap); } finally { rwLock.ExitReadLock(); } } }
private async Task DecodeLiveviewFrame(JpegPacket packet, bool retry = false) { Action trailingTask = null; if (LiveviewImageBitmap == null || sizeChanged) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { var writeable = await LiveviewUtil.AsWriteableBitmap(packet.ImageData, Dispatcher); OriginalLvSize = new BitmapSize { Width = (uint)writeable.PixelWidth, Height = (uint)writeable.PixelHeight }; var magnification = CalcLiveviewMagnification(); DebugUtil.Log(() => { return("Decode: mag: " + magnification + " offsetV: " + LvOffsetV); }); dpi = DEFAULT_DPI / magnification; RefreshOverlayControlParams(magnification); trailingTask = () => { if (Context?.Target?.Status != null) { RotateLiveviewImage(Context.Target.Status.LiveviewOrientationAsDouble, (sender, arg) => { RefreshOverlayControlParams(magnification); }); } sizeChanged = false; }; }); } else { rwLock.EnterWriteLock(); try { var toDelete = LiveviewImageBitmap; trailingTask = () => { // Dispose after it is drawn toDelete?.Dispose(); }; } finally { rwLock.ExitWriteLock(); } } using (var stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(packet.ImageData.AsBuffer()); stream.Seek(0); var bmp = await CanvasBitmap.LoadAsync(LiveviewImageCanvas, stream, (float)dpi); var size = bmp.SizeInPixels; rwLock.EnterWriteLock(); try { LiveviewImageBitmap = bmp; } finally { rwLock.ExitWriteLock(); } if (!OriginalLvSize.Equals(size)) { DisposeLiveviewImageBitmap(); if (!retry) { await DecodeLiveviewFrame(packet, true); } return; } } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { LiveviewImageCanvas.Invalidate(); trailingTask?.Invoke(); }); if (PendingPakcet != null) { var task = DecodeLiveviewFrame(PendingPakcet); PendingPakcet = null; } }