Source code: com/anotherbigidea/flash/structs/Rect.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
39 /**
40 * A SWF Rectangle structure
41 */
42 public class Rect
43 {
44 protected int bitSize = -1;
45 protected int minX;
46 protected int minY;
47 protected int maxX;
48 protected int maxY;
49
50 public int getMinX() { return minX; }
51 public int getMinY() { return minY; }
52 public int getMaxX() { return maxX; }
53 public int getMaxY() { return maxY; }
54
55 public void setMinX( int minX ) { this.minX = minX; bitSize = -1; }
56 public void setMinY( int minY ) { this.minY = minY; bitSize = -1; }
57 public void setMaxX( int maxX ) { this.maxX = maxX; bitSize = -1; }
58 public void setMaxY( int maxY ) { this.maxY = maxY; bitSize = -1; }
59
60 public Rect( int minX, int minY, int maxX, int maxY )
61 {
62 this.minX = minX;
63 this.minY = minY;
64 this.maxX = maxX;
65 this.maxY = maxY;
66 }
67
68 public Rect( InStream in ) throws IOException
69 {
70 in.synchBits();
71 bitSize = (int)in.readUBits( 5 );
72 minX = (int)in.readSBits( bitSize );
73 maxX = (int)in.readSBits( bitSize );
74 minY = (int)in.readSBits( bitSize );
75 maxY = (int)in.readSBits( bitSize );
76 }
77
78 public Rect()
79 {
80 this( 0, 0, 11000, 8000 ); //default size
81 }
82
83 /**
84 * Calculate the minimum bit size based on the current values
85 */
86 protected int getBitSize()
87 {
88 if( bitSize == -1 ) //bitsize not defined
89 {
90 int bsMinX = OutStream.determineSignedBitSize( minX );
91 int bsMaxX = OutStream.determineSignedBitSize( maxX );
92 int bsMinY = OutStream.determineSignedBitSize( minY );
93 int bsMaxY = OutStream.determineSignedBitSize( maxY );
94
95 bitSize = bsMinY;
96 if( bitSize < bsMaxX ) bitSize = bsMaxX;
97 if( bitSize < bsMinX ) bitSize = bsMinX;
98 if( bitSize < bsMaxY ) bitSize = bsMaxY;
99 }
100
101 return bitSize;
102 }
103
104 public long getLength()
105 {
106 int bits = 5 + ( getBitSize() * 4 );
107 int bytes = bits / 8;
108
109 if( bytes * 8 < bits ) bytes++;
110
111 return bytes;
112 }
113
114 /**
115 * Write the rect contents to the output stream
116 */
117 public void write( OutStream out ) throws IOException
118 {
119 out.flushBits();
120
121 out.writeUBits( 5, getBitSize() );
122 out.writeSBits( bitSize, minX );
123 out.writeSBits( bitSize, maxX );
124 out.writeSBits( bitSize, minY );
125 out.writeSBits( bitSize, maxY );
126
127 out.flushBits();
128 }
129
130 public String toString()
131 {
132 return "Rect bitsize=" + bitSize +
133 " (" + minX + "," + minY + ")-("+ maxX + "," + maxY + ")";
134 }
135 }