/[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 176 - (show annotations) (download)
Fri Jul 23 18:57:45 2010 UTC (9 years, 1 month ago) by william
File size: 10395 byte(s)
Moved Import/Export Raw Pixels ->
From: PixelByteEditor
To: HexEditor
Reason: Clean up PixelByteEditor

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

  ViewVC Help
Powered by ViewVC 1.1.22