updated copyright.
[platform/core/graphics/tizenvg.git] / src / lib / sw_engine / tvgSwImage.cpp
1 /*
2  * Copyright (c) 2020 - 2023 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 "tvgMath.h"
24 #include "tvgSwCommon.h"
25
26 /************************************************************************/
27 /* Internal Class Implementation                                        */
28 /************************************************************************/
29
30 static inline bool _onlyShifted(const Matrix* m)
31 {
32     if (mathEqual(m->e11, 1.0f) && mathEqual(m->e22, 1.0f) && mathZero(m->e12) && mathZero(m->e21)) return true;
33     return false;
34 }
35
36
37 static bool _genOutline(SwImage* image, Polygon* triangles, uint32_t triangleCount, const Matrix* transform, SwMpool* mpool, unsigned tid)
38 {
39     image->outline = mpoolReqOutline(mpool, tid);
40     auto outline = image->outline;
41
42      if (outline->reservedPtsCnt < 5) {
43         outline->reservedPtsCnt = 5;
44         outline->pts = static_cast<SwPoint*>(realloc(outline->pts, outline->reservedPtsCnt * sizeof(SwPoint)));
45         outline->types = static_cast<uint8_t*>(realloc(outline->types, outline->reservedPtsCnt * sizeof(uint8_t)));
46      }
47
48     if (outline->reservedCntrsCnt < 1) {
49         outline->reservedCntrsCnt = 1;
50         outline->cntrs = static_cast<uint32_t*>(realloc(outline->cntrs, outline->reservedCntrsCnt * sizeof(uint32_t)));
51         outline->closed = static_cast<bool*>(realloc(outline->closed, outline->reservedCntrsCnt * sizeof(bool)));
52         outline->closed[0] = true;
53     }
54
55     Point to[4];
56     if (triangleCount > 0) {
57         // TODO: Optimise me. We appear to calculate this exact min/max bounding area in multiple
58         // places. We should be able to re-use one we have already done? Also see:
59         //   tvgPictureImpl.h --> bounds
60         //   tvgSwRasterTexmap.h --> _rasterTexmapPolygonMesh
61         //
62         // TODO: Should we calculate the exact path(s) of the triangle mesh instead?
63         // i.e. copy tvgSwShape.capp -> _genOutline?
64         //
65         // TODO: Cntrs?
66         Point min = { triangles[0].vertex[0].pt.x, triangles[0].vertex[0].pt.y };
67         Point max = { triangles[0].vertex[0].pt.x, triangles[0].vertex[0].pt.y };
68
69         for (uint32_t i = 0; i < triangleCount; ++i) {
70             if (triangles[i].vertex[0].pt.x < min.x) min.x = triangles[i].vertex[0].pt.x;
71             else if (triangles[i].vertex[0].pt.x > max.x) max.x = triangles[i].vertex[0].pt.x;
72             if (triangles[i].vertex[0].pt.y < min.y) min.y = triangles[i].vertex[0].pt.y;
73             else if (triangles[i].vertex[0].pt.y > max.y) max.y = triangles[i].vertex[0].pt.y;
74
75             if (triangles[i].vertex[1].pt.x < min.x) min.x = triangles[i].vertex[1].pt.x;
76             else if (triangles[i].vertex[1].pt.x > max.x) max.x = triangles[i].vertex[1].pt.x;
77             if (triangles[i].vertex[1].pt.y < min.y) min.y = triangles[i].vertex[1].pt.y;
78             else if (triangles[i].vertex[1].pt.y > max.y) max.y = triangles[i].vertex[1].pt.y;
79
80             if (triangles[i].vertex[2].pt.x < min.x) min.x = triangles[i].vertex[2].pt.x;
81             else if (triangles[i].vertex[2].pt.x > max.x) max.x = triangles[i].vertex[2].pt.x;
82             if (triangles[i].vertex[2].pt.y < min.y) min.y = triangles[i].vertex[2].pt.y;
83             else if (triangles[i].vertex[2].pt.y > max.y) max.y = triangles[i].vertex[2].pt.y;
84         }
85
86         to[0] = {min.x, min.y};
87         to[1] = {max.x, min.y};
88         to[2] = {max.x, max.y};
89         to[3] = {min.x, max.y};
90     } else {
91         auto w = static_cast<float>(image->w);
92         auto h = static_cast<float>(image->h);
93         to[0] = {0, 0};
94         to[1] = {w, 0};
95         to[2] = {w, h};
96         to[3] = {0, h};
97     }
98     for (int i = 0; i < 4; i++) {
99         outline->pts[outline->ptsCnt] = mathTransform(&to[i], transform);
100         outline->types[outline->ptsCnt] = SW_CURVE_TYPE_POINT;
101         ++outline->ptsCnt;
102     }
103
104     outline->pts[outline->ptsCnt] = outline->pts[0];
105     outline->types[outline->ptsCnt] = SW_CURVE_TYPE_POINT;
106     ++outline->ptsCnt;
107
108     outline->cntrs[outline->cntrsCnt] = outline->ptsCnt - 1;
109     ++outline->cntrsCnt;
110
111     image->outline = outline;
112
113     return true;
114 }
115
116
117 /************************************************************************/
118 /* External Class Implementation                                        */
119 /************************************************************************/
120
121 bool imagePrepare(SwImage* image, Polygon* triangles, uint32_t triangleCount, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
122 {
123     image->direct = _onlyShifted(transform);
124
125     //Fast track: Non-transformed image but just shifted.
126     if (image->direct) {
127         image->ox = -static_cast<int32_t>(round(transform->e13));
128         image->oy = -static_cast<int32_t>(round(transform->e23));
129     //Figure out the scale factor by transform matrix
130     } else {
131         auto scaleX = sqrtf((transform->e11 * transform->e11) + (transform->e21 * transform->e21));
132         auto scaleY = sqrtf((transform->e22 * transform->e22) + (transform->e12 * transform->e12));
133         image->scale = (fabsf(scaleX - scaleY) > 0.01f) ? 1.0f : scaleX;
134
135         if (mathZero(transform->e12) && mathZero(transform->e21)) image->scaled = true;
136         else image->scaled = false;
137     }
138
139     if (!_genOutline(image, triangles, triangleCount, transform, mpool, tid)) return false;
140     return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, image->direct);
141 }
142
143
144 bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias)
145 {
146     if ((image->rle = rleRender(image->rle, image->outline, renderRegion, antiAlias))) return true;
147
148     return false;
149 }
150
151
152 void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid)
153 {
154     mpoolRetOutline(mpool, tid);
155     image->outline = nullptr;
156 }
157
158
159 void imageReset(SwImage* image)
160 {
161     rleReset(image->rle);
162 }
163
164
165 void imageFree(SwImage* image)
166 {
167     rleFree(image->rle);
168 }