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

Contents of /trunk/gr2lib_typedefs/typedefs/TextureCreator/default_texture_creator.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: 8556 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 //#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 System.Drawing.Imaging;
13 using System.Drawing;
14
15 namespace gr2lib_typedefs.typedefs.TextureCreator
16 {
17 public class default_texture_creator : ATextureResource
18 {
19 //public override granny_texture_encoding EncodingType { get { return granny_texture_encoding.GrannyRawTextureEncoding; } }
20
21 public override string TextureCreatorName { get { return "Default Texture Creator"; } }
22 #region Constructors
23 public default_texture_creator() : base() { }
24 public default_texture_creator(string filename) : base(filename) { }
25 public default_texture_creator(byte[] data) : base(data) { }
26 public default_texture_creator(MemoryStream ms) : base(ms) { }
27 #region IBindableTexture Members
28 public new static bool IsBindable { get { return true; } }
29 #endregion
30 #endregion
31
32 #region ATextureResource Members
33 protected override void Parse()
34 {
35 try
36 {
37 //throw new NotImplementedException("default texture creation has not been implemented");
38 this.Stream.Seek(0, SeekOrigin.Begin);
39 System.Drawing.Image _image = null;
40 this.TextureName = new FilePath(this.filename);
41
42 try
43 {
44 _image = System.Drawing.Image.FromStream(this.Stream);
45 }
46 catch (Exception ex)
47 {
48 if (this.TextureName.FullPath == "no filename given") throw new InvalidOperationException("Unable to open the image", ex);
49 throw new InvalidOperationException("Unable to open the image: " + this.TextureName.Name, ex);
50 }
51
52 this.TextureWidth = _image.Width;
53 this.TextureHeight = _image.Height;
54 this.TextureBitsPerPixel = System.Drawing.Image.GetPixelFormatSize(_image.PixelFormat);
55 this.TextureMipMapCount = 0;
56 this.TextureRawPixelFormat = _image.PixelFormat;
57
58 this.PixelBytes = new byte[]{0};
59
60
61
62 try
63 {
64 Bitmap bmp = new Bitmap(_image);
65 BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, _image.PixelFormat);
66 this.PixelBytesCount = bmpData.Stride * bmp.Height;
67 this.PixelBytes = new byte[PixelBytesCount];
68 // Copy the RGB values into the array.
69 Marshal.Copy(bmpData.Scan0, this.PixelBytes, 0, this.PixelBytesCount);
70 bmp.UnlockBits(bmpData);
71 }
72 catch (Exception ex)
73 {
74 throw new InvalidOperationException("Unable to read the opened image's rbga data", ex);
75 }
76
77 // get pixel format
78 switch ((PixelFormat)this.TextureRawPixelFormat)
79 {
80 case PixelFormat.Format16bppArgb1555:
81 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyBGRA5551PixelFormat;
82 break;
83 case PixelFormat.Format16bppRgb555:
84 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyBGR555PixelFormat;
85 break;
86 case PixelFormat.Format16bppRgb565:
87 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyBGR565PixelFormat;
88 break;
89 case PixelFormat.Format24bppRgb:
90 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyBGR888PixelFormat;
91 break;
92 case PixelFormat.Format32bppArgb:
93 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyBGRA8888PixelFormat;
94 break;
95 case PixelFormat.Format32bppRgb:
96 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyBGRA8888PixelFormat;
97 break;
98 case PixelFormat.Undefined:
99 default:
100 this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat;
101 throw new InvalidOperationException("Attempted to read an image with an unsupported pixel format.");
102 }
103 // get layout
104 this.NativeTextureData.NativeTextureLayout = granny_pixel_layouts.GetPixelLayoutFromEnum(this.TextureSpecificPixelFormat);
105
106 // create Granny2 Texture
107 this.NativeTextureData.NativeTextureBuilder = coreapi.TextureSupport.GrannyBeginRawTexture(this.TextureWidth, this.TextureHeight, this.NativeTextureData.NativeTextureLayout.NativePointer, this.TextureStride);
108 if (this.NativeTextureData.NativeTextureBuilder == IntPtr.Zero) throw new InvalidOperationException("Cannot process texure as the pointer to the Texture_Builder is null");
109 coreapi.TextureSupport.GrannyEncodeImage(this.NativeTextureData.NativeTextureBuilder, this.TextureWidth, this.TextureHeight, this.TextureStride, this.TextureMipMapCount, this.PixelBytesPointer);
110 IntPtr ResultingTextureSizePTR = Marshal.AllocHGlobal(coreapi.TextureSupport.GrannyGetResultingTextureSize(this.NativeTextureData.NativeTextureBuilder));
111 this.NativeTextureData.NativeTexturePointer = coreapi.TextureSupport.GrannyEndTextureInPlace(this.NativeTextureData.NativeTextureBuilder, ResultingTextureSizePTR);
112 if (this.NativeTextureData.NativeTexturePointer == IntPtr.Zero) throw new InvalidOperationException("The texture that was created, has a null pointer");
113
114
115
116 this.TextureCreatorType = TextureCreators._default;
117 this.NativeTextureData.NativeTextureEncoding = new gr2lib.core.helpers.NameValuePair<string, granny_texture_encoding>(
118 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyRawTextureEncoding), granny_texture_encoding.GrannyRawTextureEncoding);
119 this.NativeTextureData.NativeTextureSubFormat = new gr2lib.core.helpers.NameValuePair<string, granny_pixel_layout>(
120 coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>(this.TextureSpecificPixelFormat), this.TextureSpecificPixelFormat);
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 this.HaveBindableTexture = true;
124 }
125 catch (Exception ex)
126 {
127 this.HaveBindableTexture = false;
128 //Debug.WriteLine(ex.ToString());
129 throw new InvalidOperationException("Failed to read texture using: " + new default_texture_creator().TextureCreatorName, ex);
130 }
131
132 }
133 protected override MemoryStream UnParse()
134 {
135 throw new NotImplementedException("default texture creation has not been implemented");
136 }
137 #endregion
138
139
140
141 #region RessourceHandler for Default Textures
142 public class ResourceHandler : ATextureResourceHandler, IBindableTexture
143 {
144 #region IBindableTexture Members
145 public bool IsBindable { get { return default_texture_creator.IsBindable; } }
146 #endregion
147 public ResourceHandler()
148 {
149 // add magic values for each supported texture type
150 this.Add(typeof(default_texture_creator), new List<string>(new string[] { "*" }));
151 }
152 }
153 #endregion
154 }
155 }

  ViewVC Help
Powered by ViewVC 1.1.22