1 /*
2 * Copyright (c) 2020 - 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 #include "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25
26 #include "tvgMath.h"
27 #include "tvgSwCommon.h"
28
29 /************************************************************************/
30 /* Internal Class Implementation */
31 /************************************************************************/
32
_onlyShifted(const Matrix & m)33 static inline bool _onlyShifted(const Matrix& m)
34 {
35 if (tvg::equal(m.e11, 1.0f) && tvg::equal(m.e22, 1.0f) && tvg::zero(m.e12) && tvg::zero(m.e21)) return true;
36 return false;
37 }
38
39
_genOutline(SwImage * image,const Matrix & transform,SwMpool * mpool,unsigned tid)40 static bool _genOutline(SwImage* image, const Matrix& transform, SwMpool* mpool, unsigned tid)
41 {
42 image->outline = mpoolReqOutline(mpool, tid);
43 auto outline = image->outline;
44
45 outline->pts.reserve(5);
46 outline->types.reserve(5);
47 outline->cntrs.reserve(1);
48 outline->closed.reserve(1);
49
50 Point to[4];
51 auto w = static_cast<float>(image->w);
52 auto h = static_cast<float>(image->h);
53 to[0] = {0, 0};
54 to[1] = {w, 0};
55 to[2] = {w, h};
56 to[3] = {0, h};
57
58 for (int i = 0; i < 4; i++) {
59 outline->pts.push(mathTransform(&to[i], transform));
60 outline->types.push(SW_CURVE_TYPE_POINT);
61 }
62
63 outline->pts.push(outline->pts[0]);
64 outline->types.push(SW_CURVE_TYPE_POINT);
65 outline->cntrs.push(outline->pts.count - 1);
66 outline->closed.push(true);
67
68 image->outline = outline;
69
70 return true;
71 }
72
73
74 /************************************************************************/
75 /* External Class Implementation */
76 /************************************************************************/
77
imagePrepare(SwImage * image,const Matrix & transform,const SwBBox & clipRegion,SwBBox & renderRegion,SwMpool * mpool,unsigned tid)78 bool imagePrepare(SwImage* image, const Matrix& transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
79 {
80 image->direct = _onlyShifted(transform);
81
82 //Fast track: Non-transformed image but just shifted.
83 if (image->direct) {
84 image->ox = -static_cast<int32_t>(nearbyint(transform.e13));
85 image->oy = -static_cast<int32_t>(nearbyint(transform.e23));
86 //Figure out the scale factor by transform matrix
87 } else {
88 auto scaleX = sqrtf((transform.e11 * transform.e11) + (transform.e21 * transform.e21));
89 auto scaleY = sqrtf((transform.e22 * transform.e22) + (transform.e12 * transform.e12));
90 image->scale = (fabsf(scaleX - scaleY) > 0.01f) ? 1.0f : scaleX;
91
92 if (tvg::zero(transform.e12) && tvg::zero(transform.e21)) image->scaled = true;
93 else image->scaled = false;
94 }
95
96 if (!_genOutline(image, transform, mpool, tid)) return false;
97 return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, image->direct);
98 }
99
100
imageGenRle(SwImage * image,const SwBBox & renderRegion,bool antiAlias)101 bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias)
102 {
103 if ((image->rle = rleRender(image->rle, image->outline, renderRegion, antiAlias))) return true;
104
105 return false;
106 }
107
108
imageDelOutline(SwImage * image,SwMpool * mpool,uint32_t tid)109 void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid)
110 {
111 mpoolRetOutline(mpool, tid);
112 image->outline = nullptr;
113 }
114
115
imageReset(SwImage * image)116 void imageReset(SwImage* image)
117 {
118 rleReset(image->rle);
119 }
120
121
imageFree(SwImage * image)122 void imageFree(SwImage* image)
123 {
124 rleFree(image->rle);
125 }
126
127 #endif /* LV_USE_THORVG_INTERNAL */
128
129