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 "tvgPictureImpl.h" |
24 | |
25 | /************************************************************************/ |
26 | /* External Class Implementation */ |
27 | /************************************************************************/ |
28 | |
29 | Picture::Picture() : pImpl(new Impl(this)) |
30 | { |
31 | Paint::pImpl->id = TVG_CLASS_ID_PICTURE; |
32 | Paint::pImpl->method(new PaintMethod<Picture::Impl>(pImpl)); |
33 | } |
34 | |
35 | |
36 | Picture::~Picture() |
37 | { |
38 | delete(pImpl); |
39 | } |
40 | |
41 | |
42 | unique_ptr<Picture> Picture::gen() noexcept |
43 | { |
44 | return unique_ptr<Picture>(new Picture); |
45 | } |
46 | |
47 | |
48 | uint32_t Picture::identifier() noexcept |
49 | { |
50 | return TVG_CLASS_ID_PICTURE; |
51 | } |
52 | |
53 | |
54 | Result Picture::load(const std::string& path) noexcept |
55 | { |
56 | if (path.empty()) return Result::InvalidArguments; |
57 | |
58 | return pImpl->load(path); |
59 | } |
60 | |
61 | |
62 | Result Picture::load(const char* data, uint32_t size, const string& mimeType, bool copy) noexcept |
63 | { |
64 | if (!data || size <= 0) return Result::InvalidArguments; |
65 | |
66 | return pImpl->load(data, size, mimeType, copy); |
67 | } |
68 | |
69 | |
70 | TVG_DEPRECATED Result Picture::load(const char* data, uint32_t size, bool copy) noexcept |
71 | { |
72 | return load(data, size, "" , copy); |
73 | } |
74 | |
75 | |
76 | Result Picture::load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept |
77 | { |
78 | if (!data || w <= 0 || h <= 0) return Result::InvalidArguments; |
79 | |
80 | return pImpl->load(data, w, h, copy); |
81 | } |
82 | |
83 | |
84 | Result Picture::size(float w, float h) noexcept |
85 | { |
86 | if (pImpl->size(w, h)) return Result::Success; |
87 | return Result::InsufficientCondition; |
88 | } |
89 | |
90 | |
91 | Result Picture::size(float* w, float* h) const noexcept |
92 | { |
93 | if (!pImpl->loader) return Result::InsufficientCondition; |
94 | if (w) *w = pImpl->w; |
95 | if (h) *h = pImpl->h; |
96 | return Result::Success; |
97 | } |
98 | |
99 | |
100 | const uint32_t* Picture::data(uint32_t* w, uint32_t* h) const noexcept |
101 | { |
102 | //Try it, If not loaded yet. |
103 | pImpl->load(); |
104 | |
105 | if (pImpl->loader) { |
106 | if (w) *w = static_cast<uint32_t>(pImpl->loader->w); |
107 | if (h) *h = static_cast<uint32_t>(pImpl->loader->h); |
108 | } else { |
109 | if (w) *w = 0; |
110 | if (h) *h = 0; |
111 | } |
112 | if (pImpl->surface) return pImpl->surface->buf32; |
113 | else return nullptr; |
114 | } |
115 | |
116 | |
117 | Result Picture::mesh(const Polygon* triangles, uint32_t triangleCnt) noexcept |
118 | { |
119 | if (!triangles && triangleCnt > 0) return Result::InvalidArguments; |
120 | if (triangles && triangleCnt == 0) return Result::InvalidArguments; |
121 | |
122 | pImpl->mesh(triangles, triangleCnt); |
123 | return Result::Success; |
124 | } |
125 | |
126 | |
127 | uint32_t Picture::mesh(const Polygon** triangles) const noexcept |
128 | { |
129 | if (triangles) *triangles = pImpl->rm.triangles; |
130 | return pImpl->rm.triangleCnt; |
131 | } |
132 | |