/**************** 以上是 频域滤波 的处理部分 ****************/

        /**************** 以下是 选项卡切换选项 的处理部分 ****************/

        /*
         * 参考资料:
         * 响应,切换选项卡:
         * [Is there Selected Tab Changed Event in the standard WPF Tab Control](http://stackoverflow.com/questions/772841/is-there-selected-tab-changed-event-in-the-standard-wpf-tab-control)
         */

        private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(e.Source is TabControl) || this.imgSrc == null)
            {
                return;
            }

            if (tabItem_Origin.IsSelected)
            {
                OriginImg(this.imgSrc);
            }
            else if (tabItem_Quantization.IsSelected)
            {
                this.imageView.Source
                      = this.imgRes
                      = new Bgra32Image(this.bgraImgSrc).Quantize((uint)this.sliderQuantization.Value).ToBitmapSource();
            }
            else if (tabItem_Sampling.IsSelected)
            {
                this.imageView.Source
                      = this.imgRes
                      = new Bgra32Image(this.bgraImgSrc).Sample((uint)this.sliderSampling.Value).ToBitmapSource();
            }
            else if (tabItem_Binary.IsSelected)
            {
                this.imageView.Source
                      = this.imgRes
                      = new Gray8Image(this.gray8ImgSrc).GrayProcessing((uint)this.sliderGray8.Value).ToBitmapSource();
            }
            else if (tabItem_Img2txt.IsSelected)
            {
                this.imgTxt = ImgFunc.Bitmap2string(this.imgRes);
            }
            else if (tabItem_PointOps.IsSelected)
            {
                this.imageView.Source
                      = this.imgRes
                      = new Gray8Image(this.gray8ImgSrc).GrayProcessing(0).ToBitmapSource();
            }
            else if (tabItem_HistogramEqualization.IsSelected)
            {
                this.imageView.Source
                      = this.imgRes
                      = new Bgra32Image(this.bgraImgSrc).HistogramEqualization().ToBitmapSource();
            }
            else if (tabItem_Transform.IsSelected)
            {
                this.TransformReset();
            }
            else if (tabItem_Filter.IsSelected)
            {
                /* 更新 数据表格 */
                datagrid_FilterMatrix.ItemsSource = ImgFunc.ConvertArray2DToDataTable(filter_matrix).AsDataView();
            }

            UpdateHistogram();
        }
        private void buttonFilter_Click(object sender, RoutedEventArgs e)
        {
            if (!(e.Source is Button))
            {
                return;
            }

            if (e.Source == button_FilterIncreaseCol)
            {
                filter_matrix_wid += 2;
                if (filter_matrix_wid > 1)
                {
                    button_FilterReduceCol.IsEnabled = true;
                }
            }
            else if (e.Source == button_FilterReduceCol)
            {
                filter_matrix_wid -= 2;
                if (filter_matrix_wid <= 1)
                {
                    button_FilterReduceCol.IsEnabled = false;
                }
            }
            else if (e.Source == button_FilterIncreaseRow)
            {
                filter_matrix_hgt += 2;
                if (filter_matrix_hgt > 1)
                {
                    button_FilterReduceRow.IsEnabled = true;
                }
            }
            else if (e.Source == button_FilterReduceRow)
            {
                filter_matrix_hgt -= 2;
                if (filter_matrix_hgt <= 1)
                {
                    button_FilterReduceRow.IsEnabled = false;
                }
            }

            filter_matrix = ImgFunc.ResizeArray2D(filter_matrix, filter_matrix_wid, filter_matrix_hgt);

            datagrid_FilterMatrix.ItemsSource = ImgFunc.ConvertArray2DToDataTable(filter_matrix).AsDataView();
        }
        private void comboBox_Filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.Source == comboBox_Filter)
            {
                switch (comboBox_Filter.SelectedIndex)
                {
                case 0: { filter_matrix = new double[, ] {
                              { 0, -1, 0 },
                              { -1, 4, -1 },
                              { 0, -1, 0 },
                          }; break; }

                case 1: { filter_matrix = new double[, ] {
                              { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                              { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                              { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                          }; break; }

                case 2: { filter_matrix = new double[, ] {
                              { 1 / 10.0, 1 / 10.0, 1 / 10.0 },
                              { 1 / 10.0, 2 / 10.0, 1 / 10.0 },
                              { 1 / 10.0, 1 / 10.0, 1 / 10.0 },
                          }; break; }

                case 3: { filter_matrix = new double[, ] {
                              { 1 / 16.0, 1 / 8.0, 1 / 16.0 },
                              { 1 / 8.0, 1 / 4.0, 1 / 8.0 },
                              { 1 / 16.0, 1 / 8.0, 1 / 16.0 },
                          }; break; }

                case 4: { filter_matrix = new double[, ] {
                              { 0, -1, 0 },
                              { -1, 5, -1 },
                              { 0, -1, 0 },
                          }; break; }

                case 5: { filter_matrix = new double[, ] {
                              { -1, -1, -1 },
                              { -1, 9, -1 },
                              { -1, -1, -1 },
                          }; break; }

                case 6: { filter_matrix = new double[, ] {
                              { 1, -2, 1 },
                              { -2, 5, -2 },
                              { 1, -2, 1 },
                          }; break; }

                case 7: { filter_matrix = new double[, ] {
                              { 0, 0, 0 },
                              { -1, 1, 0 },
                              { 0, 0, 0 },
                          }; break; }

                case 8: { filter_matrix = new double[, ] {
                              { 0, -1, 0 },
                              { 0, 1, 0 },
                              { 0, 0, 0 },
                          }; break; }

                case 9: { filter_matrix = new double[, ] {
                              { -1, 0, 0 },
                              { 0, 1, 0 },
                              { 0, 0, 0 },
                          }; break; }

                case 10: { filter_matrix = new double[, ] {
                               { -1, -1, -1, -1, -1 },
                               { 0, 0, 0, 0, 0 },
                               { 1, 1, 1, 1, 1 },
                           }; break; }

                case 11: { filter_matrix = new double[, ] {
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                           }; break; }

                case 12: { filter_matrix = new double[, ] {
                               { -1, 0, -1 },
                               { 0, 4, 0 },
                               { -1, 0, -1 },
                           }; break; }

                case 13: { filter_matrix = new double[, ] {
                               { -1, -1, -1 },
                               { -1, 8, -1 },
                               { -1, -1, -1 },
                           }; break; }

                case 14: { filter_matrix = new double[, ] {
                               { -1, -1, -1 },
                               { -1, 9, -1 },
                               { -1, -1, -1 },
                           }; break; }

                case 15: { filter_matrix = new double[, ] {
                               { 1, -2, -1 },
                               { -2, 4, -2 },
                               { 1, -2, -1 },
                           }; break; }

                default: break;
                }

                datagrid_FilterMatrix.ItemsSource = ImgFunc.ConvertArray2DToDataTable(filter_matrix).AsDataView();
                if (realtimeProcessing)
                {
                    buttonFilterCalc_Click(null, null);
                }
            }
            else if (e.Source == comboBox_FilterOther)
            {
                BitmapSource img = null;
                switch (comboBox_FilterOther.SelectedIndex)
                {
                case 0: img = new Bgra32Image(this.bgraImgSrc).Filter("median").ToBitmapSource(); break;

                case 1: img = new Bgra32Image(this.bgraImgSrc).Filter("roberts").ToBitmapSource(); break;

                case 2: img = new Bgra32Image(this.bgraImgSrc).Filter("sobel").ToBitmapSource(); break;
                }
                if (img != null)
                {
                    this.imageView.Source = this.imgRes = img;
                }
            }
        }