From: "Earl F. Glynn" Subject: Re: help on YUV Date: 01 Jan 1999 00:00:00 GMT Message-ID: <76jjoo$hoo@bgtnsc02.worldnet.att.net> References: <76its9$29718@forums.borland.com> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0 Organization: AT&T WorldNet Services Newsgroups: borland.public.delphi.graphics dEpperson: ede wrote in message <76its9$29718@forums.borland.com>... >Are there any good resource manuals on YUV? And can you manipulate a YUV >file in D3. > >I've done bitmap and jpeg, EMF manipulation of bitmap, but I've never seen >anything in D3 for YUV. Got a big project coming up so would appreciate any >help. I worked with someone named Daniel ("D.Halan" ) in converting his YUV files to RGB format for display in Delphi. The code below was for a specific YUV file (but it was proprietary so I can't give you that as part of the example). Perhaps the code will help you get started. For general information about various color spaces and conversions, look for the "Color Conversion" and "Color Spaces" in Section A (Color Information) at www.efg2.com/Lab/Library/Color.htm efg _________________________________ Earl F. Glynn Overland Park, KS USA ================================================================ unit yuv_read; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TFormYUVRGB = class(TForm) ButtonRead: TButton; ImageYUV: TImage; CheckBoxBW: TCheckBox; procedure ButtonReadClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var FormYUVRGB: TFormYUVRGB; tosiz : integer = 0; implementation {$R *.DFM} procedure TFormYUVRGB.ButtonReadClick(Sender: TObject); CONST YUVwidth = 720; YUVheight= 486; TYPE TYUVWord = // A "Word" is always 2-byte in all versions of Delphi RECORD UV: BYTE; // U if index even; V if index odd Y : BYTE; // Y for all array elements END; TRGBArray = ARRAY[0..YUVwidth-1] OF TRGBTriple; pRGBArray = ^TRGBArray; VAR BitmapRGB : TBitmap; BufferYUV : ARRAY[0..YUVwidth-1] OF TYUVWord; FilePointer: INTEGER; Flag : BOOLEAN; i : INTEGER; j : INTEGER; Row : pRGBArray; StreamYUV : TFileStream; U : INTEGER; V : INTEGER; Y : INTEGER; // This may be "overkill". Check upper and lower bounds to be safe. FUNCTION FixValue(CONST x: DOUBLE): BYTE; VAR i: INTEGER; BEGIN i := ROUND(x); IF i > 255 THEN RESULT := 255 ELSE IF i < 0 THEN RESULT := 0 ELSE RESULT := BYTE(i) END {FixValue}; begin StreamYUV := TFileStream.Create('texmex.yuv', fmOpenRead); TRY FilePointer := 0; StreamYUV.Seek (FilePointer, soFromBeginning); flag := CheckBoxBW.Checked; // slow to access visual component BitmapRGB := TBitmap.Create; TRY BitmapRGB.Width := YUVwidth; BitmapRGB.Height := YUVheight; BitmapRGB.PixelFormat := pf24Bit; FOR j := 0 TO YUVheight-1 DO BEGIN StreamYUV.Read(BufferYUV, SizeOf(BufferYUV)); Row := BitmapRGB.Scanline[j]; FOR i := 0 TO YUVwidth-1 DO BEGIN // Don't try to optimize this for now. Let's just get the // assignments right. IF i MOD 2 = 0 // Even THEN BEGIN Y := BufferYUV[i].Y; U := BufferYUV[i].UV; V := BufferYUV[i+1].UV END ELSE BEGIN // Odd Y := BufferYUV[i].Y; U := BufferYUV[i-1].UV; V := BufferYUV[i].UV END; WITH Row[i] DO BEGIN IF Flag THEN BEGIN // For Black and White, just use Y for R, G and B rgbtRed := Y; rgbtGreen := Y; rgbtBlue := Y END ELSE BEGIN // Color conversions from // www.cc.iastate.edu/olc_answers/packages/graphics/color-space.faq.html // or http://disney.ctr.columbia.edu/jrsthesis/node26.html // BUT notice caution in // Poynton Color FAQ, www.poynton.com (MODIFIED 13 Feb 2003) // Item #33! // rgbtRed := FixValue(Y + 1.140*V); // rgbtGreen := FixValue(Y - 0.396*U - 0.581*V); // rgbtBlue := FixValue(Y + 2.029*U) // See "HELP! YUV-RGB convertion (sic)", 12/12/97 by lweng@analogic.com for this // formula. rgbtRed := FixValue(1.164*(Y-16) + 1.596*(V-128)); rgbtGreen := FixValue(1.164*(Y-16) - 0.392*(U-128) - 0.813*(V-128)); rgbtBlue := FixValue(1.164*(Y-16) + 2.017*(U-128)) END END END END; BitmapRGB.SaveToFile('texmex.BMP'); ImageYUV.Picture.Graphic := BitmapRGB FINALLY BitmapRGB.Free END FINALLY StreamYUV.Free END end; end.