[Bf-blender-cvs] [0e38002dd5d] master: Cleanup: Loops in VSE effect processing

Richard Antalik noreply at git.blender.org
Tue Dec 28 16:43:09 CET 2021


Commit: 0e38002dd5dda049b5b8ff0ba635f4192984163d
Author: Richard Antalik
Date:   Tue Dec 28 16:34:00 2021 +0100
Branches: master
https://developer.blender.org/rB0e38002dd5dda049b5b8ff0ba635f4192984163d

Cleanup: Loops in VSE effect processing

Some effect functions looped over alternating lines, previously with
different factors. Since only one factor is used, code can be
simplified by looping all lines in one for loop.

There should be no functional changes.

===================================================================

M	source/blender/sequencer/intern/effects.c

===================================================================

diff --git a/source/blender/sequencer/intern/effects.c b/source/blender/sequencer/intern/effects.c
index 1cf0d7bb34f..f2ee1a058e2 100644
--- a/source/blender/sequencer/intern/effects.c
+++ b/source/blender/sequencer/intern/effects.c
@@ -209,20 +209,15 @@ static void init_alpha_over_or_under(Sequence *seq)
 static void do_alphaover_effect_byte(
     float fac, int x, int y, unsigned char *rect1, unsigned char *rect2, unsigned char *out)
 {
-  int xo;
-  unsigned char *cp1, *cp2, *rt;
-  float tempc[4], rt1[4], rt2[4];
-
-  xo = x;
-  cp1 = rect1;
-  cp2 = rect2;
-  rt = out;
-
-  while (y--) {
-    x = xo;
-    while (x--) {
+  unsigned char *cp1 = rect1;
+  unsigned char *cp2 = rect2;
+  unsigned char *rt = out;
+
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
       /* rt = rt1 over rt2  (alpha from rt1) */
 
+      float tempc[4], rt1[4], rt2[4];
       straight_uchar_to_premul_float(rt1, cp1);
       straight_uchar_to_premul_float(rt2, cp2);
 
@@ -246,54 +241,18 @@ static void do_alphaover_effect_byte(
       cp2 += 4;
       rt += 4;
     }
-
-    if (y == 0) {
-      break;
-    }
-    y--;
-
-    x = xo;
-    while (x--) {
-      straight_uchar_to_premul_float(rt1, cp1);
-      straight_uchar_to_premul_float(rt2, cp2);
-
-      float mfac = 1.0f - (fac * rt1[3]);
-
-      if (fac <= 0.0f) {
-        *((unsigned int *)rt) = *((unsigned int *)cp2);
-      }
-      else if (mfac <= 0.0f) {
-        *((unsigned int *)rt) = *((unsigned int *)cp1);
-      }
-      else {
-        tempc[0] = fac * rt1[0] + mfac * rt2[0];
-        tempc[1] = fac * rt1[1] + mfac * rt2[1];
-        tempc[2] = fac * rt1[2] + mfac * rt2[2];
-        tempc[3] = fac * rt1[3] + mfac * rt2[3];
-
-        premul_float_to_straight_uchar(rt, tempc);
-      }
-      cp1 += 4;
-      cp2 += 4;
-      rt += 4;
-    }
   }
 }
 
 static void do_alphaover_effect_float(
     float fac, int x, int y, float *rect1, float *rect2, float *out)
 {
-  int xo;
-  float *rt1, *rt2, *rt;
-
-  xo = x;
-  rt1 = rect1;
-  rt2 = rect2;
-  rt = out;
+  float *rt1 = rect1;
+  float *rt2 = rect2;
+  float *rt = out;
 
-  while (y--) {
-    x = xo;
-    while (x--) {
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
       /* rt = rt1 over rt2  (alpha from rt1) */
 
       float mfac = 1.0f - (fac * rt1[3]);
@@ -314,32 +273,6 @@ static void do_alphaover_effect_float(
       rt2 += 4;
       rt += 4;
     }
-
-    if (y == 0) {
-      break;
-    }
-    y--;
-
-    x = xo;
-    while (x--) {
-      float mfac = 1.0f - (fac * rt1[3]);
-
-      if (fac <= 0.0f) {
-        memcpy(rt, rt2, sizeof(float[4]));
-      }
-      else if (mfac <= 0.0f) {
-        memcpy(rt, rt1, sizeof(float[4]));
-      }
-      else {
-        rt[0] = fac * rt1[0] + mfac * rt2[0];
-        rt[1] = fac * rt1[1] + mfac * rt2[1];
-        rt[2] = fac * rt1[2] + mfac * rt2[2];
-        rt[3] = fac * rt1[3] + mfac * rt2[3];
-      }
-      rt1 += 4;
-      rt2 += 4;
-      rt += 4;
-    }
   }
 }
 
@@ -377,19 +310,15 @@ static void do_alphaover_effect(const SeqRenderData *context,
 static void do_alphaunder_effect_byte(
     float fac, int x, int y, unsigned char *rect1, unsigned char *rect2, unsigned char *out)
 {
-  int xo;
-  unsigned char *cp1, *cp2, *rt;
-  float tempc[4], rt1[4], rt2[4];
+  unsigned char *cp1 = rect1;
+  unsigned char *cp2 = rect2;
+  unsigned char *rt = out;
 
-  xo = x;
-  cp1 = rect1;
-  cp2 = rect2;
-  rt = out;
-
-  while (y--) {
-    x = xo;
-    while (x--) {
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
       /* rt = rt1 under rt2  (alpha from rt2) */
+
+      float tempc[4], rt1[4], rt2[4];
       straight_uchar_to_premul_float(rt1, cp1);
       straight_uchar_to_premul_float(rt2, cp2);
 
@@ -421,59 +350,18 @@ static void do_alphaunder_effect_byte(
       cp2 += 4;
       rt += 4;
     }
-
-    if (y == 0) {
-      break;
-    }
-    y--;
-
-    x = xo;
-    while (x--) {
-      straight_uchar_to_premul_float(rt1, cp1);
-      straight_uchar_to_premul_float(rt2, cp2);
-
-      if (rt2[3] <= 0.0f && fac >= 1.0f) {
-        *((unsigned int *)rt) = *((unsigned int *)cp1);
-      }
-      else if (rt2[3] >= 1.0f) {
-        *((unsigned int *)rt) = *((unsigned int *)cp2);
-      }
-      else {
-        float temp_fac = (fac * (1.0f - rt2[3]));
-
-        if (fac <= 0) {
-          *((unsigned int *)rt) = *((unsigned int *)cp2);
-        }
-        else {
-          tempc[0] = (temp_fac * rt1[0] + rt2[0]);
-          tempc[1] = (temp_fac * rt1[1] + rt2[1]);
-          tempc[2] = (temp_fac * rt1[2] + rt2[2]);
-          tempc[3] = (temp_fac * rt1[3] + rt2[3]);
-
-          premul_float_to_straight_uchar(rt, tempc);
-        }
-      }
-      cp1 += 4;
-      cp2 += 4;
-      rt += 4;
-    }
   }
 }
 
 static void do_alphaunder_effect_float(
     float fac, int x, int y, float *rect1, float *rect2, float *out)
 {
-  int xo;
-  float *rt1, *rt2, *rt;
-
-  xo = x;
-  rt1 = rect1;
-  rt2 = rect2;
-  rt = out;
+  float *rt1 = rect1;
+  float *rt2 = rect2;
+  float *rt = out;
 
-  while (y--) {
-    x = xo;
-    while (x--) {
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
       /* rt = rt1 under rt2  (alpha from rt2) */
 
       /* this complex optimization is because the
@@ -502,37 +390,6 @@ static void do_alphaunder_effect_float(
       rt2 += 4;
       rt += 4;
     }
-
-    if (y == 0) {
-      break;
-    }
-    y--;
-
-    x = xo;
-    while (x--) {
-      if (rt2[3] <= 0 && fac >= 1.0f) {
-        memcpy(rt, rt1, sizeof(float[4]));
-      }
-      else if (rt2[3] >= 1.0f) {
-        memcpy(rt, rt2, sizeof(float[4]));
-      }
-      else {
-        float temp_fac = fac * (1.0f - rt2[3]);
-
-        if (fac == 0) {
-          memcpy(rt, rt2, sizeof(float[4]));
-        }
-        else {
-          rt[0] = temp_fac * rt1[0] + rt2[0];
-          rt[1] = temp_fac * rt1[1] + rt2[1];
-          rt[2] = temp_fac * rt1[2] + rt2[2];
-          rt[3] = temp_fac * rt1[3] + rt2[3];
-        }
-      }
-      rt1 += 4;
-      rt2 += 4;
-      rt += 4;
-    }
   }
 }
 
@@ -570,37 +427,15 @@ static void do_alphaunder_effect(const SeqRenderData *context,
 static void do_cross_effect_byte(
     float fac, int x, int y, unsigned char *rect1, unsigned char *rect2, unsigned char *out)
 {
-  int xo;
-  unsigned char *rt1, *rt2, *rt;
-
-  xo = x;
-  rt1 = rect1;
-  rt2 = rect2;
-  rt = out;
+  unsigned char *rt1 = rect1;
+  unsigned char *rt2 = rect2;
+  unsigned char *rt = out;
 
   int temp_fac = (int)(256.0f * fac);
   int temp_mfac = 256 - temp_fac;
 
-  while (y--) {
-    x = xo;
-    while (x--) {
-      rt[0] = (temp_mfac * rt1[0] + temp_fac * rt2[0]) >> 8;
-      rt[1] = (temp_mfac * rt1[1] + temp_fac * rt2[1]) >> 8;
-      rt[2] = (temp_mfac * rt1[2] + temp_fac * rt2[2]) >> 8;
-      rt[3] = (temp_mfac * rt1[3] + temp_fac * rt2[3]) >> 8;
-
-      rt1 += 4;
-      rt2 += 4;
-      rt += 4;
-    }
-
-    if (y == 0) {
-      break;
-    }
-    y--;
-
-    x = xo;
-    while (x--) {
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
       rt[0] = (temp_mfac * rt1[0] + temp_fac * rt2[0]) >> 8;
       rt[1] = (temp_mfac * rt1[1] + temp_fac * rt2[1]) >> 8;
       rt[2] = (temp_mfac * rt1[2] + temp_fac * rt2[2]) >> 8;
@@ -615,36 +450,14 @@ static void do_cross_effect_byte(
 
 static void do_cross_effect_float(float fac, int x, int y, float *rect1, float *rect2, float *out)
 {
-  int xo;
-  float *rt1, *rt2, *rt;
-
-  xo = x;
-  rt1 = rect1;
-  rt2 = rect2;
-  rt = out;
+  float *rt1 = rect1;
+  float *rt2 = rect2;
+  float *rt = out;
 
   float mfac = 1.0f - fac;
 
-  while (y--) {
-    x = xo;
-    while (x--) {
-      rt[0] = mfac * rt1[0] + fac * rt2[0];
-      rt[1] = mfac * rt1[1] + fac * rt2[1];
-      rt[2] = mfac * rt1[2] + fac * rt2[2];
-      rt[3] = mfac * rt1[3] + fac * rt2[3];
-
-      rt1 += 4;
-      rt2 += 4;
-      rt += 4;
-    }
-
-    if (y == 0) {
-      break;
-    }
-    y--;
-
-    x = xo;
-    while (x--) {
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
       rt[0] = mfac * rt1[0] + fac * rt2[0];
       rt[1] = mfac * rt1[1] + fac * rt2[1];
       rt[2] = mfac * rt1[2] + fac * rt2[2];
@@ -845,41 +658,16 @@ static void free_gammacross(Sequence *UNUSED(seq), const bool UNUSED(do_id_user)
 static void do_gammacross_effect_byte(
     float fac, int x, int y, unsigned char *rect1, unsigned char *rect2, unsigned char *out)
 {
-  int xo;
-  unsigned char *cp1, *cp2, *rt;
-  float rt1[4], rt2[4], tempc[4];
-
-  xo = x;
-  cp1 = rect1;
-  cp2 = rect2;
-  rt = out;
+  unsigned char *cp1 = rect1;
+  unsigned char *cp2 = rect2;
+  unsigned char *rt = out;
 
   float mfac = 1.0f - fac;
 
-  while (y--) {
-    x = xo;
-    while (x--) {
-      straight_uchar_to_premul_float(rt1, cp1);
-      straight_uchar_to_premul_float(rt2, cp2);
-
-      tempc[0] = gammaCorrect(mfac * invGammaCorrect(rt1[0]) + fac * invGammaCorrect(rt2[0]));
-      tempc[1] = gammaCorrect(mfac * invGammaCorrect(rt1[1]) + fac * invGammaCorrect(rt2[1]));
-      tempc[2] = gammaCorrect(mfac * invGammaCorrect(rt1[2]) + fac * invGammaCorrect(rt2[2]));
-      tempc[3] = gammaCorrect(mfac * invGammaCorrect(rt1[3]) + fac * invGammaCorrect(rt2[3]));
-
-      premul_float_to_straight_uchar(rt, tempc);
-      cp1 += 4;
-      cp2 += 4;
-      rt += 4;
-    }
-
-    if (y == 0) {
-      break;
-    }
-    y--;
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
+      float rt1[4], rt2[4], tempc[4];
 
-    x = xo;
-    while (x--) {
       straight_uchar_to_premul_float(rt1, cp1);
       straight_uchar_to_premul_float(rt2, cp2);
 
@@ -899,38 +687,19 @@ static void do_gammacross_effect_byte(
 static void do_gammacross_effect_float(
     float fac, int x, int y, float *rect1, float *rect2, float *out)
 {
-  int xo;
-  float *rt1, *rt2, *rt;
-
-  xo = x;
-  rt1 = rect1;
-  rt2 = rect2;
-  rt = out;
+  float *rt1 = rect1;
+  float *rt2 = rect2;
+  float *rt = out;
 
   float mfac = 1.0f - fac;
 
-  while (y--) {
-    x = xo * 4;
-    while (x--) {
+  for (int i = 0; i < y; i++) {
+    for (int j = 0; j < x; j++) {
       *rt = gammaCorrect(mfac * invGammaCorrect(*rt1) + fac * invGammaCorrect(*rt2));
       rt1++;
     

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list