2 * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
24 #include "tvgSwCommon.h"
26 /************************************************************************/
27 /* Internal Class Implementation */
28 /************************************************************************/
30 static inline bool _onlyShifted(const Matrix* m)
32 if (mathEqual(m->e11, 1.0f) && mathEqual(m->e22, 1.0f) && mathZero(m->e12) && mathZero(m->e21)) return true;
37 static bool _genOutline(SwImage* image, Polygon* triangles, uint32_t triangleCount, const Matrix* transform, SwMpool* mpool, unsigned tid)
39 image->outline = mpoolReqOutline(mpool, tid);
40 auto outline = image->outline;
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)));
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;
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
62 // TODO: Should we calculate the exact path(s) of the triangle mesh instead?
63 // i.e. copy tvgSwShape.capp -> _genOutline?
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 };
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;
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;
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;
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};
91 auto w = static_cast<float>(image->w);
92 auto h = static_cast<float>(image->h);
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;
104 outline->pts[outline->ptsCnt] = outline->pts[0];
105 outline->types[outline->ptsCnt] = SW_CURVE_TYPE_POINT;
108 outline->cntrs[outline->cntrsCnt] = outline->ptsCnt - 1;
111 image->outline = outline;
117 /************************************************************************/
118 /* External Class Implementation */
119 /************************************************************************/
121 bool imagePrepare(SwImage* image, Polygon* triangles, uint32_t triangleCount, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
123 image->direct = _onlyShifted(transform);
125 //Fast track: Non-transformed image but just shifted.
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
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;
135 if (mathZero(transform->e12) && mathZero(transform->e21)) image->scaled = true;
136 else image->scaled = false;
139 if (!_genOutline(image, triangles, triangleCount, transform, mpool, tid)) return false;
140 return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, image->direct);
144 bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias)
146 if ((image->rle = rleRender(image->rle, image->outline, renderRegion, antiAlias))) return true;
152 void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid)
154 mpoolRetOutline(mpool, tid);
155 image->outline = nullptr;
159 void imageReset(SwImage* image)
161 rleReset(image->rle);
165 void imageFree(SwImage* image)