Recent

Author Topic: BGRA.ReplaceTransparent - new error introduced  (Read 271 times)

winni

  • Hero Member
  • *****
  • Posts: 1894
BGRA.ReplaceTransparent - new error introduced
« on: October 03, 2020, 08:00:21 pm »
Hi!

I just did a ReplaceTransparent and got a strange result:

The transparent pixels are correct replaced - ok.
Those pixels with an alpha channel with 1..254 are replaced with a
BGRAPixelTransparent! Not ok.

I Think the error came with the introduction of the TGenericUniversalBitmap.

Before that there were no problems.

A little bit of code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button5Click(Sender: TObject);
  2. var Bulb, dest: TBGRAbitmap;
  3. begin
  4. Bulb := TBGRABitmap.Create ('./bulb-light-off.png');
  5. dest := TBGRABitmap.Create (2*bulb.width, bulb.Height);
  6. dest.PutImage(0,0,bulb,dmDrawWithTransparency);
  7. bulb.ReplaceTransparent(CSSWhite);
  8. //bulb.ReplaceColor (BGRAPixelTransparent,cssWhite); // same problem
  9. dest.PutImage(Bulb.width,0,bulb,dmDrawWithTransparency);
  10. dest.SaveToFile('./TwoBulbs.png');
  11.  
  12. bulb.Free;
  13. dest.Free;
  14. end;  
  15.  

And an image in the attachment.

Winni
« Last Edit: October 03, 2020, 08:02:00 pm by winni »

circular

  • Hero Member
  • *****
  • Posts: 3505
    • Personal webpage
Re: BGRA.ReplaceTransparent - new error introduced
« Reply #1 on: October 04, 2020, 01:36:41 pm »
Well, as far as I can see, the semitransparent colors (alpha from 1 to 254) are not replaced.
Conscience is the debugger of the mind

winni

  • Hero Member
  • *****
  • Posts: 1894
Re: BGRA.ReplaceTransparent - new error introduced
« Reply #2 on: October 04, 2020, 01:53:27 pm »
Hi circular!

Look at the shadow at the bottom of the circle:

In the left original those are black semitransprent pixels.

And in the right the bitmap was treated with BGRA.ReplaceTransparent:

The black semitransparent pixel vanished and are replaced by BGRAPixelTransparent
and the light blue forum background is shown.

Winni

circular

  • Hero Member
  • *****
  • Posts: 3505
    • Personal webpage
Re: BGRA.ReplaceTransparent - new error introduced
« Reply #3 on: October 04, 2020, 04:02:24 pm »
The light blue color is shown on both sides, that's normal for black semitransparent pixels. But they are still there, there is a gradient. At least on my screen.
Conscience is the debugger of the mind

winni

  • Hero Member
  • *****
  • Posts: 1894
Re: BGRA.ReplaceTransparent - new error introduced
« Reply #4 on: October 04, 2020, 06:22:20 pm »
Hi!

Sorry. Did not think enough.

What I want is to replace all transparent AND semitransparent pixels with a solid background color. For the semitransparent pixels you have to do a merging of the colors.


I did it this way:
Code: Pascal  [Select][+][-]
  1. procedure replaceColorAndAlpha (bmp: TBGRABitmap; after: TBGRAPixel);
  2. var i : integer;
  3.     P: PBGRAPixel;
  4.  
  5. begin
  6.     p := bmp.Data;
  7.     for i := bmp.NbPixels-1 downto 0 do
  8.       begin
  9.       if p^ = BGRAPixelTransparent then p^ := after else
  10.         begin
  11.          if p^.alpha < 255 then
  12.            begin
  13.            p^ := MergeBGRA(p^,p^.alpha,after,255-p^.alpha);
  14.            p^.alpha := 255;
  15.            end;
  16.         end;// else
  17.         inc(p);
  18.     end; // for
  19.    bmp.InvalidateBitmap;
  20. end;  

Sorry about my mistake.

Winni

circular

  • Hero Member
  • *****
  • Posts: 3505
    • Personal webpage
Re: BGRA.ReplaceTransparent - new error introduced
« Reply #5 on: October 05, 2020, 10:00:20 am »
No problem.

Oh you want to fill the background with transparency. The simplest would be to create a new bitmap, fill it with the color, and then do PutImage on it.

Of course that needs some memory allocation.

Using MergeBGRA like that will not exactly give you the result you want. For the weight of the color, the alpha is taken into account. So I would rather suggest:
Code: Pascal  [Select][+][-]
  1.          if p^.alpha < 255 then
  2.            begin
  3.            weight := p^.alpha;
  4.            p^.alpha := 255;
  5.            p^ := MergeBGRA(p^,weight,after,255-weight);
  6.            end;
  7.  
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018