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