From: "Andrew Rybenkov" Newsgroups: borland.public.delphi.graphics References: <3c4306aa_2@dnews> <3c431ba8_1@dnews> Subject: Re: That (in)famous Scanline property (example) - long Date: Mon, 14 Jan 2002 22:06:30 +0300 Lines: 109 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 NNTP-Posting-Host: 217.106.208.65 Message-ID: <3c432d5f_2@dnews> X-Trace: dnews 1011035487 217.106.208.65 (14 Jan 2002 11:11:27 -0800) Path: dnews Xref: dnews borland.public.delphi.graphics:45154 > Perhaps because it is not a difficult to handle API code and works almost > the same ?! try GetDIBits/SetDIBits - you will be astonished how things will speed up. > If i would have used Getdibits so many times in my application as i used > scanline now i would be crazy ! certainly not. Ok, here goes trivial example (no error-checking,etc.): ---------------------------------------------------------------------- unit SimpleDIBits; interface uses Windows, Graphics; type TColorRec = packed record b,g,r,a: Byte; end; TColorArray = array[0..MaxInt div SizeOf(TColorRec)-1] of TColorRec; PColorArray = ^TColorArray; function BmpToArray(const aBitmap: TBitmap): PColorArray; procedure BmpFromArray(var aBitmap: TBitmap; p: Pointer; w,h: Integer); var bmi: BITMAPINFO; implementation function BmpToArray(const aBitmap: TBitmap): PColorArray; var w,h: Integer; p: PColorArray; begin w := aBitmap.Width; h := aBitmap.Height; GetMem(p,w*h*SizeOf(TColorRec)); with bmi.bmiHeader do begin biWidth := w; biHeight := -h; end; GetDIBits(aBitmap.Canvas.Handle,aBitmap.Handle,0,h,p,bmi,DIB_RGB_COLORS); Result := p; end; procedure BmpFromArray(var aBitmap: TBitmap; p: Pointer; w,h: Integer); begin aBitmap.Height := 0; aBitmap.Width := w; aBitmap.Height := h; with bmi.bmiHeader do begin biWidth := w; biHeight := -h; end; SetDIBits(aBitmap.Canvas.Handle,aBitmap.Handle,0,h,p,bmi,DIB_RGB_COLORS); end; initialization with bmi.bmiHeader do begin biSize := SizeOf(bmi.bmiHeader); biWidth := 0; biHeight := 0; biPlanes := 1; biBitCount := 32; biCompression := BI_RGB; biSizeImage := 0; biXPelsPerMeter := 1; //dont care biYPelsPerMeter := 1; //dont care biClrUsed := 0; biClrImportant := 0; end; end. ---------------------------------------------------------------------- then in your code var PColors: PColorArray; SomeBmp: TBitmap; w,h: Integer; w := SomeBitmap.Width; h := SomeBitmap.Height; PColors := BmpToArray(SomeBmp); ... do whatever you want - pixel [x,y] will be PColors[x + y*h] ... BMPFromArray(SomeOrOtherBmp,PColors,w,h); FreeMem(PColors); yet again - just compare speed. and here you do NOT care of SomeBMP.PixelFormat - whatever. -- Andrew Rybenkov, the programmer who walks by himself.