public virtual Color[,] apply(Color[,] imageToProcess, MainViewModel reportProgressTo) { if (decoratingKernel != null) { imageToProcess = decoratingKernel.apply(imageToProcess, reportProgressTo); } int xOffset = (this.width - 1) / 2; int yOffset = (this.height - 1) / 2; Color[,] imageToReturn = new Color[imageToProcess.GetLength(0), imageToProcess.GetLength(1)]; Array.Copy(imageToProcess, imageToReturn, imageToProcess.GetLength(0) * imageToProcess.GetLength(1)); for (int x = xOffset; x < imageToProcess.GetLength(0) - xOffset; x++) // GetLength(x), where x is the dimension, give you the length of the specified part of the array. { for (int y = yOffset; y < imageToProcess.GetLength(1) - yOffset; y++) { int sum = processPixel(x, y, imageToProcess, reportProgressTo); //sum = sum < 0 ? 0 : sum > 255 ? 255 : sum; imageToReturn[x, y] = Color.FromArgb(sum, sum, sum); } } return(imageToReturn); }
public virtual Color[,] apply(Color[,] imageToProcess, MainViewModel reportProgressTo) { if (this.decoratingFilter != null) { return(decoratingFilter.apply(imageToProcess, reportProgressTo)); } return(imageToProcess); }
public void ApplyImage() { if (InputImage == null || decoratedFilter == null) { return; // Get out if no input image or filter has been selected } if (ProcessImage != null && !hasAppliedThisImage) { ProcessImage.Dispose(); // Reset output image ProcessImage = new Bitmap(InputImage.Size.Width, InputImage.Size.Height); } else if (ProcessImage == null) { ProcessImage = new Bitmap(InputImage.Size.Width, InputImage.Size.Height); } IsBusy = true; System.Drawing.Color[,] InputColors = new System.Drawing.Color[InputImage.Size.Width, InputImage.Size.Height]; System.Drawing.Color[,] OutputColors; if (hasAppliedThisImage) { for (int x = 0; x < ProcessImage.Size.Width; x++) { for (int y = 0; y < ProcessImage.Size.Height; y++) { InputColors[x, y] = ProcessImage.GetPixel(x, y); } } } else { for (int x = 0; x < InputImage.Size.Width; x++) { for (int y = 0; y < InputImage.Size.Height; y++) { InputColors[x, y] = InputImage.GetPixel(x, y); } } } HasProgress = Visibility.Visible; MaxProgress = decoratedFilter.GetMaximumProgress(InputImage.Size.Width, InputImage.Size.Height); ThreadPool.QueueUserWorkItem(o => { OutputColors = decoratedFilter.apply(InputColors, this); Application.Current.Dispatcher.BeginInvoke(new Action(() => { for (int x = 0; x < InputImage.Size.Width; x++) { for (int y = 0; y < InputImage.Size.Height; y++) { ProcessImage.SetPixel(x, y, OutputColors[x, y]); } } NewImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( // Display output image ProcessImage.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(ProcessImage.Size.Width, ProcessImage.Size.Height)); if (hasAppliedThisImage) { foreach (FilterType newAddition in currentFilterList) { oldFilterList.Add(newAddition); } } else { oldFilterList = currentFilterList; hasAppliedThisImage = true; } if (OutputImage != null) { OutputImage.Dispose(); } OutputImage = null; decoratedFilter = null; // Filter has been applied, a new one has to be made. Also fixes 'cached' filtering HasProgress = Visibility.Hidden; IsBusy = false; // Debug for Progressbar Console.WriteLine("Progress: {0}, MaxProgress: {1}", Progress, MaxProgress); Console.WriteLine("Procent: {0}", (Progress / MaxProgress) * 100); Progress = 0; // Reset progress })); }); }