Recent

Author Topic: [SOLVED]Replacing transparency with a custom colour?  (Read 7007 times)

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED]Replacing transparency with a custom colour?
« on: December 04, 2017, 09:27:09 am »

I have the code below.
Is there a way to replace transparrent pixels (when any) to pixels with a custom colour (i.e. black)? 


Code: Pascal  [Select][+][-]
  1.     Bitmap:=TBGRABitmap.Create(round(mNewHeight/Ratio),mNewHeight);
  2.     Bitmap.Canvas.StretchDraw(rect(-cropleft,-CropTop,round(mNewHeight/Ratio)+CropRight,mNewHeight+CropBottom),aPicture.Graphic);
  3.     case Angle of
  4.       90: BGRAReplace(Bitmap, Bitmap.RotateCW);
  5.       180: begin BGRAReplace(Bitmap, Bitmap.RotateCW); BGRAReplace(Bitmap, Bitmap.RotateCW); end;
  6.       270: BGRAReplace(Bitmap, Bitmap.RotateCCW);
  7.     end; //case
  8.     aPicture.Assign(Bitmap);




Edit:

I tried the code below, buit in vain:
Code: Pascal  [Select][+][-]
  1. ...
  2.     Bitmap:=TBGRABitmap.Create(round(mNewHeight/Ratio),mNewHeight);
  3.  
  4.     BackgroundColour.red:=0;
  5.     BackgroundColour.green:=0;
  6.     BackgroundColour.blue:=0;
  7.     Bitmap.Rectangle(0,0,round(mNewHeight/Ratio),mNewHeight, BackgroundColour,BackgroundColour,TDrawMode.dmDrawWithTransparency {or some other value});
  8. ...
« Last Edit: December 07, 2017, 07:54:02 am by CM630 »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Replacing transparency with a custom colour?
« Reply #1 on: December 05, 2017, 04:03:14 pm »
This code is to access all pixels in a bitmap, so you can find all the alpha channels (p^.alpha <> 255) and fill these with any color you want.

Code: Pascal  [Select][+][-]
  1. // all pixels //
  2. var
  3.   i: integer;
  4.   p: PBGRAPixel;
  5. begin
  6.   p := Bitmap.Data;
  7.  
  8.   for i := Bitmap.NBPixels-1 downto 0 do
  9.   begin
  10.     p^.red := ;
  11.     p^.green := ;
  12.     p^.blue := ;
  13.     p^.alpha := ;
  14.     Inc(p);
  15.   end;
  16.  
  17. // scan line //
  18. var
  19.   x, y: integer;
  20.   p: PBGRAPixel;
  21. begin
  22.   for y := 0 to Bitmap.Height - 1 do
  23.   begin
  24.     p := Bitmap.Scanline[y];
  25.     for x := 0 to Bitmap.Width - 1 do
  26.     begin
  27.       p^.red := ;
  28.       p^.green := ;
  29.       p^.blue := ;
  30.       p^.alpha := ;
  31.       Inc(p);
  32.     end;
  33.   end;
  34.   Bitmap.InvalidateBitmap;

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Replacing transparency with a custom colour?
« Reply #2 on: December 05, 2017, 06:15:14 pm »
what is the problem with   Bitmap.ReplaceTransparent?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Replacing transparency with a custom colour?
« Reply #3 on: December 05, 2017, 06:48:39 pm »
I see an issue or two

Why you're using Bitmap.Canvas.StretchDraw?

According to all suggestions from @circular, the author of BGRABitmap is discouraged to use Bitmap.Canvas functions since it is slower. You must use Bitmap or Bitmap.Canvas2D but never Bitmap.Canvas...

Also why you have transparency? When you draw you can use dmSet instead of dmDrawWithTransparency and remove transparency directly.

When you create the bitmap choose a color, like TBGRABitmap.Create(100,100,BGRABlack)...

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Replacing transparency with a custom colour?
« Reply #4 on: December 06, 2017, 08:29:17 am »
...Why you're using Bitmap.Canvas.StretchDraw?...
...You must use Bitmap or Bitmap.Canvas2D but never Bitmap.Canvas...
I have either found an example with Canvas or somebody hinted me to do it this way.
And Bitmap.Canvas2d.StretchDraw(... does not compile.


...
Also why you have transparency? When you draw you can use dmSet instead of dmDrawWithTransparency and remove transparency directly. When you create the bitmap choose a color, like TBGRABitmap.Create(100,100,BGRABlack)...

Eeerrrr... I am loading some PNG files and they seem to come with transparrency. And I need to get rid of it.

Unfortunately TBGRABitmap.Create(...,...,BGRABlack) did not help.

what is the problem with   Bitmap.ReplaceTransparent?


The code below does nothing:


Code: Pascal  [Select][+][-]
  1.  BackgroundColour.red:=0;
  2.     BackgroundColour.green:=0;
  3.     BackgroundColour.blue:=0;
  4.     Bitmap.ReplaceTransparent(BackgroundColour) ;  



I will try Lainz's first proposal and feed back.
« Last Edit: December 06, 2017, 08:54:19 am by CM630 »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Replacing transparency with a custom colour?
« Reply #5 on: December 06, 2017, 08:56:00 am »

The code below (with or without the { }) results in empty images.
I will create a code snippet and attach it.




Code: Pascal  [Select][+][-]
  1. procedure RemoveTransparrency(Bitmap: tBGRABitmap);
  2. var
  3.   i: integer;
  4.   p: PBGRAPixel;
  5.   x, y: integer;
  6. // all pixels //
  7. begin
  8.   p := Bitmap.Data;
  9.  
  10.  
  11.   for i := Bitmap.NBPixels-1 downto 0 do
  12.   begin
  13.     p^.red := 0;
  14.     p^.green := 0;
  15.     p^.blue := 0;
  16.     p^.alpha := 0;
  17.     Inc(p);
  18.   end; //for i
  19.  
  20.  
  21. // scan line //
  22.   {for y := 0 to Bitmap.Height - 1 do
  23.   begin
  24.     p := Bitmap.Scanline[y];
  25.     for x := 0 to Bitmap.Width - 1 do
  26.     begin
  27.       p^.red := 0;
  28.       p^.green := 0;
  29.       p^.blue := 0;
  30.       p^.alpha := 0;
  31.       Inc(p);
  32.     end; //for x
  33.   end; //for y}
  34.   Bitmap.InvalidateBitmap;
  35. end;    
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Replacing transparency with a custom colour?
« Reply #6 on: December 06, 2017, 09:07:01 am »
The code below (with or without the { }) results in empty images.
Although sounding harsh, i would say *duh*

Quote
I will create a code snippet and attach it.
No need. the 'error' is obvious


Code: Pascal  [Select][+][-]
  1.   // for all pixels that are in the bitmap
  2.   for i := Bitmap.NBPixels-1 downto 0 do
  3.   begin
  4.     p^.red := 0;    // Set red value to 0
  5.     p^.green := 0; // Set green value to 0
  6.     p^.blue := 0;  // Set blue value to 0
  7.     p^.alpha := 0; // Set alpha to zero
  8.     Inc(p);
  9.   end; //for i
  10.   // Same for the scanline
  11.  
So what did you expect when you clear all the pixels ?

I think what lainz meant, is something like:
Code: [Select]
  for i := Bitmap.NBPixels-1 downto 0 do
  begin
   // 'convert' only _alpha_ indicated pixels ...
   if p^.alpha <> 255 then
   begin
     // ... into white
     p^.Alpha := 255;
     p^.red := 255;   
     p^.green := 255;
     p^.blue := 255;
  end;
end;

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Replacing transparency with a custom colour?
« Reply #7 on: December 06, 2017, 09:20:06 am »
So what did you expect when you clear all the pixels ?
I have absolutely no idea what I am doing. I had no clue that removing transparrency would be so hard.
Your solution makes all the image white, while with zeroes it gets all black.


I have attached a snippet with a sample png.

Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Replacing transparency with a custom colour?
« Reply #8 on: December 06, 2017, 10:03:49 am »
The provided png example is 24 bit so does not have any transparency.

That you perhaps experience it as transparency is another ballgame. I do not have enough knowledge on bgrabitmap to be able to have a quick answer for you. As a guess you want to change the black background (r, g and b values are 0) into another colour. Which colour you want that to be i have no idea. So check for r,g and b being zero and change only those pixels who have all r,b and b values set to zero at the same time, into another colour of your choice.

In short a true transparent image has a additional channel (called alpha) which tells how transparent a single pixel is. There can be 3 states: not transparent at all, completely transparent, or somewhere in the middle.

Think of it as a see through glass window. When all pixels are completely transparent then you can see through the window. If you would be able to draw a pixel on that glass window with a black paint then this pixel would not be transparent at all. Would you for instance draw a pixel with red oil-paint then this pixel could be seen through, although the color is still reddish. The magnitude of which the pixel can be seen through depends on the value of the alpha channel.

Again, your picture has 24 bits colours, so has no transparency information added. Therefor it would need to have 32-bit colours.

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Replacing transparency with a custom colour?
« Reply #9 on: December 06, 2017, 10:50:26 am »
I want the black background to be displayed as a black background.I commented  //ResizeImage(MyPicture,MyPicture.Height,rect(0,0,0,0)); in my code and it is displayed as black.
So it seems that ResizeImage adds a transparency layer to my image (black becomes transparent).

Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Replacing transparency with a custom colour?
« Reply #10 on: December 06, 2017, 01:45:03 pm »
I want the black background to be displayed as a black background.I commented  //ResizeImage(MyPicture,MyPicture.Height,rect(0,0,0,0)); in my code and it is displayed as black.
So it seems that ResizeImage adds a transparency layer to my image (black becomes transparent).

Give us a project we can compile!

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Replacing transparency with a custom colour?
« Reply #11 on: December 06, 2017, 03:47:26 pm »
I unzipped the attached file and it compiles here just fine. :o
I have created it from scratch and just added BGRA's package to project.
I will see if I can do sth. tomorrow.
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Replacing transparency with a custom colour?
« Reply #12 on: December 06, 2017, 04:28:25 pm »
I unzipped the attached file and it compiles here just fine. :o
I have created it from scratch and just added BGRA's package to project.
I will see if I can do sth. tomorrow.

Sorry I did not see that post, I'm testing it.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Replacing transparency with a custom colour?
« Reply #13 on: December 06, 2017, 04:35:02 pm »
Here it is fixed

And note that the png does not have transparency, when your bitmap has transparency just change BGRABlack to something else.
« Last Edit: December 06, 2017, 04:40:31 pm by lainz »

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Replacing transparency with a custom colour?
« Reply #14 on: December 07, 2017, 08:04:06 am »
...the png does not have transparency...
Thanks, I have finally realised that but I had no idea how to handle the situation.
Not everything works fine!
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

 

TinyPortal © 2005-2018