Source code: com/anotherbigidea/flash/structs/ColorTransform.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 public class ColorTransform
40 {
41 protected double multRed = 1.0;
42 protected double multGreen = 1.0;
43 protected double multBlue = 1.0;
44
45 protected int addRed = 0;
46 protected int addGreen = 0;
47 protected int addBlue = 0;
48
49 protected double multAlpha = 1.0; //used by AlphaTransform
50 protected int addAlpha = 0; //used by AlphaTransform
51
52 public double getMultRed() { return multRed; }
53 public double getMultGreen() { return multGreen; }
54 public double getMultBlue() { return multBlue; }
55
56 public int getAddRed() { return addRed; }
57 public int getAddGreen() { return addGreen; }
58 public int getAddBlue() { return addBlue; }
59
60 public void setMultRed ( double multRed ) { this.multRed = multRed; }
61 public void setMultGreen( double multGreen ) { this.multGreen = multGreen; }
62 public void setMultBlue ( double multBlue ) { this.multBlue = multBlue; }
63
64 public void setAddRed ( int addRed ) { this.addRed = addRed; }
65 public void setAddGreen ( int addGreen ) { this.addGreen = addGreen; }
66 public void setAddBlue ( int addBlue ) { this.addBlue = addBlue; }
67
68 /**
69 * An identity transform
70 */
71 public ColorTransform() {}
72
73 public ColorTransform( double multRed, double multGreen, double multBlue,
74 int addRed, int addGreen, int addBlue )
75 {
76 this.multRed = multRed;
77 this.multGreen = multGreen;
78 this.multBlue = multBlue;
79 this.addRed = addRed;
80 this.addGreen = addGreen;
81 this.addBlue = addBlue;
82 }
83
84 public ColorTransform( int addRed, int addGreen, int addBlue )
85 {
86 this.addRed = addRed;
87 this.addGreen = addGreen;
88 this.addBlue = addBlue;
89 }
90
91 public ColorTransform( double multRed, double multGreen, double multBlue )
92 {
93 this.multRed = multRed;
94 this.multGreen = multGreen;
95 this.multBlue = multBlue;
96 }
97
98 public ColorTransform( InStream in ) throws IOException
99 {
100 in.synchBits();
101
102 //--Add and mult are reversed
103 boolean hasAddTerms = ( in.readUBits(1) == 1 );
104 boolean hasMultTerms = ( in.readUBits(1) == 1 );
105
106 int numBits = (int)in.readUBits(4);
107
108 if( hasMultTerms )
109 {
110 multRed = ((double)in.readSBits( numBits ))/256.0;
111 multGreen = ((double)in.readSBits( numBits ))/256.0;
112 multBlue = ((double)in.readSBits( numBits ))/256.0;
113 }
114
115 if( hasAddTerms )
116 {
117 addRed = in.readSBits( numBits );
118 addGreen = in.readSBits( numBits );
119 addBlue = in.readSBits( numBits );
120 }
121 }
122
123 public void write( OutStream out ) throws IOException
124 {
125 out.flushBits();
126
127 boolean hasAddTerms = ( addRed != 0 )
128 || ( addGreen != 0 )
129 || ( addBlue != 0 );
130
131 boolean hasMultTerms = ( multRed != 1.0 )
132 || ( multGreen != 1.0 )
133 || ( multBlue != 1.0 );
134
135 int intMultRed = (int)(multRed * 256.0);
136 int intMultGreen = (int)(multGreen * 256.0);
137 int intMultBlue = (int)(multBlue * 256.0);
138
139 //--Figure out the bit sizes
140 int numBits = 1;
141
142 if( hasAddTerms )
143 {
144 int redBits = out.determineSignedBitSize( addRed );
145 int greenBits = out.determineSignedBitSize( addGreen );
146 int blueBits = out.determineSignedBitSize( addBlue );
147
148 if( numBits < redBits ) numBits = redBits;
149 if( numBits < greenBits ) numBits = greenBits;
150 if( numBits < blueBits ) numBits = blueBits;
151 }
152
153 if( hasMultTerms )
154 {
155 int redBits = out.determineSignedBitSize( intMultRed );
156 int greenBits = out.determineSignedBitSize( intMultGreen );
157 int blueBits = out.determineSignedBitSize( intMultBlue );
158
159 if( numBits < redBits ) numBits = redBits;
160 if( numBits < greenBits ) numBits = greenBits;
161 if( numBits < blueBits ) numBits = blueBits;
162 }
163
164 //--Add and mult are reversed
165 out.writeUBits( 1, hasAddTerms ? 1L : 0L );
166 out.writeUBits( 1, hasMultTerms ? 1L : 0L );
167 out.writeUBits( 4, numBits );
168
169 if( hasMultTerms )
170 {
171 out.writeSBits( numBits, intMultRed );
172 out.writeSBits( numBits, intMultGreen );
173 out.writeSBits( numBits, intMultBlue );
174 }
175
176 if( hasAddTerms )
177 {
178 out.writeSBits( numBits, addRed );
179 out.writeSBits( numBits, addGreen );
180 out.writeSBits( numBits, addBlue );
181 }
182
183 out.flushBits();
184 }
185
186 public void writeWithoutAlpha( OutStream out ) throws IOException
187 {
188 write( out );
189 }
190
191 public void writeWithAlpha( OutStream out ) throws IOException
192 {
193 out.flushBits();
194
195 boolean hasAddTerms = ( addRed != 0 )
196 || ( addGreen != 0 )
197 || ( addBlue != 0 )
198 || ( addAlpha != 0 );
199
200 boolean hasMultTerms = ( multRed != 1.0 )
201 || ( multGreen != 1.0 )
202 || ( multBlue != 1.0 )
203 || ( multAlpha != 1.0 );
204
205 int intMultRed = (int)(multRed * 256.0);
206 int intMultGreen = (int)(multGreen * 256.0);
207 int intMultBlue = (int)(multBlue * 256.0);
208 int intMultAlpha = (int)(multAlpha * 256.0);
209
210 //--Figure out the bit sizes
211 int numBits = 1;
212
213 if( hasAddTerms )
214 {
215 int redBits = out.determineSignedBitSize( addRed );
216 int greenBits = out.determineSignedBitSize( addGreen );
217 int blueBits = out.determineSignedBitSize( addBlue );
218 int alphaBits = out.determineSignedBitSize( addAlpha );
219
220 if( numBits < redBits ) numBits = redBits;
221 if( numBits < greenBits ) numBits = greenBits;
222 if( numBits < blueBits ) numBits = blueBits;
223 if( numBits < alphaBits ) numBits = alphaBits;
224 }
225
226 if( hasMultTerms )
227 {
228 int redBits = out.determineSignedBitSize( intMultRed );
229 int greenBits = out.determineSignedBitSize( intMultGreen );
230 int blueBits = out.determineSignedBitSize( intMultBlue );
231 int alphaBits = out.determineSignedBitSize( intMultAlpha );
232
233 if( numBits < redBits ) numBits = redBits;
234 if( numBits < greenBits ) numBits = greenBits;
235 if( numBits < blueBits ) numBits = blueBits;
236 if( numBits < alphaBits ) numBits = alphaBits;
237 }
238
239 //--Add and mult are reversed
240 out.writeUBits( 1, hasAddTerms ? 1L : 0L );
241 out.writeUBits( 1, hasMultTerms ? 1L : 0L );
242 out.writeUBits( 4, numBits );
243
244 if( hasMultTerms )
245 {
246 out.writeSBits( numBits, intMultRed );
247 out.writeSBits( numBits, intMultGreen );
248 out.writeSBits( numBits, intMultBlue );
249 out.writeSBits( numBits, intMultAlpha );
250 }
251
252 if( hasAddTerms )
253 {
254 out.writeSBits( numBits, addRed );
255 out.writeSBits( numBits, addGreen );
256 out.writeSBits( numBits, addBlue );
257 out.writeSBits( numBits, addAlpha );
258 }
259
260 out.flushBits();
261 }
262
263 public String toString()
264 {
265 return " cxform(+rgb,*rgb)=(" + addRed + "," + addGreen + "," + addBlue
266 + "," + multRed + "," + multGreen + "," + multBlue + ")";
267 }
268 }