/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/Pixel.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coretypes/implementation/Pixel.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 78 - (show annotations) (download)
Thu Jul 15 19:29:52 2010 UTC (9 years, 5 months ago) by william
File size: 9058 byte(s)
Put Non Working Code in PixelList.Export inside a #region clause

1 using System;
2 using System.Collections.Generic;
3 using System.Collections;
4 using System.Text;
5 using gr2lib.core.helpers;
6 using System.IO;
7 using System.Drawing;
8 using System.ComponentModel;
9 using System.Runtime.InteropServices;
10 using System.Drawing.Imaging;
11
12
13 namespace gr2lib.core.coretypes.implementation
14 {
15 public enum RGBA_ORDER
16 {
17 none = 0,
18 RGBA = 1,
19 BGRA = 2,
20 ARGB = 3
21 }
22 public class RGBA
23 {
24 #region implicit operators
25 public static implicit operator RGBA(uint rgba)
26 {
27 return new RGBA(rgba);
28 }
29 public static implicit operator uint(RGBA rgba)
30 {
31 return rgba.GetRGBA();
32 }
33 #endregion
34 private RGBA_ORDER src_order = RGBA_ORDER.RGBA;
35 private RGBA_ORDER dest_order = RGBA_ORDER.RGBA;
36 private uint rgba = 0x00000000;
37
38 private uint R;
39 private uint G;
40 private uint B;
41 private uint A;
42
43 public RGBA(uint rgba)
44 {
45 this.src_order = RGBA_ORDER.RGBA;
46 this.dest_order = RGBA_ORDER.RGBA;
47 this.rgba = rgba;
48 this.ParseRGBA();
49 }
50
51 public RGBA(uint rgba, RGBA_ORDER src_order, RGBA_ORDER dest_order)
52 {
53 this.src_order = src_order;
54 this.dest_order = dest_order;
55 this.rgba = rgba;
56 this.ParseRGBA();
57 }
58 private void ParseRGBA()
59 {
60 switch (src_order)
61 {
62 case RGBA_ORDER.RGBA: A = (uint)(rgba >> 24); B = (uint)((rgba >> 16) & 0xff); G = (uint)((rgba >> 8) & 0xff); R = (uint)((rgba) & 0xff); break;
63 case RGBA_ORDER.BGRA: A = (uint)(rgba >> 24); R = (uint)((rgba >> 16) & 0xff); B = (uint)((rgba >> 8) & 0xff); B = (uint)((rgba) & 0xff); break;
64 case RGBA_ORDER.ARGB: B = (uint)(rgba >> 24); G = (uint)((rgba >> 16) & 0xff); R = (uint)((rgba >> 8) & 0xff); A = (uint)((rgba) & 0xff); break;
65 default: break;
66 }
67 }
68
69 public uint GetRGBA()
70 {
71 uint n_rgba = 0;
72 switch (dest_order)
73 {
74 case RGBA_ORDER.RGBA: n_rgba = Convert.ToUInt32("0x" + A.ToString("X2") + B.ToString("X2") + G.ToString("X2") + R.ToString("X2"), 16); break;
75 case RGBA_ORDER.BGRA: n_rgba = Convert.ToUInt32("0x" + A.ToString("X2") + R.ToString("X2") + B.ToString("X2") + B.ToString("X2"), 16); break;
76 case RGBA_ORDER.ARGB: n_rgba = Convert.ToUInt32("0x" + B.ToString("X2") + G.ToString("X2") + R.ToString("X2") + A.ToString("X2"), 16); break;
77 default: n_rgba = rgba; break;
78 }
79 return n_rgba;
80 }
81
82 public override string ToString()
83 {
84 return "0x" + GetRGBA().ToString("X8");
85 }
86 }
87
88 public class PixelList : IList<Pixel>
89 {
90 #region implicit conversion operators
91 public static implicit operator PixelList(byte[] b)
92 {
93 PixelList _list = new PixelList(b.Length); foreach (byte _b in b) { Pixel _pixel = new Pixel(); _pixel.UInt8 = _b; _list.Add(_pixel); } return _list;
94 }
95 public static implicit operator byte[](PixelList pixellist)
96 {
97 List<byte> b = new List<byte>(pixellist.Count); foreach (Pixel p in pixellist) { b.Add(p.UInt8); } return b.ToArray();
98 }
99 public static byte[] ConvertToBytes(List<Pixel> pixellist)
100 {
101 List<byte> b = new List<byte>(pixellist.Count); foreach (Pixel p in pixellist) { b.Add(p.UInt8); } return b.ToArray();
102 }
103
104 #endregion
105 private List<Pixel> _pixels;
106 public PixelList() { _pixels = new List<Pixel>(); }
107 public PixelList(IEnumerable<Pixel> collection) { _pixels = new List<Pixel>(collection); }
108 public PixelList(int capacity) { _pixels = new List<Pixel>(capacity); }
109
110 #region IList<Pixel> Members
111 public Pixel this[int index] { get { return _pixels[index]; } set { _pixels[index] = value; } }
112 public int IndexOf(Pixel item) { return _pixels.IndexOf(item); }
113 public void Insert(int index, Pixel item) { _pixels.Insert(index, item); }
114 public void RemoveAt(int index) { _pixels.RemoveAt(index); }
115 public void Add(Pixel item) { _pixels.Add(item); }
116 public void Clear() { _pixels.Clear(); }
117 public bool Contains(Pixel item) { return _pixels.Contains(item); }
118 public void CopyTo(Pixel[] array, int arrayIndex) { _pixels.CopyTo(array, arrayIndex); }
119 public bool Remove(Pixel item) { return _pixels.Remove(item); }
120 public Pixel[] ToArray() { return _pixels.ToArray(); }
121 public int Count { get { return _pixels.Count; } }
122 public bool IsReadOnly { get { return false; } }
123 IEnumerator IEnumerable.GetEnumerator() { return _pixels.GetEnumerator(); }
124 IEnumerator<Pixel> IEnumerable<Pixel>.GetEnumerator() { return _pixels.GetEnumerator(); }
125 #endregion
126
127 public void Export(string filename, int width, int height, int stride)
128 {
129 #region NON-WORKING-CODE
130 ////Console.WriteLine("Export To -> " + filename);
131
132 // if (!System.IO.Directory.Exists(new FilePath(filename).Path))
133 // {
134 // System.IO.Directory.CreateDirectory(new FilePath(filename).Path);
135 // }
136
137 // byte[] pix = ConvertToBytes(_pixels);
138 // //byte[] rotated_pix;
139 // //List<uint> pix_int = new List<uint>();
140
141 // //MemoryStream ms = new MemoryStream(pix);
142
143 // //BinaryReader r = new BinaryReader(ms);
144 // //r.BaseStream.Seek(0, SeekOrigin.Begin);
145
146 // //while (r.BaseStream.Position < r.BaseStream.Length)
147 // //{
148 // // uint rgba = r.ReadUInt32();
149 // // RGBA _rgba = new RGBA(rgba, RGBA_ORDER.RGBA, RGBA_ORDER.ARGB);
150 // // pix_int.Add(_rgba);
151 // //}
152 // //r.Close();
153
154 // //MemoryStream rotated_ms = new MemoryStream(pix.Length);
155 // //BinaryWriter w = new BinaryWriter(rotated_ms);
156 // //w.Seek(0, SeekOrigin.Begin);
157 // //foreach (uint i in pix_int)
158 // //{
159 // // w.Write(i);
160 // //}
161 // //w.Flush();
162
163 // //BinaryReader roated_pix_reader = new BinaryReader(rotated_ms);
164 // //roated_pix_reader.BaseStream.Seek(0, SeekOrigin.Begin);
165
166 // //rotated_pix = roated_pix_reader.ReadBytes((int)roated_pix_reader.BaseStream.Length);
167
168 // //Here create the Bitmap to the know height, width and format
169 // Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb);
170
171 // //Create a BitmapData and Lock all pixels to be written
172 // BitmapData bmpData = bmp.LockBits(
173 // new Rectangle(0, 0, bmp.Width, bmp.Height),
174 // ImageLockMode.WriteOnly, bmp.PixelFormat);
175 // //Copy the data from the byte array into BitmapData.Scan0
176 // Marshal.Copy(pix, 0, bmpData.Scan0, pix.Length);
177
178 // //Unlock the pixels
179 // bmp.UnlockBits(bmpData);
180 // bmp.Save(filename);
181 #endregion
182
183 throw new NotImplementedException("pixel exporting has not been implemented, yet");
184
185 }
186 public void Import(byte[] pixel_data)
187 {
188 throw new NotImplementedException("pixel importing has not been implemented, yet");
189 }
190 }
191
192 public class Pixel
193 {
194 #region Implicit operators
195 public static implicit operator Pixel(byte b)
196 {
197 Pixel p = new Pixel();
198 p.UInt8 = b;
199 return p;
200 }
201 public static implicit operator byte(Pixel p)
202 {
203 return p.UInt8;
204 }
205 #endregion
206 public byte UInt8;
207
208 public IntPtr NativePointer;
209
210 public Pixel()
211 {
212 this.UInt8 = 0;
213 this.NativePointer = IntPtr.Zero;
214 }
215 internal static Pixel ReadFromMemory(IntPtr pointer)
216 {
217 if (pointer == IntPtr.Zero) return null;
218 native.Pixel native = Helpers.ReadFromMemory<native.Pixel>(pointer);
219 Pixel managed = ReadFromNative(native);
220 managed.NativePointer = pointer;
221 return managed;
222 }
223
224 internal static Pixel ReadFromNative(native.Pixel native)
225 {
226 Pixel managed = new Pixel();
227 managed.UInt8 = native.UInt8;
228 return managed;
229 }
230 }
231 }

  ViewVC Help
Powered by ViewVC 1.1.22