/[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 36 - (show annotations) (download)
Thu Aug 5 19:00:37 2010 UTC (9 years, 6 months ago) by william
File size: 5403 byte(s)
Add classes in an attempt to create an intelligent texture creator/importer/exporter/parser/etc,etc

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
12 namespace gr2lib_typedefs.typedefs.TextureCreator
13 {
14 public abstract class ATextureResource : ITextureResource, IBaseTextureResource, IDisposable
15 {
16 #region IDisposable Support
17 public void Dispose()
18 {
19 this.UnParse();
20 }
21 #endregion
22
23 #region Constructors
24 protected ATextureResource() { }
25 protected ATextureResource(MemoryStream ms) { stream = ms; this.init(); this.Parse(); }
26 protected ATextureResource(byte[] data) : this(new MemoryStream(data)) { }
27 protected ATextureResource(string filename) : this(new BinaryFileReader(filename).Stream) { }
28
29 private void init()
30 {
31 this.TextureName = new FilePath();
32 this.TextureWidth = 0;
33 this.TextureHeight = 0;
34 this.TextureBytesPerPixel = 0;
35 this.TextureMipMapCount = 0;
36 this.TextureRawPixelFormat = System.Drawing.Imaging.PixelFormat.Undefined;
37 this.TextureSpecificPixelFormat = new object();
38 this.AsBytes = new byte[this.AsBytesCount];
39 }
40 #endregion
41
42 #region Attributes
43 /// <summary>
44 /// Resource data stream
45 /// </summary>
46 protected MemoryStream stream = null;
47 protected virtual string TextureCreatorName { get { return "Unknown Texture Creator"; } }
48 #endregion
49
50 #region ITextureResource Members
51 /// <summary>
52 /// The resource content as a Stream
53 /// </summary>
54 public virtual MemoryStream Stream { get { return stream; } }
55 public IntPtr AsBytesPointer { get { unsafe { fixed (byte* p = this.AsBytes) { return (IntPtr)p; } } } }
56 public int AsBytesCount
57 {
58 get { if (this.AsBytes != null) return this.AsBytes.Length; return 0; }
59 //private set { _PixelBytesCount = value; }
60 }
61 protected bool _IsAnIBindableTexture;
62 protected virtual bool IsAnIBindableTexture { get { return _IsAnIBindableTexture; } set { _IsAnIBindableTexture = value; } }
63 /// <summary>
64 /// The resource content as a byte array
65 /// </summary>
66 //[RefreshPropertiesAttribute(RefreshProperties.All)]
67 [Editor(typeof(HexEditor), typeof(UITypeEditor))]
68 public virtual byte[] AsBytes
69 {
70 get
71 {
72 stream.Position = 0;
73 byte[] _bytes = new BinaryReader(this.Stream).ReadBytes((int)this.Stream.Length);
74 return _bytes;
75 }
76 set
77 {
78 BinaryWriter w = new BinaryWriter(this.Stream);
79 w.Seek(0, SeekOrigin.Begin);
80 for (int i = 0; i < value.Length; i++)
81 {
82 w.Write(value[i]);
83 }
84 w.Flush();
85 }
86 }
87 #endregion
88
89 #region Abstract Data I/O
90 protected abstract void Parse();
91 protected abstract void UnParse();
92 #endregion
93
94 #region Sub-Classes
95 private class BinaryFileReader
96 {
97 public BinaryFileReader(string filename)
98 {
99 FileStream _fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
100 BinaryReader r = new BinaryReader(_fs);
101 r.BaseStream.Seek(0, SeekOrigin.Begin);
102 ms = new MemoryStream(r.ReadBytes((int)r.BaseStream.Length));
103 _fs.Close();
104 }
105 private MemoryStream ms;
106 public MemoryStream Stream
107 {
108 get { return ms; }
109 }
110 }
111 #endregion
112
113
114 #region IBaseTextureResource Members
115 private FilePath _Name;
116 public FilePath TextureName { get { return _Name; } private set { _Name = value; } }
117 private int _Width;
118 public int TextureWidth { get { return _Width; } private set { _Width = value; } }
119 private int _Height;
120 public int TextureHeight { get { return _Height; } private set { _Height = value; } }
121 public int TextureBytesPerPixel { get { return this.TextureBitsPerPixel / 8; } private set { this.TextureBitsPerPixel = (value * 8); } }
122 private int _BitsPerPixel;
123 public int TextureBitsPerPixel { get { return _BitsPerPixel; } private set { _BitsPerPixel = value; } }
124
125 public int TextureStride { get { return this.TextureWidth * this.TextureBytesPerPixel; } }
126
127 private int _MipMapCount;
128 public int TextureMipMapCount { get { return _MipMapCount; } private set { _MipMapCount = value; } }
129
130 private object _RawPixelFormat;
131 public object TextureRawPixelFormat { get { return _RawPixelFormat; } private set { _RawPixelFormat = value; } }
132 private object _TexturePixelFormat;
133 public object TextureSpecificPixelFormat { get { return _TexturePixelFormat; } private set { _TexturePixelFormat = value; } }
134 #endregion
135 }
136 }

  ViewVC Help
Powered by ViewVC 1.1.22