Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgPicture.h

raw symbols SCIP
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 #ifndef _TVG_PICTURE_H_
24 #define _TVG_PICTURE_H_
25
26 #include <string>
27 #include "tvgPaint.h"
28 #include "tvgLoader.h"
29
30
31 struct PictureIterator : Iterator
32 {
33 Paint* paint = nullptr;
34 Paint* ptr = nullptr;
35
36 PictureIterator(Paint* p) : paint(p) {}
37
38 const Paint* next() override
39 {
40 if (!ptr) ptr = paint;
41 else ptr = nullptr;
42 return ptr;
43 }
44
45 uint32_t count() override
46 {
47 if (paint) return 1;
48 else return 0;
49 }
50
51 void begin() override
52 {
53 ptr = nullptr;
54 }
55 };
56
57
58 struct Picture::Impl
59 {
60 ImageLoader* loader = nullptr;
61
62 Paint* paint = nullptr; //vector picture uses
63 RenderSurface* surface = nullptr; //bitmap picture uses
64 RenderData rd = nullptr; //engine data
65 float w = 0, h = 0;
66 Picture* picture = nullptr;
67 uint8_t cFlag = CompositionFlag::Invalid;
68 bool resizing = false;
69
70 void queryComposition(uint8_t opacity);
71 bool render(RenderMethod* renderer);
72 bool size(float w, float h);
73 RenderRegion bounds(RenderMethod* renderer);
74 Result load(ImageLoader* ploader);
75
76 Impl(Picture* p) : picture(p)
77 {
78 }
79
80 ~Impl()
81 {
82 LoaderMgr::retrieve(loader);
83 if (surface) {
84 if (auto renderer = PP(picture)->renderer) {
85 renderer->dispose(rd);
86 }
87 }
88 delete(paint);
89 }
90
91 RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, TVG_UNUSED bool clipper)
92 {
93 auto flag = static_cast<RenderUpdateFlag>(pFlag | load());
94
95 if (surface) {
96 if (flag == RenderUpdateFlag::None) return rd;
97
98 //Overriding Transformation by the desired image size
99 auto sx = w / loader->w;
100 auto sy = h / loader->h;
101 auto scale = sx < sy ? sx : sy;
102 auto m = transform * Matrix{scale, 0, 0, 0, scale, 0, 0, 0, 1};
103
104 rd = renderer->prepare(surface, rd, m, clips, opacity, flag);
105 } else if (paint) {
106 if (resizing) {
107 loader->resize(paint, w, h);
108 resizing = false;
109 }
110 queryComposition(opacity);
111 rd = paint->pImpl->update(renderer, transform, clips, opacity, flag, false);
112 }
113 return rd;
114 }
115
116 bool bounds(float* x, float* y, float* w, float* h, bool stroking)
117 {
118 if (x) *x = 0;
119 if (y) *y = 0;
120 if (w) *w = this->w;
121 if (h) *h = this->h;
122 return true;
123 }
124
125 Result load(const string& path)
126 {
127 if (paint || surface) return Result::InsufficientCondition;
128
129 bool invalid; //Invalid Path
130 auto loader = static_cast<ImageLoader*>(LoaderMgr::loader(path, &invalid));
131 if (!loader) {
132 if (invalid) return Result::InvalidArguments;
133 return Result::NonSupport;
134 }
135 return load(loader);
136 }
137
138 Result load(const char* data, uint32_t size, const string& mimeType, bool copy)
139 {
140 if (paint || surface) return Result::InsufficientCondition;
141 auto loader = static_cast<ImageLoader*>(LoaderMgr::loader(data, size, mimeType, copy));
142 if (!loader) return Result::NonSupport;
143 return load(loader);
144 }
145
146 Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy)
147 {
148 if (paint || surface) return Result::InsufficientCondition;
149
150 auto loader = static_cast<ImageLoader*>(LoaderMgr::loader(data, w, h, copy));
151 if (!loader) return Result::FailedAllocation;
152
153 return load(loader);
154 }
155
156 Paint* duplicate(Paint* ret)
157 {
158 if (ret) TVGERR("RENDERER", "TODO: duplicate()");
159
160 load();
161
162 auto picture = Picture::gen().release();
163 auto dup = picture->pImpl;
164
165 if (paint) dup->paint = paint->duplicate();
166
167 if (loader) {
168 dup->loader = loader;
169 ++dup->loader->sharing;
170 PP(picture)->renderFlag |= RenderUpdateFlag::Image;
171 }
172
173 dup->surface = surface;
174 dup->w = w;
175 dup->h = h;
176 dup->resizing = resizing;
177
178 return picture;
179 }
180
181 Iterator* iterator()
182 {
183 load();
184 return new PictureIterator(paint);
185 }
186
187 uint32_t* data(uint32_t* w, uint32_t* h)
188 {
189 //Try it, If not loaded yet.
190 load();
191
192 if (loader) {
193 if (w) *w = static_cast<uint32_t>(loader->w);
194 if (h) *h = static_cast<uint32_t>(loader->h);
195 } else {
196 if (w) *w = 0;
197 if (h) *h = 0;
198 }
199 if (surface) return surface->buf32;
200 else return nullptr;
201 }
202
203 RenderUpdateFlag load();
204 };
205
206 #endif //_TVG_PICTURE_H_