[Lazarus] Converting C++ Uint8 to Red,Green,Blue

Leonardo M. Ramé l.rame at griensu.com
Tue Apr 19 15:48:51 CEST 2011


On 2011-04-19 10:39:59 -0300, Leonardo M. Ramé wrote:
> Hi, I'm working with a library that let me get a pointer to an UInt8
> array containing the pixels of an image.
> 
> To show the image on screen, using Lazarus, I must know the
> Red,Green,Blue and Alpha values of each pixel (am I right?), how can I
> convert each byte of the UInt8 array to RGBA?
> 
> Thanks in advance,
> -- 

Let me answer to myself.

I didn't know that I can use lBuffer as an array, so, the index 0 ->
Red, 1 -> Green, 2 -> Blue, 3 -> Alpha.

Here's how a buffer can be paint on a form using TBGRABitmap:


lBmp := TBGRABitmap.Create(FImgWidth, FImgHeight);
try
  for y := 0 to lBmp.Height-1 do
  begin
    p := lBmp.Scanline[y];
    for x := 0 to lBmp.Width-1 do
    begin
      p^.red := lBuffer[0];
      p^.green := lBuffer[1];
      p^.blue := lBuffer[2];
      p^.alpha := lBuffer[3];
      inc(p);
      inc(lBuffer);
    end;
  end;
  // Self is a TForm
  lBmp.Draw(Self.Canvas, 0, 0);
finally
  lbmp.Free;
end;


-- 
Leonardo M. Ramé
http://leonardorame.blogspot.com




More information about the Lazarus mailing list