1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998 Raph Levien
3 *
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.
8 *
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.
13 *
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.
18 */
20 #include "art_misc.h"
21 #include "art_uta.h"
22 #include "art_rect.h"
23 #include "art_rect_uta.h"
25 /* Functions to decompose a microtile array into a list of rectangles. */
27 /**
28 * art_rect_list_from_uta: Decompose uta into list of rectangles.
29 * @uta: The source uta.
30 * @max_width: The maximum width of the resulting rectangles.
31 * @max_height: The maximum height of the resulting rectangles.
32 * @p_nrects: Where to store the number of returned rectangles.
33 *
34 * Allocates a new list of rectangles, sets *@p_nrects to the number
35 * in the list. This list should be freed with art_free().
36 *
37 * Each rectangle bounded in size by (@max_width, @max_height).
38 * However, these bounds must be at least the size of one tile.
39 *
40 * This routine provides a precise implementation, i.e. the rectangles
41 * cover exactly the same area as the uta. It is thus appropriate in
42 * cases where the overhead per rectangle is small compared with the
43 * cost of filling in extra pixels.
44 *
45 * Return value: An array containing the resulting rectangles.
46 **/
47 ArtIRect *
48 art_rect_list_from_uta (ArtUta *uta, int max_width, int max_height,
49 int *p_nrects)
50 {
51 ArtIRect *rects;
52 int n_rects, n_rects_max;
53 int x, y;
54 int width, height;
55 int ix;
56 int left_ix;
57 ArtUtaBbox *utiles;
58 ArtUtaBbox bb;
59 int x0, y0, x1, y1;
60 int *glom;
61 int glom_rect;
63 n_rects = 0;
64 n_rects_max = 1;
65 rects = art_new (ArtIRect, n_rects_max);
67 width = uta->width;
68 height = uta->height;
69 utiles = uta->utiles;
71 glom = art_new (int, width * height);
72 for (ix = 0; ix < width * height; ix++)
73 glom[ix] = -1;
75 ix = 0;
76 for (y = 0; y < height; y++)
77 for (x = 0; x < width; x++)
78 {
79 bb = utiles[ix];
80 if (bb)
81 {
82 x0 = ((uta->x0 + x) << ART_UTILE_SHIFT) + ART_UTA_BBOX_X0(bb);
83 y0 = ((uta->y0 + y) << ART_UTILE_SHIFT) + ART_UTA_BBOX_Y0(bb);
84 y1 = ((uta->y0 + y) << ART_UTILE_SHIFT) + ART_UTA_BBOX_Y1(bb);
86 left_ix = ix;
87 /* now try to extend to the right */
88 while (x != width - 1 &&
89 ART_UTA_BBOX_X1(bb) == ART_UTILE_SIZE &&
90 (((bb & 0xffffff) ^ utiles[ix + 1]) & 0xffff00ff) == 0 &&
91 (((uta->x0 + x + 1) << ART_UTILE_SHIFT) +
92 ART_UTA_BBOX_X1(utiles[ix + 1]) -
93 x0) <= max_width)
94 {
95 bb = utiles[ix + 1];
96 ix++;
97 x++;
98 }
99 x1 = ((uta->x0 + x) << ART_UTILE_SHIFT) + ART_UTA_BBOX_X1(bb);
102 /* if rectangle nonempty */
103 if ((x1 ^ x0) | (y1 ^ y0))
104 {
105 /* try to glom onto an existing rectangle */
106 glom_rect = glom[left_ix];
107 if (glom_rect != -1 &&
108 x0 == rects[glom_rect].x0 &&
109 x1 == rects[glom_rect].x1 &&
110 y0 == rects[glom_rect].y1 &&
111 y1 - rects[glom_rect].y0 <= max_height)
112 {
113 rects[glom_rect].y1 = y1;
114 }
115 else
116 {
117 if (n_rects == n_rects_max)
118 art_expand (rects, ArtIRect, n_rects_max);
119 rects[n_rects].x0 = x0;
120 rects[n_rects].y0 = y0;
121 rects[n_rects].x1 = x1;
122 rects[n_rects].y1 = y1;
123 glom_rect = n_rects;
124 n_rects++;
125 }
126 if (y != height - 1)
127 glom[left_ix + width] = glom_rect;
128 }
129 }
130 ix++;
131 }
133 art_free (glom);
134 *p_nrects = n_rects;
135 return rects;
136 }