/[Sims3RigEditor]/trunk/gr2lib_typedefs/typedefs/CustomTextureCreator.cs
ViewVC logotype

Contents of /trunk/gr2lib_typedefs/typedefs/CustomTextureCreator.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations) (download)
Thu Aug 5 12:59:04 2010 UTC (9 years, 6 months ago) by william
File size: 11306 byte(s)
bulk commit - continue update to support s3tc texture import (DDS)

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using gr2lib_typedefs.interfaces;
5 using gr2lib.core.ui.helpers;
6 using System.Runtime.InteropServices;
7 using System.Diagnostics;
8 using System.IO;
9 using gr2lib.core.typedefs;
10 using gr2lib.core;
11 using System.Drawing.Imaging;
12 using Microsoft.Xna.Framework.Graphics;
13 using System.Windows.Forms;
14 using Microsoft.Xna.Framework.Graphics.PackedVector;
15
16 namespace gr2lib_typedefs.typedefs
17 {
18 public class CustomTextureCreator : UserControl, ICustomTextureCreator
19 {
20 #region Constructors
21 public CustomTextureCreator()
22 {
23 this.init();
24 }
25 public CustomTextureCreator(string filename)
26 {
27 this.init();
28 this.Name = new FilePath(filename);
29
30 this.GetImageType();
31 this.GetImageData();
32 }
33 #endregion
34
35 #region Constructor Initializer
36 private void init()
37 {
38 this.Name = new FilePath();
39 this.Width = 0;
40 this.Height = 0;
41 this.BytesPerPixel = 0;
42 this.MipMapCount = 1;
43 this.RawPixelFormat = System.Drawing.Imaging.PixelFormat.Undefined;
44 this.TexturePixelFormat = new object();
45 this.ImageType = TextureCreator_ImageType.Undefined;
46 //this.SubImageType = TextureCreator_SubImageType.Undefined;
47 this.PixelBytesCount = 0;
48 this.PixelBytes = new byte[this.PixelBytesCount];
49
50 //this.IsST3C = false;
51
52 // initialize private members
53 //this._ddsFile = new DdsFileTypePlugin.DdsFile();
54 this._image = null;
55
56 // Create Presentation Parameters
57 PresentationParameters pp = new PresentationParameters();
58 pp.BackBufferCount = 1;
59 pp.IsFullScreen = false;
60 pp.SwapEffect = SwapEffect.Discard;
61 pp.BackBufferWidth = this.Width;
62 pp.BackBufferHeight = this.Height;
63 pp.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8;
64 pp.EnableAutoDepthStencil = true;
65 pp.PresentationInterval = PresentInterval.Default;
66 pp.BackBufferFormat = SurfaceFormat.Unknown;
67 pp.MultiSampleType = MultiSampleType.None;
68 // Create Graphics device
69 this.mGraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, this.Handle, pp);
70
71 }
72 #endregion
73
74 #region Private Members
75 //private DdsFileTypePlugin.DdsFile _ddsFile;
76 GraphicsDevice mGraphicsDevice;
77 private Texture2D _texture;
78 private System.Drawing.Image _image;
79 #endregion
80
81 #region Texture Initialization Methods
82 #region private void GetImageType()
83 private void GetImageType()
84 {
85 try
86 {
87 this._texture = Texture2D.FromFile(mGraphicsDevice, this.Name.FullPath);
88
89 this.Width = this._texture.Width;
90 this.Height = this._texture.Height;
91
92 this.RawPixelFormat = this._texture.Format;
93 this.TexturePixelFormat = new object();
94 switch (this._texture.Format)
95 {
96 case SurfaceFormat.Color:
97 Color[] _color_bytes = new Color[this._texture.Width * this._texture.Height];
98 this.mGraphicsDevice.SetRenderTarget(0, null);
99 this._texture.GetData<Color>(_color_bytes);
100
101 this.PixelBytes = new byte[this._texture.Width * this._texture.Height * 4];
102 this.BytesPerPixel = 4;
103 for (int i = 0; i < _color_bytes.Length; i++)
104 {
105 this.PixelBytes[i + 0] = _color_bytes[i].R;
106 this.PixelBytes[i + 1] = _color_bytes[i].G;
107 this.PixelBytes[i + 2] = _color_bytes[i].B;
108 this.PixelBytes[i + 3] = _color_bytes[i].A;
109 i += 4;
110 }
111 break;
112 case SurfaceFormat.Bgr565:
113 this.TexturePixelFormat = granny_s3tc_texture_format.GrannyS3TCBGR565;
114 Bgr565[] _bgr565_bytes = new Bgr565[this._texture.Width * this._texture.Height];
115 this.mGraphicsDevice.SetRenderTarget(0, null);
116 this._texture.GetData<Bgr565>(_bgr565_bytes);
117
118 this.PixelBytes = new byte[this._texture.Width * this._texture.Height * 2];
119 this.BytesPerPixel = 2;
120 for (int i = 0; i < _bgr565_bytes.Length; i++)
121 {
122 byte[] data = new byte[this.BytesPerPixel];
123 data = BitConverter.GetBytes(_bgr565_bytes[i].PackedValue);
124 this.PixelBytes[i + 0] = data[1];
125 this.PixelBytes[i + 1] = data[0];
126 i += 2;
127 }
128 break;
129 case SurfaceFormat.Bgra5551:
130 this.TexturePixelFormat = granny_s3tc_texture_format.GrannyS3TCBGRA5551;
131 Bgra5551[] _bgra5551_bytes = new Bgra5551[this._texture.Width * this._texture.Height];
132 this.mGraphicsDevice.SetRenderTarget(0, null);
133 this._texture.GetData<Bgra5551>(_bgra5551_bytes);
134
135 this.PixelBytes = new byte[this._texture.Width * this._texture.Height * 4];
136 this.BytesPerPixel = 4;
137 for (int i = 0; i < _bgra5551_bytes.Length; i++)
138 {
139 this.PixelBytes[i + 0] = (byte)_bgra5551_bytes[i].ToVector4().X;
140 this.PixelBytes[i + 1] = (byte)_bgra5551_bytes[i].ToVector4().Y;
141 this.PixelBytes[i + 2] = (byte)_bgra5551_bytes[i].ToVector4().Z;
142 this.PixelBytes[i + 3] = (byte)_bgra5551_bytes[i].ToVector4().W;
143 }
144 break;
145 break;
146 }
147
148 }
149 catch
150 {
151 //this.ImageType = TextureCreator_ImageType.Undefined;
152 }
153 }
154 #endregion
155
156 #region private void GetImageData()
157 private void GetImageData()
158 {
159 //this.Width = _image.Width;
160 //this.Height = _image.Height;
161 //this.BytesPerPixel = System.Drawing.Image.GetPixelFormatSize(_image.PixelFormat) / 8;
162 //this.MipMapCount = 1;
163
164 //this.RawPixelFormat = _image.PixelFormat;
165 //this.TexturePixelFormat = new object();
166
167 //this.PixelBytes = new byte[this.Width * this.Height * this.BytesPerPixel];
168 //try { using (MemoryStream ms = new MemoryStream(this.Width * this.Height * this.BytesPerPixel)) { _image.Save(ms, ImageFormat.Png); this.PixelBytes = ms.ToArray(); } }
169 //catch (Exception) { throw; }
170
171 //// get Texure Pixel Format;
172 //if ((PixelFormat)this.RawPixelFormat == PixelFormat.Format16bppRgb555)
173 //{
174 // this.TexturePixelFormat = granny_pixel_layout.GrannyRGB555PixelFormat;
175 //}
176 //else if ((PixelFormat)this.RawPixelFormat == PixelFormat.Format16bppRgb565)
177 //{
178 // this.TexturePixelFormat = granny_pixel_layout.GrannyRGB565PixelFormat;
179 //}
180 //else if ((PixelFormat)this.RawPixelFormat == PixelFormat.Format16bppArgb1555)
181 //{
182 // this.TexturePixelFormat = granny_pixel_layout.GrannyRGBA5551PixelFormat;
183 //}
184
185 ////GrannyRGBA4444PixelFormat
186 ////else if ((PixelFormat)this.RawPixelFormat == PixelFormat.Format16bppArgb1555)
187 ////{
188 //// this.TexturePixelFormat = granny_pixel_layout.GrannyRGBA5551PixelFormat;
189 ////}
190
191 //else if ((PixelFormat)this.RawPixelFormat == PixelFormat.Format24bppRgb)
192 //{
193 // this.TexturePixelFormat = granny_pixel_layout.GrannyRGB888PixelFormat;
194 //}
195 //else if ((PixelFormat)this.RawPixelFormat == PixelFormat.Format32bppArgb)
196 //{
197 // this.TexturePixelFormat = granny_pixel_layout.GrannyRGBA8888PixelFormat;
198 //}
199 //else
200 //{
201 // this.TexturePixelFormat = new object();
202 //}
203
204 }
205 #endregion
206 #endregion
207 #region ICustomTextureCreator Members
208
209 #region ICustomTextureCreatorTextureData Members
210 private FilePath _Name;
211 public FilePath Name { get { return _Name; } private set { _Name = value; } }
212 private int _Width;
213 public int Width { get { return _Width; } private set { _Width = value; } }
214 private int _Height;
215 public int Height { get { return _Height; } private set { _Height = value; } }
216 private int _BytesPerPixel;
217 public int BytesPerPixel { get { return _BytesPerPixel; } private set { _BytesPerPixel = value; } }
218
219 public int Stride { get { return this.Width * this.BytesPerPixel; } }
220
221 private int _MipMapCount;
222 public int MipMapCount { get { return _MipMapCount; } private set { _MipMapCount = value; } }
223
224 private object _RawPixelFormat;
225 public object RawPixelFormat { get { return _RawPixelFormat; } private set { _RawPixelFormat = value; } }
226 private object _TexturePixelFormat;
227 public object TexturePixelFormat { get { return _TexturePixelFormat; } private set { _TexturePixelFormat = value; } }
228
229 private TextureCreator_ImageType _ImageType;
230 public TextureCreator_ImageType ImageType { get { return _ImageType; } private set { _ImageType = value; } }
231
232 //private TextureCreator_SubImageType _SubImageType;
233 //public TextureCreator_SubImageType SubImageType { get { return _SubImageType; } private set { _SubImageType = value; } }
234 #endregion
235
236 #region ICustomTextureCreatorPixelData Members
237 private int _PixelBytesCount;
238 public int PixelBytesCount { get { return _PixelBytesCount; } private set { _PixelBytesCount = value; } }
239 public IntPtr PixelBytesPointer { get { unsafe { fixed (byte* p = this.PixelBytes) { return (IntPtr)p; } } } }
240 private byte[] _PixelBytes;
241 public byte[] PixelBytes { get { return _PixelBytes; } private set { _PixelBytes = value; } }
242 #endregion
243
244 #endregion
245
246 //#region ICustomTextureCreatorTextureType Members
247 //private bool _IsST3C;
248 //public bool IsST3C { get { return _IsST3C; } private set { _IsST3C = value; } }
249 //#endregion
250
251 //#region Private Sub-Classes
252 //#endregion
253
254 }
255 }

  ViewVC Help
Powered by ViewVC 1.1.22