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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/gr2lib/core/ui/typeeditors/PixelBytesEditor.cs	2010/07/23 17:18:46	175
+++ trunk/gr2lib/core/ui/typeeditors/PixelBytesEditor.cs	2010/07/23 18:57:45	176
@@ -41,74 +41,10 @@
 
         private void PixelBytesEditor_Load(object sender, EventArgs e)
         {
-            btnImport.FlatStyle = FlatStyle.Standard;
-            btnExport.FlatStyle = FlatStyle.Standard;
             btnViewImage.FlatStyle = FlatStyle.Standard;
             btnDebugPixels.FlatStyle = FlatStyle.Standard;
         }
 
-        #region IMPORT PIXELS SUPPORT
-        private void btnImport_Click(object sender, EventArgs e)
-        {
-            //throw new NotImplementedException("Import Has Not Been Implemented");
-            DialogResult result = PixelImporter.ShowDialog();
-            if (result != DialogResult.OK) return;
-
-            // read bytes from file
-            FileStream fs = new FileStream(PixelImporter.FileName, FileMode.Open);
-            List<Pixel> _pixels = new List<Pixel>();
-            BinaryReader r = new BinaryReader(fs);
-            r.BaseStream.Seek(0, SeekOrigin.Begin);
-            byte[] bytes = r.ReadBytes((int)r.BaseStream.Length);
-            r.Close();
-
-            foreach (byte b in bytes)
-            {
-                Pixel p = new Pixel();
-                p.UInt8 = b;
-                _pixels.Add(p);
-            }
-
-            if (_pixels.Count != Value.Count)
-            {
-                MessageBox.Show("Import Size = " + _pixels.Count + " (" + string.Format("0x{0}", _pixels.Count.ToString("X8")) + ")" + "\nTarget Size = " + Value.Count + " (" + string.Format("0x{0}", Value.Count.ToString("X8")) + ")", "Pixel Data Import Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
-                return;
-            }
-            else
-            {
-                MessageBox.Show("Successfully imported: " + _pixels.Count + " (" + string.Format("0x{0}", _pixels.Count.ToString("X8")) + ")" + " pixel bytes", "Pixel Data Import Successfull!", MessageBoxButtons.OK, MessageBoxIcon.Information);
-                Value = _pixels;
-            }
-
-        }
-        #endregion
-
-        #region EXPORT PIXELS SUPPORT
-        private void btnExport_Click(object sender, EventArgs e)
-        {
-            //throw new NotImplementedException("Export Has Not Been Implemented");
-            DialogResult result = PixelExporter.ShowDialog();
-            if (result != DialogResult.OK) return;
-
-            try
-            {
-                FileStream fs = new FileStream(PixelExporter.FileName, FileMode.Create);
-                BinaryWriter w = new BinaryWriter(fs);
-                foreach (byte b in Value.ToArray())
-                {
-                    w.Write(b);
-                }
-                w.Flush();
-                w.Close();
-                MessageBox.Show("Successfully exported: " + Value.Count + " (" + string.Format("0x{0}", Value.Count.ToString("X8")) + ")" + " pixel bytes", "Pixel Data Export Successfull!", MessageBoxButtons.OK, MessageBoxIcon.Information);
-            }
-            catch (Exception ex)
-            {
-                MessageBox.Show("Failed to export pixel data!\n\nMessage: " + ex.Message, "Pixel Data Export Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
-                return;
-            }
-        }
-        #endregion
 
         #region VIEW RAW PIXELS SUPPORT
         private void btnViewRaw_Click(object sender, EventArgs e)
@@ -116,6 +52,7 @@
             // throw new NotImplementedException("View Raw Has Not Been Implemented");
             List<byte> _bytes = new List<byte>();
             foreach (Pixel p in this.Value) { _bytes.Add(p.UInt8); }
+            //MessageBox.Show("Pixel Count: 0x" + this.Value.Count.ToString("X8"));
             HexEditor _editor = new HexEditor(_bytes);
             _editor.ShowDialog();
 
@@ -162,11 +99,39 @@
         }
         #endregion
 
-
         #region DEBUG PIXELS SUPPORT
         private void btnDebugPixels_Click(object sender, EventArgs e)
         {
-            
+            DialogResult result = TextureImporter.ShowDialog();
+            if (result != DialogResult.OK) return;
+
+            System.Drawing.Image _image = System.Drawing.Image.FromFile(TextureImporter.FileName);
+            Bitmap b = new Bitmap(_image);//note this has several overloads, including a path to an image
+            BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);
+            int bitsPerPixel = System.Drawing.Image.GetPixelFormatSize(b.PixelFormat);
+            /*This time we convert the IntPtr to a ptr*/
+            unsafe
+            {
+                List<byte> _byte_data_ = new List<byte>();
+                byte* scan0 = (byte*)bData.Scan0.ToPointer();
+                for (int i = 0; i < bData.Height; ++i)
+                {
+                    for (int j = 0; j < bData.Width; ++j)
+                    {
+                        byte* data = scan0 + i * bData.Stride + j * bitsPerPixel / 8;
+                        //data is a pointer to the first byte of the 3-byte color data
+                        byte[] _data = new byte[bitsPerPixel / 8];
+                        Marshal.Copy((IntPtr)data, _data, 0, bitsPerPixel / 8);
+                        foreach (byte _byte in _data)
+                        {
+                            _byte_data_.Add(_byte);
+                        }
+                    }
+                }
+                Console.WriteLine("Byte Data (Count): 0x{0}", _byte_data_.Count.ToString("X8"));
+            }
+            b.UnlockBits(bData);
+    
         }
         #endregion
 

 

  ViewVC Help
Powered by ViewVC 1.1.22