/[gr2lib]/trunk/gr2lib/core/ui/typeeditors/PixelBytesEditor.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/ui/typeeditors/PixelBytesEditor.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 191 - (show annotations) (download)
Sat Jul 24 06:31:50 2010 UTC (9 years, 1 month ago) by william
File size: 10648 byte(s)
In coreapi:
*Add more texture methods
*Move everthing into related classes, to group related methods,etc
*Update all classes to use these grouped classes

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using gr2lib.core.ui.rigeditor.interfaces;
10 using gr2lib.core.coretypes.implementation;
11 using System.IO;
12 using gr2lib.core.typedefs;
13 using System.Runtime.InteropServices;
14 using System.Drawing.Imaging;
15 using gr2lib.core.ui.rigeditor;
16
17 namespace gr2lib.core.ui.typeeditors
18 {
19 public partial class PixelBytesEditor : BaseEditorUser<List<Pixel>>
20 {
21
22 /// <summary>
23 /// default constructor
24 /// </summary>
25 public PixelBytesEditor()
26 : base()
27 {
28 InitializeComponent();
29 }
30 /// <summary>
31 /// default constructor, specifying PixelBytes of type List of Pixel
32 /// </summary>
33 /// <param name="PixelBytes"></param>
34 public PixelBytesEditor(List<Pixel> PixelBytes)
35 : base(PixelBytes)
36 {
37 InitializeComponent();
38 }
39 /// <summary>
40 /// default constructor, specifying object of type List of Pixel
41 /// </summary>
42 /// <param name="PixelBytes"></param>
43 public PixelBytesEditor(object PixelBytes)
44 : base(PixelBytes)
45 {
46 InitializeComponent();
47 }
48
49
50
51 private void PixelBytesEditor_Load(object sender, EventArgs e)
52 {
53 btnViewImage.FlatStyle = FlatStyle.Standard;
54 btnDebugPixels.FlatStyle = FlatStyle.Standard;
55 }
56
57
58 #region VIEW RAW PIXELS SUPPORT
59 private void btnViewRaw_Click(object sender, EventArgs e)
60 {
61 // throw new NotImplementedException("View Raw Has Not Been Implemented");
62 List<byte> _bytes = new List<byte>();
63 foreach (Pixel p in this.Value) { _bytes.Add(p.UInt8); }
64 //MessageBox.Show("Pixel Count: 0x" + this.Value.Count.ToString("X8"));
65 HexEditor _editor = new HexEditor(_bytes);
66 _editor.ShowDialog();
67
68 Texture ReferenceTexture = Value[0].ReferenceTexture;
69
70 this.Value.Clear();
71 this.Value = new List<Pixel>();
72 foreach (byte b in _editor.Value)
73 {
74 Pixel p = new Pixel();
75 p.UInt8 = b;
76 p.ReferenceTexture = ReferenceTexture;
77 this.Value.Add(p);
78 }
79 }
80 #endregion
81
82 #region VIEW IMAGE SUPPORT
83 private void btnViewImage_Click(object sender, EventArgs e)
84 {
85 // throw new NotImplementedException("View Image Has Not Been Implemented");
86 //MessageBox.Show("View Image Has Not Been Implemented", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
87 Bitmap image_data;
88 ExportTexture(false, out image_data);
89 if (image_data != null)
90 {
91 ImageViewer _viewer = new ImageViewer(Value[0].ReferenceTexture.FromFileName.Name, image_data, new Size(Value[0].ReferenceTexture.Width, Value[0].ReferenceTexture.Height));
92 _viewer.ShowDialog();
93 }
94 }
95 #endregion
96
97 #region IMAGE SAVE SUPPORT
98 private void btnSaveImage_Click(object sender, EventArgs e)
99 {
100 // throw new NotImplementedException("Image Save Has Not Been Implemented");
101 Bitmap null_image;
102 ExportTexture(true, out null_image);
103 }
104 #endregion
105
106 #region IMAGE IMPORT SUPPORT
107 private void btnImportImage_Click(object sender, EventArgs e)
108 {
109 // throw new NotImplementedException("Image Import Has Not Been Implemented");
110 MessageBox.Show("Image Import Has Not Been Implemented", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
111 }
112 #endregion
113
114 #region DEBUG PIXELS SUPPORT
115 private void btnDebugPixels_Click(object sender, EventArgs e)
116 {
117 DialogResult result = TextureImporter.ShowDialog();
118 if (result != DialogResult.OK) return;
119
120 System.Drawing.Image _image = System.Drawing.Image.FromFile(TextureImporter.FileName);
121 Bitmap b = new Bitmap(_image);//note this has several overloads, including a path to an image
122 BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);
123 int bitsPerPixel = System.Drawing.Image.GetPixelFormatSize(b.PixelFormat);
124 /*This time we convert the IntPtr to a ptr*/
125 unsafe
126 {
127 List<byte> _byte_data_ = new List<byte>();
128 byte* scan0 = (byte*)bData.Scan0.ToPointer();
129 for (int i = 0; i < bData.Height; ++i)
130 {
131 for (int j = 0; j < bData.Width; ++j)
132 {
133 byte* data = scan0 + i * bData.Stride + j * bitsPerPixel / 8;
134 //data is a pointer to the first byte of the 3-byte color data
135 byte[] _data = new byte[bitsPerPixel / 8];
136 Marshal.Copy((IntPtr)data, _data, 0, bitsPerPixel / 8);
137 foreach (byte _byte in _data)
138 {
139 _byte_data_.Add(_byte);
140 }
141 }
142 }
143 Console.WriteLine("Byte Data (Count): 0x{0}", _byte_data_.Count.ToString("X8"));
144 }
145 b.UnlockBits(bData);
146
147 }
148 #endregion
149
150 #region Texture Export Support
151 private void ExportTexture(bool SaveFile, out Bitmap _image_data)
152 {
153 _image_data = null;
154 if (SaveFile)
155 {
156 DialogResult result = TextureExporter.ShowDialog();
157 if (result != DialogResult.OK) return;
158 }
159 //throw new NotImplementedException("Debug Has Not Been Implemented");
160 //MessageBox.Show("Debug Has Not Been Implemented", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
161 List<byte> _bytes = new List<byte>();
162 foreach (Pixel p in this.Value) { _bytes.Add(new byte()); }
163 byte[] __bytes = _bytes.ToArray();
164 Texture ReferenceTexture = Value[0].ReferenceTexture;
165 gr2lib.core.coretypes.implementation.Layout SourcePixelLayout;
166 coreapi.TextureSupport.GrannySwapRGBAToBGRA(ReferenceTexture.SubFormat.Value, out SourcePixelLayout);
167 //string filename = @"exported_images\" + ReferenceTexture.FromFileName.Name;
168 bool TextureCopied = false;
169 byte[] buffer = new byte[ReferenceTexture.Width * ReferenceTexture.Height * ReferenceTexture.Layout.BytesPerPixel];
170 unsafe
171 {
172 int stride = ReferenceTexture.Width * ReferenceTexture.Layout.BytesPerPixel;
173
174 // Ensure it is padded to a multiple of 4
175 if (stride % ReferenceTexture.Layout.BytesPerPixel != 0)
176 {
177 stride = (ReferenceTexture.Width * ReferenceTexture.Layout.BytesPerPixel + ReferenceTexture.Layout.BytesPerPixel) & ~ReferenceTexture.Layout.BytesPerPixel;
178 }
179 fixed (byte* p = buffer)
180 {
181 IntPtr byte_ptr = (IntPtr)p;
182 TextureCopied = coreapi.TextureSupport.GrannyCopyTextureImage(ReferenceTexture.NativePointer,
183 0,
184 0,
185 SourcePixelLayout.NativePointer,
186 ReferenceTexture.Width,
187 ReferenceTexture.Height,
188 stride,
189 byte_ptr);
190 if (TextureCopied)
191 {
192 if (SaveFile)
193 {
194 unsafe
195 {
196 fixed (byte* ptr = buffer)
197 {
198
199 using (Bitmap image = new Bitmap(ReferenceTexture.Width, ReferenceTexture.Height, stride, PixelFormat.Format32bppArgb, new IntPtr(ptr)))
200 {
201 Bitmap output = new Bitmap(image);
202 output.Save(TextureExporter.FileName);
203 }
204 }
205 }
206 MessageBox.Show("Succesfully Exported: " + new FileInfo(TextureExporter.FileName).Name, "Image Export Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
207 }
208 else
209 {
210
211 unsafe
212 {
213 fixed (byte* ptr = buffer)
214 {
215
216 using (Bitmap image = new Bitmap(ReferenceTexture.Width, ReferenceTexture.Height, stride, PixelFormat.Format32bppArgb, new IntPtr(ptr)))
217 {
218 Bitmap output = new Bitmap(image);
219 _image_data = output;
220 }
221 }
222 }
223 }
224 }
225 else
226 {
227 if (SaveFile)
228 {
229 MessageBox.Show("Failed to Export: " + new FileInfo(TextureExporter.FileName).Name, "Image Export Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
230 }
231 else
232 {
233 MessageBox.Show("Was Unable to Load Image Data:", "Image Viewer", MessageBoxButtons.OK, MessageBoxIcon.Error);
234 _image_data = null;
235 }
236 }
237 }
238
239 }
240 }
241 #endregion
242 }
243 }

  ViewVC Help
Powered by ViewVC 1.1.22