1 /*
2     Ming, an SWF output library
3     Copyright (C) 2002  Opaque Industries - http://www.opaque.net/
4 
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 /* $Id$ */
21 
22 #include <stdlib.h>
23 
24 #include "rect.h"
25 #include "libming.h"
26 
27 int
SWFRect_numBits(SWFRect rect)28 SWFRect_numBits(SWFRect rect)
29 {
30 	return 5 + 4*max(max(SWFOutput_numSBits(rect->minX),
31 					 SWFOutput_numSBits(rect->maxX)),
32 			 max(SWFOutput_numSBits(rect->minY),
33 					 SWFOutput_numSBits(rect->maxY)));
34 }
35 
36 
37 void
SWFOutput_writeRect(SWFOutput out,SWFRect rect)38 SWFOutput_writeRect(SWFOutput out, SWFRect rect)
39 {
40 	int nBits = max(max(SWFOutput_numSBits(rect->minX),
41 					SWFOutput_numSBits(rect->maxX)),
42 			max(SWFOutput_numSBits(rect->minY),
43 					SWFOutput_numSBits(rect->maxY)));
44 
45 	if(nBits>=32)
46 		SWF_error("SWFRect too large for file format");
47 
48 	SWFOutput_writeBits(out, nBits, 5);
49 	SWFOutput_writeSBits(out, rect->minX, nBits);
50 	SWFOutput_writeSBits(out, rect->maxX, nBits);
51 	SWFOutput_writeSBits(out, rect->minY, nBits);
52 	SWFOutput_writeSBits(out, rect->maxY, nBits);
53 	SWFOutput_byteAlign(out);
54 }
55 
56 
57 void
destroySWFRect(SWFRect rect)58 destroySWFRect(SWFRect rect)
59 {
60 	free(rect);
61 }
62 
63 
64 SWFRect
newSWFRect(int minX,int maxX,int minY,int maxY)65 newSWFRect(int minX, int maxX, int minY, int maxY)
66 {
67 	SWFRect rect = (SWFRect)malloc(sizeof(struct SWFRect_s));
68 
69 	/* If malloc failed, return NULL to signify this */
70 	if (NULL == rect)
71 		return NULL;
72 
73 	rect->minX = min(minX, maxX);
74 	rect->maxX = max(minX, maxX);
75 	rect->minY = min(minY, maxY);
76 	rect->maxY = max(minY, maxY);
77 
78 	return rect;
79 }
80 
81 
82 int
SWFRect_getWidth(SWFRect r)83 SWFRect_getWidth(SWFRect r)
84 {
85 	return r->maxX - r->minX;
86 }
87 
88 
89 int
SWFRect_getHeight(SWFRect r)90 SWFRect_getHeight(SWFRect r)
91 {
92 	return r->maxY - r->minY;
93 }
94 
95 
96 void
SWFRect_getBounds(SWFRect rect,int * minX,int * maxX,int * minY,int * maxY)97 SWFRect_getBounds(SWFRect rect, int *minX, int *maxX, int *minY, int *maxY)
98 {
99 	*minX = rect->minX;
100 	*maxX = rect->maxX;
101 	*minY = rect->minY;
102 	*maxY = rect->maxY;
103 }
104 
105 
106 void
SWFRect_setBounds(SWFRect rect,int minX,int maxX,int minY,int maxY)107 SWFRect_setBounds(SWFRect rect, int minX, int maxX, int minY, int maxY)
108 {
109 	rect->minX = minX;
110 	rect->maxX = maxX;
111 	rect->minY = minY;
112 	rect->maxY = maxY;
113 }
114 
115 
116 SWFRect
SWFRect_copy(SWFRect rect)117 SWFRect_copy(SWFRect rect)
118 {
119 	return newSWFRect(rect->minX, rect->maxX, rect->minY, rect->maxY);
120 }
121 
122 
123 void
SWFRect_includeRect(SWFRect a,SWFRect b)124 SWFRect_includeRect(SWFRect a, SWFRect b)
125 {
126 	if ( b->minX < a->minX )
127 		a->minX = b->minX;
128 
129 	if ( b->maxX > a->maxX )
130 		a->maxX = b->maxX;
131 
132 	if ( b->minX < a->minX )
133 		a->minX = b->minX;
134 
135 	if ( b->maxX > a->maxX )
136 		a->maxX = b->maxX;
137 }
138 
139 
140 /* make rect big enough to include (x,y) + width extra */
141 void
SWFRect_includePoint(SWFRect rect,int x,int y,int width)142 SWFRect_includePoint(SWFRect rect, int x, int y, int width)
143 {
144 	if ( x-width-1 < rect->minX )
145 		rect->minX = x-width;
146 
147 	if ( x+width > rect->maxX )
148 		rect->maxX = x+width;
149 
150 	if ( y-width-1 < rect->minY )
151 		rect->minY = y-width;
152 
153 	if ( y+width > rect->maxY )
154 		rect->maxY = y+width;
155 }
156 
157 
158 /*
159  * Local variables:
160  * tab-width: 2
161  * c-basic-offset: 2
162  * End:
163  */
164