TDIB.Darker 與 Lighter 以指定幅度調整影像整體亮度,分別對應變暗與變亮操作。本文提供載入圖片後按鈕觸發雙向亮度調整的完整範例。

unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DIB, StdCtrls;
type
TForm1 = class(TForm)
DXPaintBox1: TDXPaintBox;
Button1: TButton; // 載入圖片
Button2: TButton; // 變暗
Button3: TButton; // 變亮
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;
var Form1: TForm1;
implementation {$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const ImgPath1 = 'C:\Temp\DX.jpg';
begin
DXPaintBox1.DIB.LoadFromFile(ImgPath1);
DXPaintBox1.Width := DXPaintBox1.DIB.Width;
DXPaintBox1.Height := DXPaintBox1.DIB.Height;
DXPaintBox1.Repaint;
end;
procedure TForm1.Button2Click(Sender: TObject); // Darker
begin
// Darker(Amount: Integer)
DXPaintBox1.DIB.Darker(130);
DXPaintBox1.Repaint;
end;
procedure TForm1.Button3Click(Sender: TObject); // Lighter
begin
// Lighter(Amount: Integer)
DXPaintBox1.DIB.Lighter(110);
DXPaintBox1.Repaint;
end;
end.
Darker(Amount: Integer) // Amount: 0~255,建議 0~200
Lighter(Amount: Integer) // Amount: 0~255,建議 0~200
| 操作 | 視覺效果 | 典型用途 |
|---|---|---|
| Darker(130) | 整體壓暗,高光保留、陰部細節流失 | 夜間模式、陰影、劇情暗幕 |
| Lighter(110) | 整體提亮、陰部細節顯現、高光過曝 | 曝光補償、霧效、夢境濾鏡 |
var Phase: Integer = 0; Dir: Integer = 1;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
DXPaintBox1.DIB.Assign(OriginalDIB);
Phase := Phase + Dir * 5;
if Phase > 100 then begin Phase := 100; Dir := -1; end;
if Phase < -100 then begin Phase := -100; Dir := 1; end;
if Phase >= 0 then DXPaintBox1.DIB.Lighter(Phase)
else DXPaintBox1.DIB.Darker(-Phase);
DXPaintBox1.Repaint;
end;
Darker/Lighter 為**線性位移**,不符合人眼感知(Gamma 2.2)。專業影像處理應使用 Gamma 校正:
procedure GammaCorrect(DIB: TDIB; Gamma: Single); var x, y: Integer; c: TRGBQuad; inv: Single; begin inv := 1 / Gamma; for y := 0 to DIB.Height - 1 do for x := 0 to DIB.Width - 1 do begin c := DIB.Pixels[x, y]; c.rgbRed := Round(255 * Power(c.rgbRed / 255, inv)); c.rgbGreen := Round(255 * Power(c.rgbGreen / 255, inv)); c.rgbBlue := Round(255 * Power(c.rgbBlue / 255, inv)); DIB.Pixels[x, y] := c; end; end;