OSDN Git Service

Vignette filter: precompute more values, simplify math, cleanup code
authorRajeev Sharma <rdsharma@google.com>
Wed, 1 Aug 2012 22:46:30 +0000 (15:46 -0700)
committerRajeev Sharma <rdsharma@google.com>
Wed, 1 Aug 2012 22:46:30 +0000 (15:46 -0700)
Change-Id: I29cfc313a5173fd393944dd9af58d0111a3d726a

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/vignette.rsh

index 9429564..a1e4ae5 100644 (file)
  * limitations under the License.
  */
 
-static float2 scale;
-static float2 center, dimensions;
-static float range, inv_max_dist, shade, slope;
-static float2 overDimensions;
+static float2 neg_center, axis_scale, inv_dimensions;
+static float sloped_neg_range, sloped_inv_max_dist, shade, opp_shade;
 
 void init_vignette(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y,
         float desired_scale, float desired_shade, float desired_slope) {
 
-    center.x = center_x;
-    center.y = center_y;
-    dimensions.x = (float)dim_x;
-    dimensions.y = (float)dim_y;
+    neg_center.x = -center_x;
+    neg_center.y = -center_y;
+    inv_dimensions.x = 1.f / (float)dim_x;
+    inv_dimensions.y = 1.f / (float)dim_y;
+
+    axis_scale = (float2)1.f;
+    if (dim_x > dim_y)
+        axis_scale.y = (float)dim_y / (float)dim_x;
+    else
+        axis_scale.x = (float)dim_x / (float)dim_y;
+
+    const float max_dist = 0.5 * length(axis_scale);
+    sloped_inv_max_dist = desired_slope * 1.f/max_dist;
 
-    float max_dist = 0.5;
-    if (dim_x > dim_y) {
-        scale.x = 1.0;
-        scale.y = dimensions.y / dimensions.x;
-        max_dist *= sqrt(scale.y*scale.y + 1);
-    } else {
-        scale.x = dimensions.x / dimensions.y;
-        scale.y = 1.0;
-        max_dist *= sqrt(scale.x*scale.x + 1);
-    }
-    inv_max_dist = 1.0/max_dist;
     // Range needs to be between 1.3 to 0.6. When scale is zero then range is
     // 1.3 which means no vignette at all because the luminousity difference is
     // less than 1/256.  Expect input scale to be between 0.0 and 1.0.
-    range = 1.3 - 0.7*sqrt(desired_scale);
-    shade = desired_shade;
-    slope = desired_slope;
+    const float neg_range = 0.7*sqrt(desired_scale) - 1.3;
+    sloped_neg_range = exp(neg_range * desired_slope);
 
-    overDimensions = ((float2)1.f) / dimensions;
+    shade = desired_shade;
+    opp_shade = 1.f - desired_shade;
 }
 
 void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
     // Convert x and y to floating point coordinates with center as origin
     const float4 fin = convert_float4(*in);
-    float2 coord = {(float)x, (float)y};
-    coord *= overDimensions;
-    coord -= center;
-    const float dist = length(scale * coord);
-    const float lumen = shade / (1.0 + exp((dist * inv_max_dist - range) * slope)) + (1.0 - shade);
+    const float2 inCoord = {(float)x, (float)y};
+    const float2 coord = mad(inCoord, inv_dimensions, neg_center);
+    const float sloped_dist_ratio = length(axis_scale * coord)  * sloped_inv_max_dist;
+    const float lumen = opp_shade + shade / ( 1.0 + sloped_neg_range * exp(sloped_dist_ratio) );
     float4 fout;
     fout.rgb = fin.rgb * lumen;
     fout.w = fin.w;