const
RedCanal =
0
;
GreenCanal =
1
;
BlueCanal =
2
;
GrayCanal =
3
;
type
TRGB24 =
array
[
0
..
2
]
of
Byte
;
ARGB24 =
array
[
0
..
0
]
of
TRGB24;
PRGB24 = ^ARGB24;
TTypeCanal =
Byte
;
PHistogram = ^THistogram;
THistogram =
object
m_Count:
LongWord
;
m_MaxColor:
Byte
;
m_Color:
Array
[
0
..
255
]
of
LongWord
;
Procedure
GetData(BitMap: TBitmap; rc: TRect; tcnl: TTypeCanal);
end
;
var
Hist: THistogram;
canal: TTypeCanal = RedCanal;
Procedure
DrawHistogram(canvas: TCanvas; Histogram: PHistogram; rc: TRect);
var
x, dx, dy:
Real
;
y:
LongWord
;
i:
Byte
;
begin
canvas
.
Brush
.
Color := clBlack;
canvas
.
Pen
.
Color := canvas
.
Brush
.
Color;
canvas
.
Pen
.
Style := psSolid;
canvas
.
Rectangle(rc);
dx := (rc
.
Right - rc
.
Left) /
256.0
;
dy := (rc
.
Right - rc
.
Left) / (Histogram
.
m_Count) *
30
;
x := rc
.
Left;
if
(canal = GrayCanal)
then
canvas
.
Brush
.
Color := clGray
else
canvas
.
Brush
.
Color :=
$FF
shl
(canal *
8
);
canvas
.
Pen
.
Style := psClear;
for
i :=
0
to
255
do
begin
y := rc
.
Bottom - Round(Histogram
.
m_Color[i] * dy);
canvas
.
Rectangle(Round(x), y, Round(x + dx) +
1
, rc
.
Bottom +
1
);
x := x + dx;
end
end
;
Procedure
THistogram
.
GetData(BitMap: TBitmap; rc: TRect; tcnl: TTypeCanal);
var
x0, y0, rWidth, rHeight:
LongInt
;
i, x, y, Mx, My:
LongInt
;
LinePict: PRGB24;
Color: TRGB24;
colorI:
Byte
;
begin
x0 := rc
.
Left;
y0 := rc
.
top;
rWidth := rc
.
Right - rc
.
Left -
1
;
rHeight := rc
.
Bottom - rc
.
top -
1
;
m_Count := rWidth * rHeight;
for
i :=
0
to
255
do
m_Color[i] :=
0
;
m_MaxColor :=
0
;
Mx := x0 + rWidth;
My := y0 + rHeight;
for
y := y0
to
My
do
begin
LinePict := BitMap
.
ScanLine[y];
for
x := x0
to
Mx
do
begin
Color := LinePict[x];
if
(canal = GrayCanal)
then
colorI := (Color[
0
] + Color[
1
] + Color[
2
])
div
3
else
colorI := Color[canal];
Inc(m_Color[colorI]);
if
(m_Color[colorI] > m_Color[m_MaxColor])
then
m_MaxColor := colorI;
end
;
end
;
end
;
procedure
TForm1
.
Button12Click(Sender: TObject);
var
BitMap: TBitmap;
rc, rcDraw: TRect;
begin
BitMap := TBitmap
.
Create;
try
BitMap
.
Width := Image1
.
Picture
.
Width;
BitMap
.
Height := Image1
.
Picture
.
Height;
BitMap
.
canvas
.
Draw(
0
,
0
, Image1
.
Picture
.
Graphic);
BitMap
.
PixelFormat := pf24bit;
rc := Rect(
0
,
0
, BitMap
.
Width, BitMap
.
Height);
Hist
.
GetData(BitMap, rc, canal);
rcDraw := Rect(
0
,
0
, Image3
.
Width, Image3
.
Height);
DrawHistogram(Image3
.
canvas, @Hist, rcDraw);
finally
BitMap
.
Free;
end
;
end
;