1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
Unit RGBYUV;
interface
uses Windows, SysUtils;
type
PRGBA = ^TRGBA;
TRGBA = packed record case integer of
0 : (Color : integer);
1 : (B, G, R, A : byte);
end;
PYUV = ^TYUV;
TYUV= packed record
Y, U, V: single;
end;
TPixel = record
RGBA : TRGBA;
YUV : TYUV;
end;
TYUVImage = array of array of TPixel;
procedure BitmapYUV(BMP : TBitmap; var YUVImage : TYUVImage);
implementation
var
PrecalcYR, PrecalcYG, PrecalcYB : array[byte] of single;
procedure InitializePrecalcYRGB;
var N : integer;
begin
for N := 0 to 255 do
begin
PrecalcYR[N] := N * 0.299;
PrecalcYG[N] := N * 0.587;
PrecalcYB[N] := N * 0.114;
end;
end;
procedure BitmapYUV(BMP : TBitmap; var YUVImage : TYUVImage);
var
Y, X, W, H : integer;
SL : PRGBA;
begin
if BMP.PixelFormat <> pf32bit then
BMP.PixelFormat := pf32bit;
SetLength(YUVImage, BMP.Height, BMP.Width);
W := BMP.Width-1;
H := BMP.Height-1;
SL := BMP.ScanLine[BMP.Height-1];
for Y := 0 to H do
for X := 0 to W do
begin
YUVImage[Y,X].RGBA := SL^;
YUVImage[Y,X].YUV.Y := PrecalcYR[SL^.R] + PrecalcYG[SL^.G] + PrecalcYB[SL^.B];
YUVImage[Y,X].YUV.U := 0.492*(SL^.B-YUVImage[Y,X].YUV.Y) + 128;
YUVImage[Y,X].YUV.V := 0.877*(SL^.R-YUVImage[Y,X].YUV.Y) + 128;
inc(SL);
end;
end;
initialization
InitializePrecalcYRGB;
end. |
Partager