lottie/vector: added updateLuma() api to convert RGBA value to HSL 01/197201/2
authorsubhransu mohanty <sub.mohanty@samsung.com>
Thu, 10 Jan 2019 07:31:39 +0000 (16:31 +0900)
committersubhransu mohanty <sub.mohanty@samsung.com>
Fri, 11 Jan 2019 00:58:29 +0000 (09:58 +0900)
Change-Id: Icacfe4ffdf957d10e7bd654878895b856e045136

src/vector/vbitmap.cpp
src/vector/vbitmap.h

index 25c7919..370c69c 100644 (file)
@@ -19,6 +19,7 @@
 #include "vbitmap.h"
 #include <string.h>
 #include "vglobal.h"
+#include "vdrawhelper.h"
 
 V_BEGIN_NAMESPACE
 
@@ -99,6 +100,35 @@ struct VBitmap::Impl {
     {
         //@TODO
     }
+
+    void updateLuma() {
+        if (mFormat != VBitmap::Format::ARGB32_Premultiplied) return;
+
+        for (uint col = 0 ; col < mHeight ; col++) {
+            uint *pixel = (uint *) (mData + mStride * col);
+            for (uint row = 0 ; row < mWidth; row++) {
+                int alpha = vAlpha(*pixel);
+                if (alpha == 0) {
+                    pixel++;
+                    continue;
+                }
+
+                int red = vRed(*pixel);
+                int green = vGreen(*pixel);
+                int blue = vBlue(*pixel);
+
+                if (alpha != 255) {
+                    //un multiply
+                    red = (red * 255) / alpha;
+                    green = (green * 255) / alpha;
+                    blue = (blue * 255) / alpha;
+                }
+                int luminosity = (0.299*red + 0.587*green + 0.114*blue);
+                *pixel = luminosity << 24;
+                pixel++;
+            }
+        }
+    }
 };
 
 VBitmap::VBitmap(uint width, uint height, VBitmap::Format format)
@@ -165,4 +195,20 @@ void VBitmap::fill(uint pixel)
     if (mImpl) mImpl->fill(pixel);
 }
 
+/*
+ * This is special function which converts
+ * RGB value to Luminosity and stores it in
+ * the Alpha component of the pixel.
+ * After this conversion the bitmap data is no more
+ * in RGB space. but the Alpha component contains the
+ *  Luminosity value of the pixel in HSL color space.
+ * NOTE: this api has its own special usecase
+ * make sure you know what you are doing before using
+ * this api.
+ */
+void VBitmap::updateLuma()
+{
+    if (mImpl) mImpl->updateLuma();
+}
+
 V_END_NAMESPACE
index f99b8f2..e40c52a 100644 (file)
@@ -46,6 +46,7 @@ public:
     uchar *         data() const;
 
     void    fill(uint pixel);
+    void    updateLuma();
 private:
     struct Impl;
     std::shared_ptr<Impl> mImpl;