Board index » delphi » Graphics.TBitmap and Wintypes.TBitmap puzzle. Hot to use SetBitmapbits

Graphics.TBitmap and Wintypes.TBitmap puzzle. Hot to use SetBitmapbits

Hallo,

Two structures called TBitmap (in Unit Graphics and WinTypes) leave me
in the dark. I'd like to manipulate a bitmap directly, not with the
Canvas.Pixels array, since this is too slow - especially for TImage.
(Another puzzle: Accessing Form1.Canvas.Pixels is faster than
accessing TImage1.Canvas.Pixels).
The SetBitmapbits function gives crazy colors, although it works somehow.
But maybe I'm wrong from the very beginning which is like this:

    VAR   BM : TBitmap;  { this is the Graphics thing}
          BM1: Wintypes.TBitmap;
    BEGIN
      BM := TBitmap.Create;
      BM.Height := 100; BM.Width := 100;
      GetObject(BM.Handle,Sizeof(BM1),@BM1);

Now the BM1 structure contains the relevant values as one would expect,
like bmWidth, bmHeight, bmPlanes etc. Looks ok.
Now I have a large byte array BA containing color values. (Actually it's
a pointer to a memory block created with GlobalAlloc, so I can get
across the magic 64Kb border.)

       SetBitmapBits(BM.Handle, 1000, @BA);

This does something, but not what I hoped for. A check with GetBitmapBits
shows that my bytes have arrived, but a check with BM.Canvas.Pixels
gives completely different color values.

Could someone comment on any of these lines?

TIA
Wolfgang

--
--------------------------------------------------------------------------
Dr. W. Gross, Abt. f. Exp. Chirurgie, Uniklinik Heidelberg, Germany
Wolfgang.Gr...@exchi.uni-heidelberg.de

 

Re:Graphics.TBitmap and Wintypes.TBitmap puzzle. Hot to use SetBitmapbits


ex...@sun0.urz.uni-heidelberg.de (Dr. W. Gross) wrote:

Quote
>Hallo,

>Two structures called TBitmap (in Unit Graphics and WinTypes) leave me
>in the dark. I'd like to manipulate a bitmap directly, not with the
>Canvas.Pixels array, since this is too slow - especially for TImage.
>(Another puzzle: Accessing Form1.Canvas.Pixels is faster than
>accessing TImage1.Canvas.Pixels).
>The SetBitmapbits function gives crazy colors, although it works somehow.
>But maybe I'm wrong from the very beginning which is like this:

>    VAR   BM : TBitmap;  { this is the Graphics thing}
>          BM1: Wintypes.TBitmap;
>    BEGIN
>      BM := TBitmap.Create;
>      BM.Height := 100; BM.Width := 100;
>      GetObject(BM.Handle,Sizeof(BM1),@BM1);

>Now the BM1 structure contains the relevant values as one would expect,
>like bmWidth, bmHeight, bmPlanes etc. Looks ok.
>Now I have a large byte array BA containing color values. (Actually it's
>a pointer to a memory block created with GlobalAlloc, so I can get
>across the magic 64Kb border.)

>       SetBitmapBits(BM.Handle, 1000, @BA);

>This does something, but not what I hoped for. A check with GetBitmapBits
>shows that my bytes have arrived, but a check with BM.Canvas.Pixels
>gives completely different color values.

        What you get from GetBitmapBits is an array (or "huge array") of
indices into the palette. Ie if you're running at 256 colors you get one
byte per pixel - if you're at 32K colors or 64K colors you get 2 bytes
per pixel, and if you're at 24-bit color I haven't quite got straight yet
whether you get 3 or 4 bytes per pixel; "must" be 3 bytes but that doesn't
seem to work.
        OTOH Canvas.Pixels gives actual RGB color values (typically). The
function that does what Pixels does but for a large number of pixels at a
time would be GetDiBits (or the Delphi GetDIB wrapper).

        You don't need GlobalAlloc, by the way, you can use TMemoryStream
for this sort of thing. (You SearchAll for TMemoryStream and then trace back
all the SeeAlso's to see how it works...)

--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

Other Threads