function getbmpbrightness(thebitmap: tbitmap): byte;
var
i,j: integer;
p: pbytearray;
hh,ww: integer;
sum: int64;
total: int64;
begin
sum:=0;
total:=0;
with thebitmap do
begin
hh:=thebitmap.height;
ww:=thebitmap.width;
i:=0;
while i<hh do
begin
p:=scanline;
inc(i,3);
for j:=0 to ww-1 do
begin
sum:=sum+p[j*3]+p[j*3+1]+p[j*3+2];// &&&
total:=total+3;// &&&
end;
end;
end;
result:=sum div total;
end;
procedure bmpSmooth(mybitmap: tbitmap; smoothlevel: byte; x1,y1,x2,y2: integer);
var
p,p1,p2: pbytearray;
i,j: integer;
thew,theh: integer;
s1,s2,s3: integer;
rvalue: integer;
gvalue: integer;
bvalue: integer;
tmpbitmap: tbitmap;
maxnum,minnum: integer;
begin
tmpbitmap:=tbitmap.create;
tmpbitmap.assign(mybitmap);
with mybitmap do
begin
thew:=width;
theh:=height;
if (x1=x2) or (y1=y2) then
begin
x1:=2;
y1:=2;
x2:=thew-2;
y2:=theh-2;
end
else
begin
minnum:=min(y1,y2);
maxnum:=max(y1,y2);
y1:=minnum;
y2:=maxnum;
minnum:=min(x1,x2);
maxnum:=max(x1,x2);
x1:=minnum;
x2:=maxnum;
end;
if x1<2 then x1:=2;
if y1<2 then y1:=2;
if x2>thew-2 then x2:=thew-2;
if y2>theh-2 then y2:=theh-2;
for i:=y1 to y2 do
begin
p:=ScanLine;
p1:=tmpbitmap.scanline[i-1];
p2:=tmpbitmap.scanline[i+1];
for j:=x1 to x2 do
begin
s1:=j*3;
s2:=j*3+1;
s3:=j*3+2;
case smoothlevel of
1: begin // 轻微
rvalue:=(p[s3]*3+p[s3-3]+p[s3+3]+p1[s3]+p1[s3-3]+p1[s3+3]+
p2[s3]+p2[s3-3]+p2[s3+3]) div 11;
gvalue:=(p[s2]*3+p[s2-3]+p[s2+3]+p1[s2]+p1[s2-3]+p1[s2+3]+
p2[s2]+p2[s2-3]+p2[s2+3]) div 11;
bvalue:=(p[s1]*3+p[s1-3]+p[s1+3]+p1[s1]+p1[s1-3]+p1[s1+3]+
p2[s1]+p2[s1-3]+p2[s1+3]) div 11;
end;
2: begin // 普通
rvalue:=(p[s3]*2+p[s3-3]+p[s3+3]+p1[s3]+p1[s3-3]+p1[s3+3]+
p2[s3]+p2[s3-3]+p2[s3+3]) div 10;
gvalue:=(p[s2]*2+p[s2-3]+p[s2+3]+p1[s2]+p1[s2-3]+p1[s2+3]+
p2[s2]+p2[s2-3]+p2[s2+3]) div 10;
bvalue:=(p[s1]*2+p[s1-3]+p[s1+3]+p1[s1]+p1[s1-3]+p1[s1+3]+
p2[s1]+p2[s1-3]+p2[s1+3]) div 10;
end;
3: begin // 加强
rvalue:=(p[s3]+p[s3-3]+p[s3+3]+p1[s3]+p1[s3-3]+p1[s3+3]+
p2[s3]+p2[s3-3]+p2[s3+3]) div 9;
gvalue:=(p[s2]+p[s2-3]+p[s2+3]+p1[s2]+p1[s2-3]+p1[s2+3]+
p2[s2]+p2[s2-3]+p2[s2+3]) div 9;
bvalue:=(p[s1]+p[s1-3]+p[s1+3]+p1[s1]+p1[s1-3]+p1[s1+3]+
p2[s1]+p2[s1-3]+p2[s1+3]) div 9;
end;
else
begin
rvalue:=p[s3];
gvalue:=p[s2];
bvalue:=p[s1];
end;
end;
p[s3]:=rvalue;
p[s2]:=gvalue;
p[s1]:=bvalue;
end;
end;
end;
tmpbitmap.free;
end;
// 直方图均衡
// darkness为黑度,即均衡输出亮度的下限。
// brightness为白度,为均衡输出亮度的上限。
// 可以参考PHOTOSHOP类似功能的概念。
// x1,y1,x2,y2为处理的区域。
procedure grayextend(mybitmap: tbitmap; darkness,brightness: byte;
x1,y1,x2,y2: integer);
var
his: array [0..255,1..3] of real;
ihis: array [0..255,1..3] of extended;
hiseq: array [0..255,1..3] of integer;
sum: extended;
i,j: integer;
thew,theh: integer;
p: pbytearray;
sumreal: array [1..3] of real;
rate: real;
maxnum,minnum: integer;
begin
for i:=0 to 255 do
for j:=1 to 3 do
ihis[i,j]:=0;
sum:=0;
with mybitmap do
begin
thew:=width;
theh:=height;
if (x1=x2) or (y1=y2) then
begin
x1:=1;
y1:=1;
x2:=thew-1;
y2:=theh-1;
end
else
begin
minnum:=min(y1,y2);
maxnum:=max(y1,y2);
y1:=minnum;
y2:=maxnum;
minnum:=min(x1,x2);
maxnum:=max(x1,x2);
x1:=minnum;
x2:=maxnum;
end;
if x1<1 then x1:=1;
if y1<1 then y1:=1;
if x2>thew-1 then x2:=thew-1;
if y2>theh-1 then y2:=theh-1;
for i:=y1 to y2 do
begin
p:=scanline;
for j:=x1 to x2 do
begin
ihis[p[j*3],1]:=ihis[p[j*3],1]+i*i;
ihis[p[j*3+1],2]:=ihis[p[j*3+1],2]+i*i;
ihis[p[j*3+2],3]:=ihis[p[j*3+2],3]+i*i;
sum:=sum+i*i;
end;
end;
for i:=0 to 255 do
for j:=1 to 3 do his[i,j]:=ihis[i,j]/sum;
for i:=0 to 255 do
begin
sumreal[1]:=0;
sumreal[2]:=0;
sumreal[3]:=0;
for j:=0 to i do
begin
sumreal[1]:=sumreal[1]+his[j,1];
sumreal[2]:=sumreal[2]+his[j,2];
sumreal[3]:=sumreal[3]+his[j,3];
end;
rate:=1;
hiseq[i,1]:=round((brightness-darkness)*sumreal[1]*rate+darkness);
hiseq[i,2]:=round((brightness-darkness)*sumreal[2]*rate+darkness);
hiseq[i,3]:=round((brightness-darkness)*sumreal[3]*rate+darkness);
end;
for i:=y1 to y2 do
begin
p:=scanline;
for j:=x1 to x2 do
begin
p[j*3]:=hiseq[p[j*3],1];
p[j*3+1]:=hiseq[p[j*3+1],2];
p[j*3+2]:=hiseq[p[j*3+2],3];
end;
end;
end; // end of with
end;