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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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,
--
Leonardo M. Ramé
http://leonardorame.blogspot.com

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

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

Felipe Monteiro de Carvalho
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

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

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

Leonardo M. Ramé
In reply to this post by Leonardo M. Ramé
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

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus