1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998 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.
23 #include "art_rect_uta.h"
25 /* Functions to decompose a microtile array into a list of rectangles. */
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.
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().
37 * Each rectangle bounded in size by (@max_width, @max_height).
38 * However, these bounds must be at least the size of one tile.
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.
45 * Return value: An array containing the resulting rectangles.
48 art_rect_list_from_uta (ArtUta *uta, int max_width, int max_height,
52 int n_rects, n_rects_max;
65 rects = art_new (ArtIRect, n_rects_max);
71 glom = art_new (int, width * height);
72 for (ix = 0; ix < width * height; ix++)
76 for (y = 0; y < height; y++)
77 for (x = 0; x < width; x++)
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);
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]) -
99 x1 = ((uta->x0 + x) << ART_UTILE_SHIFT) + ART_UTA_BBOX_X1(bb);
102 /* if rectangle nonempty */
103 if ((x1 ^ x0) | (y1 ^ y0))
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)
113 rects[glom_rect].y1 = y1;
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;
127 glom[left_ix + width] = glom_rect;