* * *

Author Topic: Make a transparent form with BGRABitmap  (Read 2358 times)

RAW

  • Sr. Member
  • ****
  • Posts: 310
Re: Make a transparent form with BGRABitmap
« Reply #15 on: July 17, 2017, 10:04:24 pm »
Thanks circular... obviously I need to learn something about graphics... asap...

It compiled very fine, but at runtime I get a SIGSEGV:
..exactly here:
Code: Pascal  [Select]
  1. If p^.alpha = 0

So I thought there must be something referred to "p", because if I use this line
Code: Pascal  [Select]
  1. p:= BGRA.Scanline[x];
then there is no error even if this is wrong...  :)


I thought I can use it like this: first precalculate and then premultiply...
Code: Pascal  [Select]
  1. Var
  2.  p: PBGRAPixel;
  3.  x, y: Integer;  
  4.  
  5. // precalculate bigger bitmap
  6. For x:= BGRA.Width-1 DownTo 0
  7. Do
  8.  Begin
  9.   If p^.alpha = 0
  10.   Then p^:= BGRAPixelTransparent
  11.   Else
  12.    Begin
  13.     p^.red  := p^.red  *(p^.alpha+1) SHR 8;
  14.     p^.green:= p^.green*(p^.alpha+1) SHR 8;
  15.     p^.blue := p^.blue *(p^.alpha+1) SHR 8;
  16.    End;
  17.   Inc(p);
  18.  End;
  19.  
  20. // premultiply bigger bitmap
  21. For y:= 0 To BGRA.Height-1
  22. Do
  23.  Begin
  24.   p:= BGRA.Scanline[y];
  25.  
  26.   For x:= BGRA.Width-1 DownTo 0
  27.   Do
  28.    Begin
  29.     p^.red  := p^.red  *p^.alpha Div 255;
  30.     p^.green:= p^.green*p^.alpha Div 255;
  31.     p^.blue := p^.blue *p^.alpha Div 255;
  32.     Inc(p);
  33.    End;
  34.  End;
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

circular

  • Hero Member
  • *****
  • Posts: 2579
    • Personal webpage
Re: Make a transparent form with BGRABitmap
« Reply #16 on: July 17, 2017, 11:42:33 pm »
Oh ok maybe I did not express myself very well. Let's try again. I am not talking about precaculating, but about doing the computation as follows:
Code: Pascal  [Select]
  1.   For y:= 0 To PNG.Height-1 Do
  2.   Begin
  3.     p:= PNG.Scanline[y];
  4.     for x := PNG.Width-1 downto 0 do
  5.     begin
  6.       if p^.alpha = 0 then
  7.         p^ := BGRAPixelTransparent
  8.       else
  9.       begin
  10.         p^.red := p^.red*(p^.alpha+1) shr 8;
  11.         p^.green := p^.green*(p^.alpha+1) shr 8;
  12.         p^.blue := p^.blue*(p^.alpha+1) shr 8;
  13.       end;
  14.       inc(p);
  15.     end;
  16.   End;
  17.  

But of course, you could also do the precalculation using SHR and then applying it. That would avoid a multiply+SHR and use a memory access instead. Could be useful on a big picture.
Conscience is the debugger of the mind

RAW

  • Sr. Member
  • ****
  • Posts: 310
Re: Make a transparent form with BGRABitmap
« Reply #17 on: July 18, 2017, 12:45:23 am »
Ah, ok.. understood... thanks again.
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

circular

  • Hero Member
  • *****
  • Posts: 2579
    • Personal webpage
Re: Make a transparent form with BGRABitmap
« Reply #18 on: July 18, 2017, 09:04:58 pm »
You're welcome.
Conscience is the debugger of the mind

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus