Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/anotherbigidea/flash/structs/Matrix.java


1   /****************************************************************
2    * Copyright (c) 2001, David N. Main, All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or
5    * without modification, are permitted provided that the 
6    * following conditions are met:
7    *
8    * 1. Redistributions of source code must retain the above 
9    * copyright notice, this list of conditions and the following 
10   * disclaimer. 
11   * 
12   * 2. Redistributions in binary form must reproduce the above 
13   * copyright notice, this list of conditions and the following 
14   * disclaimer in the documentation and/or other materials 
15   * provided with the distribution.
16   * 
17   * 3. The name of the author may not be used to endorse or 
18   * promote products derived from this software without specific 
19   * prior written permission. 
20   * 
21   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY 
22   * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
23   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
24   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
25   * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
27   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
28   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
29   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
30   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
31   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
32   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   ****************************************************************/
34  package com.anotherbigidea.flash.structs;
35  
36  import java.io.*;
37  import com.anotherbigidea.io.*;
38  import com.anotherbigidea.flash.SWFConstants;
39  
40  public class Matrix
41  {
42      protected double scaleX = 1.0;
43      protected double scaleY = 1.0;
44      
45      protected double skew0 = 0.0;
46      protected double skew1 = 0.0;
47      
48      protected double translateX = 0.0;
49      protected double translateY = 0.0;      
50      
51      public double getScaleX()  { return scaleX; }
52      public double getScaleY()  { return scaleY; }
53      public double getSkew0 ()  { return skew0; }
54      public double getSkew1 ()  { return skew1; }
55      public double getTranslateX() { return translateX; }
56      public double getTranslateY() { return translateY; }
57  
58      public void setScaleX    ( double scaleX  ) { this.scaleX = scaleX; }
59      public void setScaleY    ( double scaleY  ) { this.scaleY = scaleY; }
60      public void setSkew0     ( double skew0   ) { this.skew0  = skew0; }
61      public void setSkew1     ( double skew1   ) { this.skew1  = skew1; }
62      public void setTranslateX( double translateX ) { this.translateX = translateX; }
63      public void setTranslateY( double translateY ) { this.translateY = translateY; }    
64      
65      /**
66       * An identity matrix
67       */
68      public Matrix()
69      {
70          this( 1.0, 1.0, 0.0, 0.0, 0, 0 );
71      }
72      
73      public Matrix( double translateX, double translateY )
74      {
75          this( 1.0, 1.0, 0.0, 0.0, translateX, translateY );
76      }    
77      
78      /**
79       * Copy another matrix
80       */
81      public Matrix( Matrix copy )
82      {
83          if( copy == null ) return;
84          scaleX     = copy.scaleX;
85          scaleY     = copy.scaleY;
86          skew0      = copy.skew0;
87          skew1      = copy.skew1;
88          translateX = copy.translateX;
89          translateY = copy.translateY;      
90      }
91      
92      public Matrix( double scaleX,  double scaleY, 
93                     double skew0,   double skew1,
94                     double translateX, double translateY )
95      {
96          this.scaleX = scaleX;
97          this.scaleY = scaleY;
98  
99          this.skew0 = skew0;
100         this.skew1 = skew1;
101         
102         setTranslateX( translateX );
103         setTranslateY( translateY );
104     }
105     
106     public Matrix( InStream in ) throws IOException 
107     {
108         in.synchBits();
109         
110         if( in.readUBits(1) == 1 ) //has scale values
111         {
112             int scaleBits = (int)in.readUBits(5);
113             scaleX = ((double)in.readSBits( scaleBits ))/65536.0;
114             scaleY = ((double)in.readSBits( scaleBits ))/65536.0;
115         }
116 
117         if( in.readUBits(1) == 1 ) //has rotate/skew values
118         {
119             int skewBits  = (int)in.readUBits(5);
120             skew0 = ((double)in.readSBits( skewBits ))/65536.0;
121             skew1 = ((double)in.readSBits( skewBits ))/65536.0;
122         }
123         
124         int translateBits = (int)in.readUBits(5);
125         translateX = in.readSBits( translateBits );
126         translateY = in.readSBits( translateBits );        
127     }
128     
129     public void write( OutStream out ) throws IOException 
130     {
131         out.flushBits();
132         
133         if( scaleX != 1.0 || scaleY != 1.0 ) //if non-default values
134         {
135             int intScaleX = (int)(scaleX * 65536.0);
136             int intScaleY = (int)(scaleY * 65536.0);
137             
138             int scaleBits  = out.determineSignedBitSize( intScaleX );
139             int scaleBits2 = out.determineSignedBitSize( intScaleY );
140             if( scaleBits < scaleBits2 ) scaleBits = scaleBits2;
141             
142             out.writeUBits( 1, 1 );
143             out.writeUBits( 5, scaleBits );
144             out.writeSBits( scaleBits, intScaleX );
145             out.writeSBits( scaleBits, intScaleY );            
146         }
147         else
148         {
149             out.writeUBits( 1, 0 );
150         }
151                 
152         if( skew0 != 0.0 || skew1 != 0.0 ) //if non-default values
153         {
154             int intSkew0 = (int)(skew0 * 65536.0);
155             int intSkew1 = (int)(skew1 * 65536.0);
156             
157             int skewBits  = out.determineSignedBitSize( intSkew0 );
158             int skewBits2 = out.determineSignedBitSize( intSkew1 );
159             if( skewBits < skewBits2 ) skewBits = skewBits2;
160             
161             out.writeUBits( 1, 1 );
162             out.writeUBits( 5, skewBits );
163             out.writeSBits( skewBits, intSkew0 );
164             out.writeSBits( skewBits, intSkew1 );            
165         }
166         else
167         {
168             out.writeUBits( 1, 0 );
169         }
170 
171         if( translateX == 0 && translateY == 0 )
172         {
173             out.writeUBits( 5, 0 );
174         }
175         else
176         {
177             int translateBits  = out.determineSignedBitSize( (int)translateX );
178             int translateBits2 = out.determineSignedBitSize( (int)translateY );
179             if( translateBits < translateBits2 ) translateBits = translateBits2;   
180         
181             out.writeUBits( 5, translateBits );
182             out.writeSBits( translateBits, (int)translateX );
183             out.writeSBits( translateBits, (int)translateY );      
184         }
185         
186         out.flushBits();
187     }
188     
189     public String toString()
190     {
191         return " Matrix(sx,sy,s0,s1,tx,ty)=(" +
192                scaleX + "," + scaleY + "," + skew0 + "," + skew1 + "," +
193                translateX + "," + translateY + ")";
194     }
195 }