View Issue Details

IDProjectCategoryView StatusLast Update
0034264FPCFCLpublic2018-09-12 17:25
ReporterOndrej PokornyAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityhave not tried
Status closedResolutionfixed 
Product VersionProduct Build 
Target Version3.2.0Fixed in Version3.3.1 
Summary0034264: AlphaBlend refactoring (simplify, reduce operations)
Description1.) The AlphaBlend function uses 3 times "color2.alpha / $ffff", which can be utilized as another "factor" variable.

2.) In case one of the color alphas is 0 (=fully transparent), the result is trivial and the computations don't have to be executed.

The code result is not modified.
Additional InformationPatch attached.
TagsNo tags attached.
Fixed in Revision39747
FPCOldBugId
FPCTarget
Attached Files
  • alphablend-01.patch (1,349 bytes)
    Index: packages/fcl-image/src/fpcolcnv.inc
    ===================================================================
    --- packages/fcl-image/src/fpcolcnv.inc	(revision 39741)
    +++ packages/fcl-image/src/fpcolcnv.inc	(working copy)
    @@ -298,18 +298,25 @@
     
     function AlphaBlend(color1, color2: TFPColor): TFPColor;
     var
    -  factor: single;
    +  factor1, factor2: single;
     begin
       if color2.alpha = $ffff then
         Result := color2
       else
    +  if color2.alpha = 0 then
    +    Result := color1
    +  else
    +  if color1.alpha = 0 then
    +    Result := color2
    +  else
       begin
    -    factor := (color1.alpha / $ffff) * (1 - color2.alpha / $ffff);
    +    factor1 := (color1.alpha / $ffff) * (1 - color2.alpha / $ffff);
    +    factor2 := color2.alpha / $ffff;
     
    -    Result.red := Round(color1.red * factor + color2.red * color2.alpha / $ffff);
    -    Result.green := Round(color1.green * factor + color2.green * color2.alpha / $ffff);
    -    Result.blue := Round(color1.blue * factor + color2.blue * color2.alpha / $ffff);
    -    Result.alpha := Round(factor * $ffff + color2.alpha);
    +    Result.red := Round(color1.red * factor1 + color2.red * factor2);
    +    Result.green := Round(color1.green * factor1 + color2.green * factor2);
    +    Result.blue := Round(color1.blue * factor1 + color2.blue * factor2);
    +    Result.alpha := Round(factor1 * $ffff + color2.alpha);
       end;
     end;
     
    
    alphablend-01.patch (1,349 bytes)

Activities

Ondrej Pokorny

2018-09-12 11:14

reporter  

alphablend-01.patch (1,349 bytes)
Index: packages/fcl-image/src/fpcolcnv.inc
===================================================================
--- packages/fcl-image/src/fpcolcnv.inc	(revision 39741)
+++ packages/fcl-image/src/fpcolcnv.inc	(working copy)
@@ -298,18 +298,25 @@
 
 function AlphaBlend(color1, color2: TFPColor): TFPColor;
 var
-  factor: single;
+  factor1, factor2: single;
 begin
   if color2.alpha = $ffff then
     Result := color2
   else
+  if color2.alpha = 0 then
+    Result := color1
+  else
+  if color1.alpha = 0 then
+    Result := color2
+  else
   begin
-    factor := (color1.alpha / $ffff) * (1 - color2.alpha / $ffff);
+    factor1 := (color1.alpha / $ffff) * (1 - color2.alpha / $ffff);
+    factor2 := color2.alpha / $ffff;
 
-    Result.red := Round(color1.red * factor + color2.red * color2.alpha / $ffff);
-    Result.green := Round(color1.green * factor + color2.green * color2.alpha / $ffff);
-    Result.blue := Round(color1.blue * factor + color2.blue * color2.alpha / $ffff);
-    Result.alpha := Round(factor * $ffff + color2.alpha);
+    Result.red := Round(color1.red * factor1 + color2.red * factor2);
+    Result.green := Round(color1.green * factor1 + color2.green * factor2);
+    Result.blue := Round(color1.blue * factor1 + color2.blue * factor2);
+    Result.alpha := Round(factor1 * $ffff + color2.alpha);
   end;
 end;
 
alphablend-01.patch (1,349 bytes)

Michael Van Canneyt

2018-09-12 17:19

administrator   ~0110695

Applied, thanks for the patch.

Ondrej Pokorny

2018-09-12 17:25

reporter   ~0110697

Thank you!

Issue History

Date Modified Username Field Change
2018-09-12 11:14 Ondrej Pokorny New Issue
2018-09-12 11:14 Ondrej Pokorny File Added: alphablend-01.patch
2018-09-12 13:28 Michael Van Canneyt Assigned To => Michael Van Canneyt
2018-09-12 13:28 Michael Van Canneyt Status new => assigned
2018-09-12 17:19 Michael Van Canneyt Fixed in Revision => 39747
2018-09-12 17:19 Michael Van Canneyt Note Added: 0110695
2018-09-12 17:19 Michael Van Canneyt Status assigned => resolved
2018-09-12 17:19 Michael Van Canneyt Fixed in Version => 3.3.1
2018-09-12 17:19 Michael Van Canneyt Resolution open => fixed
2018-09-12 17:19 Michael Van Canneyt Target Version => 3.2.0
2018-09-12 17:25 Ondrej Pokorny Note Added: 0110697
2018-09-12 17:25 Ondrej Pokorny Status resolved => closed