public Bitmap colorSubstitution(Bitmap image, ColorSubstitutionFilter changer)
        {
            Bitmap bmpNew = new Bitmap(image);

            BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);


            IntPtr ptr = bmpData.Scan0;


            byte[] resultBuffer = new byte[bmpData.Stride * bmpNew.Height];


            Marshal.Copy(ptr, resultBuffer, 0, resultBuffer.Length);

            int limit = resultBuffer.Length;

            //almacenamiento de variables temporales

            byte sourceRed = 0, sourceGreen = 0, sourceBlue = 0, sourceAlpha = 0;
            int  resultRed = 0, resultGreen = 0, resultBlue = 0;

            byte minValue = 0;
            byte maxValue = 255;

            Color sourceColor = changer.getSourceColor();

            Color newColor = changer.getNewColor();

            int colorThreshold = changer.getThreshold();



            for (int k = 0; k < limit; k += 4)
            {
                sourceAlpha = resultBuffer[k + 3];

                if (sourceAlpha != 0)
                {
                    //obtener valores del pixel
                    sourceBlue  = resultBuffer[k];
                    sourceGreen = resultBuffer[k + 1];
                    sourceRed   = resultBuffer[k + 2];


                    if ((sourceBlue < sourceColor.B + colorThreshold &&
                         sourceBlue > sourceColor.B - colorThreshold) &&


                        (sourceGreen < sourceColor.G + colorThreshold &&
                         sourceGreen > sourceColor.G - colorThreshold) &&


                        (sourceRed < sourceColor.R + colorThreshold &&
                         sourceRed > sourceColor.R - colorThreshold))
                    {
                        resultBlue = sourceColor.B - sourceBlue + newColor.B;


                        if (resultBlue > maxValue)
                        {
                            resultBlue = maxValue;
                        }
                        else if (resultBlue < minValue)
                        {
                            resultBlue = minValue;
                        }


                        resultGreen = sourceColor.G - sourceGreen + newColor.G;


                        if (resultGreen > maxValue)
                        {
                            resultGreen = maxValue;
                        }
                        else if (resultGreen < minValue)
                        {
                            resultGreen = minValue;
                        }


                        resultRed = sourceColor.R - sourceRed + newColor.R;


                        if (resultRed > maxValue)
                        {
                            resultRed = maxValue;
                        }
                        else if (resultRed < minValue)
                        {
                            resultRed = minValue;
                        }


                        resultBuffer[k]     = (byte)resultBlue;
                        resultBuffer[k + 1] = (byte)resultGreen;
                        resultBuffer[k + 2] = (byte)resultRed;
                        resultBuffer[k + 3] = sourceAlpha;
                    }
                }
            }

            Console.WriteLine("SE aplicó un filtro a la imagen");
            Marshal.Copy(resultBuffer, 0, ptr, resultBuffer.Length);
            bmpNew.UnlockBits(bmpData);

            bmpData      = null;
            resultBuffer = null;

            saveImage(bmpNew);

            return(bmpNew);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Permite dirigir el programa al filtro que el usuario quiere aplicar.
        /// </summary>
        private void filterTypeImage()
        {
            MessageBox.Show("    Aplicando filtro    ", "Notificación", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            Bitmap    imagePixels = new Bitmap(imageContainer.Image);
            Stopwatch watch       = new Stopwatch();
            bool      local       = simpleModeB.Checked;

            updateTime(0, " ");
            updateTime(1, " ");
            Image newImage;

            if (local)
            {
                Console.WriteLine(filterIndex);
                switch (filterIndex)
                {
                case 0:
                    watch.Start();
                    newImage = traditionalFilter.sepiaFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    optimizedFilter.sepiaFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 1:
                    watch.Start();
                    newImage = traditionalFilter.grayScaleFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    optimizedFilter.grayScaleFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 2:
                    try
                    {
                        watch.Start();
                        newImage = traditionalFilter.opacityFilter(imagePixels, Convert.ToDouble(filterPercentage.Text));
                        watch.Stop();
                        this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                        watch.Start();
                        optimizedFilter.opacityFilter(imagePixels, Convert.ToDouble(filterPercentage.Text));
                        watch.Stop();
                        this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                        this.imageContainer.Image = newImage;
                    }
                    catch
                    {
                        MessageBox.Show("Verifica el dato de entrada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;

                case 3:
                    watch.Start();
                    newImage = traditionalFilter.invertColorsFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    optimizedFilter.invertColorsFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 4:
                    watch.Start();
                    newImage = traditionalFilter.GaussinBlurFilter(imagePixels, new Rectangle(0, 0, imagePixels.Width, imagePixels.Height), 4);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    GaussianBlur gaussian = new GaussianBlur(imagePixels);
                    gaussian.Process(3);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 5:
                    try
                    {
                        watch.Start();
                        newImage = traditionalFilter.brightFilter(imagePixels, Convert.ToDouble(filterPercentage.Text) / 100);
                        watch.Stop();
                        this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                        watch.Start();
                        optimizedFilter.brightFilter(imagePixels, Convert.ToDouble(filterPercentage.Text) / 100);
                        watch.Stop();
                        this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                        this.imageContainer.Image = newImage;
                    }
                    catch
                    {
                        MessageBox.Show("Verifica el dato de entrada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;

                case 6:
                    watch.Start();
                    newImage = traditionalFilter.colorsBalance(imagePixels);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    optimizedFilter.colorsBalance(imagePixels);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 7:
                    watch.Start();
                    newImage = traditionalFilter.colorSubstitution(imagePixels, colorSubstitution);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    //esperar un segundo
                    Thread.Sleep(1000);
                    optimizedFilter.colorSubstitution(imagePixels, colorSubstitution);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 8:
                    try
                    {
                        watch.Start();
                        newImage = traditionalFilter.CrudeHighPass(imagePixels, Convert.ToInt32(filterPercentage.Text) / 100);
                        watch.Stop();
                        this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                        watch.Start();
                        optimizedFilter.CrudeHighPass(imagePixels, Convert.ToInt32(filterPercentage.Text) / 100);
                        watch.Stop();
                        this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                        this.imageContainer.Image = newImage;
                    }
                    catch
                    {
                        MessageBox.Show("Verifica el dato de entrada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;

                case 9:
                    watch.Start();
                    newImage = traditionalFilter.EdgeFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    optimizedFilter.EdgeFilter(imagePixels);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 10:
                    watch.Start();
                    newImage = traditionalFilter.solariseFilter(imagePixels, 25, 40, 52);
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    watch.Start();
                    optimizedFilter.solariseFilter(imagePixels, 25, 40, 52);
                    watch.Stop();
                    this.updateTime(2, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                default:
                    break;
                }
            }
            else
            {
                switch (filterIndex)
                {
                case 0:
                    watch.Start();
                    ConnectionManager.FilterName = "Sepia";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 1:
                    watch.Start();
                    ConnectionManager.FilterName = "GrayScale";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 2:
                    double opacityValue = 0;
                    try
                    {
                        opacityValue = double.Parse(filterPercentage.Text);
                        if (opacityValue <= 0 || opacityValue > 255)
                        {
                            MessageBox.Show("Error: el numero debe ser un entero de 0 a 255", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error: el numero debe ser un entero de 0 a 255", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    watch.Start();
                    ConnectionManager.ParamValue = opacityValue;
                    ConnectionManager.FilterName = "Opacity";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 3:
                    watch.Start();
                    ConnectionManager.FilterName = "InvertColors";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 4:
                    watch.Start();
                    ConnectionManager.FilterName = "GaussianBlur";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 5:
                    watch.Start();

                    double BrightValue = 0;
                    try
                    {
                        BrightValue = double.Parse(filterPercentage.Text);
                        if (BrightValue < 0 || BrightValue > 255)
                        {
                            MessageBox.Show("Error: Debe ser un numero entero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error: el numero debe ser un entero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    ConnectionManager.ParamValue = BrightValue;
                    ConnectionManager.FilterName = "Bright";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 6:
                    watch.Start();
                    ConnectionManager.FilterName = "ColorsBalance";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    this.updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 7:
                    Color sourceColor    = colorSubstitution.getSourceColor();
                    Color newColor       = colorSubstitution.getNewColor();
                    int   colorThreshold = colorSubstitution.getThreshold();

                    watch.Start();
                    ConnectionManager.FilterName = "ColorSubstitution";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.Colors.Clear();
                    ConnectionManager.Colors.Add(sourceColor);
                    ConnectionManager.Colors.Add(newColor);
                    ConnectionManager.ParamValue = colorThreshold;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 8:
                    watch.Start();
                    ConnectionManager.FilterName = "SolariseFilter";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 9:
                    watch.Start();
                    ConnectionManager.FilterName = "EdgeDetection";
                    ConnectionManager.Bitmap     = imagePixels;
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                case 10:
                    ConnectionManager.FilterName = "CrudeHighPass";
                    double darknessValue = 0;
                    Console.WriteLine(darknessValue);
                    try
                    {
                        darknessValue = double.Parse(filterPercentage.Text);
                        if (darknessValue <= 0 || darknessValue > 255)
                        {
                            MessageBox.Show("Error: el numero debe ser un entero de 0 a 255", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error: el numero debe ser un entero de 0 a 255", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    ConnectionManager.ParamValue = darknessValue;
                    ConnectionManager.Bitmap     = imagePixels;
                    watch.Start();
                    ConnectionManager.AddConnections(servers);
                    newImage = ConnectionManager.ApplyFilter();
                    watch.Stop();
                    updateTime(1, watch.Elapsed.TotalSeconds.ToString() + " s");
                    this.imageContainer.Image = newImage;
                    break;

                default:
                    break;
                }
            }
        }