As mentioned in the previous article, the principle of image brightness adjustment is that the three color components of each pixel increase or decrease a fixed value at the same time. Now, what if the increase or decrease values of three pixels are not the same? Well, this is the topic to be discussed today.
It should be easy to understand that if we need a picture to be reddish, we only need to increase the red weight of each pixel. For a more practical scenario, for example, to process a colorful photo as an old black/white photo with a pan-brown color, the simplest method is to process it into a 256-level grayscale image and then increase the red weight of each pixel.
Old Rules: Give functions.
/** // <Summary>
/// Color adjustment
/// </Summary>
/// <Param name = "BMP"> original graph </param>
/// <Param name = "rval"> r increment </param>
/// <Param name = "gval"> G increment </param>
/// <Param name = "bval"> incremental B </param>
/// <Returns> processed graph </returns>
Public static bitmap kicolorbalance (bitmap BMP, int rval, int gval, int bval)
...{
If (BMP = NULL)
...{
Return NULL;
}
Int H = BMP. height;
Int W = BMP. width;
Try
...{
If (rval> 255 | rval <-255 | gval> 255 | gval <-255 | bval> 255 | bval <-255)
...{
Return NULL;
}
Bitmapdata srcdata = BMP. lockbits (New rectangle (0, 0, W, h), imagelockmode. readwrite, pixelformat. format24bpprgb );
Unsafe
...{
Byte * P = (byte *) srcdata. scan0.topointer ();
Int noffset = srcdata. stride-W * 3;
Int R, G, B;
For (INT y = 0; y ...{
For (INT x = 0; x <W; X ++)
...{
B = P [0] + bval;
If (bval> = 0)
...{
If (B> 255) B = 255;
}
Else
...{
If (B <0) B = 0;
}
G = P [1] + gval;
If (gval> = 0)
...{
If (G> 255) G = 255;
}
Else
...{
If (G <0) G = 0;
}
R = P [2] + rval;
If (rval> = 0)
...{
If (r> 255) r = 255;
}
Else
...{
If (r <0) r = 0;
}
P [0] = (byte) B;
P [1] = (byte) g;
P [2] = (byte) R;
P + = 3;
}
P + = noffset;
}
} // End of unsafe
BMP. unlockbits (srcdata );
Return BMP;
}
Catch
...{
Return NULL;
}
} // End of color