fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / lib / art / art_uta_ops.c
blob1b4251607b0dd906c511f313d4df58a988764532
1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998-2000 Raph Levien
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "config.h"
21 #include "art_uta_ops.h"
23 #include <string.h>
24 #include "art_misc.h"
25 #include "art_uta.h"
27 #ifndef MIN
28 #define MIN(a,b) ((a) < (b) ? (a) : (b))
29 #endif
31 #ifndef MAX
32 #define MAX(a,b) ((a) > (b) ? (a) : (b))
33 #endif
35 /**
36 * art_uta_union: Compute union of two uta's.
37 * @uta1: One uta.
38 * @uta2: The other uta.
40 * Computes the union of @uta1 and @uta2. The union is approximate,
41 * but coverage is guaranteed over all pixels included in either of
42 * the arguments, ie more pixels may be covered than the "exact"
43 * union.
45 * Note: this routine is used in the Gnome Canvas to accumulate the
46 * region that needs to be repainted. However, since it copies over
47 * the entire uta (which might be largish) even when the update may be
48 * small, it can be a performance bottleneck. There are two approaches
49 * to this problem, both of which are probably worthwhile. First, the
50 * generated uta's should always be limited to the visible window,
51 * thus guaranteeing that uta's never become large. Second, there
52 * should be a new, destructive union operation that only touches a
53 * small part of the uta when the update is small.
55 * Return value: The new union uta.
56 **/
57 ArtUta *
58 art_uta_union (ArtUta *uta1, ArtUta *uta2)
60 ArtUta *uta;
61 int x0, y0, x1, y1;
62 int x, y;
63 int ix, ix1, ix2;
64 ArtUtaBbox bb, bb1, bb2;
66 x0 = MIN(uta1->x0, uta2->x0);
67 y0 = MIN(uta1->y0, uta2->y0);
68 x1 = MAX(uta1->x0 + uta1->width, uta2->x0 + uta2->width);
69 y1 = MAX(uta1->y0 + uta1->height, uta2->y0 + uta2->height);
70 uta = art_uta_new (x0, y0, x1, y1);
72 /* could move the first two if/else statements out of the loop */
73 ix = 0;
74 for (y = y0; y < y1; y++)
76 ix1 = (y - uta1->y0) * uta1->width + x0 - uta1->x0;
77 ix2 = (y - uta2->y0) * uta2->width + x0 - uta2->x0;
78 for (x = x0; x < x1; x++)
80 if (x < uta1->x0 || y < uta1->y0 ||
81 x >= uta1->x0 + uta1->width || y >= uta1->y0 + uta1->height)
82 bb1 = 0;
83 else
84 bb1 = uta1->utiles[ix1];
86 if (x < uta2->x0 || y < uta2->y0 ||
87 x >= uta2->x0 + uta2->width || y >= uta2->y0 + uta2->height)
88 bb2 = 0;
89 else
90 bb2 = uta2->utiles[ix2];
92 if (bb1 == 0)
93 bb = bb2;
94 else if (bb2 == 0)
95 bb = bb1;
96 else
97 bb = ART_UTA_BBOX_CONS(MIN(ART_UTA_BBOX_X0(bb1),
98 ART_UTA_BBOX_X0(bb2)),
99 MIN(ART_UTA_BBOX_Y0(bb1),
100 ART_UTA_BBOX_Y0(bb2)),
101 MAX(ART_UTA_BBOX_X1(bb1),
102 ART_UTA_BBOX_X1(bb2)),
103 MAX(ART_UTA_BBOX_Y1(bb1),
104 ART_UTA_BBOX_Y1(bb2)));
105 uta->utiles[ix] = bb;
106 ix++;
107 ix1++;
108 ix2++;
111 return uta;