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 #ifndef GRAPHIC_LITE_FILL_PATTERN_RGBA_H
16 #define GRAPHIC_LITE_FILL_PATTERN_RGBA_H
17 
18 #include <gfx_utils/image_info.h>
19 #include "gfx_utils/color.h"
20 #include "fill_base.h"
21 /**
22  * @file span_pattern_rgba.h
23  * @brief Defines Scan line of pattern
24  * @since 1.0
25  * @version 1.0
26  */
27 
28 namespace OHOS {
29 class FillPatternRgba : public SpanBase {
30 #if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG
31 public:
FillPatternRgba()32     FillPatternRgba() {}
33 
FillPatternRgba(const ImageInfo * image,PatternRepeatMode patternRepeat,float startX,float startY)34     FillPatternRgba(const ImageInfo* image, PatternRepeatMode patternRepeat, float startX, float startY)
35         : patternRepeat_(patternRepeat)
36     {
37         if (image->header.colorMode == ARGB8888) {
38             patternImage_ = reinterpret_cast<Color32*>(const_cast<uint8_t*>(image->data));
39             patternImageheigth_ = image->header.height;
40             patternImagewidth_ = image->header.width;
41             patternStartX_ = startX;
42             patternStartY_ = startY;
43         }
44     }
45 
Attach(const ImageInfo * image,PatternRepeatMode patternRepeat,float startX,float startY)46     void Attach(const ImageInfo* image, PatternRepeatMode patternRepeat, float startX, float startY)
47     {
48         patternRepeat_ = patternRepeat;
49         if (image->header.colorMode == ARGB8888) {
50             patternImage_ = reinterpret_cast<Color32*>(const_cast<uint8_t*>(image->data));
51             patternImageheigth_ = image->header.height;
52             patternImagewidth_ = image->header.width;
53             patternStartX_ = startX;
54             patternStartY_ = startY;
55         }
56     }
57 
58     /**
59      * @brief black
60      * @return black
61      */
NoColor()62     ColorType NoColor() const
63     {
64         ColorType fillColor;
65         fillColor.red = 0;
66         fillColor.green = 0;
67         fillColor.blue = 0;
68         fillColor.alpha = MAX_COLOR_NUM;
69         return fillColor;
70     }
71 
72     /**
73      * @brief Prepare Ready for render_scanlines_aa in
74      */
Prepare()75     void Prepare() {}
76 
Generate(Rgba8T * span,int32_t x,int32_t y,uint32_t len)77     void Generate(Rgba8T* span, int32_t x, int32_t y, uint32_t len)
78     {
79         y = static_cast<int32_t>(y - patternStartY_);
80         x = static_cast<int32_t>(x - patternStartX_);
81         for (; len; --len, span++, x++) {
82             if (patternRepeat_ == NO_REPEAT) {
83                 if (x >= patternImagewidth_ ||
84                         y >= patternImageheigth_) {
85                     ChangeColor(span, NoColor());
86                 } else {
87                     ChangeColor(span, patternImage_[patternImagewidth_ * y + x]);
88                 }
89             }
90 
91             if (patternRepeat_ == REPEAT) {
92                 x = x % patternImagewidth_;
93                 y = y % patternImageheigth_;
94                 if (x >= patternImagewidth_ || y >= patternImageheigth_) {
95                     ChangeColor(span, NoColor());
96                 } else {
97                     ChangeColor(span, patternImage_[patternImagewidth_ * y + x]);
98                 }
99             }
100 
101             if (patternRepeat_ == REPEAT_X) {
102                 x = x % patternImagewidth_;
103                 if (y >= patternImageheigth_) {
104                     ChangeColor(span, NoColor());
105                 } else {
106                     y = y % patternImageheigth_;
107                     if (x >= patternImagewidth_ || y >= patternImageheigth_) {
108                         ChangeColor(span, NoColor());
109                     } else {
110                         ChangeColor(span, patternImage_[patternImagewidth_ * y + x]);
111                     }
112                 }
113             }
114 
115             if (patternRepeat_ == REPEAT_Y) {
116                 y = y % patternImageheigth_;
117                 if (x >= patternImageheigth_) {
118                     ChangeColor(span, NoColor());
119                 } else {
120                     x = x % patternImagewidth_;
121                     if (x >= patternImagewidth_ ||
122                        y >= patternImageheigth_) {
123                         ChangeColor(span, NoColor());
124                     } else {
125                         ChangeColor(span, patternImage_[patternImagewidth_ * y + x]);
126                     }
127                 }
128             }
129         }
130     }
131 private:
132     PatternRepeatMode patternRepeat_;
133     const Color32* patternImage_;
134     uint16_t patternImageheigth_;
135     uint16_t patternImagewidth_;
136     float patternStartX_;
137     float patternStartY_;
138 
ChangeColor(Rgba8T * color,ColorType colorType)139     void ChangeColor(Rgba8T* color, ColorType colorType)
140     {
141         color->red = colorType.red;
142         color->green = colorType.green;
143         color->blue = colorType.blue;
144         color->alpha = colorType.alpha;
145     }
146 #endif
147 };
148 } // namespace OHOS
149 #endif
150