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

Contents of /trunk/gr2lib_typedefs/typedefs/TextureCreator/ATextureResource.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: 11331 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 System.Windows.Forms;
5 using gr2lib_typedefs.typedefs.TextureCreator.interfaces;
6 using System.ComponentModel;
7 using gr2lib.core.ui.typeeditors;
8 using System.Drawing.Design;
9 using System.IO;
10 using gr2lib.core.ui.helpers;
11 using gr2lib.core.typedefs;
12
13 namespace gr2lib_typedefs.typedefs.TextureCreator
14 {
15 public abstract class ATextureResource : UserControl,IBaseTextureResource, IDisposable
16 {
17 //#region Granny2 Support
18 //private IntPtr _TextureBuilder = IntPtr.Zero;
19 //public IntPtr TextureBuilder { get { return _TextureBuilder; } set { _TextureBuilder = value; } }
20 //private IntPtr _ResultingTexturePointer = IntPtr.Zero;
21 //public IntPtr ResultingTexturePointer { get { return _ResultingTexturePointer; } set { _ResultingTexturePointer = value; } }
22 //private gr2lib.core.coretypes.implementation.Layout _ResultingTextureLayout = new gr2lib.core.coretypes.implementation.Layout();
23 //public gr2lib.core.coretypes.implementation.Layout ResultingTextureLayout { get { return _ResultingTextureLayout; } set { _ResultingTextureLayout = value; } }
24 //#endregion
25
26 #region IDisposable Support
27 public new void Dispose()
28 {
29 this.UnParse();
30 }
31 #endregion
32
33 #region Constructors
34 protected ATextureResource() { }
35 protected ATextureResource(MemoryStream ms) { if (ms == null) { stream = this.UnParse(); } else { stream = ms; this.Parse(); } }//this.SourceType = DataSourceType.memorystream; }
36 protected ATextureResource(byte[] data) : this(new MemoryStream(data)) { }//this.SourceType = DataSourceType.bytearray; }
37 protected ATextureResource(string filename) : this(new BinaryFileReader(filename).Stream) { this.filename = filename; }//this.SourceType = DataSourceType.file; }
38
39 //private void init()
40 //{
41 // this.TextureName = new FilePath();
42 // this.TextureWidth = 0;
43 // this.TextureHeight = 0;
44 // this.TextureBytesPerPixel = 0;
45 // this.TextureMipMapCount = 0;
46 // this.TextureRawPixelFormat = System.Drawing.Imaging.PixelFormat.Undefined;
47 // this.TextureSpecificPixelFormat = new object();
48 // //this.AsBytes = new byte[this.AsBytesCount];
49 // //this.SourceType = DataSourceType.undefined;
50 // this.HaveBindableTexture = false;
51 // #region Setup Granny2 Support
52 // this.TextureBuilder = IntPtr.Zero;
53 // this.ResultingTexturePointer = IntPtr.Zero;
54 // #endregion
55
56 // //// reset Stream Position
57 // //this.Stream.Position = 0;
58 // //this.Stream.Seek(0, SeekOrigin.Begin);
59 //}
60 #endregion
61
62 #region Attributes
63 /// <summary>
64 /// Resource data stream
65 /// </summary>
66 protected MemoryStream stream = null;
67 public virtual string TextureCreatorName { get { return "Unknown Texture Creator"; } }
68 #endregion
69
70 #region ITextureResource Members
71 /// <summary>
72 /// The resource content as a Stream
73 /// </summary>
74 public virtual MemoryStream Stream { get { return stream; } }
75
76 protected byte[] _PixelBytes = new byte[] { };
77 public virtual byte[] PixelBytes
78 {
79 get { return _PixelBytes; }
80 set { if (value == null) { _PixelBytes = new byte[] { }; } else { _PixelBytes = value; } }
81 }
82
83 public IntPtr AsBytesPointer { get { unsafe { fixed (byte* p = this.AsBytes) { return (IntPtr)p; } } } }
84 public IntPtr PixelBytesPointer { get { unsafe { fixed (byte* p = this.PixelBytes) { return (IntPtr)p; } } } }
85
86 private int _AsBytesCount = 0;
87 public int AsBytesCount
88 {
89 get { return _AsBytesCount; }
90 set { _AsBytesCount = value; }
91 }
92 private int _PixelBytesCount = 0;
93 public int PixelBytesCount
94 {
95 get { return _PixelBytesCount; }
96 set { _PixelBytesCount = value; }
97 }
98 //protected abstract bool IsAnIBindableTexture { get; }
99 #region IBindableTexture Members
100 public static bool IsBindable { get { return false; } }
101 #endregion
102 //protected DataSourceType _SourceType;
103 //protected DataSourceType SourceType { get { return _SourceType; } set { _SourceType = value; } }
104
105 //public virtual granny_texture_encoding EncodingType { get { return granny_texture_encoding.GrannyUserTextureEncoding; } }
106
107 /// <summary>
108 /// The resource content as a byte array
109 /// </summary>
110 //[RefreshPropertiesAttribute(RefreshProperties.All)]
111 [Editor(typeof(HexEditor), typeof(UITypeEditor))]
112 public virtual byte[] AsBytes
113 {
114 get
115 {
116 stream.Position = 0;
117 stream.Seek(0, SeekOrigin.Begin);
118 byte[] _bytes = new BinaryReader(this.Stream).ReadBytes((int)this.Stream.Length);
119 return _bytes;
120 }
121 set
122 {
123 this.stream = new MemoryStream(value);
124 BinaryWriter w = new BinaryWriter(this.Stream);
125 w.Seek(0, SeekOrigin.Begin);
126 for (int i = 0; i < value.Length; i++)
127 {
128 w.Write(value[i]);
129 }
130 w.Flush();
131 }
132 }
133 #endregion
134
135 #region Abstract Data I/O
136 protected abstract void Parse();
137 protected abstract MemoryStream UnParse();
138
139 #region IBindableTexture Members
140 //public abstract bool CheckForBindableTexture(string filename);
141 //public abstract bool CheckForBindableTexture(byte[] data);
142 //public abstract bool CheckForBindableTexture(MemoryStream ms);
143 private bool _HaveBindableTexture;
144 public bool HaveBindableTexture { get { return _HaveBindableTexture; } set { _HaveBindableTexture = value; } }
145 #endregion
146 #endregion
147
148 #region Sub-Classes
149 protected internal class BinaryFileReader
150 {
151 public BinaryFileReader(string filename)
152 {
153 FileStream _fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
154 BinaryReader r = new BinaryReader(_fs);
155 r.BaseStream.Seek(0, SeekOrigin.Begin);
156 ms = new MemoryStream(r.ReadBytes((int)r.BaseStream.Length));
157 _fs.Close();
158 }
159 private MemoryStream ms;
160 public MemoryStream Stream
161 {
162 get { return ms; }
163 }
164 }
165 protected internal class BinaryMagicReader : IBinaryMagicReader
166 {
167
168 public BinaryMagicReader(MemoryStream ms)
169 {
170 this.stream = ms;
171 this.ReadMagic();
172 }
173 public BinaryMagicReader(MemoryStream ms, int MagicSize)
174 {
175 this.stream = ms;
176 this.MagicSize = MagicSize;
177 this.ReadMagic();
178 }
179
180 private void ReadMagic()
181 {
182 if (stream == null) throw new InvalidOperationException("Attempted to read the magic value from a null stream.");
183 BinaryReader r = new BinaryReader(this.stream);
184 r.BaseStream.Seek(0, SeekOrigin.Begin);
185 _MagicBytes = r.ReadBytes(this.MagicSize);
186 }
187
188 private MemoryStream stream = null;
189 private int MagicSize = sizeof(uint);
190 /// <summary>
191 /// Returns MagicBytes as a string prefixed with "0x"
192 /// </summary>
193 public string Magic
194 {
195 get { return string.Format("0x{0}",this.BytesToString()); }
196 }
197
198 private string BytesToString()
199 {
200 string ret = "";
201 for (int i = 0; i < this.MagicBytes.Length; i++)
202 {
203 ret = ret + this.MagicBytes[i].ToString("X2");
204 }
205 return ret;
206 }
207
208 private byte[] _MagicBytes = new byte[] { };
209 /// <summary>
210 /// Returns the bytes that make up the magic
211 /// </summary>
212 public byte[] MagicBytes
213 {
214 get { return _MagicBytes; }
215 }
216 /// <summary>
217 /// Returns Magic bytes as char array
218 /// </summary>
219 public string MagicChars
220 {
221 get { return string.Format("{0}", System.Text.ASCIIEncoding.ASCII.GetString(this.MagicBytes)); }
222 }
223 }
224
225 //protected enum DataSourceType
226 //{
227 // undefined,
228 // file,
229 // bytearray,
230 // memorystream
231 //}
232 #endregion
233
234
235 #region IBaseTextureResource Members
236
237 private ATextureTypeData _NativeTextureData = new ATextureTypeData();
238 public ATextureTypeData NativeTextureData { get { return _NativeTextureData; } set { _NativeTextureData = value; } }
239
240 //protected granny_texture_encoding _TextureEncodingType = granny_texture_encoding.GrannyUserTextureEncoding;
241 //public granny_texture_encoding TextureEncodingType { get { return _TextureEncodingType; } set { _TextureEncodingType = value; } }
242 protected string filename = null;
243 protected TextureCreators _TextureCreatorType = TextureCreators.undefined;
244 public TextureCreators TextureCreatorType { get { return _TextureCreatorType; } set { _TextureCreatorType = value; } }
245
246 private FilePath _Name = null;
247 public FilePath TextureName { get { return _Name; } set { _Name = value; } }
248 private int _Width = 0;
249 public int TextureWidth { get { return _Width; } set { _Width = value; } }
250 private int _Height = 0;
251 public int TextureHeight { get { return _Height; } set { _Height = value; } }
252 public int TextureBytesPerPixel { get { return this.TextureBitsPerPixel / 8; } set { this.TextureBitsPerPixel = (value * 8); } }
253 private int _BitsPerPixel = 0;
254 public int TextureBitsPerPixel { get { return _BitsPerPixel; } set { _BitsPerPixel = value; } }
255
256 public int TextureStride { get { return this.TextureWidth * this.TextureBytesPerPixel; } }
257
258 private int _MipMapCount = 0;
259 public int TextureMipMapCount { get { return _MipMapCount; } set { _MipMapCount = value; } }
260
261 private object _RawPixelFormat = new object();
262 public object TextureRawPixelFormat { get { return _RawPixelFormat; } set { _RawPixelFormat = value; } }
263 private granny_pixel_layout _TexturePixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat;
264 public granny_pixel_layout TextureSpecificPixelFormat { get { return _TexturePixelFormat; } set { _TexturePixelFormat = value; } }
265 #endregion
266 }
267 }

  ViewVC Help
Powered by ViewVC 1.1.22