/[Sims3RigEditor]/trunk/gr2lib_typedefs/typeeditors/PixelBytesEditor.cs
ViewVC logotype

Contents of /trunk/gr2lib_typedefs/typeeditors/PixelBytesEditor.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: 16338 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 SHOW_DEBUG_BUTTON // when defined, will display the Debug Button
2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Drawing;
6 using System.Data;
7
8 using System.Text;
9 using System.Windows.Forms;
10
11 using gr2lib.core.coretypes.implementation;
12 using System.IO;
13 using gr2lib.core.typedefs;
14 using System.Runtime.InteropServices;
15 using System.Drawing.Imaging;
16 using gr2lib.core.ui.helpers;
17 using gr2lib_typedefs.typedefs;
18 using System.Diagnostics;
19 using gr2lib_typedefs.typedefs.TextureCreator;
20 using gr2lib_typedefs.typedefs.TextureCreator.interfaces;
21
22 namespace gr2lib.core.ui.typeeditors
23 {
24 public partial class PixelBytesEditor : BaseEditorUser<List<Pixel>>
25 {
26 //private S3TCCustomTextureCreator s3tc_texture_creator;
27 /// <summary>
28 /// default constructor
29 /// </summary>
30 public PixelBytesEditor()
31 : base()
32 {
33 InitializeComponent();
34 }
35 /// <summary>
36 /// default constructor, specifying PixelBytes of type List of Pixel
37 /// </summary>
38 /// <param name="PixelBytes"></param>
39 public PixelBytesEditor(List<Pixel> PixelBytes)
40 : base(PixelBytes)
41 {
42 InitializeComponent();
43 }
44 /// <summary>
45 /// default constructor, specifying object of type List of Pixel
46 /// </summary>
47 /// <param name="PixelBytes"></param>
48 public PixelBytesEditor(object PixelBytes)
49 : base(PixelBytes)
50 {
51 InitializeComponent();
52 }
53
54
55
56 private void PixelBytesEditor_Load(object sender, EventArgs e)
57 {
58 btnViewImage.FlatStyle = FlatStyle.Standard;
59 btnImportImage.FlatStyle = FlatStyle.Standard;
60 btnSaveImage.FlatStyle = FlatStyle.Standard;
61 btnViewRaw.FlatStyle = FlatStyle.Standard;
62 }
63
64
65 #region VIEW RAW PIXELS SUPPORT
66 private void btnViewRaw_Click(object sender, EventArgs e)
67 {
68 // throw new NotImplementedException("View Raw Has Not Been Implemented");
69 IntPtr NativeTexturePointer = Value[0].NativeTexturePointer;
70 Texture NativeTexture = Value[0].NativeTexture;
71
72
73 //gr2lib.core.coretypes.implementation.Layout SourcePixelLayout = NativeTexture.Layout;
74 //byte[] buffer = new byte[NativeTexture.Width * NativeTexture.Height * NativeTexture.Layout.BytesPerPixel];
75 //bool TextureCopied = false;
76 //unsafe
77 //{
78 // int stride = NativeTexture.Width * NativeTexture.Layout.BytesPerPixel;
79
80 // fixed (byte* p = buffer)
81 // {
82 // IntPtr byte_ptr = (IntPtr)p;
83 // TextureCopied = coreapi.TextureSupport.GrannyCopyTextureImage(NativeTexturePointer,
84 // 0,
85 // 0,
86 // SourcePixelLayout.NativePointer,
87 // NativeTexture.Width,
88 // NativeTexture.Height,
89 // stride,
90 // byte_ptr);
91 // }
92 //}
93 List<byte> _bytes = new List<byte>();
94 foreach (Pixel p in this.Value) { _bytes.Add(p.UInt8); }
95 //foreach (byte b in _bytes) { _bytes.Add(b); }
96 //MessageBox.Show("Pixel Count: 0x" + this.Value.Count.ToString("X8"));
97 HexEditor _editor = new HexEditor(_bytes);
98 _editor.ShowDialog();
99
100
101
102 if (_editor.BytesChanged)
103 {
104 this.Value.Clear();
105 this.Value = new List<Pixel>();
106 foreach (byte b in _editor.Value)
107 {
108 Pixel p = new Pixel();
109 p.UInt8 = b;
110 p.NativeTexture = NativeTexture;
111 p.NativeTexturePointer = NativeTexturePointer;
112 this.Value.Add(p);
113 }
114 }
115
116
117
118
119 }
120 #endregion
121 #region VIEW IMAGE SUPPORT
122 private void btnViewImage_Click(object sender, EventArgs e)
123 {
124 // throw new NotImplementedException("View Image Has Not Been Implemented");
125 //MessageBox.Show("View Image Has Not Been Implemented", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
126 Bitmap image_data;
127 ExportTexture(false, out image_data);
128 if (image_data != null)
129 {
130 ImageViewer _viewer = new ImageViewer(Value[0].NativeTexture.FromFileName.Name, image_data, new Size(Value[0].NativeTexture.Width, Value[0].NativeTexture.Height));
131 _viewer.ShowDialog();
132 }
133 }
134 #endregion
135 #region IMAGE SAVE SUPPORT
136 private void btnSaveImage_Click(object sender, EventArgs e)
137 {
138 // throw new NotImplementedException("Image Save Has Not Been Implemented");
139 Bitmap null_image;
140 ExportTexture(true, out null_image);
141 }
142 #endregion
143 #region IMAGE IMPORT SUPPORT
144 private void btnImportImage_Click(object sender, EventArgs e)
145 {
146 IBinaryMagicReader _magic_reader = null;
147 FilePath _fs = new FilePath();
148 try
149 {
150 IntPtr NativeTexturePointer = Value[0].NativeTexturePointer;
151 Texture NativeTexture = Value[0].NativeTexture;
152 DialogResult result = TextureImporter.ShowDialog();
153 if (result != DialogResult.OK) return;
154 _fs = new FilePath(TextureImporter.FileName);
155 try
156 {
157 IBaseTextureResource _texture = TextureBinder.CreateNewTexture(_fs.FullPath, out _magic_reader);
158
159 //// do any handling that needs to be done on a per-texture-creator basis
160 //switch (_texture.TextureCreatorType)
161 //{
162 // case TextureCreators.s3tc:
163 // break;
164 // case TextureCreators.bink:
165 // break;
166 // case TextureCreators._default:
167 // break;
168 // default:
169 // throw new InvalidOperationException("Texture is bound to an undefined texture creator.");
170 //}
171
172
173 NativeTexture.Encoding = _texture.NativeTextureData.NativeTextureEncoding;
174 NativeTexture.SubFormat = _texture.NativeTextureData.NativeTextureSubFormat;
175 NativeTexture.Layout = _texture.NativeTextureData.NativeTextureLayout;
176 NativeTexture.NativePointer = _texture.NativeTextureData.NativeTexturePointer;
177
178 NativeTexturePointer = NativeTexture.NativePointer;
179 NativeTexture.Width = _texture.TextureWidth;
180 NativeTexture.Height = _texture.TextureHeight;
181 NativeTexture.FromFileName = _texture.TextureName;
182
183
184
185 ////gr2lib.core.coretypes.implementation.Layout SourcePixelLayout = NativeTexture.Layout;
186 ////bool TextureCopied = false;
187 ////unsafe
188 ////{
189 //// TextureCopied = coreapi.TextureSupport.GrannyCopyTextureImage(NativeTexturePointer,
190 //// 0,
191 //// 0,
192 //// coreapi.TextureSupport.GrannySwapRGBAToBGRA(NativeTexture.SubFormat.Value).NativePointer,
193 //// NativeTexture.Width,
194 //// NativeTexture.Height,
195 //// _s3tc_creator.TextureStride,
196 //// _s3tc_creator.AsBytesPointer);
197 ////}
198 ////if (!TextureCopied) return;
199
200
201 this.Value.Clear();
202 this.Value = new List<Pixel>();
203 foreach (byte _b in _texture.PixelBytes)
204 {
205 Pixel p = new Pixel();
206 p.UInt8 = _b;
207 p.NativeTexture = NativeTexture;
208 p.NativeTexturePointer = NativeTexturePointer;
209 this.Value.Add(p);
210 }
211 }
212 catch (KeyNotFoundException ex)
213 {
214 throw new InvalidOperationException("Attempted to import an unsupported image type.",ex);
215 }
216
217 MessageBox.Show("Succesfully Imported: " + _fs.Name, "Import Success:", MessageBoxButtons.OK, MessageBoxIcon.Information);
218 }
219 #region catch (Exception ex)
220 catch (Exception ex)
221 {
222 Debug.WriteLine(ex.ToString());
223 string Message = "";
224 string innermessage = "";
225 string OverrideMessage = "";
226 if (ex.InnerException != null)
227 {
228 innermessage = ex.InnerException.Message;
229 if (ex is InvalidOperationException && ex.InnerException is KeyNotFoundException)
230 {
231 string OverrideMessage_Message = "Please pass the following on to the application Author:\n\n";
232 //string OverrideMessage_Value = "";
233 if (!(_magic_reader == null))
234 {
235 OverrideMessage = string.Format("{0}Image Magic: {1}\nImage Type: {2}",
236 OverrideMessage_Message,
237 _magic_reader.Magic,
238 _magic_reader.MagicChars);
239 }
240 else
241 {
242 OverrideMessage = string.Format("{0}Image Magic: {1}\nImage Type: {2}",
243 OverrideMessage_Message,
244 "Uknown Magic Value",
245 "Unkown Image Type");
246 }
247 }
248 }
249 Message = string.Format("Failed to Import: {0}\n\nException - \n\tMessage: {1}\n\tInner Message: {2}\n{3}", _fs.Name, ex.Message, innermessage, OverrideMessage);
250 MessageBox.Show(Message, "Import Failure:", MessageBoxButtons.OK, MessageBoxIcon.Error);
251 }
252 #endregion
253 }
254 #endregion
255 #region Texture Export Support
256 private void ExportTexture(bool SaveFile, out Bitmap _image_data)
257 {
258 _image_data = null;
259 if (SaveFile)
260 {
261 DialogResult result = TextureExporter.ShowDialog();
262 if (result != DialogResult.OK) return;
263 }
264 //throw new NotImplementedException("Debug Has Not Been Implemented");
265 //MessageBox.Show("Debug Has Not Been Implemented", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
266 List<byte> _bytes = new List<byte>();
267 foreach (Pixel p in this.Value) { _bytes.Add(new byte()); }
268 byte[] __bytes = _bytes.ToArray();
269 IntPtr NativeTexturePointer = Value[0].NativeTexturePointer;
270 Texture NativeTexture = Value[0].NativeTexture;
271 gr2lib.core.coretypes.implementation.Layout SourcePixelLayout = NativeTexture.Layout;
272 //granny_pixel_layout SwappedLayout;
273 //coreapi.TextureSupport.GrannySwapRGBAToBGRA(NativeTexture.SubFormat.Value, out SourcePixelLayout, out SwappedLayout);
274
275 //SourcePixelLayout = NativeTexture.Layout;
276 //NativeTexture.Encoding.Value = granny_texture_encoding.GrannyRawTextureEncoding;
277
278 //string filename = @"exported_images\" + ReferenceTexture.FromFileName.Name;
279 bool TextureCopied = false;
280 byte[] buffer = new byte[NativeTexture.Width * NativeTexture.Height * NativeTexture.Layout.BytesPerPixel];
281
282 //SourcePixelLayout = NativeTexture.Layout;
283 //coreapi.TextureSupport.GrannyGetRecommendedPixelLayout(ReferenceTexture.NativePointer, SourcePixelLayout.NativePointer);
284 unsafe
285 {
286 int stride = NativeTexture.Width * NativeTexture.Layout.BytesPerPixel;
287
288 fixed (byte* p = buffer)
289 {
290 IntPtr byte_ptr = (IntPtr)p;
291 TextureCopied = coreapi.TextureSupport.GrannyCopyTextureImage(NativeTexturePointer,
292 0,
293 0,
294 SourcePixelLayout.NativePointer,
295 NativeTexture.Width,
296 NativeTexture.Height,
297 stride,
298 byte_ptr);
299 if (TextureCopied)
300 {
301 if (SaveFile)
302 {
303 unsafe
304 {
305 fixed (byte* ptr = buffer)
306 {
307
308 using (Bitmap image = new Bitmap(NativeTexture.Width, NativeTexture.Height, stride, PixelFormat.Format32bppArgb, new IntPtr(ptr)))
309 {
310 Bitmap output = new Bitmap(image);
311 output.Save(TextureExporter.FileName);
312 }
313 }
314 }
315 MessageBox.Show("Succesfully Exported: " + new FileInfo(TextureExporter.FileName).Name, "Image Export Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
316 }
317 else
318 {
319
320 unsafe
321 {
322 fixed (byte* ptr = buffer)
323 {
324
325 using (Bitmap image = new Bitmap(NativeTexture.Width, NativeTexture.Height, stride, PixelFormat.Format32bppArgb, new IntPtr(ptr)))
326 {
327 Bitmap output = new Bitmap(image);
328 _image_data = output;
329 }
330 }
331 }
332 }
333 }
334 else
335 {
336 if (SaveFile)
337 {
338 MessageBox.Show("Failed to Export: " + new FileInfo(TextureExporter.FileName).Name, "Image Export Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
339 }
340 else
341 {
342 MessageBox.Show("Was Unable to Load Image Data:", "Image Viewer", MessageBoxButtons.OK, MessageBoxIcon.Error);
343 _image_data = null;
344 }
345 }
346 }
347
348 }
349 }
350 #endregion
351 }
352 }

  ViewVC Help
Powered by ViewVC 1.1.22