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