1 /* libmypaint - The MyPaint Brush Library
2  * Copyright (C) 2008 Martin Renold <martinxyz@gmx.ch>
3  * Copyright (C) 2012 Jon Nordby <jononor@gmail.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "config.h"
19 
20 #include "mypaint-rectangle.h"
21 #include <stdlib.h>
22 #include <string.h>
23 
memdup(const void * src,size_t len)24 void *memdup(const void *src, size_t len)
25 {
26         void *p = malloc(len);
27         if (p)
28             memcpy(p, src, len);
29         return p;
30 }
31 
32 MyPaintRectangle *
mypaint_rectangle_copy(MyPaintRectangle * self)33 mypaint_rectangle_copy(MyPaintRectangle *self)
34 {
35     return (MyPaintRectangle *)memdup(self, sizeof(MyPaintRectangle));
36 }
37 
38 void
mypaint_rectangle_expand_to_include_point(MyPaintRectangle * r,int x,int y)39 mypaint_rectangle_expand_to_include_point(MyPaintRectangle *r, int x, int y)
40 {
41     if (r->width == 0) {
42         r->width = 1; r->height = 1;
43         r->x = x; r->y = y;
44     } else {
45         if (x < r->x) { r->width += r->x-x; r->x = x; } else
46         if (x >= r->x+r->width) { r->width = x - r->x + 1; }
47 
48         if (y < r->y) { r->height += r->y-y; r->y = y; } else
49         if (y >= r->y+r->height) { r->height = y - r->y + 1; }
50     }
51 }
52 
53 void
mypaint_rectangle_expand_to_include_rect(MyPaintRectangle * r,MyPaintRectangle * other)54 mypaint_rectangle_expand_to_include_rect(MyPaintRectangle *r, MyPaintRectangle *other)
55 {
56     mypaint_rectangle_expand_to_include_point(r, other->x, other->y);
57     mypaint_rectangle_expand_to_include_point(r, other->x + other->width - 1, other->y + other->height - 1);
58 }
59