1

as the title says i am trying to make a darken effect(like it's in photoshop). So far i have 2 pictureboxes, one for each picture, and then the third picturebox for the output. Output should be a bitmap of the first two with the darken effect applied on them. Looks something like this - https://i.stack.imgur.com/zeS88.png

if (pixelA <= pixelB) {pixelC = pixelA}
if (pixelA > pixelB) {pixelC = pixelB}

This is the main logic of darken effect, but i dont know how to implement it in my program. This is my Form1 class

public void applyEffect(string Method)
        {
            imgData3 = new imgData(imgData1.img.GetLength(0), imgData1.img.GetLength(1));

        for (int x = 0; x < imgData1.img.GetLength(0); x++)
        {
            for (int y = 0; y < imgData1.img.GetLength(1); y++)
            {
                imgData3.img[x, y] = new rgbiPixels();
                switch(Method)
                {
                    case "Darken":
                        {
                            imgData3.img[x, y].effectDarken(imgData1.img[x, y], imgData2.img[x, y]);
                            break;
                        }
                }
            }
        }
        pictureBox3.Image = imgData3.drawImage();
    }

And this is my rgbiPixels class where i should darken just one pixel, because the rest of them are taken care in Form1 class.

public class rgbiPixels
    {
        public byte R;
        public byte G;
        public byte B;
        public byte I;

    public rgbiPixels()
    {
        R = 0;
        G = 0;
        B = 0;
        I = 0;
    }

    public rgbiPixels(byte r, byte g, byte b)
    {
        R = r;
        G = g;
        B = b;
        I = (byte)Math.Round(0.0722f * b + 0.7152f * g + 0.2126f * r);
    }


    //--------------------------------------------------
    public void effectDarken(rgbiPixels a, rgbiPixels b)
    {
        R = Convert.ToByte(a.R + b.R);
        G = Convert.ToByte(a.G + b.G);
        B = Convert.ToByte(a.B + b.B);
    }
    //--------------------------------------------------
}

So my question is how can i implement my logic into rgbiPixels class?

hazmatsuit
  • 57
  • 1
  • 7
  • the pseudocode on top of your post has it already. compare the intensities (`I` component) and then decide if you let `effectDarken` return `a` or `b`, accordingly: `return (a.I <= b.I) ? a : b;` – Cee McSharpface May 10 '17 at 14:11
  • This c++ code is well worth studying. It contains code for all blend modes and I found it easy to adapt to c#. – TaW May 10 '17 at 14:13
  • @TaW what C++ code? – Romano Zumbé May 10 '17 at 14:19
  • Whoops, the link has got away. [Here is is](http://stackoverflow.com/questions/5919663/how-does-photoshop-blend-two-images-together) - And here is my darken snippet: `case Mode.Darken: c3 = Color.FromArgb((byte)(Math.Min(c1.R, c2.R)), (byte)(Math.Min(c1.G, c2.G)), (byte)(Math.Min(c1.B, c2.B)));` – TaW May 10 '17 at 14:52
  • created a radio button to make it all work, but it doesnt
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                pictureBox3.Image = imgData3.drawImage();
            } 
    – hazmatsuit May 10 '17 at 15:57

1 Answers1

0

According to your pseudo code specification, the effectDarken should be a function, should compare pixel intensities, and can be implemented like this:

public rgbiPixels effectDarken(rgbiPixels a, rgbiPixels b)
{
    return (a.I <= b.I) ? a : b;
}

If you keep the void method, the resulting color will not have a correct value for I because it is only computed in the constructor (you might not care, just as a precaution).

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77