C # Image Processing: Color adjustment

Source: Internet
Author: User

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.