Пример #1
0
        /// <summary>
        /// Controla la aplición del filtro de remplazo de color
        /// </summary>
        /// <param name="filterInfo"> Objeto que contiene los datos que el filtro requiere para operar</param>
        /// <returns>String en base 64 una vez que el filtro fue aplicado</returns>
        public string applyColorSubstitution(ChangeColorInfo filterInfo)
        {
            Image  image       = imageManager.decodeImage(filterInfo.image);
            Bitmap imagePixels = new Bitmap(image);

            return(colorSubstitution(imagePixels, filterInfo));
        }
Пример #2
0
        /// <summary>
        /// Remplaza un pixel por uno que tiene el nuevo color especificado, la similitud entre un pixel
        /// y el color a remplazar tiene un grado de tolerancia que posee threshold.
        ///
        /// </summary>
        /// <param name="image"> Mapa de bits de la imagen a procesar</param>
        /// <param name="filterInfo">Objeto con la información que requiere el filtro(viejo color,nuevo color, threshold)</param>
        /// <returns>String en base 64 una vez que el filtro fue aplicado</returns>
        public string colorSubstitution(Bitmap image, ChangeColorInfo filterInfo)
        {
            string[] colorInfo = filterInfo.oldColor.Split(',');

            Color sourceColor = Color.FromArgb(Convert.ToInt32(colorInfo[0]), Convert.ToInt32(colorInfo[1]), Convert.ToInt32(colorInfo[2]), Convert.ToInt32(colorInfo[3]));

            colorInfo = filterInfo.newColor.Split(',');
            Color newColor = Color.FromArgb(Convert.ToInt32(colorInfo[0]), Convert.ToInt32(colorInfo[1]), Convert.ToInt32(colorInfo[2]), Convert.ToInt32(colorInfo[3]));

            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;



            int colorThreshold = Convert.ToInt32(filterInfo.threshold.ToString());



            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;
                    }
                }
            }

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


            bmpNew.UnlockBits(bmpData);

            bmpData      = null;
            resultBuffer = null;

            return(imageManager.encodeImage(bmpNew));
        }