1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * This file is part of openfx-supportext <https://github.com/devernay/openfx-supportext>,
4 * Copyright (C) 2013-2018 INRIA
5 *
6 * openfx-supportext is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * openfx-supportext is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with openfx-supportext. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
18 * ***** END LICENSE BLOCK ***** */
19
20 /*
21 * OFX Coords helpers
22 */
23
24 #ifndef openfx_supportext_ofxsCoords_h
25 #define openfx_supportext_ofxsCoords_h
26
27 #include <cmath>
28 #include <cfloat>
29 #include <algorithm>
30
31 #include "ofxsImageEffect.h"
32
33 #ifndef M_LN2
34 #define M_LN2 0.693147180559945309417232121458176568 /* loge(2) */
35 #endif
36
37 namespace OFX {
38 namespace Coords {
39 template <typename Rect>
40 bool
rectIsEmpty(const Rect & r)41 rectIsEmpty(const Rect & r)
42 {
43 return (r.x2 <= r.x1) || (r.y2 <= r.y1);
44 }
45
46 /// Bounding box of two rectangles
47 /// bbox may be aliased to a or b
48 template <typename Rect>
49 void
rectBoundingBox(const Rect & a,const Rect & b,Rect * bbox)50 rectBoundingBox(const Rect & a,
51 const Rect & b,
52 Rect* bbox)
53 {
54 if ( rectIsEmpty(a) ) {
55 *bbox = b;
56
57 return;
58 }
59 if ( rectIsEmpty(b) ) {
60 *bbox = a;
61
62 return;
63 }
64 bbox->x1 = (std::min)(a.x1, b.x1);
65 bbox->x2 = (std::max)( bbox->x1, (std::max)(a.x2, b.x2) );
66 bbox->y1 = (std::min)(a.y1, b.y1);
67 bbox->y2 = (std::max)( bbox->y1, (std::max)(a.y2, b.y2) );
68 }
69
70 template <typename Rect>
71 bool
rectIsInfinite(const Rect & r)72 rectIsInfinite(const Rect & r)
73 {
74 return (r.x1 <= kOfxFlagInfiniteMin) || (r.x2 >= kOfxFlagInfiniteMax) ||
75 (r.y1 <= kOfxFlagInfiniteMin) || (r.y2 >= kOfxFlagInfiniteMax);
76 }
77
78 /// compute the intersection of two rectangles, and return true if they intersect
79 /// intersection may be aliased to r1 or r2
80 template <typename Rect>
81 bool
rectIntersection(const Rect & r1,const Rect & r2,Rect * intersection)82 rectIntersection(const Rect & r1,
83 const Rect & r2,
84 Rect* intersection)
85 {
86 if ( rectIsEmpty(r1) || rectIsEmpty(r2) ) {
87 if (intersection) {
88 intersection->x1 = 0;
89 intersection->x2 = 0;
90 intersection->y1 = 0;
91 intersection->y2 = 0;
92 }
93
94 return false;
95 }
96
97 if ( ( r1.x1 > r2.x2) || ( r2.x1 > r1.x2) || ( r1.y1 > r2.y2) || ( r2.y1 > r1.y2) ) {
98 if (intersection) {
99 intersection->x1 = 0;
100 intersection->x2 = 0;
101 intersection->y1 = 0;
102 intersection->y2 = 0;
103 }
104
105 return false;
106 }
107
108 if (intersection) {
109 intersection->x1 = (std::max)(r1.x1, r2.x1);
110 // the region must be *at least* empty, thus the maximin.
111 intersection->x2 = (std::max)( intersection->x1, (std::min)(r1.x2, r2.x2) );
112 intersection->y1 = (std::max)(r1.y1, r2.y1);
113 // the region must be *at least* empty, thus the maximin.
114 intersection->y2 = (std::max)( intersection->y1, (std::min)(r1.y2, r2.y2) );
115 }
116
117 return true;
118 }
119
120 /**
121 * @brief Scales down the rectangle in pixel coordinates by the given power of 2, and return the smallest *enclosing* rectangle in pixel coordinates
122 * Never use this with canonical coordinates, or never round canonical coordinates to use this: use toPixelEnclosing instead.
123 **/
124 inline
125 OfxRectI
downscalePowerOfTwoSmallestEnclosing(const OfxRectI & r,unsigned int thisLevel)126 downscalePowerOfTwoSmallestEnclosing(const OfxRectI & r,
127 unsigned int thisLevel)
128 {
129 if (thisLevel == 0) {
130 return r;
131 }
132 OfxRectI ret;
133 int pot = (1 << thisLevel);
134 int pot_minus1 = pot - 1;
135 if (r.x1 <= kOfxFlagInfiniteMin) {
136 ret.x1 = kOfxFlagInfiniteMin;
137 } else {
138 ret.x1 = r.x1 >> thisLevel;
139 assert(ret.x1 * pot <= r.x1);
140 }
141 if (r.x2 >= kOfxFlagInfiniteMax) {
142 ret.x2 = kOfxFlagInfiniteMax;
143 } else {
144 ret.x2 = (r.x2 + pot_minus1) >> thisLevel;
145 assert(ret.x2 * pot >= r.x2);
146 }
147 if (r.y1 <= kOfxFlagInfiniteMin) {
148 ret.y1 = kOfxFlagInfiniteMin;
149 } else {
150 ret.y1 = r.y1 >> thisLevel;
151 assert(ret.y1 * pot <= r.y1);
152 }
153 if (r.y2 >= kOfxFlagInfiniteMax) {
154 ret.y2 = kOfxFlagInfiniteMax;
155 } else {
156 ret.y2 = (r.y2 + pot_minus1) >> thisLevel;
157 assert(ret.y2 * pot >= r.y2);
158 }
159
160 return ret;
161 }
162
163 inline
164 double
scaleFromMipmapLevel(unsigned int level)165 scaleFromMipmapLevel(unsigned int level)
166 {
167 return 1. / (1 << level);
168 }
169
170 inline void
toPixelEnclosing(const OfxRectD & regionOfInterest,const OfxPointD & renderScale,double par,OfxRectI * rect)171 toPixelEnclosing(const OfxRectD & regionOfInterest,
172 const OfxPointD & renderScale,
173 double par,
174 OfxRectI *rect)
175 {
176 assert(par);
177 if ( rectIsEmpty(regionOfInterest) ) {
178 rect->x1 = rect->y1 = rect->x2 = rect->y2 = 0;
179
180 return;
181 }
182 rect->x1 = (int)std::floor(regionOfInterest.x1 * renderScale.x / par);
183 rect->y1 = (int)std::floor(regionOfInterest.y1 * renderScale.y);
184 rect->x2 = (int)std::ceil(regionOfInterest.x2 * renderScale.x / par);
185 rect->y2 = (int)std::ceil(regionOfInterest.y2 * renderScale.y);
186 }
187
188 inline void
toPixelNearest(const OfxRectD & regionOfInterest,const OfxPointD & renderScale,double par,OfxRectI * rect)189 toPixelNearest(const OfxRectD & regionOfInterest,
190 const OfxPointD & renderScale,
191 double par,
192 OfxRectI *rect)
193 {
194 assert(par);
195 if ( rectIsEmpty(regionOfInterest) ) {
196 rect->x1 = rect->y1 = rect->x2 = rect->y2 = 0;
197
198 return;
199 }
200 rect->x1 = (int)std::floor(regionOfInterest.x1 * renderScale.x / par + 0.5);
201 rect->y1 = (int)std::floor(regionOfInterest.y1 * renderScale.y + 0.5);
202 rect->x2 = (int)std::ceil(regionOfInterest.x2 * renderScale.x / par - 0.5);
203 rect->y2 = (int)std::ceil(regionOfInterest.y2 * renderScale.y - 0.5);
204 }
205
206 inline void
toPixelSub(const OfxRectD & regionOfInterest,const OfxPointD & renderScale,double par,OfxRectD * rect)207 toPixelSub(const OfxRectD & regionOfInterest,
208 const OfxPointD & renderScale,
209 double par,
210 OfxRectD *rect)
211 {
212 assert(par);
213 if ( rectIsEmpty(regionOfInterest) ) {
214 rect->x1 = rect->y1 = rect->x2 = rect->y2 = 0;
215
216 return;
217 }
218 rect->x1 = regionOfInterest.x1 * renderScale.x / par;
219 rect->y1 = regionOfInterest.y1 * renderScale.y;
220 rect->x2 = regionOfInterest.x2 * renderScale.x / par;
221 rect->y2 = regionOfInterest.y2 * renderScale.y;
222 }
223
224 inline void
toPixel(const OfxPointD & p_canonical,const OfxPointD & renderScale,double par,OfxPointI * p_pixel)225 toPixel(const OfxPointD & p_canonical,
226 const OfxPointD & renderScale,
227 double par,
228 OfxPointI *p_pixel)
229 {
230 assert(par);
231 p_pixel->x = (int)std::floor(p_canonical.x * renderScale.x / par);
232 p_pixel->y = (int)std::floor(p_canonical.y * renderScale.y);
233 }
234
235 // subpixel version (no rounding)
236 inline void
toPixelSub(const OfxPointD & p_canonical,const OfxPointD & renderScale,double par,OfxPointD * p_pixel)237 toPixelSub(const OfxPointD & p_canonical,
238 const OfxPointD & renderScale,
239 double par,
240 OfxPointD *p_pixel)
241 {
242 assert(par);
243 p_pixel->x = p_canonical.x * renderScale.x / par - 0.5;
244 p_pixel->y = p_canonical.y * renderScale.y - 0.5;
245 }
246
247 // transforms the middle of the given pixel to canonical coordinates
248 inline void
toCanonical(const OfxPointI & p_pixel,const OfxPointD & renderScale,double par,OfxPointD * p_canonical)249 toCanonical(const OfxPointI & p_pixel,
250 const OfxPointD & renderScale,
251 double par,
252 OfxPointD *p_canonical)
253 {
254 assert(par);
255 p_canonical->x = (p_pixel.x + 0.5) * par / renderScale.x;
256 p_canonical->y = (p_pixel.y + 0.5) / renderScale.y;
257 }
258
259 // subpixel version (no rounding)
260 inline void
toCanonicalSub(const OfxPointD & p_pixel,const OfxPointD & renderScale,double par,OfxPointD * p_canonical)261 toCanonicalSub(const OfxPointD & p_pixel,
262 const OfxPointD & renderScale,
263 double par,
264 OfxPointD *p_canonical)
265 {
266 assert(par);
267 p_canonical->x = (p_pixel.x + 0.5) * par / renderScale.x;
268 p_canonical->y = (p_pixel.y + 0.5) / renderScale.y;
269 }
270
271 inline void
toCanonical(const OfxRectI & rect,const OfxPointD & renderScale,double par,OfxRectD * regionOfInterest)272 toCanonical(const OfxRectI & rect,
273 const OfxPointD & renderScale,
274 double par,
275 OfxRectD *regionOfInterest)
276 {
277 assert(par);
278 if ( rectIsEmpty(rect) ) {
279 regionOfInterest->x1 = regionOfInterest->y1 = regionOfInterest->x2 = regionOfInterest->y2 = 0;
280
281 return;
282 }
283 regionOfInterest->x1 = rect.x1 * par / renderScale.x;
284 regionOfInterest->y1 = rect.y1 / renderScale.y;
285 regionOfInterest->x2 = rect.x2 * par / renderScale.x;
286 regionOfInterest->y2 = rect.y2 / renderScale.y;
287 }
288
289 inline void
toCanonical(const OfxRectD & rect,const OfxPointD & renderScale,double par,OfxRectD * regionOfInterest)290 toCanonical(const OfxRectD & rect,
291 const OfxPointD & renderScale,
292 double par,
293 OfxRectD *regionOfInterest)
294 {
295 assert(par);
296 if ( rectIsEmpty(rect) ) {
297 regionOfInterest->x1 = regionOfInterest->y1 = regionOfInterest->x2 = regionOfInterest->y2 = 0;
298
299 return;
300 }
301 regionOfInterest->x1 = rect.x1 * par / renderScale.x;
302 regionOfInterest->y1 = rect.y1 / renderScale.y;
303 regionOfInterest->x2 = rect.x2 * par / renderScale.x;
304 regionOfInterest->y2 = rect.y2 / renderScale.y;
305 }
306
307 inline
308 unsigned int
mipmapLevelFromScale(double s)309 mipmapLevelFromScale(double s)
310 {
311 assert(0. < s && s <= 1.);
312 unsigned int retval = (unsigned int)(std::max)(0., -std::floor(std::log(s) / M_LN2 + 0.5));
313
314 return retval;
315 }
316 } // Coords
317 } // OFX
318
319
320 #endif // openfx_supportext_ofxsCoords_h
321