From 4082d8ea27a6d386a01681e51e9776b552f6cb5c Mon Sep 17 00:00:00 2001
From: Merov Linden <merov@lindenlab.com>
Date: Tue, 21 Jan 2014 16:39:23 -0800
Subject: [PATCH] ACME-1236 : Fixes gamma and brightness code. Also fixes
 example xml filter files.

---
 .../llimage_libtest/brighten.xml                 |  2 +-
 .../llimage_libtest/colorize.xml                 | 16 ++++++++++------
 .../llimage_libtest/contrast.xml                 |  7 -------
 .../integration_tests/llimage_libtest/darken.xml |  2 +-
 .../integration_tests/llimage_libtest/gamma.xml  |  9 +--------
 indra/llimage/llimagefilter.cpp                  | 13 +++++++------
 indra/llimage/llimagefilter.h                    |  2 +-
 7 files changed, 21 insertions(+), 30 deletions(-)

diff --git a/indra/integration_tests/llimage_libtest/brighten.xml b/indra/integration_tests/llimage_libtest/brighten.xml
index d17b96d2d7a..9b4232229fd 100755
--- a/indra/integration_tests/llimage_libtest/brighten.xml
+++ b/indra/integration_tests/llimage_libtest/brighten.xml
@@ -2,7 +2,7 @@
     <array>
         <array>
             <string>brighten</string>
-            <real>50.0</real>
+            <real>0.5</real>
             <real>1.0</real>
             <real>1.0</real>
             <real>1.0</real>
diff --git a/indra/integration_tests/llimage_libtest/colorize.xml b/indra/integration_tests/llimage_libtest/colorize.xml
index 18c6cd3425c..72e58b0ffe0 100644
--- a/indra/integration_tests/llimage_libtest/colorize.xml
+++ b/indra/integration_tests/llimage_libtest/colorize.xml
@@ -1,20 +1,24 @@
 <llsd>
     <array>
         <array>
-            <string>linearize</string>
+            <string>stencil</string>
+            <string>vignette</string>
+            <string>blend</string>
             <real>0.0</real>
             <real>1.0</real>
+            <real>0.0</real>
+            <real>0.0</real>
             <real>1.0</real>
-            <real>1.0</real>
+            <real>10.0</real>
         </array>
         <array>
             <string>colorize</string>
             <real>1.0</real>
-            <real>1.0</real>
-            <real>1.0</real>
-            <real>0.2</real>
             <real>0.0</real>
-            <real>0.2</real>
+            <real>0.0</real>
+            <real>0.5</real>
+            <real>0.5</real>
+            <real>0.0</real>
         </array>
     </array>
 </llsd>
diff --git a/indra/integration_tests/llimage_libtest/contrast.xml b/indra/integration_tests/llimage_libtest/contrast.xml
index 8dcdd1a9a93..00746b8a9e4 100644
--- a/indra/integration_tests/llimage_libtest/contrast.xml
+++ b/indra/integration_tests/llimage_libtest/contrast.xml
@@ -1,12 +1,5 @@
 <llsd>
     <array>
-        <array>
-            <string>linearize</string>
-            <real>0.0</real>
-            <real>1.0</real>
-            <real>1.0</real>
-            <real>1.0</real>
-        </array>
         <array>
             <string>contrast</string>
             <real>1.5</real>
diff --git a/indra/integration_tests/llimage_libtest/darken.xml b/indra/integration_tests/llimage_libtest/darken.xml
index 8d110452e98..5cec3589b66 100755
--- a/indra/integration_tests/llimage_libtest/darken.xml
+++ b/indra/integration_tests/llimage_libtest/darken.xml
@@ -2,7 +2,7 @@
     <array>
         <array>
             <string>darken</string>
-            <real>50.0</real>
+            <real>0.5</real>
             <real>1.0</real>
             <real>1.0</real>
             <real>1.0</real>
diff --git a/indra/integration_tests/llimage_libtest/gamma.xml b/indra/integration_tests/llimage_libtest/gamma.xml
index 7505a03027f..19af09b0462 100644
--- a/indra/integration_tests/llimage_libtest/gamma.xml
+++ b/indra/integration_tests/llimage_libtest/gamma.xml
@@ -1,15 +1,8 @@
 <llsd>
     <array>
-        <array>
-            <string>linearize</string>
-            <real>0.0</real>
-            <real>1.0</real>
-            <real>1.0</real>
-            <real>1.0</real>
-        </array>
         <array>
             <string>gamma</string>
-            <real>1.5</real>
+            <real>1.7</real>
             <real>1.0</real>
             <real>1.0</real>
             <real>1.0</real>
diff --git a/indra/llimage/llimagefilter.cpp b/indra/llimage/llimagefilter.cpp
index 75661a6d4bd..c02679e04fc 100755
--- a/indra/llimage/llimagefilter.cpp
+++ b/indra/llimage/llimagefilter.cpp
@@ -80,7 +80,6 @@ LLImageFilter::~LLImageFilter()
  * Improve perf: make sure filter is not called more than necessary in viewer (seems to be called 3 times per change)
  * Make filter definition resolution independent (do not use pixel size anywhere)
  * Add gradient coloring as a filter
- * Add convolve3x3
  
  params:
  * vignette : center_x, center_y, width, feather
@@ -251,12 +250,12 @@ void LLImageFilter::executeFilter(LLPointer<LLImageRaw> raw_image)
         else if (filter_name == "brighten")
         {
             LLColor3 color((float)(mFilterData[i][2].asReal()),(float)(mFilterData[i][3].asReal()),(float)(mFilterData[i][4].asReal()));
-            filterBrightness((S32)(mFilterData[i][1].asReal()),color);
+            filterBrightness((float)(mFilterData[i][1].asReal()),color);
         }
         else if (filter_name == "darken")
         {
             LLColor3 color((float)(mFilterData[i][2].asReal()),(float)(mFilterData[i][3].asReal()),(float)(mFilterData[i][4].asReal()));
-            filterBrightness((S32)(-mFilterData[i][1].asReal()),color);
+            filterBrightness((float)(-mFilterData[i][1].asReal()),color);
         }
         else if (filter_name == "linearize")
         {
@@ -807,7 +806,7 @@ void LLImageFilter::filterGamma(F32 gamma, const LLColor3& alpha)
     
     for (S32 i = 0; i < 256; i++)
     {
-        F32 gamma_i = llclampf((float)(powf((float)(i)/255.0,gamma)));
+        F32 gamma_i = llclampf((float)(powf((float)(i)/255.0,1.0/gamma)));
         // Blend in with alpha values
         gamma_red_lut[i]   = (U8)((1.0 - alpha.mV[0]) * (float)(i) + alpha.mV[0] * 255.0 * gamma_i);
         gamma_green_lut[i] = (U8)((1.0 - alpha.mV[1]) * (float)(i) + alpha.mV[1] * 255.0 * gamma_i);
@@ -969,15 +968,17 @@ void LLImageFilter::filterContrast(F32 slope, const LLColor3& alpha)
     colorCorrect(contrast_red_lut,contrast_green_lut,contrast_blue_lut);
 }
 
-void LLImageFilter::filterBrightness(S32 add, const LLColor3& alpha)
+void LLImageFilter::filterBrightness(F32 add, const LLColor3& alpha)
 {
     U8 brightness_red_lut[256];
     U8 brightness_green_lut[256];
     U8 brightness_blue_lut[256];
     
+    S32 add_value = (S32)(add * 255.0);
+    
     for (S32 i = 0; i < 256; i++)
     {
-        U8 value_i = (U8)(llclampb((S32)((S32)(i) + add)));
+        U8 value_i = (U8)(llclampb(i + add_value));
         // Blend in with alpha values
         brightness_red_lut[i]   = (U8)((1.0 - alpha.mV[0]) * (float)(i) + alpha.mV[0] * value_i);
         brightness_green_lut[i] = (U8)((1.0 - alpha.mV[1]) * (float)(i) + alpha.mV[1] * value_i);
diff --git a/indra/llimage/llimagefilter.h b/indra/llimage/llimagefilter.h
index 738c6936861..3e3be3d88b6 100755
--- a/indra/llimage/llimagefilter.h
+++ b/indra/llimage/llimagefilter.h
@@ -84,7 +84,7 @@ class LLImageFilter
     void filterEqualize(S32 nb_classes, const LLColor3& alpha); // Use histogram to equalize constrast between nb_classes throughout the image
     void filterColorize(const LLColor3& color, const LLColor3& alpha);  // Colorize with color and alpha per channel
     void filterContrast(F32 slope, const LLColor3& alpha);      // Change contrast according to slope: > 1.0 more contrast, < 1.0 less contrast
-    void filterBrightness(S32 add, const LLColor3& alpha);      // Change brightness according to add: > 0 brighter, < 0 darker
+    void filterBrightness(F32 add, const LLColor3& alpha);      // Change brightness according to add: > 0 brighter, < 0 darker
     
     // Filter Primitives
     void colorTransform(const LLMatrix3 &transform);
-- 
GitLab