From fd97df6735c42deecdbc89c00fff43b3c4335f31 Mon Sep 17 00:00:00 2001 From: subhransu mohanty Date: Thu, 10 Jan 2019 16:31:39 +0900 Subject: [PATCH] lottie/vector: added updateLuma() api to convert RGBA value to HSL Change-Id: Icacfe4ffdf957d10e7bd654878895b856e045136 --- src/vector/vbitmap.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++ src/vector/vbitmap.h | 1 + 2 files changed, 47 insertions(+) diff --git a/src/vector/vbitmap.cpp b/src/vector/vbitmap.cpp index 25c7919..370c69c 100644 --- a/src/vector/vbitmap.cpp +++ b/src/vector/vbitmap.cpp @@ -19,6 +19,7 @@ #include "vbitmap.h" #include #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 diff --git a/src/vector/vbitmap.h b/src/vector/vbitmap.h index f99b8f2..e40c52a 100644 --- a/src/vector/vbitmap.h +++ b/src/vector/vbitmap.h @@ -46,6 +46,7 @@ public: uchar * data() const; void fill(uint pixel); + void updateLuma(); private: struct Impl; std::shared_ptr mImpl; -- 2.34.1