1 /* Simple rectangle algebra. Should build rectangle list algebra on top of
2 * this.
3 *
4 * J. Cupitt, 8/4/93.
5 *
6 * 17/3/11
7 * - move to vips_ prefix
8 * - gtk-doc comments
9 */
10
11 /*
12
13 This file is part of VIPS.
14
15 VIPS is free software; you can redistribute it and/or modify
16 it under the terms of the GNU Lesser General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU Lesser General Public License for more details.
24
25 You should have received a copy of the GNU Lesser General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 02110-1301 USA
29
30 */
31
32 /*
33
34 These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
35
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif /*HAVE_CONFIG_H*/
41 #include <vips/intl.h>
42
43 #include <stdio.h>
44
45 #include <vips/vips.h>
46
47 /**
48 * SECTION: rectangle
49 * @short_description: the VIPS rectangle class
50 * @stability: Stable
51 * @see_also: <link linkend="VipsRegion">region</link>
52 * @include: vips/vips.h
53 *
54 * The #VipsRect class and associated types and macros.
55 */
56
57 /**
58 * VipsRect:
59 * @left: left edge of rectangle
60 * @top: top edge of rectangle
61 * @width: width of rectangle
62 * @height: height of rectangle
63 *
64 * A #VipsRect is a rectangular area of pixels. This is a struct for
65 * performing simple rectangle algebra.
66 */
67
68 /**
69 * vips_rect_includespoint:
70 * @r: rectangle to test
71 * @x: position to test for
72 * @y: position to test for
73 *
74 * Does @r contain point (@x, @y)?
75 *
76 * Returns: %TRUE if @r contains (@x, @y).
77 */
78 gboolean
vips_rect_includespoint(const VipsRect * r,int x,int y)79 vips_rect_includespoint( const VipsRect *r, int x, int y )
80 {
81 return( r->left <= x &&
82 r->top <= y &&
83 r->left + r->width > x &&
84 r->top + r->height > y );
85 }
86
87 /**
88 * vips_rect_isempty: (method)
89 * @r: rectangle to test
90 *
91 * Is @r empty? ie. zero width or height.
92 *
93 * Returns: %TRUE if @r contains no pixels.
94 */
95 gboolean
vips_rect_isempty(const VipsRect * r)96 vips_rect_isempty( const VipsRect *r )
97 {
98 return( r->width <= 0 || r->height <= 0 );
99 }
100
101 /**
102 * vips_rect_includesrect: (method)
103 * @r1: outer rectangle
104 * @r2: inner rectangle
105 *
106 * Is @r2 a subset of @r1?
107 *
108 * Returns: %TRUE if @r2 is a subset of @r1.
109 */
110 gboolean
vips_rect_includesrect(const VipsRect * r1,const VipsRect * r2)111 vips_rect_includesrect( const VipsRect *r1, const VipsRect *r2 )
112 {
113 return( r1->left <= r2->left &&
114 r1->top <= r2->top &&
115 r1->left + r1->width >= r2->left + r2->width &&
116 r1->top + r1->height >= r2->top + r2->height );
117 }
118
119 /**
120 * vips_rect_equalsrect: (method)
121 * @r1: first rectangle
122 * @r2: second rectangle
123 *
124 * Is @r1 equal to @r2?
125 *
126 * Returns: %TRUE if @r1 is equal to @r2.
127 */
128 gboolean
vips_rect_equalsrect(const VipsRect * r1,const VipsRect * r2)129 vips_rect_equalsrect( const VipsRect *r1, const VipsRect *r2 )
130 {
131 return( r1->left == r2->left && r1->top == r2->top &&
132 r1->width == r2->width && r1->height == r2->height );
133 }
134
135 /**
136 * vips_rect_overlapsrect: (method)
137 * @r1: first rectangle
138 * @r2: second rectangle
139 *
140 * Do @r1 and @r2 have a non-empty intersection?
141 *
142 * Returns: %TRUE if @r2 and @r1 overlap.
143 */
144 gboolean
vips_rect_overlapsrect(const VipsRect * r1,const VipsRect * r2)145 vips_rect_overlapsrect( const VipsRect *r1, const VipsRect *r2 )
146 {
147 VipsRect intersection;
148
149 vips_rect_intersectrect( r1, r2, &intersection );
150
151 return( !vips_rect_isempty( &intersection ) );
152 }
153
154 /**
155 * vips_rect_marginadjust: (method)
156 * @r: rectangle to adjust
157 * @n: enlarge by
158 *
159 * Enlarge @r by @n. +1 means out one pixel.
160 */
161 void
vips_rect_marginadjust(VipsRect * r,int n)162 vips_rect_marginadjust( VipsRect *r, int n )
163 {
164 r->left -= n;
165 r->top -= n;
166 r->width += 2 * n;
167 r->height += 2 * n;
168 }
169
170 /**
171 * vips_rect_intersectrect:
172 * @r1: input rectangle 1
173 * @r2: input rectangle 2
174 * @out: (out): output rectangle
175 *
176 * Fill @out with the intersection of @r1 and @r2. @out can equal @r1 or @r2.
177 */
178 void
vips_rect_intersectrect(const VipsRect * r1,const VipsRect * r2,VipsRect * out)179 vips_rect_intersectrect( const VipsRect *r1, const VipsRect *r2, VipsRect *out )
180 {
181 int left = VIPS_MAX( r1->left, r2->left );
182 int top = VIPS_MAX( r1->top, r2->top );
183 int right = VIPS_MIN( VIPS_RECT_RIGHT( r1 ), VIPS_RECT_RIGHT( r2 ) );
184 int bottom = VIPS_MIN( VIPS_RECT_BOTTOM( r1 ), VIPS_RECT_BOTTOM( r2 ) );
185 int width = VIPS_MAX( 0, right - left );
186 int height = VIPS_MAX( 0, bottom - top );
187
188 out->left = left;
189 out->top = top;
190 out->width = width;
191 out->height = height;
192 }
193
194 /**
195 * vips_rect_unionrect:
196 * @r1: input rectangle 1
197 * @r2: input rectangle 2
198 * @out: (out): output rectangle
199 *
200 * Fill @out with the bounding box of @r1 and @r2. @out can equal @r1 or @r2.
201 */
202 void
vips_rect_unionrect(const VipsRect * r1,const VipsRect * r2,VipsRect * out)203 vips_rect_unionrect( const VipsRect *r1, const VipsRect *r2, VipsRect *out )
204 {
205 if( vips_rect_isempty( r1 ) )
206 *out = *r2;
207 else if( vips_rect_isempty( r2 ) )
208 *out = *r1;
209 else {
210 int left = VIPS_MIN( r1->left, r2->left );
211 int top = VIPS_MIN( r1->top, r2->top );
212 int width = VIPS_MAX( VIPS_RECT_RIGHT( r1 ),
213 VIPS_RECT_RIGHT( r2 ) ) - left;
214 int height = VIPS_MAX( VIPS_RECT_BOTTOM( r1 ),
215 VIPS_RECT_BOTTOM( r2 ) )- top;
216
217 out->left = left;
218 out->top = top;
219 out->width = width;
220 out->height = height;
221 }
222 }
223
224 /**
225 * vips_rect_dup: (skip)
226 * @r: rectangle to duplicate
227 *
228 * Duplicate a rect to the heap. You need to free the result with g_free().
229 *
230 * Returns: (transfer full): a pointer to copy of @r allocated on the heap.
231 */
232 VipsRect *
vips_rect_dup(const VipsRect * r)233 vips_rect_dup( const VipsRect *r )
234 {
235 VipsRect *out;
236
237 if( !(out = VIPS_NEW( NULL, VipsRect )) )
238 return( NULL );
239 *out = *r;
240
241 return( out );
242 }
243
244 /**
245 * vips_rect_normalise: (method)
246 * @r: rect to normalise
247 *
248 * Make sure width and height are >0 by moving the origin and flipping the
249 * rect.
250 */
251 void
vips_rect_normalise(VipsRect * r)252 vips_rect_normalise( VipsRect *r )
253 {
254 if( r->width < 0 ) {
255 r->left += r->width;
256 r->width *= -1;
257 }
258 if( r->height < 0 ) {
259 r->top += r->height;
260 r->height *= -1;
261 }
262 }
263