/[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 55 - (show annotations) (download)
Thu Aug 12 08:37:47 2010 UTC (9 years, 6 months ago) by william
File size: 15034 byte(s)
Fix all, current, c++ compiler warnings and linker errors

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 false; } }
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 this.TextureName = new FilePath(this.filename);
58 this.TextureWidth = _texture.Width;
59 this.TextureHeight = _texture.Height;
60 //this.TextureBitsPerPixel = (int)_texture.Format;
61 //this.TextureMipMapCount = (int)_texture.LevelCount;
62 this.TextureMipMapCount = (int)_texture.LevelCount;
63 this.TextureRawPixelFormat = _texture.Format;
64
65 int granny_s3tc_texture_format_value = -1;
66 switch ((SurfaceFormat)this.TextureRawPixelFormat)
67 {
68 case SurfaceFormat.Bgr565:
69 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGR565);
70 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGR565;
71 this.TextureBitsPerPixel = 16;
72 break;
73 case SurfaceFormat.Bgra5551:
74 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA5551);
75 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA5551;
76 this.TextureBitsPerPixel = 16;
77 break;
78
79 case SurfaceFormat.Dxt1: // can have an 1 bit alpha or no alpha (565 or 5551)
80 this.TextureBitsPerPixel = 16;
81 throw new InvalidOperationException("Dxt1 Pixel format is not supported yet: could be 565 or 5551");
82 case SurfaceFormat.Dxt2:
83 case SurfaceFormat.Dxt3:
84 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA8888MappedAlpha);
85 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA8888MappedAlpha;
86 this.TextureBitsPerPixel = 32;
87 break;
88 case SurfaceFormat.Dxt4:
89 case SurfaceFormat.Dxt5:
90 this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA8888InterpolatedAlpha);
91 granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA8888InterpolatedAlpha;
92 this.TextureBitsPerPixel = 32;
93 break;
94 default:
95 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat;
96 throw new InvalidOperationException("Attempted to read an image with an unsupported pixel format.");
97 }
98
99 // Get Pixel Bytes
100 this.GetPixelBytes();
101
102 this.NativeTextureData.NativeTextureLayout = granny_pixel_layouts.GetPixelLayoutFromEnum(this.TextureSpecificPixelFormat);
103 // create Granny2 Texture
104 this.NativeTextureData.NativeTextureBuilder = coreapi.TextureSupport.GrannyBeginS3TCTexture(this.TextureWidth, this.TextureHeight, (granny_s3tc_texture_format)granny_s3tc_texture_format_value);
105 if (this.NativeTextureData.NativeTextureBuilder == IntPtr.Zero) throw new InvalidOperationException("Cannot process texure as the pointer to the Texture_Builder is null");
106 coreapi.TextureSupport.GrannyEncodeImage(this.NativeTextureData.NativeTextureBuilder, this.TextureWidth, this.TextureHeight, this.TextureStride, this.TextureMipMapCount, this.PixelBytesPointer);
107 IntPtr ResultingTextureSizePTR = Marshal.AllocHGlobal(coreapi.TextureSupport.GrannyGetResultingTextureSize(this.NativeTextureData.NativeTextureBuilder));
108 this.NativeTextureData.NativeTexturePointer = coreapi.TextureSupport.GrannyEndTextureInPlace(this.NativeTextureData.NativeTextureBuilder, ResultingTextureSizePTR);
109 if (this.NativeTextureData.NativeTexturePointer == IntPtr.Zero) throw new InvalidOperationException("The texture that was created, has a null pointer");
110
111 // Get the Pixel Format
112
113
114
115 this.TextureCreatorType = TextureCreators.s3tc;
116 this.NativeTextureData.NativeTextureEncoding = new gr2lib.core.helpers.NameValuePair<string, granny_texture_encoding>(
117 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyS3TCTextureEncoding), granny_texture_encoding.GrannyS3TCTextureEncoding);
118 this.NativeTextureData.NativeTextureSubFormat = new gr2lib.core.helpers.NameValuePair<string, granny_pixel_layout>(
119 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>((granny_pixel_layout)granny_s3tc_texture_format_value), (granny_pixel_layout)granny_s3tc_texture_format_value);
120
121 this.NativeTextureData.NativeTextureType = new gr2lib.core.helpers.NameValuePair<string, granny_texture_type>(
122 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
123
124 this.HaveBindableTexture = true;
125 }
126 catch (Exception ex)
127 {
128 this.HaveBindableTexture = false;
129 //Debug.WriteLine(ex.ToString());
130 throw new InvalidOperationException("Failed to read texture using: " + this.TextureCreatorName, ex);
131 }
132
133 }
134 protected override MemoryStream UnParse()
135 {
136 throw new NotImplementedException("s3tc texture creation has not been implemented");
137 }
138 #endregion
139
140 private void GetPixelBytes()
141 {
142 //switch ((SurfaceFormat)this.TextureRawPixelFormat)
143 //{
144 // case SurfaceFormat.Bgr565:
145 // Bgr565[] _Bgr565 = new Bgr565[_texture.Width * _texture.Height];
146 // _texture.GetData(_Bgr565);
147 // break;
148 // case SurfaceFormat.Bgra5551:
149 // Bgra5551[] _Bgra5551 = new Bgra5551[_texture.Width * _texture.Height];
150 // _texture.GetData(_Bgra5551);
151 // break;
152 // case SurfaceFormat.Dxt1: // can have an 1 bit alpha or no alpha (565 or 5551)
153 // throw new InvalidOperationException("Dxt1 Pixel format is not supported yet: could be 565 or 5551");
154 // case SurfaceFormat.Dxt2:
155 // case SurfaceFormat.Dxt3:
156 // break;
157 // case SurfaceFormat.Dxt4:
158 // case SurfaceFormat.Dxt5:
159 // break;
160 // default:
161 // throw new InvalidOperationException("Attempted to read pixel bytes for an image with an unsupported pixel format.");
162 //}
163 //byte[] data;
164 Bgr565[] _Bgr565;
165 Bgra5551[] _Bgra5551;
166 //Color[] _Color;
167 //int BytesPerPixel = 4;
168 //int RealBytsPerPixel =4;
169 //Rectangle? _rect = new Rectangle();
170 if ((SurfaceFormat)this.TextureRawPixelFormat == SurfaceFormat.Dxt1)
171 {
172 throw new NotImplementedException("DDS Image Format: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().Name + " has not been implemented yet.");
173 //// 8 bytes per 4x4 blocks
174 //int blocks = (_texture.Width * _texture.Height) / 16;
175 //data = new byte[8 * blocks];
176 //_texture.GetData<byte>(data, 0, data.Length);
177
178 }
179 else if ((int)(SurfaceFormat)this.TextureRawPixelFormat > 28 && (int)(SurfaceFormat)this.TextureRawPixelFormat < 33)
180 {
181 throw new NotImplementedException("DDS Image Format: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().Name + " has not been implemented yet.");
182 //// 16 bytes per 4x4 blocks
183 //int blocks = (_texture.Width * _texture.Height) / 16;
184 //data = new byte[16 * blocks];
185 //_texture.GetData<byte>(data, 0, data.Length);
186 }
187 else
188 {
189 switch ((SurfaceFormat)this.TextureRawPixelFormat)
190 {
191 case SurfaceFormat.Bgr565:
192 this.PixelBytesCount = _texture.Width * _texture.Height;
193 _Bgr565 = new Bgr565[this.PixelBytesCount];
194 _texture.GetData(_Bgr565);
195 this.PixelBytes = new byte[this.PixelBytesCount];
196 MemoryStream _Bgr565_Stream = new MemoryStream(this.PixelBytesCount);
197 BinaryWriter _Bgr565_Stream_Writer = new BinaryWriter(_Bgr565_Stream);
198 for (int i = 0; i < _Bgr565.Length; i++)
199 {
200 Microsoft.Xna.Framework.Graphics.Color _color = new Microsoft.Xna.Framework.Graphics.Color(_Bgr565[i].ToVector3());
201 // write value in format: RGB, because the source is BGR
202 Bgr565 _repack = new Bgr565((float)_color.R, (float)_color.G, (float)_color.B);
203 _Bgr565_Stream_Writer.Write(_repack.PackedValue);
204 }
205 _Bgr565_Stream_Writer.Flush();
206 this.PixelBytes = _Bgr565_Stream.ToArray();
207 _Bgr565_Stream_Writer.Close();
208 break;
209 case SurfaceFormat.Bgra5551:
210 this.PixelBytesCount = _texture.Width * _texture.Height;
211 _Bgra5551 = new Bgra5551[this.PixelBytesCount];
212 _texture.GetData(_Bgra5551);
213 this.PixelBytes = new byte[this.PixelBytesCount];
214 MemoryStream _Bgra5551_Stream = new MemoryStream(this.PixelBytesCount);
215 BinaryWriter _Bgra5551_Stream_Writer = new BinaryWriter(_Bgra5551_Stream);
216 for (int i = 0; i < _Bgra5551.Length; i++)
217 {
218 Microsoft.Xna.Framework.Graphics.Color _color = new Microsoft.Xna.Framework.Graphics.Color(_Bgra5551[i].ToVector4());
219 // write value in format: ARGB, because the source is BGRA
220 Bgra5551 _repack = new Bgra5551((float)_color.R, (float)_color.G, (float)_color.B, (float)_color.A);
221 _Bgra5551_Stream_Writer.Write(_repack.PackedValue);
222 }
223 _Bgra5551_Stream_Writer.Flush();
224 this.PixelBytes = _Bgra5551_Stream.ToArray();
225 _Bgra5551_Stream_Writer.Close();
226 break;
227 case SurfaceFormat.Color:
228 throw new InvalidOperationException("Attempted to read pixel data for a dds image with a pixel format of type: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().Name);
229 default:
230 throw new InvalidOperationException("Attempted to read pixel data for a dds image with a pixel format of type: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().Name);
231 }
232 }
233
234 }
235 private static float UnpackUNorm(uint bitmask, uint value)
236 {
237 value &= bitmask;
238
239 return (((float)value) / ((float)bitmask));
240 }
241 #region RessourceHandler for s3tc Textures
242 public class ResourceHandler : ATextureResourceHandler, IBindableTexture
243 {
244 #region IBindableTexture Members
245 public bool IsBindable { get { return s3tc_texture_creator.IsBindable; } }
246 #endregion
247 public ResourceHandler()
248 {
249 // add magic values for each supported texture type
250
251 this.Add(typeof(s3tc_texture_creator), new List<string>(
252 new string[]
253 {
254 "0x44445320", // DDS MAGIC
255 }));
256 }
257 }
258 #endregion
259 }
260 }

  ViewVC Help
Powered by ViewVC 1.1.22