1 /*
2  * Copyright (c) 2022 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 /**
17  * @file fill_interpolator.h
18  * @brief Defines Scan line interpolator
19  * @since 1.0
20  * @version 1.0
21  */
22 
23 #ifndef GRAPHIC_LITE_FILL_INTERPOLATOR_H
24 #define GRAPHIC_LITE_FILL_INTERPOLATOR_H
25 
26 #include "gfx_utils/color.h"
27 #include "gfx_utils/diagram/common/common_basics.h"
28 #include "gfx_utils/trans_affine.h"
29 #include "gfx_utils/diagram/vertexprimitive/geometry_dda_line.h"
30 #include "gfx_utils/graphic_math.h"
31 namespace OHOS {
32 const uint8_t SUB_PIXEL_SHIFT = 8;
33 /**
34  * Gradient color interpolator
35  */
36 struct ColorInterpolator {
37 #if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
38 public:
39 
ColorInterpolatorColorInterpolator40     ColorInterpolator(const Rgba8T& color1, const Rgba8T& color2, uint32_t distance)
41         : colorStart_(color1), colorEnd_(color2), len_(distance), place_(0) {}
42 
43     /**
44      * @brief overwrite ++
45      */
46     void operator++()
47     {
48         ++place_;
49     }
50 
51     /**
52      * @brief Returns the color at count during the transition from colorstart to colorend
53      * @return
54      */
GetColorColorInterpolator55     Rgba8T GetColor() const
56     {
57         return colorStart_.Gradient(colorEnd_, float(place_) / len_);
58     }
59 
60 private:
61     Rgba8T colorStart_;
62     Rgba8T colorEnd_;
63     uint32_t len_;
64     uint32_t place_;
65 #endif
66 };
67 
68 /**
69  * Linear scan line inserter
70  */
71 class FillInterpolator {
72 public:
73     enum SubpixelScale {
74         SUBPIXEL_SHIFT = SUB_PIXEL_SHIFT,
75         SUBPIXEL_SCALE = 1 << SUBPIXEL_SHIFT
76     };
FillInterpolator()77     FillInterpolator() {}
FillInterpolator(TransAffine & trans)78     FillInterpolator(TransAffine& trans) : transType_(&trans) {}
FillInterpolator(TransAffine & trans,float x,float y,uint32_t len)79     FillInterpolator(TransAffine& trans,
80                            float x, float y, uint32_t len) : transType_(&trans)
81     {
82         Begin(x, y, len);
83     }
84 
GetTransformer()85     const TransAffine& GetTransformer() const
86     {
87         return *transType_;
88     }
SetTransformer(TransAffine & trans)89     void SetTransformer(TransAffine& trans)
90     {
91         transType_ = &trans;
92     }
93 
94     /*
95      * Update and set dda2lineinterpolatorx and dda2lineinterpolatory properties again
96      */
Begin(float x,float y,uint32_t len)97     void Begin(float x, float y, uint32_t len)
98     {
99         float tx;
100         float ty;
101 
102         tx = x;
103         ty = y;
104 
105         transType_->Transform(&tx, &ty);
106         int32_t x1 = MATH_ROUND32(tx * SUBPIXEL_SCALE);
107         int32_t y1 = MATH_ROUND32(ty * SUBPIXEL_SCALE);
108 
109         tx = x + len;
110         ty = y;
111         transType_->Transform(&tx, &ty);
112         int32_t x2 = MATH_ROUND32(tx * SUBPIXEL_SCALE);
113         int32_t y2 = MATH_ROUND32(ty * SUBPIXEL_SCALE);
114 
115         dda2LineInterpolatorX_ = GeometryDdaLine(x1, x2, len);
116         dda2LineInterpolatorY_ = GeometryDdaLine(y1, y2, len);
117     }
118 
119     /**
120      * @brief Overloading + + operators
121      */
122     void operator++()
123     {
124         ++dda2LineInterpolatorX_;
125         ++dda2LineInterpolatorY_;
126     }
127 
Coordinates(int32_t * x,int32_t * y)128     void Coordinates(int32_t* x, int32_t* y) const
129     {
130         *x = dda2LineInterpolatorX_.GetCoordinate();
131         *y = dda2LineInterpolatorY_.GetCoordinate();
132     }
133 
134 private:
135     TransAffine* transType_;
136     GeometryDdaLine dda2LineInterpolatorX_;
137     GeometryDdaLine dda2LineInterpolatorY_;
138 };
139 } // namespace OHOS
140 #endif
141