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

Contents of /trunk/gr2lib_typedefs/typedefs/TextureCreator/bink_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: 11382 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 System.Drawing.Imaging;
10 //using System.Drawing;
11 using System.Runtime.InteropServices;
12 using gr2lib.core;
13 using System.Windows.Media.Imaging;
14 using System.Windows.Forms;
15 using Microsoft.Xna.Framework.Graphics;
16
17 namespace gr2lib_typedefs.typedefs.TextureCreator
18 {
19 public class bink_texture_creator : ATextureResource
20 {
21 //public override granny_texture_encoding EncodingType { get { return granny_texture_encoding.GrannyBinkTextureEncoding; } }
22 public override string TextureCreatorName { get { return "Bink Texture Creator"; } }
23 #region Constructors
24 public bink_texture_creator() : base() { }
25 public bink_texture_creator(string filename) : base(filename) { }
26 public bink_texture_creator(byte[] data) : base(data) { }
27 public bink_texture_creator(MemoryStream ms) : base(ms) { }
28 #region IBindableTexture Members
29 public new static bool IsBindable { get { return true; } }
30 #endregion
31 #endregion
32 #region Private Members
33 GraphicsDevice mDevice = null;
34 PresentationParameters pp = null;
35 Texture2D _texture = null;
36 #endregion
37
38 #region ATextureResource Members
39 protected override void Parse()
40 {
41 try
42 {
43 //throw new NotImplementedException("bink texture creation has not been implemented");
44 //throw new NotImplementedException("s3tc texture creation has not been implemented");
45 this.Stream.Seek(0, SeekOrigin.Begin);
46 //DdsFileTypePlugin.DdsFile _ddsfile = new DdsFileTypePlugin.DdsFile();
47 //_ddsfile.Load(this.Stream);
48 pp = new PresentationParameters();
49 // Create device
50 mDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
51 DeviceType.Hardware, this.Handle, pp);
52
53 _texture = Texture2D.FromFile(mDevice, this.Stream, (int)this.Stream.Length);
54
55 this.TextureWidth = _texture.Width;
56 this.TextureHeight = _texture.Height;
57 this.TextureBitsPerPixel = 32;
58 this.TextureMipMapCount = 0;
59 this.TextureRawPixelFormat = _texture.Format;
60
61 this.PixelBytes = new byte[] { 0 };
62
63
64 // get pixel format
65 //switch ((SurfaceFormat)this.TextureRawPixelFormat)
66 //{
67 // case SurfaceFormat.Color:
68 // this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyRGBA8888PixelFormat;
69 // break;
70 // //case PixelFormat.Format16bppRgb555:
71 // // this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyRGB555PixelFormat;
72 // // break;
73 // //case PixelFormat.Format16bppRgb565:
74 // // this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyRGB565PixelFormat;
75 // // break;
76 // case SurfaceFormat.Bgr24:
77 // this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyRGB888PixelFormat;
78 // break;
79 // //case PixelFormat.Format32bppArgb:
80 // // this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyRGBA8888PixelFormat;
81 // // break;
82 // case SurfaceFormat.Bgr32:
83 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyRGBA8888PixelFormat;
84 // break;
85 // case SurfaceFormat.Unknown:
86 // default:
87 // this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat;
88 // throw new InvalidOperationException("Attempted to read an image with an unsupported pixel format.");
89 //}
90 // repack pixelbytes in RGBA format
91 this.GetPixelBytes();
92 // get layout
93 this.NativeTextureData.NativeTextureLayout = granny_pixel_layouts.GetPixelLayoutFromEnum(this.TextureSpecificPixelFormat);
94
95 // create Granny2 Texture
96 this.NativeTextureData.NativeTextureBuilder = coreapi.TextureSupport.GrannyBeginBinkTexture(this.TextureWidth, this.TextureHeight, (int)granny_compression_type.GrannyNoCompression, (uint)granny_bink_texture_flags.GrannyBinkEncodeAlpha);
97 if (this.NativeTextureData.NativeTextureBuilder == IntPtr.Zero) throw new InvalidOperationException("Cannot process texure as the pointer to the Texture_Builder is null");
98 coreapi.TextureSupport.GrannyEncodeImage(this.NativeTextureData.NativeTextureBuilder, this.TextureWidth, this.TextureHeight, this.TextureStride, this.TextureMipMapCount, this.PixelBytesPointer);
99 IntPtr ResultingTextureSizePTR = Marshal.AllocHGlobal(coreapi.TextureSupport.GrannyGetResultingTextureSize(this.NativeTextureData.NativeTextureBuilder));
100 this.NativeTextureData.NativeTexturePointer = coreapi.TextureSupport.GrannyEndTextureInPlace(this.NativeTextureData.NativeTextureBuilder, ResultingTextureSizePTR);
101 if (this.NativeTextureData.NativeTexturePointer == IntPtr.Zero) throw new InvalidOperationException("The texture that was created, has a null pointer");
102
103
104
105
106 this.TextureCreatorType = TextureCreators.bink;
107 this.NativeTextureData.NativeTextureEncoding = new gr2lib.core.helpers.NameValuePair<string, granny_texture_encoding>(
108 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding);
109 this.NativeTextureData.NativeTextureSubFormat = new gr2lib.core.helpers.NameValuePair<string, granny_pixel_layout>(
110 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>(this.TextureSpecificPixelFormat), this.TextureSpecificPixelFormat);
111 this.NativeTextureData.NativeTextureType = new gr2lib.core.helpers.NameValuePair<string, granny_texture_type>(
112 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
113
114
115
116 // try to compress texture?
117 if (this.NativeTextureData.NativeTextureEncoding.Value == granny_texture_encoding.GrannyBinkTextureEncoding)
118 {
119
120 unsafe
121 {
122 fixed (byte* dest = this.PixelBytes)
123 {
124 int bink_compressed_size = 0;
125 uint bink_texture_flags = (uint)(granny_bink_texture_flags.GrannyBinkEncodeAlpha | granny_bink_texture_flags.GrannyBinkUseBink1);
126 bink_compressed_size = coreapi.TextureSupport.GrannyBinkCompressTexture(
127 this.TextureWidth,
128 this.TextureHeight,
129 this.TextureStride,
130 this.PixelBytesPointer,
131 bink_texture_flags,
132 (int)granny_compression_type.GrannyNoCompression,
133 new IntPtr(dest));
134 byte[] _compressed_pixel_bytes = new byte[bink_compressed_size];
135
136 for (int i = 0; i < bink_compressed_size; i++)
137 {
138 _compressed_pixel_bytes[i] = this.PixelBytes[i];
139 }
140 //this.AssignPixelBytesFromPointer(new IntPtr(dest), bink_compress);
141 this.PixelBytes = _compressed_pixel_bytes;
142 this.PixelBytesCount = this.PixelBytes.Length;
143
144 }
145 }
146 }
147
148 this.HaveBindableTexture = true;
149 }
150 catch (Exception ex)
151 {
152 this.HaveBindableTexture = false;
153 //Debug.WriteLine(ex.ToString());
154 throw new InvalidOperationException("Failed to read texture using: " + this.TextureCreatorName, ex);
155 }
156 }
157 protected override MemoryStream UnParse()
158 {
159 throw new NotImplementedException("bink texture creation has not been implemented");
160 }
161
162 private void GetPixelBytes()
163 {
164 Color[] _ColorData;
165 this.PixelBytesCount = _texture.Width * _texture.Height;
166 MemoryStream ms = new MemoryStream();
167 BinaryWriter w = new BinaryWriter(ms);
168 switch ((SurfaceFormat)this.TextureRawPixelFormat)
169 {
170 case SurfaceFormat.Color:
171 case SurfaceFormat.Bgr24:
172 case SurfaceFormat.Bgr32:
173 case SurfaceFormat.Rgb32:
174 case SurfaceFormat.Rgba32:
175 _ColorData = new Color[this.PixelBytesCount];
176 _texture.GetData(_ColorData);
177 this.PixelBytes = new byte[this.PixelBytesCount];
178 ms = new MemoryStream(this.PixelBytesCount);
179 for (int i = 0; i < _ColorData.Length; i++)
180 {
181 Microsoft.Xna.Framework.Graphics.Color _color = new Microsoft.Xna.Framework.Graphics.Color(_ColorData[i].ToVector4());
182 // write value in format: RGB, because the source is BGR
183 Color _repack = new Color((float)_color.B / 255, (float)_color.G / 255, (float)_color.R / 255, (float)_color.A / 255);
184 w.Write(_repack.PackedValue);
185 }
186 w.Flush();
187 this.PixelBytes = ms.ToArray();
188 w.Close();
189 break;
190 default:
191 throw new InvalidOperationException("Attempted to read pixel data for a non-supported pixel format.");
192 }
193 }
194 #endregion
195
196 #region RessourceHandler for Bink Textures
197 public class ResourceHandler : ATextureResourceHandler, IBindableTexture
198 {
199 #region IBindableTexture Members
200 public bool IsBindable { get { return bink_texture_creator.IsBindable; } }
201 #endregion
202 public ResourceHandler()
203 {
204 // add magic values for each supported texture type
205 this.Add(typeof(bink_texture_creator), new List<string>(new string[] { "*" }));
206 }
207 }
208 #endregion
209 }
210 }

  ViewVC Help
Powered by ViewVC 1.1.22