Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-04

Converting C++ Uint8 to Red,Green,Blue

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

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,

Reply by email

Felipe Monteiro de Carvalho
to Leonardo M. Ramé

It depends on the pixel format which the image uses. After you read them you can get a TColor with:

function RGBToColor(R, G, B: Byte): TColor;

from unit Graphics

Note that TColor doesn't support Alpha

Felipe Monteiro de Carvalho

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé: Hi, I'm working with a library that let me get a pointer to an UInt8 array containing the…
Leonardo M. Ram? wrote 9 lines
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;

Reply by email