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