Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwRasterC.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23
24 template<typename PIXEL_T>
25 static void inline cRasterTranslucentPixels(PIXEL_T* dst, PIXEL_T* src, uint32_t len, uint32_t opacity)
26 {
27 //TODO: 64bits faster?
28 if (opacity == 255) {
29 for (uint32_t x = 0; x < len; ++x, ++dst, ++src) {
30 *dst = *src + ALPHA_BLEND(*dst, IA(*src));
31 }
32 } else {
33 for (uint32_t x = 0; x < len; ++x, ++dst, ++src) {
34 auto tmp = ALPHA_BLEND(*src, opacity);
35 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
36 }
37 }
38 }
39
40
41 template<typename PIXEL_T>
42 static void inline cRasterPixels(PIXEL_T* dst, PIXEL_T* src, uint32_t len, uint32_t opacity)
43 {
44 //TODO: 64bits faster?
45 if (opacity == 255) {
46 for (uint32_t x = 0; x < len; ++x, ++dst, ++src) {
47 *dst = *src;
48 }
49 } else {
50 cRasterTranslucentPixels(dst, src, len, opacity);
51 }
52 }
53
54
55 template<typename PIXEL_T>
56 static void inline cRasterPixels(PIXEL_T* dst, PIXEL_T val, uint32_t offset, int32_t len)
57 {
58 dst += offset;
59
60 //fix the misaligned memory
61 auto alignOffset = (long long) dst % 8;
62 if (alignOffset > 0) {
63 if (sizeof(PIXEL_T) == 4) alignOffset /= 4;
64 else if (sizeof(PIXEL_T) == 1) alignOffset = 8 - alignOffset;
65 while (alignOffset > 0 && len > 0) {
66 *dst++ = val;
67 --len;
68 --alignOffset;
69 }
70 }
71
72 //64bits faster clear
73 if ((sizeof(PIXEL_T) == 4)) {
74 auto val64 = (uint64_t(val) << 32) | uint64_t(val);
75 while (len > 1) {
76 *reinterpret_cast<uint64_t*>(dst) = val64;
77 len -= 2;
78 dst += 2;
79 }
80 } else if (sizeof(PIXEL_T) == 1) {
81 auto val32 = (uint32_t(val) << 24) | (uint32_t(val) << 16) | (uint32_t(val) << 8) | uint32_t(val);
82 auto val64 = (uint64_t(val32) << 32) | val32;
83 while (len > 7) {
84 *reinterpret_cast<uint64_t*>(dst) = val64;
85 len -= 8;
86 dst += 8;
87 }
88 }
89
90 //leftovers
91 while (len--) *dst++ = val;
92 }
93
94
95 static bool inline cRasterTranslucentRle(SwSurface* surface, const SwRle* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
96 {
97 auto span = rle->spans;
98
99 //32bit channels
100 if (surface->channelSize == sizeof(uint32_t)) {
101 auto color = surface->join(r, g, b, a);
102 uint32_t src;
103 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
104 auto dst = &surface->buf32[span->y * surface->stride + span->x];
105 if (span->coverage < 255) src = ALPHA_BLEND(color, span->coverage);
106 else src = color;
107 auto ialpha = IA(src);
108 for (uint32_t x = 0; x < span->len; ++x, ++dst) {
109 *dst = src + ALPHA_BLEND(*dst, ialpha);
110 }
111 }
112 //8bit grayscale
113 } else if (surface->channelSize == sizeof(uint8_t)) {
114 uint8_t src;
115 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
116 auto dst = &surface->buf8[span->y * surface->stride + span->x];
117 if (span->coverage < 255) src = MULTIPLY(span->coverage, a);
118 else src = a;
119 auto ialpha = ~a;
120 for (uint32_t x = 0; x < span->len; ++x, ++dst) {
121 *dst = src + MULTIPLY(*dst, ialpha);
122 }
123 }
124 }
125 return true;
126 }
127
128
129 static bool inline cRasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
130 {
131 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
132 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
133
134 //32bits channels
135 if (surface->channelSize == sizeof(uint32_t)) {
136 auto color = surface->join(r, g, b, a);
137 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
138 auto ialpha = 255 - a;
139 for (uint32_t y = 0; y < h; ++y) {
140 auto dst = &buffer[y * surface->stride];
141 for (uint32_t x = 0; x < w; ++x, ++dst) {
142 *dst = color + ALPHA_BLEND(*dst, ialpha);
143 }
144 }
145 //8bit grayscale
146 } else if (surface->channelSize == sizeof(uint8_t)) {
147 auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x;
148 auto ialpha = ~a;
149 for (uint32_t y = 0; y < h; ++y) {
150 auto dst = &buffer[y * surface->stride];
151 for (uint32_t x = 0; x < w; ++x, ++dst) {
152 *dst = a + MULTIPLY(*dst, ialpha);
153 }
154 }
155 }
156 return true;
157 }
158
159
160 static bool inline cRasterABGRtoARGB(RenderSurface* surface)
161 {
162 TVGLOG("SW_ENGINE", "Convert ColorSpace ABGR - ARGB [Size: %d x %d]", surface->w, surface->h);
163
164 //64bits faster converting
165 if (surface->w % 2 == 0) {
166 auto buffer = reinterpret_cast<uint64_t*>(surface->buf32);
167 for (uint32_t y = 0; y < surface->h; ++y, buffer += surface->stride / 2) {
168 auto dst = buffer;
169 for (uint32_t x = 0; x < surface->w / 2; ++x, ++dst) {
170 auto c = *dst;
171 //flip Blue, Red channels
172 *dst = (c & 0xff000000ff000000) + ((c & 0x00ff000000ff0000) >> 16) + (c & 0x0000ff000000ff00) + ((c & 0x000000ff000000ff) << 16);
173 }
174 }
175 //default converting
176 } else {
177 auto buffer = surface->buf32;
178 for (uint32_t y = 0; y < surface->h; ++y, buffer += surface->stride) {
179 auto dst = buffer;
180 for (uint32_t x = 0; x < surface->w; ++x, ++dst) {
181 auto c = *dst;
182 //flip Blue, Red channels
183 *dst = (c & 0xff000000) + ((c & 0x00ff0000) >> 16) + (c & 0x0000ff00) + ((c & 0x000000ff) << 16);
184 }
185 }
186 }
187 return true;
188 }
189
190
191 static bool inline cRasterARGBtoABGR(RenderSurface* surface)
192 {
193 //exactly same with ABGRtoARGB
194 return cRasterABGRtoARGB(surface);
195 }