Changing the brightness, contrast, and saturation of an image (Bitmap) in Delphi

Source: Internet
Author: User

Recently, I am working on video processing. I encountered some image processing problems and found the solution code elsewhere.

The code processing result of contrast adjustment looks uncomfortable. I have no time to study it carefully for the moment. I will put it here first.

// Brightness change function changebrightness (BMP: tbitmap; s: integer): Boolean; var P: pbytearray; X, Y: integer; begin try // 24-bit real-color BMP. pixelformat: = pf24bit; BMP. canvas. lock; for Y: = 0 to BMP. height-1 do begin P: = BMP. scanline [y]; for X: = 0 to BMP. width-1 do begin // adjust the R, G, and B components of each pixel point begin if S> 0 then begin P [x * 3]: = min (255, P [x * 3] + S); // It cannot be out of bounds. The value range is 0 ~ 255 p [x * 3 + 1]: = min (255, P [x * 3 + 1] + S); P [x * 3 + 2]: = min (255, P [x * 3 + 2] + S); End else begin P [x * 3]: = max (0, P [x * 3] + S); // It cannot be out of bounds. The value range is-255 ~ 0 p [x * 3 + 1]: = max (0, P [x * 3 + 1] + S); P [x * 3 + 2]: = max (0, P [x * 3 + 2] + S); end; BMP. canvas. unlock; Result: = true; failed t result: = false; end;
// Contrast function changecontrast (BMP: tbitmap; s: integer): Boolean; const cmid = 128; Cmin = 10; Cmax = 246; var P: pbytearray; X, Y: integer; begin try // 24-bit real-color BMP. pixelformat: = pf24bit; BMP. canvas. lock; for Y: = 0 to BMP. height-1 do begin P: = BMP. scanline [y]; for X: = 0 to BMP. width-1 do begin // determine the threshold value as 128 If (P [x * 3]> cmid) and (P [x * 3] <= Cmax) and (P [x * 3 + 1]> cmid) and (P [x * 3 + 1] <= Cmax) and (P [x * 3 + 2]> cmid) and (P [x * 3 + 2] <= Cmax) then begin P [x * 3]: = min (255, P [x * 3] + S * P [x * 3] Div (P [x * 3] + P [x * 3 + 1] + P [x * 3 + 2 ]); P [x * 3 + 1]: = min (255, P [x * 3 + 1] + S * P [x * 3 + 1] Div (P [x * 3] + P [x * 3 + 1] + P [x * 3 + 2]); P [x * 3 + 2]: = min (255, P [x * 3 + 2] + S * P [x * 3 + 2] Div (P [x * 3] + P [x * 3 + 1] + P [x * 3 + 2]); end; If (P [x * 3]> Cmin) and (P [x * 3] <= cmid) and (P [x * 3 + 1]> Cmin) and (P [x * 3 + 1] <= cmid) and (P [x * 3 + 2]> Cmin) and (P [x * 3 + 2] <= cmid) then begin P [x * 3]: = max (0, P [x * 3]-S * P [x * 3] Div (P [x * 3] + P [x * 3 + 1] + P [x * 3 + 2 ]); P [x * 3 + 1]: = max (0, P [x * 3 + 1]-S * P [x * 3 + 1] Div (P [x * 3] + P [x * 3 + 1] + P [x * 3 + 2]); P [x * 3 + 2]: = max (0, P [x * 3 + 2]-S * P [x * 3 + 2] Div (P [x * 3] + P [x * 3 + 1] + P [x * 3 + 2]); end; end; BMP. canvas. unlock; Result: = true; failed t result: = false; end;
// Change the saturation function changesaturation (BMP: tbitmap; valuechange: integer): Boolean; const Cmax = 255; var grays: array [0 .. 767] of integer; Alpha: array [0 .. 255] of word; gray, X, Y: integer; srcrgb: prgbtriple; I: byte; begin valuechange: = valuechange + 255; for I: = 0 to Cmax do begin Alpha [I]: = (I * valuechange) SHR 8; end; X: = 0; for I: = 0 to Cmax do begin Gray: = I-Alpha [I]; grays [x]: = gray; Inc (x); grays [x]: = gray; Inc (x); grays [x]: = gray; Inc (x); end; for Y: = 0 to BMP. height-1 do begin srcrgb: = BMP. scanline [y]; for X: = 0 to BMP. width-1 do begin Gray: = grays [srcrgb. rgbtred + srcrgb. rgbtblue + srcrgb. rgbtgreen]; if Gray + Alpha [srcrgb. rgbtred]> 0 then srcrgb. rgbtred: = min (cmax, gray + Alpha [srcrgb. rgbtred]) else srcrgb. rgbtred: = 0; if Gray + Alpha [srcrgb. rgbtgreen]> 0 then srcrgb. rgbtgreen: = min (cmax, gray + Alpha [srcrgb. rgbtgreen]) else srcrgb. rgbtgreen: = 0; if Gray + Alpha [srcrgb. rgbtblue]> 0 then srcrgb. rgbtblue: = min (cmax, gray + Alpha [srcrgb. rgbtblue]) else srcrgb. rgbtblue: = 0; Inc (srcrgb); end;

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.