1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COLOR_MATRIX_H 17 #define COLOR_MATRIX_H 18 19 #include <memory> 20 #include <securec.h> 21 #include <string> 22 23 #include "utils/log.h" 24 #include "utils/scalar.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 namespace Drawing { 29 class ColorMatrix { 30 public: 31 // Color matrix is a 4x5 float type matrix. 32 constexpr static int MATRIX_SIZE = 20; ColorMatrix()33 ColorMatrix() noexcept 34 { 35 SetIdentity(); 36 } 37 ~ColorMatrix()38 ~ColorMatrix() {} 39 SetIdentity()40 void SetIdentity() 41 { 42 for (size_t i = 0; i < MATRIX_SIZE; i = i + 6) { 43 array_[i] = 1; // identity matrix, the value of the elements on the main diagonal is 1 44 } 45 } 46 SetArray(const scalar src[MATRIX_SIZE])47 void SetArray(const scalar src[MATRIX_SIZE]) 48 { 49 auto ret = memcpy_s(array_, sizeof(array_), src, sizeof(array_)); 50 if (ret != EOK) { 51 LOGE("Drawing: ColorMatrix memcpy_s failed"); 52 } 53 } 54 GetArray(scalar dst[MATRIX_SIZE])55 void GetArray(scalar dst[MATRIX_SIZE]) const 56 { 57 auto ret = memcpy_s(dst, sizeof(array_), array_, sizeof(array_)); 58 if (ret != EOK) { 59 LOGE("Drawing: ColorMatrix memcpy_s failed"); 60 } 61 } 62 SetConcat(const ColorMatrix & m1,const ColorMatrix & m2)63 void SetConcat(const ColorMatrix& m1, const ColorMatrix& m2) 64 { 65 scalar tmp[MATRIX_SIZE] = { 0 }; 66 scalar* target; 67 68 if (array_ == m1.array_ || array_ == m2.array_) { 69 target = tmp; 70 } else { 71 target = array_; 72 } 73 74 int index = 0; 75 for (int j = 0; j < MATRIX_SIZE; j = j + 5) { 76 for (int i = 0; i < 4; i++) { // Color matrix is a 4x5 float type matrix. 77 target[index++] = m1.array_[j + 0] * m2.array_[i + 0] + m1.array_[j + 1] * m2.array_[i + 5] + 78 m1.array_[j + 2] * m2.array_[i + 10] + m1.array_[j + 3] * m2.array_[i + 15]; 79 } 80 target[index++] = m1.array_[j + 0] * m2.array_[4] + m1.array_[j + 1] * m2.array_[9] + 81 m1.array_[j + 2] * m2.array_[14] + m1.array_[j + 3] * m2.array_[19] + m1.array_[j + 4]; 82 } 83 84 if (target != array_) { 85 auto ret = memcpy_s(array_, sizeof(array_), target, sizeof(array_)); 86 if (ret != EOK) { 87 LOGE("Drawing: ColorMatrix memcpy_s failed"); 88 } 89 } 90 } 91 PreConcat(const ColorMatrix & m)92 void PreConcat(const ColorMatrix& m) 93 { 94 SetConcat(*this, m); 95 } 96 PostConcat(const ColorMatrix & m)97 void PostConcat(const ColorMatrix& m) 98 { 99 SetConcat(m, *this); 100 } 101 SetScale(scalar sr,scalar sg,scalar sb,scalar sa)102 void SetScale(scalar sr, scalar sg, scalar sb, scalar sa) 103 { 104 auto ret = memset_s(array_, sizeof(array_), 0, sizeof(array_)); 105 if (ret != EOK) { 106 LOGE("Drawing: ColorMatrix memset_s failed"); 107 return; 108 } 109 array_[0] = sr; // red vector scale 110 array_[6] = sg; // green vector scale 111 array_[12] = sb; // blue vector scale 112 array_[18] = sa; // alpha vetor scale 113 } 114 115 private: 116 scalar array_[MATRIX_SIZE] = { 0 }; 117 }; 118 } // namespace Drawing 119 } // namespace Rosen 120 } // namespace OHOS 121 #endif