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

Contents of /trunk/gr2lib_typedefs/typedefs/TextureCreator/s3tc_texture_creator.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: 14848 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 //#define IS_NOT_BINDABLE // when defined, will always return false for An IBindableTexture
2 using System;
3 using System.Collections.Generic;
4 using System.Text;
5 using gr2lib_typedefs.typedefs.TextureCreator.interfaces;
6 using System.IO;
7 using gr2lib.core.typedefs;
8 using gr2lib.core.ui.helpers;
9 using gr2lib.core;
10 using System.Runtime.InteropServices;
11 using System.Diagnostics;
12 using Microsoft.Xna.Framework.Graphics;
13 using Microsoft.Xna.Framework.Graphics.PackedVector;
14 using Microsoft.Xna.Framework;
15
16 namespace gr2lib_typedefs.typedefs.TextureCreator
17 {
18 public class s3tc_texture_creator : ATextureResource
19 {
20 //public override granny_texture_encoding EncodingType { get { return granny_texture_encoding.GrannyS3TCTextureEncoding; } }
21
22 public override string TextureCreatorName { get { return "s3tc Texture Creator"; } }
23
24 #region Constructors
25 public s3tc_texture_creator() : base() { }
26 public s3tc_texture_creator(string filename) : base(filename) { }
27 public s3tc_texture_creator(byte[] data) : base(data) { }
28 public s3tc_texture_creator(MemoryStream ms) : base(ms) { }
29 #region IBindableTexture Members
30 public new static bool IsBindable { get { return true; } }
31 #endregion
32 #endregion
33
34 #region Private Members
35 GraphicsDevice mDevice = null;
36 PresentationParameters pp = null;
37 Texture2D _texture = null;
38 #endregion
39
40 #region ATextureResource Members
41 protected override void Parse()
42 {
43 try
44 {
45
46 //throw new NotImplementedException("s3tc texture creation has not been implemented");
47 this.Stream.Seek(0, SeekOrigin.Begin);
48 //DdsFileTypePlugin.DdsFile _ddsfile = new DdsFileTypePlugin.DdsFile();
49 //_ddsfile.Load(this.Stream);
50 pp = new PresentationParameters();
51 // Create device
52 mDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
53 DeviceType.Hardware, this.Handle,pp);
54
55 _texture = Texture2D.FromFile(mDevice, this.Stream, (int)this.Stream.Length);
56
57 SpriteBatch batch = new SpriteBatch(mDevice);
58 RenderTarget2D renderTarget = new RenderTarget2D(mDevice, _texture.Width, _texture.Height, 1, SurfaceFormat.Color);
59 mDevice.SetRenderTarget(0, renderTarget);
60 batch.Begin();
61 batch.Draw(_texture, new Rectangle(0, 0, _texture.Width, _texture.Height), Color.White);
62 batch.End();
63
64 this.TextureName = new FilePath(this.filename);
65 this.TextureWidth = _texture.Width;
66 this.TextureHeight = _texture.Height;
67 this.TextureBitsPerPixel = (int)_texture.Format;
68 //this.TextureMipMapCount = (int)_texture.LevelCount;
69 this.TextureMipMapCount = 0;
70 this.TextureRawPixelFormat = _texture.Format;
71
72 int granny_s3tc_texture_format_value = -1;
73 switch ((SurfaceFormat)this.TextureRawPixelFormat)
74 {
75 case SurfaceFormat.Bgr565:
76 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGR565);
77 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGR565;
78 break;
79 case SurfaceFormat.Bgra5551:
80 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA5551);
81 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA5551;
82 break;
83
84 case SurfaceFormat.Dxt1: // can have an 1 bit alpha or no alpha (565 or 5551)
85 throw new InvalidOperationException("Dxt1 Pixel format is not supported yet: could be 565 or 5551");
86 case SurfaceFormat.Dxt2:
87 case SurfaceFormat.Dxt3:
88 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA8888MappedAlpha);
89 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA8888MappedAlpha;
90 break;
91 case SurfaceFormat.Dxt4:
92 case SurfaceFormat.Dxt5:
93 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA8888InterpolatedAlpha);
94 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA8888InterpolatedAlpha;
95 break;
96 default:
97 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat;
98 throw new InvalidOperationException("Attempted to read an image with an unsupported pixel format.");
99 }
100
101 // Get Pixel Bytes
102 this.GetPixelBytes();
103
104 this.NativeTextureData.NativeTextureLayout = granny_pixel_layouts.GetPixelLayoutFromEnum(this.TextureSpecificPixelFormat);
105 // create Granny2 Texture
106 this.NativeTextureData.NativeTextureBuilder = coreapi.TextureSupport.GrannyBeginS3TCTexture(this.TextureWidth, this.TextureHeight, (granny_s3tc_texture_format)granny_s3tc_texture_format_value);
107 if (this.NativeTextureData.NativeTextureBuilder == IntPtr.Zero) throw new InvalidOperationException("Cannot process texure as the pointer to the Texture_Builder is null");
108 coreapi.TextureSupport.GrannyEncodeImage(this.NativeTextureData.NativeTextureBuilder, this.TextureWidth, this.TextureHeight, this.TextureStride, this.TextureMipMapCount, this.PixelBytesPointer);
109 IntPtr ResultingTextureSizePTR = Marshal.AllocHGlobal(coreapi.TextureSupport.GrannyGetResultingTextureSize(this.NativeTextureData.NativeTextureBuilder));
110 this.NativeTextureData.NativeTexturePointer = coreapi.TextureSupport.GrannyEndTextureInPlace(this.NativeTextureData.NativeTextureBuilder, ResultingTextureSizePTR);
111 if (this.NativeTextureData.NativeTexturePointer == IntPtr.Zero) throw new InvalidOperationException("The texture that was created, has a null pointer");
112
113 // Get the Pixel Format
114
115
116
117 this.TextureCreatorType = TextureCreators.s3tc;
118 this.NativeTextureData.NativeTextureEncoding = new gr2lib.core.helpers.NameValuePair<string, granny_texture_encoding>(
119 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyS3TCTextureEncoding), granny_texture_encoding.GrannyS3TCTextureEncoding);
120 this.NativeTextureData.NativeTextureSubFormat = new gr2lib.core.helpers.NameValuePair<string, granny_pixel_layout>(
121 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>((granny_pixel_layout)granny_s3tc_texture_format_value), (granny_pixel_layout)granny_s3tc_texture_format_value);
122
123 this.NativeTextureData.NativeTextureType = new gr2lib.core.helpers.NameValuePair<string, granny_texture_type>(
124 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
125
126 this.HaveBindableTexture = true;
127 }
128 catch (Exception ex)
129 {
130 this.HaveBindableTexture = false;
131 //Debug.WriteLine(ex.ToString());
132 throw new InvalidOperationException("Failed to read texture using: " + new s3tc_texture_creator().TextureCreatorName, ex);
133 }
134
135 }
136 protected override MemoryStream UnParse()
137 {
138 throw new NotImplementedException("s3tc texture creation has not been implemented");
139 }
140 #endregion
141
142 private void GetPixelBytes()
143 {
144 //switch ((SurfaceFormat)this.TextureRawPixelFormat)
145 //{
146 // case SurfaceFormat.Bgr565:
147 // Bgr565[] _Bgr565 = new Bgr565[_texture.Width * _texture.Height];
148 // _texture.GetData(_Bgr565);
149 // break;
150 // case SurfaceFormat.Bgra5551:
151 // Bgra5551[] _Bgra5551 = new Bgra5551[_texture.Width * _texture.Height];
152 // _texture.GetData(_Bgra5551);
153 // break;
154 // case SurfaceFormat.Dxt1: // can have an 1 bit alpha or no alpha (565 or 5551)
155 // throw new InvalidOperationException("Dxt1 Pixel format is not supported yet: could be 565 or 5551");
156 // case SurfaceFormat.Dxt2:
157 // case SurfaceFormat.Dxt3:
158 // break;
159 // case SurfaceFormat.Dxt4:
160 // case SurfaceFormat.Dxt5:
161 // break;
162 // default:
163 // throw new InvalidOperationException("Attempted to read pixel bytes for an image with an unsupported pixel format.");
164 //}
165 byte[] data;
166 Bgr565[] _Bgr565;
167 Bgra5551[] _Bgra5551;
168 //Color[] _Color;
169 //int BytesPerPixel = 4;
170 //int RealBytsPerPixel =4;
171 //Rectangle? _rect = new Rectangle();
172 if ((SurfaceFormat)this.TextureRawPixelFormat == SurfaceFormat.Dxt1)
173 {
174 // 8 bytes per 4x4 blocks
175 int blocks = (_texture.Width * _texture.Height) / 16;
176 data = new byte[8 * blocks];
177 _texture.GetData<byte>(data, 0, data.Length);
178 }
179 else if ((int)(SurfaceFormat)this.TextureRawPixelFormat > 28 && (int)(SurfaceFormat)this.TextureRawPixelFormat < 33)
180 {
181 // 16 bytes per 4x4 blocks
182 int blocks = (_texture.Width * _texture.Height) / 16;
183 data = new byte[16 * blocks];
184 _texture.GetData<byte>(data, 0, data.Length);
185 }
186 else
187 {
188 switch ((SurfaceFormat)this.TextureRawPixelFormat)
189 {
190 case SurfaceFormat.Bgr565:
191 _Bgr565 = new Bgr565[_texture.Width * _texture.Height];
192 _texture.GetData(_Bgr565);
193 this.PixelBytesCount = _texture.Width * _texture.Height * sizeof(ushort);
194 this.PixelBytes = new byte[this.PixelBytesCount];
195 MemoryStream _Bgr565_Stream = new MemoryStream(this.PixelBytesCount);
196 BinaryWriter _Bgr565_Stream_Writer = new BinaryWriter(_Bgr565_Stream);
197 for (int i = 0; i < _Bgr565.Length; i += sizeof(ushort))
198 {
199 Microsoft.Xna.Framework.Graphics.Color _color = new Microsoft.Xna.Framework.Graphics.Color(_Bgr565[i].ToVector3());
200 // write value in format: RGB, because the source is BGR
201 Bgr565 _repack = new Bgr565((float)_color.R, (float)_color.G, (float)_color.B);
202 _Bgr565_Stream_Writer.Write(_repack.PackedValue);
203 }
204 _Bgr565_Stream_Writer.Flush();
205 this.PixelBytes = _Bgr565_Stream.ToArray();
206 _Bgr565_Stream_Writer.Close();
207 break;
208 case SurfaceFormat.Bgra5551:
209 _Bgra5551 = new Bgra5551[_texture.Width * _texture.Height];
210 _texture.GetData(_Bgra5551);
211 this.PixelBytesCount = _texture.Width * _texture.Height * sizeof(ushort);
212 this.PixelBytes = new byte[this.PixelBytesCount];
213 MemoryStream _Bgra5551_Stream = new MemoryStream(this.PixelBytesCount);
214 BinaryWriter _Bgra5551_Stream_Writer = new BinaryWriter(_Bgra5551_Stream);
215 for (int i = 0; i < _Bgra5551.Length; i += sizeof(ushort))
216 {
217 Microsoft.Xna.Framework.Graphics.Color _color = new Microsoft.Xna.Framework.Graphics.Color(_Bgra5551[i].ToVector4());
218 // write value in format: ARGB, because the source is BGRA
219 Bgra5551 _repack = new Bgra5551((float)_color.R, (float)_color.G, (float)_color.B, (float)_color.A);
220 _Bgra5551_Stream_Writer.Write(_repack.PackedValue);
221 }
222 _Bgra5551_Stream_Writer.Flush();
223 this.PixelBytes = _Bgra5551_Stream.ToArray();
224 _Bgra5551_Stream_Writer.Close();
225
226
227 break;
228 case SurfaceFormat.Color:
229 throw new InvalidOperationException("Attempted to read pixel data for a dds image with a pixel format of type: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().FullName);
230 default:
231 throw new InvalidOperationException("Attempted to read pixel data for a dds image with a pixel format of type: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().FullName);
232 }
233 }
234
235 }
236 private static float UnpackUNorm(uint bitmask, uint value)
237 {
238 value &= bitmask;
239
240 return (((float)value) / ((float)bitmask));
241 }
242 #region RessourceHandler for s3tc Textures
243 public class ResourceHandler : ATextureResourceHandler, IBindableTexture
244 {
245 #region IBindableTexture Members
246 public bool IsBindable { get { return s3tc_texture_creator.IsBindable; } }
247 #endregion
248 public ResourceHandler()
249 {
250 // add magic values for each supported texture type
251
252 this.Add(typeof(s3tc_texture_creator), new List<string>(
253 new string[]
254 {
255 "0x44445320", // DDS MAGIC
256 }));
257 }
258 }
259 #endregion
260 }
261 }

  ViewVC Help
Powered by ViewVC 1.1.22