📄 unit3.pas
字号:
unit Unit3;
interface
uses windows,Graphics,comctrls;
const
MaxPixelCount = 32768;
type
pRGBArray = ^TRGBArray;
TRGBArray = ARRAY[0..MaxPixelCount-1] OF TRGBTriple;
function Min(a, b: integer): integer;
function Max(a, b: integer): integer;
procedure RGBChange(SrcBmp,DestBmp:Tbitmap;RedChange,GreenChange,
BlueChange:integer);overload;
procedure RGBChange(SrcBmp:Tbitmap;RedChange,GreenChange,BlueChange:integer;
AProgressBar:TProgressBar); overload;
procedure BrightnessChange(SrcBmp,DestBmp:Tbitmap;ValueChange:integer); overload;
procedure BrightnessChange(SrcBmp:Tbitmap;ValueChange:integer;
AProgressBar:TProgressBar); overload;
procedure GrayAverage(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure GrayWeightAverage(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure ContrastChange(SrcBmp:Tbitmap;Flag:integer;AProgressBar:TProgressBar);
procedure Saturation(SrcBmp,DestBmp: TBitmap; Amount: Integer);overload;
procedure Saturation(SrcBmp: TBitmap; Amount: Integer;AProgressBar:TProgressBar); overload;
procedure NotColor(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Exposure(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Emboss(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Engrave(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Blur(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Sharpen(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Purple(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Spooky(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure OtherEffect(SrcBmp:Tbitmap;EffectIndex:integer;
AProgressBar:TProgressBar);
implementation
function Min(a, b: integer): integer;
begin
if a < b then result := a
else result := b;
end;
function Max(a, b: integer): integer;
begin
if a > b then result := a
else result := b;
end;
//调整RGB,SrcBmp输入,DestBmp输出
procedure RGBChange(SrcBmp,DestBmp:Tbitmap;RedChange,GreenChange,BlueChange:integer);
var
// R,G,B:integer;
SrcRow, DestRow:pRGBArray;
i,j:integer;
begin
for i := 0 to SrcBmp.Height- 1 do
begin
SrcRow := SrcBmp.ScanLine[i];
DestRow :=DestBmp.ScanLine[i];
for j := 0 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
if RedChange> 0 then
DestRow[j].rgbtRed := Min(255, SrcRow[j].rgbtRed + RedChange)
else
DestRow[j].rgbtRed := Max(0, SrcRow[j].rgbtRed + RedChange);
if GreenChange> 0 then
DestRow[j].rgbtGreen := Min(255, SrcRow[j].rgbtGreen +GreenChange)
else
DestRow[j].rgbtGreen := Max(0, SrcRow[j].rgbtGreen +GreenChange);
if BlueChange> 0 then
DestRow[j].rgbtBlue := Min(255, SrcRow[j].rgbtBlue +BlueChange)
else
DestRow[j].rgbtBlue := Max(0, SrcRow[j].rgbtBlue +BlueChange);
end;
end;
end;
//调整RGB,SrcBmp同时为输入输出
procedure RGBChange(SrcBmp:Tbitmap;RedChange,GreenChange,BlueChange:integer;AProgressBar:TProgressBar); overload;
var
// R,G,B:integer;
SrcRow :pRGBArray;
i,j:integer;
begin
AProgressBar.Max:=SrcBmp.Height- 1;
for i := 0 to SrcBmp.Height- 1 do
begin
SrcRow := SrcBmp.ScanLine[i];
// DestRow :=DestBmp.ScanLine[i];
for j := 0 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
if RedChange> 0 then
SrcRow[j].rgbtRed := Min(255, SrcRow[j].rgbtRed + RedChange)
else
SrcRow[j].rgbtRed := Max(0, SrcRow[j].rgbtRed + RedChange);
if GreenChange> 0 then
SrcRow[j].rgbtGreen := Min(255, SrcRow[j].rgbtGreen +GreenChange)
else
SrcRow[j].rgbtGreen := Max(0, SrcRow[j].rgbtGreen +GreenChange);
if BlueChange> 0 then
SrcRow[j].rgbtBlue := Min(255, SrcRow[j].rgbtBlue +BlueChange)
else
SrcRow[j].rgbtBlue := Max(0, SrcRow[j].rgbtBlue +BlueChange);
end;
AProgressBar.Position:=i;
end;
end;
//调整亮度,SrcBmp输入,DestBmp输出
procedure BrightnessChange(SrcBmp,DestBmp:Tbitmap;ValueChange:integer);
var
i, j: integer;
SrcRow, DestRow: pRGBArray;
begin
// get brightness increment value
// for each row of pixels
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRow := SrcBmp.ScanLine[i];
DestRow := DestBmp.ScanLine[i];
// for each pixel in row
for j := 0 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
if ValueChange > 0 then
begin
// RGB values must be less than 256
DestRow[j].rgbtRed := Min(255, SrcRow[j].rgbtRed +ValueChange);
DestRow[j].rgbtGreen := Min(255, SrcRow[j].rgbtGreen + ValueChange);
DestRow[j].rgbtBlue := Min(255, SrcRow[j].rgbtBlue +ValueChange);
end else begin
// RGB values must be greater or equal than 0
DestRow[j].rgbtRed := Max(0, SrcRow[j].rgbtRed +ValueChange);
DestRow[j].rgbtGreen := Max(0, SrcRow[j].rgbtGreen +ValueChange);
DestRow[j].rgbtBlue := Max(0, SrcRow[j].rgbtBlue +ValueChange);
end;
end;
end;
end;
//调整亮度,SrcBmp同时为输入输出
procedure BrightnessChange(SrcBmp:Tbitmap;ValueChange:integer;AProgressBar:TProgressBar);
var
i, j: integer;
SrcRow: pRGBArray;
begin
AProgressBar.Max:=SrcBmp.Height- 1;
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRow := SrcBmp.ScanLine[i];
//DestRow := DestBmp.ScanLine[i];
// for each pixel in row
for j := 0 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
if ValueChange > 0 then
begin
// RGB values must be less than 256
SrcRow[j].rgbtRed := Min(255, SrcRow[j].rgbtRed +ValueChange);
SrcRow[j].rgbtGreen := Min(255, SrcRow[j].rgbtGreen + ValueChange);
SrcRow[j].rgbtBlue := Min(255, SrcRow[j].rgbtBlue +ValueChange);
end else begin
// RGB values must be greater or equal than 0
SrcRow[j].rgbtRed := Max(0, SrcRow[j].rgbtRed +ValueChange);
SrcRow[j].rgbtGreen := Max(0, SrcRow[j].rgbtGreen +ValueChange);
SrcRow[j].rgbtBlue := Max(0, SrcRow[j].rgbtBlue +ValueChange);
end;
end;
AProgressBar.Position:=i;
end;
end;
//调整对比度
procedure ContrastChange(SrcBmp:Tbitmap;Flag:integer;AProgressBar:TProgressBar);
// Amount: -255~255
var
X, Y: Integer;
// I: Byte;
// ColorTable: array[0..255] of TRGBTriple;
SrcRow: pRGBArray;
//pRGB: PRGBColor;
begin
case Flag of
0:
begin
AProgressBar.Position:=0;
AProgressBar.Max:=SrcBmp.Height - 1;
for Y := 0 to SrcBmp.Height - 1 do
begin
SrcRow := SrcBmp.ScanLine[Y];
for X := 0 to SrcBmp.Width - 1 do
begin
if SrcRow[x].rgbtRed>=128 then SrcRow[x].rgbtRed:=min(255,SrcRow[x].rgbtRed+3)
else
SrcRow[x].rgbtRed:=max(0,SrcRow[x].rgbtRed-3);
if SrcRow[x].rgbtGreen>=128 then SrcRow[x].rgbtGreen:=min(255,SrcRow[x].rgbtGreen+3)
else
SrcRow[x].rgbtGreen:=max(0,SrcRow[x].rgbtGreen-3);
if SrcRow[x].rgbtBlue>=128 then SrcRow[x].rgbtBlue:=min(255,SrcRow[x].rgbtBlue+3)
else
SrcRow[x].rgbtBlue:=max(0,SrcRow[x].rgbtBlue-3);
end;
AProgressBar.Position:=y;
end;
end;
1:
begin
AProgressBar.Position:=0;
AProgressBar.Max:=SrcBmp.Height - 1;
for Y := 0 to SrcBmp.Height - 1 do
begin
SrcRow := SrcBmp.ScanLine[Y];
for X := 0 to SrcBmp.Width - 1 do
begin
if SrcRow[x].rgbtRed>=128 then Max(128,SrcRow[x].rgbtRed-3)
else
SrcRow[x].rgbtRed:=min(128,SrcRow[x].rgbtRed+3);
if SrcRow[x].rgbtGreen>=128 then Max(128,SrcRow[x].rgbtGreen-3)
else
SrcRow[x].rgbtGreen:=min(128,SrcRow[x].rgbtGreen+3);
if SrcRow[x].rgbtBlue>=128 then Max(128,SrcRow[x].rgbtBlue-3)
else
SrcRow[x].rgbtBlue:=min(128,SrcRow[x].rgbtBlue+3);
end;
AProgressBar.Position:=y;
end;
end;
end; //end case
end;
//调整饱和度
procedure Saturation(SrcBmp: TBitmap; Amount: Integer;AProgressBar:TProgressBar);overload; // Amount: 0~510
var
Grays: array[0..767] of Integer;
Alpha: array[0..255] of Word;
Gray, X, Y: Integer;
SrcRow: pRGBArray;
I: Byte;
begin
for I := 0 to 255 do Alpha[I] := (I * Amount) shr 8;
x := 0;
for I := 0 to 255 do
begin
Gray := I - Alpha[I];
Grays[X] := Gray; Inc(X);
Grays[X] := Gray; Inc(X);
Grays[X] := Gray; Inc(X);
end;
AProgressBar.Position:=0;
AProgressBar.Max:=SrcBmp.Height - 1;
for Y := 0 to SrcBmp.Height - 1 do
begin
SrcRow:= SrcBmp.ScanLine[Y];
for X := 0 to SrcBmp.Width - 1 do
begin
Gray := Grays[SrcRow[x].rgbtRed +SrcRow[x].rgbtGreen+SrcRow[x].rgbtBlue];
SrcRow[x].rgbtRed:= Byte(Gray + Alpha[SrcRow[x].rgbtRed]);
SrcRow[x].rgbtGreen:= Byte(Gray + Alpha[SrcRow[x].rgbtGreen]);
SrcRow[x].rgbtBlue := Byte(Gray + Alpha[SrcRow[x].rgbtBlue]);
end;
AProgressBar.Position:=y;
end;
end;
procedure Saturation(SrcBmp,DestBmp: TBitmap; Amount: Integer);overload;
var
Grays: array[0..767] of Integer;
Alpha: array[0..255] of Word;
Gray, X, Y: Integer;
SrcRow,DestRow: pRGBArray;
I: Byte;
begin
for I := 0 to 255 do Alpha[I] := (I * Amount) shr 8;
x := 0;
for I := 0 to 255 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 SrcBmp.Height - 1 do
begin
SrcRow:= SrcBmp.ScanLine[Y];
DestRow:= DestBmp.ScanLine[Y];
for X := 0 to SrcBmp.Width - 1 do
begin
Gray := Grays[SrcRow[x].rgbtRed +SrcRow[x].rgbtGreen+SrcRow[x].rgbtBlue];
DestRow[x].rgbtRed:= Byte(Gray + Alpha[SrcRow[x].rgbtRed]);
DestRow[x].rgbtGreen:= Byte(Gray + Alpha[SrcRow[x].rgbtGreen]);
DestRow[x].rgbtBlue := Byte(Gray + Alpha[SrcRow[x].rgbtBlue]);
end;
end;
end;
//平均灰度化
procedure GrayAverage(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
var
i, j: integer;
SrcRow: pRGBArray;
AverageValue:integer;
begin
AProgressBar.Max:=SrcBmp.Height - 1;
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRow := SrcBmp.ScanLine[i];
//DestRow := DestBmp.ScanLine[i];
// for each pixel in row
for j := 0 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
// RGB values must be less than 256
AverageValue:=(SrcRow[j].rgbtRed+SrcRow[j].rgbtGreen+SrcRow[j].rgbtBlue)div 3;
SrcRow[j].rgbtRed :=AverageValue;
SrcRow[j].rgbtGreen :=AverageValue;
SrcRow[j].rgbtBlue := AverageValue;
end;
AProgressBar.Position:=i;
end;
end;
//加权平均灰度化
procedure GrayWeightAverage(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
var
i, j: integer;
SrcRow: pRGBArray;
WeightAverageValue:integer;
begin
AProgressBar.Max:=SrcBmp.Height - 1;
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRow := SrcBmp.ScanLine[i];
//DestRow := DestBmp.ScanLine[i];
// for each pixel in row
for j := 0 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
// RGB values must be less than 256
WeightAverageValue:=trunc(SrcRow[j].rgbtRed*0.31+SrcRow[j].rgbtGreen*0.59+SrcRow[j].rgbtBlue*0.11);
SrcRow[j].rgbtRed :=WeightAverageValue;
SrcRow[j].rgbtGreen :=WeightAverageValue;
SrcRow[j].rgbtBlue :=WeightAverageValue;
end;
AProgressBar.Position:=i;
end;
end;
//反色,即:底片效果
procedure NotColor(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
var
i, j: integer;
SrcRow: pRGBArray;
ScanlineBytes:integer;
begin
ScanlineBytes:=SrcBmp.Width*3;
SrcRow := SrcBmp.ScanLine[0];
AProgressBar.Max:=SrcBmp.Height - 1;
for i := 0 to SrcBmp.Height - 1 do
begin
for j := 0 to SrcBmp.Width - 1 do
begin
SrcRow[j].rgbtRed :=not SrcRow[j].rgbtRed ;
SrcRow[j].rgbtGreen :=not SrcRow[j].rgbtGreen;
SrcRow[j].rgbtBlue :=not SrcRow[j].rgbtBlue;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -