Example #1
0
            /// <summary>
            /// Executes image processing.
            /// </summary>
            public void Execute()
            {
                try
                {
                    // execute image processing command
                    _command.ExecuteInPlace(_image);
                    // previous image will be disposed automatically
                }
                catch (Exception ex)
                {
                    if (ImageProcessingExceptionOccurs != null)
                    {
                        ImageProcessingExceptionOccurs(this, EventArgs.Empty);
                    }

                    DemosTools.ShowErrorMessage("Image processing exception", ex);
                }
            }
Example #2
0
        /// <summary>
        /// Updates the captured image in image viewer.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageCaptureCompletedEventArgs"/> instance containing the event data.</param>
        private void CaptureSource_CaptureCompleted(object sender, ImageCaptureCompletedEventArgs e)
        {
            // save reference to the previously captured image
            VintasoftImage oldImage = videoPreviewImageViewer.Image;

            // get captured image
            VintasoftImage newImage = e.GetCapturedImage();

            // apply processing
            if (_processingCommand != null)
            {
                _processingCommand.ExecuteInPlace(newImage);
            }

            // show captured image in the preview viewer
            videoPreviewImageViewer.Image = newImage;

            // if previously captured image is exist
            if (oldImage != null)
            {
                // dispose previously captured image
                oldImage.Dispose();
            }

            // if capture source is started
            if (_imageCaptureSource.State == ImageCaptureState.Started)
            {
                // sleep...
                if (_imageCaptureTimeout > 0)
                {
                    Thread.Sleep(_imageCaptureTimeout);
                }
                // if capture source is started
                if (_imageCaptureSource.State == ImageCaptureState.Started)
                {
                    // initialize new image capture request
                    _imageCaptureSource.CaptureAsync();
                }
            }
        }
Example #3
0
 private void ExecuteCommand(ProcessingCommandBase command, VintasoftImage image)
 {
     command.Progress += new EventHandler <ImageProcessingProgressEventArgs>(command_Progress);
     command.ExecuteInPlace(image);
     command.Progress -= new EventHandler <ImageProcessingProgressEventArgs>(command_Progress);
 }
Example #4
0
        /// <summary>
        /// Executes image processing command synchronously or asynchronously.
        /// </summary>
        /// <param name="command">Command to execute.</param>
        /// <param name="async">A value indicating whether to execute command asynchronously.</param>
        public bool ExecuteProcessingCommand(ProcessingCommandBase command, bool async)
        {
            RectangularSelectionToolWithCopyPaste rectSelectionTool = CompositeVisualTool.FindVisualTool <RectangularSelectionToolWithCopyPaste>(_viewer.VisualTool);
            CustomSelectionTool customSelectionTool = CompositeVisualTool.FindVisualTool <CustomSelectionTool>(_viewer.VisualTool);

            if (rectSelectionTool != null)
            {
                // set the region of interest
                Rectangle selectionRectangle = ViewerSelectionRectangle;
                ProcessingCommandWithRegion commandWorkWithRegion = command as ProcessingCommandWithRegion;
                if (commandWorkWithRegion != null)
                {
                    commandWorkWithRegion.RegionOfInterest = new RegionOfInterest(selectionRectangle.Left, selectionRectangle.Top, selectionRectangle.Width, selectionRectangle.Height);
                }
                else if (command is DrawImageCommand)
                {
                    ((DrawImageCommand)command).DestRect = selectionRectangle;
                }
                else if (!selectionRectangle.IsEmpty)
                {
                    MessageBox.Show("Selected image processing command cannot work with regions. Entire image will be processed.",
                                    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (customSelectionTool != null)
            {
                RectangleF selectionBBox = RectangleF.Empty;
                if (customSelectionTool.Selection != null)
                {
                    selectionBBox = customSelectionTool.Selection.GetBoundingBox();
                }
                if (selectionBBox.Width >= 1 && selectionBBox.Height >= 1)
                {
                    if (command is ChangePixelFormatCommand ||
                        command is ChangePixelFormatToBgrCommand ||
                        command is ChangePixelFormatToBlackWhiteCommand ||
                        command is ChangePixelFormatToGrayscaleCommand ||
                        command is ChangePixelFormatToPaletteCommand ||
                        command is RotateCommand ||
                        command is ResampleCommand ||
                        command is ResizeCommand ||
                        !command.CanModifyImage)
                    {
                        MessageBox.Show("Selected image processing command cannot work with custom selection. Entire image will be processed.",
                                        "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        GraphicsPath path       = customSelectionTool.Selection.GetAsGraphicsPath();
                        RectangleF   pathBounds = path.GetBounds();
                        if (pathBounds.Width > 0 && pathBounds.Height > 0)
                        {
                            if (command is CropCommand)
                            {
                                // crop to custom selection
                                command = GetCropToPathCommand(path, pathBounds, (CropCommand)command);
                                // clear selection
                                customSelectionTool.Selection = null;
                            }
                            else
                            {
                                // process path
                                command = new ProcessPathCommand(command, path);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Selected path is empty.",
                                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return(false);
                        }
                    }
                }
            }

            // get a reference to the image for processing
            VintasoftImage imageToProcess = _viewer.Image;

            ProcessingCommandBase executeCommand = command;

            if (_executeMultithread)
            {
                ParallelizingProcessingCommand parallelizingCommand = new ParallelizingProcessingCommand(command);
                if (command is ProcessingCommandWithRegion)
                {
                    parallelizingCommand.RegionOfInterest = ((ProcessingCommandWithRegion)command).RegionOfInterest;
                }
                executeCommand = parallelizingCommand;
            }

            if (UndoManager != null)
            {
                _imageProcessingUndoMonitor = new ImageProcessingUndoMonitor(UndoManager, executeCommand);
            }

            // subscribe to the events of the image processing command
            executeCommand.Started  += new EventHandler <ImageProcessingEventArgs>(command_Started);
            executeCommand.Progress += new EventHandler <ImageProcessingProgressEventArgs>(command_Progress);
            executeCommand.Finished += new EventHandler <ImageProcessedEventArgs>(command_Finished);

            executeCommand.ExpandSupportedPixelFormats = ExpandSupportedPixelFormats;
            executeCommand.RestoreSourcePixelFormat    = false;

            // specify that image processing command is working (several commands cannot work together)
            _isImageProcessingWorking = true;
            // get the start time of the image processing command
            _processingCommandStartTime = DateTime.Now;

            // if image processing command should be executed asynchronously
            if (async)
            {
                // start the image processing command asynchronously
                ProcessingCommandTask executor = new ProcessingCommandTask(executeCommand, imageToProcess);
                executor.ImageProcessingExceptionOccurs += new EventHandler(executor_ImageProcessingExceptionOccurs);
                Thread thread = new Thread(executor.Execute);
                thread.IsBackground = true;
                thread.Start();
            }
            // if image processing command should be executed synchronously
            else
            {
                try
                {
                    // execute the image processing command synchronously
                    executeCommand.ExecuteInPlace(imageToProcess);
                }
                catch (Exception ex)
                {
                    executor_ImageProcessingExceptionOccurs(this, EventArgs.Empty);
                    DemosTools.ShowErrorMessage(ex);
                    return(false);
                }
            }

            return(true);
        }