Beispiel #1
0
        private void ConvolutionFilterEntry_Checked(object sender, RoutedEventArgs e)
        {
            int         index = -1;
            RadioButton btn   = sender as RadioButton;

            if (btn != null)
            {
                object item = btn.DataContext;
                if (item != null)
                {
                    index = ConvolutionFilterEntriesList.Items.IndexOf(item);
                }
            }

            //FilterFunctionGraphViewer.Visibility = Visibility.Collapsed;
            //FilterFunctionSettings.Visibility = Visibility.Collapsed;
            SelectedFilterEntry = convolutionFilterEntries[index];
        }
Beispiel #2
0
        private void ConvolutionFilterDeleteButton_Click(object sender, RoutedEventArgs e)
        {
            int    index = -1;
            Button btn   = sender as Button;

            if (btn != null)
            {
                object item = ((btn.Parent as Grid).Parent as RadioButton).DataContext;
                if (item != null)
                {
                    index = ConvolutionFilterEntriesList.Items.IndexOf(item);
                }
            }

            if (convolutionFilterEntries[index] == SelectedFilterEntry)
            {
                SelectedFilterEntry = null;
            }

            convolutionFilterEntries.RemoveAt(index);
        }
Beispiel #3
0
        private void SetupModule()
        {
            originalImage = currentImage = new BitmapImage(new Uri("/Res/ClaymoreRoomba.png", UriKind.Relative));


            Viewer.Source = originalImage;
            FilterFunctionGraphViewer.Visibility          = Visibility.Collapsed;
            FilterFunctionSettings.Visibility             = Visibility.Collapsed;
            FilterFunctionDitheringSettings.Visibility    = Visibility.Collapsed;
            FilterFunctionQuantizationSettings.Visibility = Visibility.Collapsed;

            // ------------ Function filters ------------
            activeDraggingPoints = new List <Ellipse>();
            functionGraphPoints  = new PointCollection();

            functionFilterEntries = new ObservableCollection <FilterEntry>();
            FunctionFilterEntriesList.ItemsSource = functionFilterEntries;

            unsafe
            {
                functionFilterEntries.Add(new FilterEntry(FilterEntryType.Graph, "Inversion", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateGraph), new Graph(FilterSettings.inversionFunctionPoints.ConvertAll(p => new GraphPoint((int)p.Value.X, (int)p.Value.Y)))))));
                functionFilterEntries.Add(new FilterEntry(FilterEntryType.Graph, "Brightness Correction", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateGraph), new Graph(FilterSettings.brightnessCorrectionFunctionPoints.ConvertAll(p => new GraphPoint((int)p.Value.X, (int)p.Value.Y)))))));
                functionFilterEntries.Add(new FilterEntry(FilterEntryType.Graph, "Contrast Enhancement", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateGraph), new Graph(FilterSettings.contrastEnhancementFunctionPoints.ConvertAll(p => new GraphPoint((int)p.Value.X, (int)p.Value.Y)))))));

                functionFilterEntries.Add(new FilterEntry(FilterEntryType.Function, "Gamma Correction", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateGamma), FilterSettings.gammaCoefficient))));
                functionFilterEntries.Add(new FilterEntry(FilterEntryType.Function, "Grayscale", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateGrayscale)))));
                functionFilterEntries.Add(new FilterEntry(FilterEntryType.Function, "Average Dithering", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateAverageDithering), FilterSettings.averageDithering_k))));
                functionFilterEntries.Add(new FilterEntry(FilterEntryType.Function, "Octree Color Quantization", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateOctreeColorQuantization), FilterSettings.octreeColorQuantization_maxColors))));
            }


            // Add new filter button
            Button addNewFunctionFilterButton = new Button()
            {
            };

            addNewFunctionFilterButton.Content = "+";
            addNewFunctionFilterButton.Margin  = new Thickness(3);
            addNewFunctionFilterButton.Width   = 18;
            addNewFunctionFilterButton.Height  = 18;
            addNewFunctionFilterButton.Click  += (senderButton, argsButton) =>
            {
                unsafe
                {
                    FilterEntry newFunctionEntry = new FilterEntry(FilterEntryType.Graph, "Custom", new FunctionFilter(new FunctionFormula(new FilterSettings.FunctionFormula_Formula(FilterSettings.CalculateGraph), new Graph(FilterSettings.identityFunctionPoints.ConvertAll(p => new GraphPoint((int)p.Value.X, (int)p.Value.Y))))));
                    functionFilterEntries.Add(newFunctionEntry);
                }

                FunctionFilterListButton.Children.RemoveAt(FunctionFilterListButton.Children.Count - 1); // Remove button from stackpanel
                FunctionFilterListButton.Children.Add(addNewFunctionFilterButton);                       // Add button again to stackpanel
            };
            FunctionFilterListButton.Children.Add(addNewFunctionFilterButton);



            // ------------ Convolution filters ------------
            convolutionFilterEntries = new ObservableCollection <FilterEntry>();
            ConvolutionFilterEntriesList.ItemsSource = convolutionFilterEntries;

            convolutionFilterEntries.Add(new FilterEntry(FilterEntryType.Convolution, "Blur", new ConvolutionFilter(FilterSettings.blurKernel, FilterSettings.blurDivisor)));
            convolutionFilterEntries.Add(new FilterEntry(FilterEntryType.Convolution, "Gaussinan Blur", new ConvolutionFilter(FilterSettings.gaussianBlurKernel, FilterSettings.gaussianBlurDivisor)));
            convolutionFilterEntries.Add(new FilterEntry(FilterEntryType.Convolution, "Sharpen", new ConvolutionFilter(FilterSettings.sharpenKernel, FilterSettings.sharpenDivisor)));
            convolutionFilterEntries.Add(new FilterEntry(FilterEntryType.Convolution, "Edge Detection", new ConvolutionFilter(FilterSettings.edgeDetectionKernel, FilterSettings.edgeDetectionDivisor)));
            convolutionFilterEntries.Add(new FilterEntry(FilterEntryType.Convolution, "Emboss", new ConvolutionFilter(FilterSettings.embossKernel, FilterSettings.embossDivisor)));
        }
Beispiel #4
0
        private void FunctionFilterEntry_Checked(object sender, RoutedEventArgs e)
        {
            FilterFunctionSettings.Visibility = Visibility.Visible;

            int         index = -1;
            RadioButton btn   = sender as RadioButton;

            if (btn != null)
            {
                object item = btn.DataContext;
                if (item != null)
                {
                    index = FunctionFilterEntriesList.Items.IndexOf(item);
                }
            }

            SelectedFilterEntry = functionFilterEntries[index];


            if (SelectedFilterEntry.Name == "Average Dithering") // Load function K settings
            {
                FilterFunctionDitheringSettings.Visibility = Visibility.Visible;
            }

            if (SelectedFilterEntry.Name == "Octree Color Quantization") // Load function color settings
            {
                FilterFunctionQuantizationSettings.Visibility = Visibility.Visible;
            }

            if (SelectedFilterEntry.Type == FilterEntryType.Graph) // Load function graph viewer if function graph is available in filter
            {
                FunctionFormula selectedFunctionGraph = (((SelectedFilterEntry.Filter as FunctionFilter).Function as FunctionFormula));

                //// Draw graph
                FilterFunctionGraphViewer.Visibility = Visibility.Visible;

                // Clear previous graph
                for (int p = activeDraggingPoints.Count - 1; p >= 0; p--)
                {
                    FilterFunctionGraph.Children.Remove(activeDraggingPoints[p]);
                    activeDraggingPoints.Remove(activeDraggingPoints[p]);
                }

                if (functionGraph != null)
                {
                    FilterFunctionGraph.Children.Remove(functionGraph); // Remove line
                }
                functionGraphPoints.Clear();                            // Remove line

                // Add new dragging points
                for (int p = 0; p < (selectedFunctionGraph.otherFunctionParams[0] as Graph).points.Count; p++)
                {
                    CreateDraggingPoint(p);
                }

                Ellipse CreateDraggingPoint(int p)
                {
                    Ellipse draggingPoint = new Ellipse()
                    {
                    };

                    draggingPoint.Width        = DRAGGING_POINT_SIZE;
                    draggingPoint.Height       = DRAGGING_POINT_SIZE;
                    draggingPoint.Cursor       = Cursors.Hand;
                    draggingPoint.Fill         = new SolidColorBrush(Colors.Black);
                    draggingPoint.Fill.Opacity = 0;
                    draggingPoint.Stroke       = new SolidColorBrush(Colors.Black);

                    Point canvasPoint = CalculateCanvasPositionFromPointValue((selectedFunctionGraph.otherFunctionParams[0] as Graph).points[p]);

                    Canvas.SetLeft(draggingPoint, canvasPoint.X);
                    Canvas.SetTop(draggingPoint, canvasPoint.Y);
                    Panel.SetZIndex(draggingPoint, 5); // Higher means top

                    draggingPoint.MouseLeftButtonDown += (senderPoint, argsPoint) =>
                    {
                        activeDraggingPoint        = draggingPoint;
                        draggingPoint.Fill.Opacity = 1;

                        drag = true;                                                                     // start dragging
                        draggingStartPoint = Mouse.GetPosition(FilterFunctionGraph);                     // save start point of dragging
                        int draggedItemIndex = activeDraggingPoints.IndexOf(senderPoint as Ellipse);
                        if (draggedItemIndex != 0 && draggedItemIndex != activeDraggingPoints.Count - 1) //If not first or last
                        {
                            previousDraggingPoint = activeDraggingPoints[draggedItemIndex - 1];
                            nextDraggingPoint     = activeDraggingPoints[draggedItemIndex + 1];
                        }
                    };
                    draggingPoint.MouseMove += (senderPoint, argsPoint) =>
                    {
                        // if dragging, then adjust rectangle position based on mouse movement
                        if (drag)
                        {
                            Ellipse draggedItem      = senderPoint as Ellipse;
                            int     draggedItemIndex = activeDraggingPoints.IndexOf(senderPoint as Ellipse);

                            Point newPoint = Mouse.GetPosition(FilterFunctionGraph);

                            // Restrain dragging
                            if (newPoint.Y < 0)
                            {
                                newPoint.Y = 0;
                            }
                            if (newPoint.Y > FilterFunctionGraph.ActualHeight + DRAGGING_POINT_SIZE / 2)
                            {
                                newPoint.Y = FilterFunctionGraph.ActualHeight + DRAGGING_POINT_SIZE / 2;
                            }

                            if (draggedItemIndex != 0 && draggedItemIndex != activeDraggingPoints.Count - 1) //If not first or last
                            {
                                double previousDraggingPointX = Canvas.GetLeft(previousDraggingPoint) + MINIMAL_DRAGGING_POINT_MARGIN + DRAGGING_POINT_SIZE / 2;
                                double nextDraggingPointX     = Canvas.GetLeft(nextDraggingPoint) - MINIMAL_DRAGGING_POINT_MARGIN + DRAGGING_POINT_SIZE / 2;
                                if (newPoint.X < previousDraggingPointX)
                                {
                                    newPoint.X = previousDraggingPointX;
                                }
                                if (newPoint.X > nextDraggingPointX)
                                {
                                    newPoint.X = nextDraggingPointX;
                                }
                            }

                            double left = Canvas.GetLeft(draggedItem);
                            double top  = Canvas.GetTop(draggedItem);

                            double newLeft = left;
                            if (draggedItemIndex != 0 && draggedItemIndex != activeDraggingPoints.Count - 1) //If not first or last
                            {
                                newLeft += (newPoint.X - draggingStartPoint.X);
                            }

                            double newTop = top + (newPoint.Y - draggingStartPoint.Y);
                            Canvas.SetLeft(draggedItem, newLeft);
                            Canvas.SetTop(draggedItem, newTop);

                            // Update line
                            functionGraphPoints[draggedItemIndex] = new Point(newLeft + DRAGGING_POINT_SIZE / 2, newTop + DRAGGING_POINT_SIZE / 2);
                            functionGraph.Points = functionGraphPoints;


                            draggingStartPoint = newPoint;
                        }
                    };
                    draggingPoint.MouseLeftButtonUp += (senderPoint, argsPoint) =>
                    {
                        StopDraggingPoint(senderPoint, selectedFunctionGraph);
                    };
                    draggingPoint.MouseLeave += (senderPoint, argsPoint) =>
                    {
                        StopDraggingPoint(senderPoint, selectedFunctionGraph);
                    };

                    FilterFunctionGraph.Children.Insert(p, draggingPoint); // Draw points on canvas
                    activeDraggingPoints.Insert(p, draggingPoint);

                    return(draggingPoint);
                }

                // Draw line on canvas
                functionGraphPoints = new PointCollection();
                for (int i = 0; i < activeDraggingPoints.Count; i++)
                {
                    Ellipse draggingPoint         = activeDraggingPoints[i];
                    Point   draggingPointPosition = new Point(Canvas.GetLeft(draggingPoint) + DRAGGING_POINT_SIZE / 2, Canvas.GetTop(draggingPoint) + DRAGGING_POINT_SIZE / 2);
                    functionGraphPoints.Add(draggingPointPosition);
                }
                functionGraph                 = new Polyline();
                functionGraph.Points          = functionGraphPoints;
                functionGraph.Stroke          = new SolidColorBrush(Colors.Black);
                functionGraph.StrokeThickness = 1.5;

                functionGraph.Cursor = Cursors.Hand;

                functionGraph.MouseLeftButtonDown += (object sender2, MouseButtonEventArgs e2) => {
                    Point mousePosition = e2.GetPosition(FilterFunctionGraph);

                    //Calculate new point value
                    Point newGraphPoint = CalculatePointValueFromCanvasPosition(new Point(mousePosition.X - DRAGGING_POINT_SIZE / 2, mousePosition.Y + DRAGGING_POINT_SIZE / 2));

                    // Add new point to graph function
                    int graphPointIndex = (((SelectedFilterEntry.Filter as FunctionFilter).Function as FunctionFormula).otherFunctionParams[0] as Graph).AddPoint(new GraphPoint((int)newGraphPoint.X, (int)newGraphPoint.Y));

                    Ellipse draggingPoint = CreateDraggingPoint(graphPointIndex);

                    //Deselect active dragging point and select this one
                    SelectActiveDraggingPoint(draggingPoint);

                    // Update line
                    Point draggingPointPosition = new Point(Canvas.GetLeft(draggingPoint) + DRAGGING_POINT_SIZE / 2, Canvas.GetTop(draggingPoint) + DRAGGING_POINT_SIZE / 2);
                    functionGraphPoints.Insert(graphPointIndex, draggingPointPosition);
                    functionGraph.Points = functionGraphPoints;
                };
                FilterFunctionGraph.Children.Add(functionGraph);
            }
            else // For filters that do not have function graph available
            {
                FilterFunctionGraphViewer.Visibility = Visibility.Collapsed;
            }
        }