Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   [GR32] How to blend two TBitmap32s (https://www.delphipraxis.net/154644-%5Bgr32%5D-how-blend-two-tbitmap32s.html)

WojTec 19. Sep 2010 15:45

[GR32] How to blend two TBitmap32s
 
I don't know how to blend two bitmaps if these bitmaps are not layers. For layers I'm using OnPixelCombine with assigned procedures like this:

Delphi-Quellcode:
procedure Multiply(F: TColor32; var B: TColor32; M: TColor32);


I tried this:
Delphi-Quellcode:
function ColorAlgebra(F, B, M: TColor32): TColor32;
begin
  BlendMode.Multiply(F, B, M); // the same procedure for OnPixelCombine
  Result := BlendRegEx(F, B, M);
end;

BlendTransfer(Blended, 0, 0, Blended.BoundsRect, ImageMap, ImageMap.BoundsRect,
  ShapeMap, ShapeMap.BoundsRect, ColorAlgebra, 200
);
but MasterAlpha parasm work diffrent than master alpha in top layer (opacity). This param decide which bitmap should be more visible (0 = first, 255 = second). How to blend bitmaps like layers?

Namenloser 19. Sep 2010 15:52

AW: [GR32] How to blend two TBitmap32s
 
Why so complicated? I've always simply done it like this:
Delphi-Quellcode:
var
  A,B: TBitmap32;
begin
  [...]
  A.MasterAlpha := 127; // 50%
  A.DrawMode := dmBlend;
  A.DrawTo(B, 0, 0);
  // B now contains the blended image
  [...]

WojTec 19. Sep 2010 16:08

Re: [GR32] How to blend two TBitmap32s
 
Delphi-Quellcode:
    ImageMap.DrawMode := dmBlend;
    ImageMap.OnPixelCombine := BlendMode.Multiply;
    ImageMap.MasterAlpha := 0;
    ImageMap.DrawTo(ShapeMap, 0, 0);

    Image321.Bitmap.Assign(ShapeMap);
I tried and effect is identical.

Namenloser 19. Sep 2010 16:20

AW: [GR32] How to blend two TBitmap32s
 
What effect is identical? It'd really help if you were more specific about what you want to achieve and what is happening instead.

mkinzler 19. Sep 2010 16:26

AW: [GR32] How to blend two TBitmap32s
 
Perhaps show a screenshot or similar

WojTec 19. Sep 2010 17:14

Re: [GR32] How to blend two TBitmap32s
 
Liste der Anhänge anzeigen (Anzahl: 1)
Ok guys, please look at attached picture:

I want to blend 2 with 1 and get 5. In my method I got 3, in @NamenLozer's method I got 4. In my program that suppots layers I got 5 and this is valid effct (opacity is 100%, white is not visible). Are you understand what I mean? I want to get 5 effect without layers. :)

@mkinzler, thanks for advise :)

Medium 19. Sep 2010 17:28

AW: [GR32] How to blend two TBitmap32s
 
That's a simple multiplication, and has nothing in the world to do with alpha. Just multiply 1 and 2, and be done.

Edit: Well, it looks like in 5 alpha controls the strength, so that the result becomes B1 * (alpha2+(1-alpha2)*B2)

Edit2: And be aware, that whenever we talk about colors, their intensity is thought to range between 0 and 1. So if you have to work with Bytes, don't forget to scale them to 0..1, or you'll get wrong results.

Namenloser 19. Sep 2010 17:38

AW: [GR32] How to blend two TBitmap32s
 
I think this should do it, but I haven't tested it:
Delphi-Quellcode:
function ColorAlgebra(F, B, M: TColor32): TColor32;
begin
  // 1. multiply
  Result := ColorModulate(F, B);
  // 2. blend
  Result := BlendRegEx(Result, B, M);
end;
[edit]
Only your assumption that alpha=0 would result in picture 2 is false. What you'd really get is a picture that has the background in the middle and then fades to solid black near the edges. Nowhere in this picture you'd see anything white.
Because if you did, then you'd logically also have some white in it when alpha>0, which is what happens in 3 and 4.
[/edit]

WojTec 19. Sep 2010 18:08

Re: [GR32] How to blend two TBitmap32s
 
@NamenLozer, this is black blured border on white background. I mean white color in multiply mode is neutral (not visible) ;)

Here is test code:

Delphi-Quellcode:
function ColorAlgebra(F, B, M: TColor32): TColor32;
begin
//  BlendMode.Multiply(F, B, M);
//  Result := BlendRegEx(F, B, M);
  // 1. multiply
  Result := ColorModulate(F, B);
  // 2. blend
  Result := BlendRegEx(Result, B, M);
end;

procedure TForm18.Image321Click(Sender: TObject);
var
  ImageMap, ShapeMap, Blended: TBitmap32;
begin
  ImageMap := TBitmap32.Create;
  ShapeMap := TBitmap32.Create;
  Blended := TBitmap32.Create;
  try
    // Bitmap.SetSize(240, 180);
    // Bitmap.Clear(clWhite32);
    ImageMap.Assign(Image321.Bitmap);
    ShapeMap.SetSize(ImageMap.Width, ImageMap.Height);
    Blended.SetSize(ImageMap.Width, ImageMap.Height);
    // vignette is generated, can use existing img too
    [vignette code here, working on ShapeMap]

    Blended.MasterAlpha := 0;
    ImageMap.MasterAlpha := 0;
    ShapeMap.MasterAlpha := 0;
//    BlendTransfer(Blended, 0, 0, Blended.BoundsRect, ImageMap, ImageMap.BoundsRect,
//      ShapeMap, ShapeMap.BoundsRect, ColorAlgebra, 0
//    );
//    Image321.Bitmap.Assign(Blended);

    ImageMap.DrawMode := dmBlend;
    ImageMap.OnPixelCombine := BlendMode.Multiply;
    ImageMap.MasterAlpha := 0;
    ImageMap.DrawTo(ShapeMap, 0, 0);
    Image321.Bitmap.Assign(ShapeMap);
  finally
    ImageMap.Free;
    ShapeMap.Free;
    Blended.Free;
  end;
end;
Whats wrong with this?

Multiply() method supports alpha. In this way I realized blending and opacity in my layers (5).


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:46 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz