Пример #1
0
        //Aumenta o disminuye la intensidad de un color. Tiene en cuenta problemas de saturación.
        //Entra: string color: rojo, verdo o azul. Porcentage en %. bool aumentar = true. false= disminuye.
        //Devuelve 0 si ha intesificado correctamente, -1 color incorrecto, -2 porcentaje incorrecto.
        public int IntensificarColor(string color, float porcentaje, bool aumentar)
        {
            if (porcentaje <= 0 || porcentaje > 100)
            {
                return(-2);
            }
            if (aumentar == true)
            {
                porcentaje = 1 + (porcentaje / 100);
            }
            else
            {
                porcentaje = 1 - (porcentaje / 100);
            }

            Pixel[,] icMatrix = new Pixel[this.alto, this.ancho];
            for (int i = 0; i < this.alto; i++)
            {
                for (int j = 0; j < this.ancho; j++)
                {
                    switch (color)
                    {
                    case "Rojo":
                        byte  R;
                        float rojoFinal = this.datos[i, j].GetR() * porcentaje;
                        if (rojoFinal > 255)
                        {
                            R = 255;
                        }
                        else
                        {
                            R = Convert.ToByte(rojoFinal);
                        }
                        icMatrix[i, j] = new Pixel(R, this.datos[i, j].GetG(), this.datos[i, j].GetB());
                        break;

                    case "Verde":
                        byte  G;
                        float verdeFinal = this.datos[i, j].GetG() * porcentaje;
                        if (verdeFinal > 255)
                        {
                            G = 255;
                        }
                        else
                        {
                            G = Convert.ToByte(verdeFinal);
                        }
                        icMatrix[i, j] = new Pixel(this.datos[i, j].GetR(), G, this.datos[i, j].GetB());
                        break;

                    case "Azul":
                        byte  B;
                        float azulFinal = this.datos[i, j].GetB() * porcentaje;
                        if (azulFinal > 255)
                        {
                            B = 255;
                        }
                        else
                        {
                            B = Convert.ToByte(azulFinal);
                        }
                        icMatrix[i, j] = new Pixel(this.datos[i, j].GetR(), this.datos[i, j].GetG(), B);
                        break;

                    default:
                        return(-1);
                    }
                }
            }
            this.datos = icMatrix;
            return(0);
        }
Пример #2
0
        public Pixel ObtenerCopia()
        {
            Pixel p = new Pixel(this.R, this.G, this.B);

            return(p);
        }
Пример #3
0
        //Cargar imagen PPM en memoria
        //Devuelve 0: si ha sido cargada correctamente; -1: No encontrado; -2: Formato incorrecto.
        public int CargarPPM()
        {
            string id;

            string[] lineaDim, lineaDatos;
            byte     r, g, b, n;

            Pixel[,] tmpMatrix;
            using (StreamReader R = new StreamReader(this.archivo))
            {
                try
                {
                    id = R.ReadLine();
                    if (id != "P1" && id != "P2" && id != "P3" && id != "P4" && id != "P5" && id != "P6")
                    {
                        return(-2);
                    }
                    this.identificador = id;
                    lineaDim           = R.ReadLine().Split(' ');
                    if (lineaDim.Length != 2)
                    {
                        return(-2);
                    }
                    this.alto  = Convert.ToInt32(lineaDim[1]);
                    this.ancho = Convert.ToInt32(lineaDim[0]);
                    n          = Convert.ToByte(R.ReadLine());
                    if (n > 255)
                    {
                        return(-2);
                    }
                    this.niveles = n;
                    tmpMatrix    = new Pixel[this.alto, this.ancho];
                    for (int i = 0; R.Peek() >= 0; i++)
                    {
                        lineaDatos = R.ReadLine().Split(' ');
                        if (lineaDatos.Length != this.ancho * 3)
                        {
                            return(-2);
                        }
                        for (int j = 0; j < lineaDatos.Length; j += 3)
                        {
                            r = Convert.ToByte(lineaDatos[j]);
                            g = Convert.ToByte(lineaDatos[j + 1]);
                            b = Convert.ToByte(lineaDatos[j + 2]);
                            tmpMatrix[i, j / 3] = new Pixel(r, g, b);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ex is FileNotFoundException)
                    {
                        return(-1);
                    }
                    if (ex is FormatException)
                    {
                        return(-2);
                    }
                    return(-1);
                }
                this.datos = tmpMatrix;
                R.Close();
                return(0);
            }
        }