/[Sims3RigEditor]/trunk/gr2lib_typedefs/typedefs/TextureCreator/RGBAPackedValue.cs
ViewVC logotype

Contents of /trunk/gr2lib_typedefs/typedefs/TextureCreator/RGBAPackedValue.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 49 - (show annotations) (download)
Sun Aug 8 07:43:37 2010 UTC (10 years, 6 months ago) by william
File size: 15040 byte(s)
continue support for ST3C textures...

Have BGR ST3C Texture Colors somwhate correct.
BGRA, still having problems with colors & alpha bein applied when there is no alpha

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Microsoft.Xna.Framework;
5 using gr2lib.core.typedefs;
6
7 namespace gr2lib_typedefs.typedefs.TextureCreator
8 {
9 public interface IRGBAShiftBits
10 {
11 byte ComponentShift { get; set; }
12 byte ComponentBits { get; set; }
13
14 byte ShiftedValue { get; set; }
15 }
16 public class RGBAShiftBits : IRGBAShiftBits
17 {
18 #region Implicit conversion
19 public static implicit operator RGBAShiftBits(byte b)
20 {
21 RGBAShiftBits _shift_bits = new RGBAShiftBits();
22 _shift_bits.ComponentShift = b;
23 return _shift_bits;
24 }
25 #endregion
26 public RGBAShiftBits()
27 {
28 this.init();
29 }
30 public RGBAShiftBits(byte ComponentShift, byte ComponentBits)
31 {
32 this.init();
33 this.ComponentShift = ComponentShift;
34 this.ComponentBits = ComponentBits;
35 }
36 private void init()
37 {
38 this.ComponentShift = 0;
39 this.ComponentBits = 8;
40 this.ShiftedValue = 0;
41 }
42 #region IRGBAShiftBits Members
43 private byte _ComponentShift;
44 public byte ComponentShift { get { return _ComponentShift; } set { _ComponentShift = value; } }
45 private byte _ComponentBits;
46 public byte ComponentBits { get { return _ComponentBits; } set { _ComponentBits = value; } }
47
48 private byte _ShiftedValue;
49 public byte ShiftedValue { get { return _ShiftedValue; } set { _ShiftedValue = value; } }
50 #endregion
51 }
52
53 public interface IRGBAPackedShiftValues
54 {
55 RGBAShiftBits a { get; set; }
56 RGBAShiftBits r { get; set; }
57 RGBAShiftBits g { get; set; }
58 RGBAShiftBits b { get; set; }
59
60 void FromArgb(RGBAShiftBits r);
61 void FromArgb(RGBAShiftBits r, RGBAShiftBits g);
62 void FromArgb(RGBAShiftBits r, RGBAShiftBits g, RGBAShiftBits b);
63 void FromArgb(RGBAShiftBits r, RGBAShiftBits g, RGBAShiftBits b, RGBAShiftBits a);
64
65 void UnPackValues(byte value);
66 void UnPackValues(ushort value);
67 void UnPackValues(uint value);
68 void UnPackValues(ulong value);
69 }
70 public class RGBAPackedShiftValues : IRGBAPackedShiftValues
71 {
72 #region Constructors
73 public RGBAPackedShiftValues()
74 {
75 this.init();
76 }
77 public RGBAPackedShiftValues(byte[] rgba_shifts, byte[] rgba_bits, granny_pixel_layout PixelFormat)
78 {
79 this.init();
80 this.PixelFormat = PixelFormat;
81 if (rgba_shifts.Length > 4)
82 {
83 throw new InvalidOperationException("Size of component shifts array cannot exceed 4: (i.e. R,G,B,A)");
84 }
85 if (rgba_bits.Length > 4)
86 {
87 throw new InvalidOperationException("Size of component bits array cannot exceed 4: (i.e. R,G,B,A)");
88 }
89
90 switch (rgba_shifts.Length)
91 {
92 case 1:
93 this.r.ComponentShift = (byte)rgba_shifts[0];
94 break;
95 case 2:
96 this.r.ComponentShift = (byte)rgba_shifts[0];
97 this.g.ComponentShift = (byte)rgba_shifts[1];
98 break;
99 case 3:
100 this.r.ComponentShift = (byte)rgba_shifts[0];
101 this.g.ComponentShift = (byte)rgba_shifts[1];
102 this.b.ComponentShift = (byte)rgba_shifts[2];
103 break;
104 case 4:
105 this.r.ComponentShift = (byte)rgba_shifts[0];
106 this.g.ComponentShift = (byte)rgba_shifts[1];
107 this.b.ComponentShift = (byte)rgba_shifts[2];
108 this.a.ComponentShift = (byte)rgba_shifts[3];
109 break;
110 default:
111 throw new InvalidOperationException("Size of component shifts array cannot exceed 4: (i.e. R,G,B,A)");
112 }
113 switch (rgba_bits.Length)
114 {
115 case 1:
116 this.r.ComponentBits = (byte)rgba_bits[0];
117 break;
118 case 2:
119 this.r.ComponentBits = (byte)rgba_bits[0];
120 this.g.ComponentBits = (byte)rgba_bits[1];
121 break;
122 case 3:
123 this.r.ComponentBits = (byte)rgba_bits[0];
124 this.g.ComponentBits = (byte)rgba_bits[1];
125 this.b.ComponentBits = (byte)rgba_bits[2];
126 break;
127 case 4:
128 this.r.ComponentBits = (byte)rgba_bits[0];
129 this.g.ComponentBits = (byte)rgba_bits[1];
130 this.b.ComponentBits = (byte)rgba_bits[2];
131 this.a.ComponentBits = (byte)rgba_bits[3];
132 break;
133 default:
134 throw new InvalidOperationException("Size of component bits array cannot exceed 4: (i.e. R,G,B,A)");
135 }
136 }
137 public RGBAPackedShiftValues(int[] rgba_shifts, int[] rgba_bits, granny_pixel_layout PixelFormat)
138 {
139 this.init();
140 this.PixelFormat = PixelFormat;
141 if (rgba_shifts.Length > 4)
142 {
143 throw new InvalidOperationException("Size of component shifts array cannot exceed 4: (i.e. R,G,B,A)");
144 }
145 if (rgba_bits.Length > 4)
146 {
147 throw new InvalidOperationException("Size of component bits array cannot exceed 4: (i.e. R,G,B,A)");
148 }
149
150 switch (rgba_shifts.Length)
151 {
152 case 1:
153 this.r.ComponentShift = (byte)rgba_shifts[0];
154 break;
155 case 2:
156 this.r.ComponentShift = (byte)rgba_shifts[0];
157 this.g.ComponentShift = (byte)rgba_shifts[1];
158 break;
159 case 3:
160 this.r.ComponentShift = (byte)rgba_shifts[0];
161 this.g.ComponentShift = (byte)rgba_shifts[1];
162 this.b.ComponentShift = (byte)rgba_shifts[2];
163 break;
164 case 4:
165 this.r.ComponentShift = (byte)rgba_shifts[0];
166 this.g.ComponentShift = (byte)rgba_shifts[1];
167 this.b.ComponentShift = (byte)rgba_shifts[2];
168 this.a.ComponentShift = (byte)rgba_shifts[3];
169 break;
170 default:
171 throw new InvalidOperationException("Size of component shifts array cannot exceed 4: (i.e. R,G,B,A)");
172 }
173 switch (rgba_bits.Length)
174 {
175 case 1:
176 this.r.ComponentBits = (byte)rgba_bits[0];
177 break;
178 case 2:
179 this.r.ComponentBits = (byte)rgba_bits[0];
180 this.g.ComponentBits = (byte)rgba_bits[1];
181 break;
182 case 3:
183 this.r.ComponentBits = (byte)rgba_bits[0];
184 this.g.ComponentBits = (byte)rgba_bits[1];
185 this.b.ComponentBits = (byte)rgba_bits[2];
186 break;
187 case 4:
188 this.r.ComponentBits = (byte)rgba_bits[0];
189 this.g.ComponentBits = (byte)rgba_bits[1];
190 this.b.ComponentBits = (byte)rgba_bits[2];
191 this.a.ComponentBits = (byte)rgba_bits[3];
192 break;
193 default:
194 throw new InvalidOperationException("Size of component bits array cannot exceed 4: (i.e. R,G,B,A)");
195 }
196 }
197
198 public RGBAPackedShiftValues(RGBAShiftBits r)
199 {
200 this.FromArgb(r);
201 }
202 public RGBAPackedShiftValues(RGBAShiftBits r, RGBAShiftBits g)
203 {
204 this.FromArgb(r, g);
205 }
206 public RGBAPackedShiftValues(RGBAShiftBits r, RGBAShiftBits g, RGBAShiftBits b)
207 {
208 this.FromArgb(r, g, b);
209 }
210 public RGBAPackedShiftValues(RGBAShiftBits r, RGBAShiftBits g, RGBAShiftBits b, RGBAShiftBits a)
211 {
212 this.FromArgb(r, g, b, a);
213 }
214
215 public RGBAPackedShiftValues(Vector2 _vector)
216 {
217 this.r = (byte)_vector.X;
218 this.g = (byte)_vector.Y;
219 }
220 public RGBAPackedShiftValues(Vector3 _vector)
221 {
222 this.r = (byte)_vector.X;
223 this.g = (byte)_vector.Y;
224 this.b = (byte)_vector.Z;
225 }
226 public RGBAPackedShiftValues(Vector4 _vector)
227 {
228 this.r = (byte)_vector.X;
229 this.g = (byte)_vector.Y;
230 this.b = (byte)_vector.Z;
231 this.a = (byte)_vector.W;
232 }
233 private void init()
234 {
235 this.a = new RGBAShiftBits();
236 this.r = new RGBAShiftBits();
237 this.g = new RGBAShiftBits();
238 this.b = new RGBAShiftBits();
239 this.PixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat;
240 }
241 #endregion
242
243 #region IRGBAPackedValue Members
244
245 public void FromArgb(RGBAShiftBits r)
246 {
247 this.init();
248 this.r = r;
249 }
250 public void FromArgb(RGBAShiftBits r, RGBAShiftBits g)
251 {
252 this.init();
253 this.r = r;
254 this.g = g;
255 }
256 public void FromArgb(RGBAShiftBits r, RGBAShiftBits g, RGBAShiftBits b)
257 {
258 this.init();
259 this.r = r;
260 this.g = g;
261 this.b = b;
262 }
263 public void FromArgb(RGBAShiftBits r, RGBAShiftBits g, RGBAShiftBits b, RGBAShiftBits a)
264 {
265 this.init();
266 this.r = r;
267 this.g = g;
268 this.b = b;
269 this.a = a;
270 }
271
272 private granny_pixel_layout _PixelFormat;
273 private granny_pixel_layout PixelFormat
274 {
275 get { return _PixelFormat; }
276 set { _PixelFormat = value; }
277 }
278
279 private RGBAShiftBits _a;
280 public RGBAShiftBits a { get { return _a; } set { _a = value; } }
281 private RGBAShiftBits _r;
282 public RGBAShiftBits r { get { return _r; } set { _r = value; } }
283 private RGBAShiftBits _g;
284 public RGBAShiftBits g { get { return _g; } set { _g = value; } }
285 private RGBAShiftBits _b;
286 public RGBAShiftBits b { get { return _b; } set { _b = value; } }
287
288 public void UnPackValues(byte value)
289 {
290 this.UnPackValues((ulong)value);
291 }
292 public void UnPackValues(ushort value)
293 {
294 this.UnPackValues((ulong)value);
295 }
296 public void UnPackValues(uint value)
297 {
298 this.UnPackValues((ulong)value);
299 }
300 public void UnPackValues(ulong value)
301 {
302 switch (this.PixelFormat)
303 {
304 case granny_pixel_layout.GrannyABGR8888PixelFormat: break;
305 case granny_pixel_layout.GrannyARGB8888PixelFormat: break;
306 case granny_pixel_layout.GrannyBGR555PixelFormat: break;
307 case granny_pixel_layout.GrannyBGR565PixelFormat:
308 a.ShiftedValue = (byte)(0xff);
309 r.ShiftedValue = (byte)((value >> b.ComponentShift) & 0x1f);
310 g.ShiftedValue = (byte)((value >> g.ComponentShift) & 0x3f);
311 b.ShiftedValue = (byte)((value >> r.ComponentShift) & 0x1f);
312 r.ShiftedValue = (byte)((b.ShiftedValue << 3) | (b.ShiftedValue >> 2));
313 g.ShiftedValue = (byte)((g.ShiftedValue << 2) | (g.ShiftedValue >> 4));
314 b.ShiftedValue = (byte)((r.ShiftedValue << 3) | (r.ShiftedValue >> 2));
315 break;
316 case granny_pixel_layout.GrannyBGR888PixelFormat: break;
317 case granny_pixel_layout.GrannyBGRA4444PixelFormat: break;
318 case granny_pixel_layout.GrannyBGRA5551PixelFormat:
319 a.ShiftedValue = (byte)((value >> a.ComponentShift) * 0xff);
320 r.ShiftedValue = (byte)((value >> b.ComponentShift) & 0x1f);
321 g.ShiftedValue = (byte)((value >> g.ComponentShift) & 0x1f);
322 b.ShiftedValue = (byte)((value >> r.ComponentShift) & 0x1f);
323 r.ShiftedValue = (byte)((b.ShiftedValue << 3) | (b.ShiftedValue >> 2));
324 g.ShiftedValue = (byte)((g.ShiftedValue << 3) | (g.ShiftedValue >> 2));
325 b.ShiftedValue = (byte)((r.ShiftedValue << 3) | (r.ShiftedValue >> 2));
326 break;
327 case granny_pixel_layout.GrannyBGRA8888PixelFormat: break;
328 case granny_pixel_layout.GrannyRGB555PixelFormat: break;
329 case granny_pixel_layout.GrannyRGB565PixelFormat:
330 a.ShiftedValue = (byte)(0xff);
331 r.ShiftedValue = (byte)((value >> r.ComponentShift) & 0x1f);
332 g.ShiftedValue = (byte)((value >> g.ComponentShift) & 0x3f);
333 b.ShiftedValue = (byte)((value >> b.ComponentShift) & 0x1f);
334 r.ShiftedValue = (byte)((r.ShiftedValue << 3) | (r.ShiftedValue >> 2));
335 g.ShiftedValue = (byte)( (g.ShiftedValue << 2) | (g.ShiftedValue >> 4));
336 b.ShiftedValue = (byte)((b.ShiftedValue << 3) | (b.ShiftedValue >> 2));
337 break;
338 case granny_pixel_layout.GrannyRGB888PixelFormat: break;
339 case granny_pixel_layout.GrannyRGBA4444PixelFormat: break;
340 case granny_pixel_layout.GrannyRGBA5551PixelFormat:
341 a.ShiftedValue = (byte)((value >> a.ComponentShift) * 0xff);
342 r.ShiftedValue = (byte)((value >> r.ComponentShift) & 0x1f);
343 g.ShiftedValue = (byte)((value >> g.ComponentShift) & 0x1f);
344 b.ShiftedValue = (byte)((value >> b.ComponentShift) & 0x1f);
345 r.ShiftedValue = (byte)((r.ShiftedValue << 3) | (r.ShiftedValue >> 2));
346 g.ShiftedValue = (byte)((g.ShiftedValue << 3) | (g.ShiftedValue >> 2));
347 b.ShiftedValue = (byte)((b.ShiftedValue << 3) | (b.ShiftedValue >> 2));
348 break;
349 case granny_pixel_layout.GrannyRGBA8888PixelFormat: break;
350 default: throw new InvalidOperationException("Attempted to unpack RGBA data for an invalid Pixel Format.");
351 }
352 }
353 #endregion
354 }
355 }

  ViewVC Help
Powered by ViewVC 1.1.22