Recent

Author Topic: [SOLVED] TBitmap with alpha  (Read 5557 times)

mercury

  • Full Member
  • ***
  • Posts: 154
[SOLVED] TBitmap with alpha
« on: March 11, 2015, 02:44:54 pm »
I just want change the color.
Code: [Select]
var
  I, J: integer;
  C: TColor;
  R, G, B: byte;
  Bitmap: TBitmap;
begin

......

    with Bitmap do begin
      for J := 0 to Height - 1 do begin
        for I := 0 to Width - 1 do begin
          C := Canvas.Pixels[I, J];
          R := integer(GetRValue(C)) div 1;
          G := integer(GetGValue(C)) div 30;
          B := integer(GetBValue(C)) div 30;
          Canvas.Pixels[I, J] := TColor(RGB(R, G, B));
        end;
      end;
    end;
end;

The color is changed, but also lost transparence.

And with this :
Code: [Select]
var
  I, J: integer;
  C: TColor;
  R, G, B: byte;
  Bitmap: TBitmap;
begin

......


    with Bitmap do begin
      for J := 0 to Height - 10 do begin
        for I := 0 to Width - 10 do begin
          C := Canvas.Pixels[I, J];
          R := integer(GetRValue(C)) div 1;
          G := integer(GetGValue(C)) div 30;
          B := integer(GetBValue(C)) div 30;
          Canvas.Pixels[I, J] := TColor(RGB(R, G, B));
        end;
      end;
    end;
end;

The picture just lost the part I changed ( J := 0 to Height - 10  and  I := 0 to Width - 10)
It's inconceivability.
Anyone can tell me why?

And also many function will destroy transparence like:
Code: [Select]
Bitmap.Width = xxx;
Bitmap.Canvas.CopyRect(xxx);

How can I keep the transparence?
Thank you.
« Last Edit: September 27, 2015, 02:50:31 pm by mercury »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TBitmap with alpha
« Reply #1 on: March 11, 2015, 04:01:39 pm »
You need to switch RGB functions to RGBA ones. TColor contains the transparency in it, so by using RGB you are overriding it with solid alpha.

mercury

  • Full Member
  • ***
  • Posts: 154
Re: TBitmap with alpha
« Reply #2 on: March 11, 2015, 04:07:34 pm »
You need to switch RGB functions to RGBA ones. TColor contains the transparency in it, so by using RGB you are overriding it with solid alpha.
seems no such a function called RGBA.

mm7

  • Full Member
  • ***
  • Posts: 222
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: TBitmap with alpha
« Reply #3 on: March 11, 2015, 09:49:12 pm »
TColor does not have translucency (Alpha). TFPColor does.
I believe to achieve translucency you need either to use FP API, or to work directly with bitmap memory. Bitmap should be created with RawImage.Description that has AlphaPrec >0, usually 8.
Then after RawImage.CreateData you can work with RawImage.Data.

In Linux, I've found that any call to Canvas functions will destroy all your previous work with RawImage.Data because the raw data will be rebuilt to native format (your current X11 Server format).

This reading may help you http://wiki.freepascal.org/Fast_direct_pixel_access

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: TBitmap with alpha
« Reply #4 on: May 02, 2015, 07:39:10 am »
Have you tried TBGRABitmap? It has alpha and much more.

VTwin
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

circular

  • Hero Member
  • *****
  • Posts: 4402
    • Personal webpage
Re: TBitmap with alpha
« Reply #5 on: May 04, 2015, 01:37:48 pm »
Yes, with BGRABitmap you could do it like that:
Code: [Select]
var
  I, J: integer;
  C: TBGRAPixel;
  Bitmap: TBitmap;
begin

......

    with Bitmap do begin
      for J := 0 to Height - 1 do begin
        for I := 0 to Width - 1 do begin
          C := GetPixel(I, J);
          C.red := C.red div 1;
          C.green := C.green div 30;
          C.blue := C.blue div 30;
          SetPixel(I,J, C);
        end;
      end;
    end;
end;

Or do something faster with a PBGRAPixel pointer.
See: http://wiki.lazarus.freepascal.org/BGRABitmap_tutorial_4
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018