PageRenderTime 38ms CodeModel.GetById 20ms app.highlight 16ms RepoModel.GetById 0ms app.codeStats 0ms

/SWFGeometry.m

https://code.google.com/p/xee/
Objective C | 199 lines | 142 code | 36 blank | 21 comment | 16 complexity | d5dcd3bf18d345d679a67c050efe5b7b MD5 | raw file
  1#import "SWFGeometry.h"
  2#import <math.h>
  3
  4
  5SWFPoint SWFPointOnLine(SWFPoint a,SWFPoint b,float t)
  6{
  7	return SWFMakePoint(
  8		(float)a.x*(1-t)+(float)b.x*t,
  9		(float)a.y*(1-t)+(float)b.y*t
 10	);
 11}
 12
 13SWFRect SWFParseRect(CSHandle *fh)
 14{
 15	int bits=[fh readBits:5];
 16	int xmin=[fh readSignedBits:bits];
 17	int xmax=[fh readSignedBits:bits];
 18	int ymin=[fh readSignedBits:bits];
 19	int ymax=[fh readSignedBits:bits];
 20
 21	[fh flushReadBits];
 22
 23	return SWFMakeRect(xmin,ymin,xmax-xmin,ymax-ymin);
 24}
 25
 26void SWFWriteRect(SWFRect rect,CSHandle *fh)
 27{
 28	int xmin=rect.x;
 29	int xmax=rect.x+rect.width;
 30	int ymin=rect.y;
 31	int ymax=rect.y+rect.height;
 32	int bits=SWFCountSignedBits4(xmin,xmax,ymin,ymax);
 33
 34	[fh writeSignedBits:5 value:bits];
 35	[fh writeSignedBits:bits value:xmin];
 36	[fh writeSignedBits:bits value:xmax];
 37	[fh writeSignedBits:bits value:ymin];
 38	[fh writeSignedBits:bits value:ymax];
 39	[fh flushWriteBits];
 40}
 41
 42SWFMatrix SWFParseMatrix(CSHandle *fh)
 43{
 44	int a00=1<<16,a01=0,a02=0;
 45	int a10=0,a11=1<<16,a12=0;
 46
 47	if([fh readBits:1])
 48	{
 49		int bits=[fh readBits:5];
 50		a00=[fh readSignedBits:bits];
 51		a11=[fh readSignedBits:bits];
 52	}
 53
 54	if([fh readBits:1])
 55	{
 56		int bits=[fh readBits:5];
 57		a10=[fh readSignedBits:bits];
 58		a01=[fh readSignedBits:bits];
 59	}
 60
 61	int bits=[fh readBits:5];
 62	a02=[fh readSignedBits:bits];
 63	a12=[fh readSignedBits:bits];
 64
 65	[fh flushReadBits];
 66
 67	return SWFMakeMatrix(a00,a01,a02,a10,a11,a12);
 68}
 69
 70void SWFWriteMatrix(SWFMatrix mtx,CSHandle *fh)
 71{
 72	if(mtx.a00!=1<<16||mtx.a11!=1<<16)
 73	{
 74		int bits=SWFCountSignedBits2(mtx.a00,mtx.a11);
 75		[fh writeBits:1 value:1];
 76		[fh writeBits:5 value:bits];
 77		[fh writeBits:bits value:mtx.a00];
 78		[fh writeBits:bits value:mtx.a11];
 79	}
 80	else [fh writeBits:1 value:0];
 81
 82	if(mtx.a01!=0||mtx.a10!=0)
 83	{
 84		int bits=SWFCountSignedBits2(mtx.a01,mtx.a10);
 85		[fh writeBits:1 value:1];
 86		[fh writeBits:5 value:bits];
 87		[fh writeBits:bits value:mtx.a10];
 88		[fh writeBits:bits value:mtx.a01];
 89	}
 90	else [fh writeBits:1 value:0];
 91
 92	int bits=SWFCountSignedBits2(mtx.a02,mtx.a12);
 93	[fh writeBits:5 value:bits];
 94	[fh writeBits:bits value:mtx.a02];
 95	[fh writeBits:bits value:mtx.a12];
 96
 97	[fh flushWriteBits];
 98}
 99
100static inline int fixmult(int a,int b) { return (((int64_t)a)*((int64_t)b))/65536; }
101
102SWFMatrix SWFMultiplyMatrices(SWFMatrix a,SWFMatrix b)
103{
104	return SWFMakeMatrix(
105		fixmult(a.a00,b.a00)+fixmult(a.a01,b.a10),
106		fixmult(a.a00,b.a01)+fixmult(a.a01,b.a11),
107		fixmult(a.a00,b.a02)+fixmult(a.a01,b.a12)+a.a02,
108		fixmult(a.a10,b.a00)+fixmult(a.a11,b.a10),
109		fixmult(a.a10,b.a01)+fixmult(a.a11,b.a11),
110		fixmult(a.a10,b.a02)+fixmult(a.a11,b.a12)+a.a12
111	);
112}
113
114SWFMatrix SWFScalingMatrix(float x_scale,float y_scale)
115{
116	return SWFMakeMatrix(
117		65536*x_scale,0,0,
118		0,65536*y_scale,0
119	);
120}
121
122SWFMatrix SWFRotationMatrix(float degrees)
123{
124	double rad=degrees*M_PI/180;
125	return SWFMakeMatrix(
126		65536*cos(rad),-65536*sin(rad),0,
127		65536*sin(rad),65536*cos(rad),0
128	);
129}
130
131
132
133/*SWFMatrix SWFMatrixFromAffineTransform(NSAffineTransform *t)
134{
135	NSAffineTransformStruct a=[t transformStruct];
136	SWFMatrix res={
137		a.m11*65536.0,a.m21*65536.0,a.tX*20.0,
138		a.m12*65536.0,a.m22*65536.0,a.tY*20.0
139	};
140	return res;
141}
142
143NSAffineTransform *SWFAffineTransformFromMatrix(SWFMatrix m)
144{
145	NSAffineTransformStruct a={
146		(float)m.a00/65536.0,(float)m.a10/65536.0,
147		(float)m.a01/65536.0,(float)m.a11/65536.0,
148		(float)m.a02/20.0,(float)m.a12/20.0
149	}
150	NSAffineTransform *t=[NSAffineTransform transform];
151	[transform setTransformStruct:a];
152	return t;
153}*/
154
155
156
157
158static inline int imax(int a,int b) { return a>b?a:b; }
159
160int SWFCountBits(uint32_t val)
161{
162	int res=0;
163	if(val==0) return 0;
164	if(val&0xFFFF0000) { res|=16; val>>=16; }
165	if(val&0x0000FF00) { res|=8; val>>=8; }
166	if(val&0x000000F0) { res|=4; val>>=4; }
167	if(val&0x0000000C) { res|=2; val>>=2; }
168	if(val&0x00000002) { res|=1; }
169	return res+1;
170}
171
172
173int SWFCountBits2(uint32_t val1,uint32_t val2)
174{
175	return imax(SWFCountBits(val1),SWFCountBits(val2));
176}
177
178int SWFCountBits4(uint32_t val1,uint32_t val2,uint32_t val3,uint32_t val4)
179{
180	return imax(SWFCountBits2(val1,val2),SWFCountBits2(val3,val4));
181}
182
183
184int SWFCountSignedBits(int32_t val)
185{
186	if(val==0) return 0;
187	else if(val<0) return SWFCountBits(~val)+1;
188	else return SWFCountBits(val)+1;
189}
190
191int SWFCountSignedBits2(int32_t val1,int32_t val2)
192{
193	return imax(SWFCountSignedBits(val1),SWFCountSignedBits(val2));
194}
195
196int SWFCountSignedBits4(int32_t val1,int32_t val2,int32_t val3,int32_t val4)
197{
198	return imax(SWFCountSignedBits2(val1,val2),SWFCountSignedBits2(val3,val4));
199}