Exemplo n.º 1
0
        /// <summary>
        /// Converts the YUV2 image data into a bitmap image.
        /// </summary>
        /// <param name="yuvPixelArray">The image data in YUV2 format.</param>
        /// <param name="width">The width of the image in pixels.</param>
        /// <param name="height">The height of the image in pixels.</param>
        /// <returns>A BitmapImage instance containing the image.</returns>
        public static BitmapImage Yuy2PixelArrayToBitmapImage(byte[] yuvPixelArray, int width, int height)
        {
            BitmapImage bitmapImage = null;

            byte[] rgbPixelArray = ImageProcessingUtils.Yuy2PixelArrayToRgbPixelArray(yuvPixelArray, width, height);

            if (rgbPixelArray != null)
            {
                bitmapImage = new BitmapImage();

                InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
                stream.AsStreamForWrite().Write(rgbPixelArray, 0, rgbPixelArray.Length);
                stream.Seek(0);

                try
                {
                    bitmapImage.SetSource(stream);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    bitmapImage = null;
                }
            }

            return(bitmapImage);
        }
Exemplo n.º 2
0
        private void StoreLoadedImage(ImageData imageData)
        {
            progressBar.Visibility = Visibility.Visible;

            BitmapImage bitmapImage = ImageProcessingUtils.ImageMemoryStreamToBitmapImage(imageData.ImageMemoryStream);

            if (_imageIndex == 0)
            {
                thumbnailImage1.Source = bitmapImage;
            }
            else
            {
                thumbnailImage2.Source = bitmapImage;
            }

            _imageWidthInPixels[_imageIndex]  = imageData.ImageWidthInPixels;
            _imageHeightInPixels[_imageIndex] = imageData.ImageHeightInPixels;
            byte[] rgbPixelArray = ImageProcessingUtils.ImageMemoryStreamToRgbPixelArray(imageData.ImageMemoryStream);
            _yuvPixelArray[_imageIndex] = ImageProcessingUtils.RgbPixelArrayToYuy2PixelArray(rgbPixelArray, _imageWidthInPixels[_imageIndex], _imageHeightInPixels[_imageIndex]);

            _photoProcessor.SetFrame(
                _yuvPixelArray[_imageIndex],
                _imageWidthInPixels[_imageIndex], _imageHeightInPixels[_imageIndex],
                _imageIndex);

            progressBar.Visibility = Visibility.Collapsed;
        }
Exemplo n.º 3
0
        internal async void ApplyEffectInternalAsync()
        {
            _isProcessing = true;
            _processedPhotoAsWriteableBitmap = null;
            byte[] yuvPixelArray = null;

            try
            {
                yuvPixelArray = _photoProcessor.ProcessFrames(0, _settings.RemoveNoise, _settings.ApplyEffectOnly);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }

            if (yuvPixelArray != null)
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    _processedPhotoAsWriteableBitmap =
                        await ImageProcessingUtils.Yuy2PixelArrayToWriteableBitmapAsync(
                            yuvPixelArray, _imageWidthInPixels[0], _imageHeightInPixels[0]);

                    pageImage.Source = _processedPhotoAsWriteableBitmap;
                    _processedPhotoAsWriteableBitmap.Invalidate();
                    _isProcessing = false;
                });
            }
            else
            {
                _isProcessing = false;
            }
        }
        private async void OnColorMapImageTapped(object sender, TappedRoutedEventArgs e)
        {
            RenderTargetBitmap bitmap = new RenderTargetBitmap();
            await bitmap.RenderAsync(colorMapImage);

            uint width  = (uint)bitmap.PixelWidth;
            uint height = (uint)bitmap.PixelHeight;

            Point point  = e.GetPosition(colorMapImage);
            int   pointX = (int)((double)(point.X / colorMapImage.ActualWidth) * width);
            int   pointY = (int)((double)(point.Y / colorMapImage.ActualHeight) * height);

            System.Diagnostics.Debug.WriteLine("Picked point is [" + (int)point.X + "; " + (int)point.Y
                                               + "], size of the color map image is "
                                               + (int)colorMapImage.ActualWidth + "x" + (int)colorMapImage.ActualHeight
                                               + " => point in image is [" + pointX + "; " + pointY + "]");

            IBuffer pixelBuffer = await bitmap.GetPixelsAsync();

            _brightnessSliderValueChangedByPickedColor = true;
            PreviewColor = ImageProcessingUtils.GetColorAtPoint(pixelBuffer, width, height, new Point()
            {
                X = pointX, Y = pointY
            });
            _originalColor = PreviewColor;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sets the new target color.
        /// </summary>
        /// <param name="newColor">The new color.</param>
        /// <param name="saveSettings">If true, will save the color to the local storage.</param>
        private void SetColor(Color newColor, bool saveSettings = true)
        {
            colorPickerControl.CurrentColor = newColor;
            _settings.TargetColor           = newColor;

            if (colorPickerControl.Visibility == Visibility.Visible)
            {
                colorPickerControl.PreviewColor = newColor;
            }

            byte[] yuv = ImageProcessingUtils.RgbToYuv(newColor.R, newColor.G, newColor.B);

            _photoProcessor.SetTargetYUV(new float[]
            {
                (float)yuv[0],
                (float)yuv[1],
                (float)yuv[2]
            });

            if (saveSettings)
            {
                _settings.Save();
            }

            ApplyEffectAsync();
        }
        private void OnBrightnessSliderValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            if (!_brightnessSliderValueChangedByPickedColor)
            {
                PreviewColor = ImageProcessingUtils.ApplyBrightnessToColor(_originalColor, e.NewValue);
            }

            _brightnessSliderValueChangedByPickedColor = false;
        }
        private static void OnPreviewColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerControl control = d as ColorPickerControl;

            if (control != null)
            {
                Color newColor = (Color)e.NewValue;
                control.brightnessSlider.Value      = ImageProcessingUtils.BrightnessFromColor(newColor);
                control.previewColorGrid.Background = new SolidColorBrush(newColor);
            }
        }
        /// <summary>
        /// Reads the given image file and writes it to the buffers while also
        /// scaling a preview image.
        ///
        /// Note that this method can't handle null argument!
        /// </summary>
        /// <param name="file">The selected image file.</param>
        /// <returns>True if successful, false otherwise.</returns>
        private async Task <bool> HandleSelectedImageFileAsync(StorageFile file)
        {
            System.Diagnostics.Debug.WriteLine(DebugTag + "HandleSelectedImageFileAsync(): " + file.Name);
            var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            ImageData.Initialize();

            var image = new BitmapImage();

            image.SetSource(fileStream);
            ImageData.ImageWidthInPixels  = image.PixelWidth;
            ImageData.ImageHeightInPixels = image.PixelHeight;

            bool success = false;

            try
            {
                // JPEG images can be used as such
                Stream stream = fileStream.AsStream();
                stream.Position = 0;
                stream.CopyTo(ImageData.ImageMemoryStream);
                stream.Dispose();
                success = true;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(DebugTag
                                                   + "Cannot use stream as such (not probably in JPEG format): " + e.Message);
            }

            if (!success)
            {
                try
                {
                    await ImageProcessingUtils.FileStreamToJpegStreamAsync(fileStream,
                                                                           ImageData.ImageWidthInPixels,
                                                                           ImageData.ImageHeightInPixels,
                                                                           (IRandomAccessStream)ImageData.ImageMemoryStream.AsInputStream());

                    success = true;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(DebugTag
                                                       + "Failed to convert the file stream content into JPEG format: "
                                                       + e.ToString());
                }
            }

            fileStream.Dispose();
            ImageData.SeekStreamToBeginning();
            return(success);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Converts the given pixel array into a writeable bitmap.
        /// </summary>
        /// <param name="pixelArray">The pixel array.</param>
        /// <param name="width">The width of the image in pixels.</param>
        /// <param name="height">The height of the image in pixels.</param>
        /// <returns></returns>
        public static async Task <WriteableBitmap> PixelArrayToWriteableBitmapAsync(byte[] pixelArray, int width, int height)
        {
            string videoRecordFormat = CameraUtils.ResolveVideoRecordFormat(VideoEngine.Instance.MediaCapture);

            WriteableBitmap bitmap =
                (videoRecordFormat.Equals(VideoEncodingPropertiesSubTypeYUV2) ||
                 videoRecordFormat.Equals(VideoEncodingPropertiesSubTypeMJPG))
                    ? await ImageProcessingUtils.Yuy2PixelArrayToWriteableBitmapAsync(pixelArray, width, height)
                    : await ImageProcessingUtils.Nv12PixelArrayToWriteableBitmapAsync(pixelArray, width, height);

            return(bitmap);
        }
Exemplo n.º 10
0
        private async void OnPostProcessCompleteAsync(
            byte[] pixelArray, int imageWidth, int imageHeight,
            ObjectDetails fromObjectDetails, ObjectDetails toObjectDetails)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                WriteableBitmap bitmap = await ImageProcessingUtils.PixelArrayToWriteableBitmapAsync(pixelArray, imageWidth, imageHeight);

                if (bitmap != null)
                {
                }
            });
        }
Exemplo n.º 11
0
        private async void OnFrameCapturedAsync(byte[] pixelArray, int frameWidth, int frameHeight, int frameId)
        {
            _videoEngine.Messenger.FrameCaptured -= OnFrameCapturedAsync;
            System.Diagnostics.Debug.WriteLine("OnFrameCapturedAsync");

            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                Wbitmap = await ImageProcessingUtils.PixelArrayToWriteableBitmapAsync(pixelArray, frameWidth, frameHeight);

                if (Wbitmap != null)
                {
                    CapturePhoto(Wbitmap);
                }

                _videoEngine.Messenger.FrameCaptured += OnFrameCapturedAsync;
            });
        }
Exemplo n.º 12
0
        /// <summary>
        /// Sets the new target color.
        /// </summary>
        /// <param name="newColor">The new color.</param>
        /// <param name="saveSettings">If true, will save the color to the local storage.</param>
        private void SetColor(Color newColor, bool saveSettings = true)
        {
            colorPickerControl.CurrentColor = newColor;
            _settings.TargetColor           = newColor;

            if (colorPickerControl.Visibility == Visibility.Visible)
            {
                colorPickerControl.PreviewColor = newColor;
            }

            byte[] yuv = ImageProcessingUtils.RgbToYuv(newColor.R, newColor.G, newColor.B);
            _videoEngine.SetYuv(yuv);

            if (saveSettings)
            {
                _settings.Save();
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Stores the given image in YUV2 format into a file.
        /// </summary>
        /// <param name="yuvPixelArray">The image data in YUV2 format.</param>
        /// <param name="width">The width of the image in pixels.</param>
        /// <param name="height">The height of the image in pixels.</param>
        /// <param name="fileName">The desired file name.</param>
        public async static Task Yuy2PixelArrayToWriteableBitmapFileAsync(byte[] yuvPixelArray, int width, int height, string fileName)
        {
            byte[] rgbPixelArray = ImageProcessingUtils.Yuy2PixelArrayToRgbPixelArray(yuvPixelArray, width, height);
            var    file          = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(
                fileName, CreationCollisionOption.GenerateUniqueName);

            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                Guid          BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
                BitmapEncoder encoder           = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);

                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                     (uint)width,
                                     (uint)height,
                                     96.0,
                                     96.0,
                                     rgbPixelArray);
                await encoder.FlushAsync();
            }
        }
Exemplo n.º 14
0
        private async void OnFrameCapturedAsync(byte[] pixelArray, int frameWidth, int frameHeight, int frameId)
        {
            _videoEngine.Messenger.FrameCaptured -= OnFrameCapturedAsync;
            System.Diagnostics.Debug.WriteLine("OnFrameCapturedAsync");

            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                WriteableBitmap bitmap = await ImageProcessingUtils.PixelArrayToWriteableBitmapAsync(pixelArray, frameWidth, frameHeight);

                if (bitmap != null)
                {
                    capturedPhotoImage.Source = bitmap;
                    bitmap.Invalidate();
                    capturedPhotoImage.Visibility = Visibility.Visible;

                    if (frameId == ColorPickFrameRequestId)
                    {
                        int scaledWidth = (int)(_videoEngine.ResolutionWidth *viewfinderCanvas.ActualHeight / _videoEngine.ResolutionHeight);
                        int pointX      = (int)(((_viewFinderCanvasTappedPoint.X - (viewfinderCanvas.ActualWidth - scaledWidth) / 2) / scaledWidth) * frameWidth);
                        int pointY      = (int)((_viewFinderCanvasTappedPoint.Y / viewfinderCanvas.ActualHeight) * frameHeight);

                        SetColor(ImageProcessingUtils.GetColorAtPoint(
                                     bitmap, (uint)frameWidth, (uint)frameHeight, new Point()
                        {
                            X = pointX, Y = pointY
                        }));
                    }

                    if (_hideCapturePhotoImageTimer != null)
                    {
                        _hideCapturePhotoImageTimer.Dispose();
                        _hideCapturePhotoImageTimer = null;
                    }

                    _hideCapturePhotoImageTimer = new Timer(HideCapturedPhotoImageAsync, null, 8000, Timeout.Infinite);
                }

                _videoEngine.Messenger.FrameCaptured += OnFrameCapturedAsync;
            });
        }
Exemplo n.º 15
0
        private async void OnPostProcessCompleteAsync(
            byte[] pixelArray, int imageWidth, int imageHeight,
            ObjectDetails fromObjectDetails, ObjectDetails toObjectDetails)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                WriteableBitmap bitmap = await ImageProcessingUtils.PixelArrayToWriteableBitmapAsync(pixelArray, imageWidth, imageHeight);

                if (bitmap != null)
                {
                    processingResultImage.Source = bitmap;
                    bitmap.Invalidate();

                    if (fromObjectDetails.centerX > toObjectDetails.centerX) // TODO This is wrong, but the problem is in BufferTransform
                    {
                        imageLeftSideLabel.Text  = "1";
                        imageRightSideLabel.Text = "2";
                    }
                    else
                    {
                        imageLeftSideLabel.Text  = "2";
                        imageRightSideLabel.Text = "1";
                    }

                    if (imageWidth > 0)
                    {
                        SetRectangleSizeAndPositionBasedOnObjectDetails(
                            ref firstRectangle, ref firstRectangleTranslateTransform,
                            processingResultImage, fromObjectDetails);

                        SetRectangleSizeAndPositionBasedOnObjectDetails(
                            ref secondRectangle, ref secondRectangleTranslateTransform,
                            processingResultImage, toObjectDetails);
                    }

                    processingResultGrid.Visibility = Visibility.Visible;
                }
            });
        }
Exemplo n.º 16
0
        /// <summary>
        /// Converts the given image in YUV2 format into a writeable bitmap.
        /// </summary>
        /// <param name="yuvPixelArray">The image data in YUV2 format.</param>
        /// <param name="width">The width of the image in pixels.</param>
        /// <param name="height">The height of the image in pixels.</param>
        /// <returns>A WriteableBitmap instance containing the image.</returns>
        public async static Task <WriteableBitmap> Yuy2PixelArrayToWriteableBitmapAsync(byte[] yuvPixelArray, int width, int height)
        {
            WriteableBitmap writeableBitmap = null;

            byte[] rgbPixelArray = ImageProcessingUtils.Yuy2PixelArrayToRgbPixelArray(yuvPixelArray, width, height);

            if (rgbPixelArray != null)
            {
                writeableBitmap = new WriteableBitmap(width, height);

                using (Stream stream = writeableBitmap.PixelBuffer.AsStream())
                {
                    if (stream.CanWrite)
                    {
                        await stream.WriteAsync(rgbPixelArray, 0, rgbPixelArray.Length);

                        stream.Flush();
                    }
                }
            }

            return(writeableBitmap);
        }
 public float[] TargetYuv()
 {
     return(ImageProcessingUtils.ColorToYuv(_settings.TargetColor));
 }
Exemplo n.º 18
0
 /// <summary>
 /// Sets the target YUV values for the effect.
 /// </summary>
 /// <param name="yuv">The new target YUV.</param>
 public void SetYuv(byte[] yuv)
 {
     App.Settings.TargetColor      = ImageProcessingUtils.YuvToColor(yuv);
     Messenger.SettingsChangedFlag = true;
 }