Exemplo n.º 1
0
        public virtual double GetMaximumProgress(int imageWidth, int imageHeight)
        {
            if (this.decoratingKernel != null)
            {
                return(decoratingKernel.GetMaximumProgress(imageWidth, imageHeight));
            }

            return(0.0);
        }
Exemplo n.º 2
0
        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
                }));
            });
        }