/[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 86 - (show annotations) (download)
Fri Jul 16 23:35:21 2010 UTC (9 years, 11 months ago) by william
File size: 10139 byte(s)
Add Try/Catch blocks in to all API Calls

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

  ViewVC Help
Powered by ViewVC 1.1.22