/[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 76 - (show annotations) (download)
Thu Jul 15 19:28:46 2010 UTC (11 years, 4 months ago) by william
File size: 8994 byte(s)
Add/Update: PixelList

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 ////Console.WriteLine("Export To -> " + filename);
130
131 // if (!System.IO.Directory.Exists(new FilePath(filename).Path))
132 // {
133 // System.IO.Directory.CreateDirectory(new FilePath(filename).Path);
134 // }
135
136 // byte[] pix = ConvertToBytes(_pixels);
137 // //byte[] rotated_pix;
138 // //List<uint> pix_int = new List<uint>();
139
140 // //MemoryStream ms = new MemoryStream(pix);
141
142 // //BinaryReader r = new BinaryReader(ms);
143 // //r.BaseStream.Seek(0, SeekOrigin.Begin);
144
145 // //while (r.BaseStream.Position < r.BaseStream.Length)
146 // //{
147 // // uint rgba = r.ReadUInt32();
148 // // RGBA _rgba = new RGBA(rgba, RGBA_ORDER.RGBA, RGBA_ORDER.ARGB);
149 // // pix_int.Add(_rgba);
150 // //}
151 // //r.Close();
152
153 // //MemoryStream rotated_ms = new MemoryStream(pix.Length);
154 // //BinaryWriter w = new BinaryWriter(rotated_ms);
155 // //w.Seek(0, SeekOrigin.Begin);
156 // //foreach (uint i in pix_int)
157 // //{
158 // // w.Write(i);
159 // //}
160 // //w.Flush();
161
162 // //BinaryReader roated_pix_reader = new BinaryReader(rotated_ms);
163 // //roated_pix_reader.BaseStream.Seek(0, SeekOrigin.Begin);
164
165 // //rotated_pix = roated_pix_reader.ReadBytes((int)roated_pix_reader.BaseStream.Length);
166
167 // //Here create the Bitmap to the know height, width and format
168 // Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb);
169
170 // //Create a BitmapData and Lock all pixels to be written
171 // BitmapData bmpData = bmp.LockBits(
172 // new Rectangle(0, 0, bmp.Width, bmp.Height),
173 // ImageLockMode.WriteOnly, bmp.PixelFormat);
174 // //Copy the data from the byte array into BitmapData.Scan0
175 // Marshal.Copy(pix, 0, bmpData.Scan0, pix.Length);
176
177 // //Unlock the pixels
178 // bmp.UnlockBits(bmpData);
179 // bmp.Save(filename);
180 throw new NotImplementedException("pixel exporting has not been implemented, yet");
181
182 }
183 public void Import(byte[] pixel_data)
184 {
185 throw new NotImplementedException("pixel importing has not been implemented, yet");
186 }
187 }
188
189 public class Pixel
190 {
191 #region Implicit operators
192 public static implicit operator Pixel(byte b)
193 {
194 Pixel p = new Pixel();
195 p.UInt8 = b;
196 return p;
197 }
198 public static implicit operator byte(Pixel p)
199 {
200 return p.UInt8;
201 }
202 #endregion
203 public byte UInt8;
204
205 public IntPtr NativePointer;
206
207 public Pixel()
208 {
209 this.UInt8 = 0;
210 this.NativePointer = IntPtr.Zero;
211 }
212 internal static Pixel ReadFromMemory(IntPtr pointer)
213 {
214 if (pointer == IntPtr.Zero) return null;
215 native.Pixel native = Helpers.ReadFromMemory<native.Pixel>(pointer);
216 Pixel managed = ReadFromNative(native);
217 managed.NativePointer = pointer;
218 return managed;
219 }
220
221 internal static Pixel ReadFromNative(native.Pixel native)
222 {
223 Pixel managed = new Pixel();
224 managed.UInt8 = native.UInt8;
225 return managed;
226 }
227 }
228 }

  ViewVC Help
Powered by ViewVC 1.1.22