/
BufferRgb332.cs
185 lines (162 loc) · 6.07 KB
/
BufferRgb332.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Meadow.Peripherals.Displays;
using System;
namespace Meadow.Foundation.Graphics.Buffers
{
/// <summary>
/// Represents a 8bpp color pixel buffer
/// </summary>
public class BufferRgb332 : PixelBufferBase
{
/// <summary>
/// Color mode of the buffer
/// </summary>
public override ColorMode ColorMode => ColorMode.Format8bppRgb332;
/// <summary>
/// Create a new BufferRgb332 object
/// </summary>
/// <param name="width">The width in pixels</param>
/// <param name="height">The height in pixels</param>
/// <param name="buffer">The backing buffer</param>
public BufferRgb332(int width, int height, byte[] buffer) : base(width, height, buffer) { }
/// <summary>
/// Create a new BufferRgb332 object
/// </summary>
/// <param name="width">The width in pixels</param>
/// <param name="height">The height in pixels</param>
public BufferRgb332(int width, int height) : base(width, height) { }
/// <summary>
/// Create a new BufferRgb332 object
/// </summary>
public BufferRgb332() : base() { }
/// <summary>
/// Get the pixel color
/// </summary>
/// <param name="x">The X pixel position</param>
/// <param name="y">The Y pixel position</param>
/// <returns>The pixel color as an 8bpp value</returns>
public int GetPixel8bpp(int x, int y)
{
return Buffer[y * Width + x];
}
/// <summary>
/// Get the pixel color
/// </summary>
/// <param name="x">The X pixel position</param>
/// <param name="y">The Y pixel position</param>
/// <returns>The pixel color</returns>
public override Color GetPixel(int x, int y)
{
byte r, g, b;
int index = y * Width + x;
r = (byte)(Buffer[index] >> 5 & 0x07);
g = (byte)(Buffer[index] >> 2 & 0x07);
b = (byte)(Buffer[index] & 0x03);
r = (byte)(r * 255 / 7);
g = (byte)(g * 255 / 7);
b = (byte)(b * 255 / 3);
return new Color(r, g, b);
}
/// <summary>
/// Set the pixel to a shade of gray
/// </summary>
/// <param name="x">X pixel position</param>
/// <param name="y">Y pixel position</param>
/// <param name="gray">The pixel gray value</param>
public void SetPixel(int x, int y, byte gray)
{
Buffer[y * Width + x] = gray;
}
/// <summary>
/// Set the pixel color
/// </summary>
/// <param name="x">X pixel position</param>
/// <param name="y">Y pixel position</param>
/// <param name="color">The pixel color</param>
public override void SetPixel(int x, int y, Color color)
{
SetPixel(x, y, color.Color8bppRgb332);
}
/// <summary>
/// Fill buffer with a color
/// </summary>
/// <param name="color">The fill color</param>
public override void Fill(Color color)
{
// split the color in to two byte values
Buffer[0] = color.Color8bppRgb332;
int arrayMidPoint = Buffer.Length / 2;
int copyLength;
for (copyLength = 1; copyLength < arrayMidPoint; copyLength <<= 1)
{
Array.Copy(Buffer, 0, Buffer, copyLength, copyLength);
}
Array.Copy(Buffer, 0, Buffer, copyLength, Buffer.Length - copyLength);
}
/// <summary>
/// Fill with a color
/// </summary>
/// <param name="x">X start position in pixels</param>
/// <param name="y">Y start position in pixels</param>
/// <param name="width">Width in pixels</param>
/// <param name="height">Height in pixels</param>
/// <param name="color">The fill color</param>
/// <exception cref="ArgumentOutOfRangeException">Throws an exception if fill area is beyond the buffer bounds</exception>
public override void Fill(int x, int y, int width, int height, Color color)
{
if (x < 0 || x + width > Width ||
y < 0 || y + height > Height)
{
throw new ArgumentOutOfRangeException();
}
byte value = color.Color8bppRgb332;
int index = y * Width + x - 1;
//fill the first line
for (int i = 0; i < width; i++)
{
Buffer[++index] = value;
}
//array copy the rest
for (int j = 0; j < height - 1; j++)
{
Array.Copy(Buffer,
(y + j) * Width + x,
Buffer,
(y + j + 1) * Width + x,
width);
}
}
/// <summary>
/// Invert the pixel
/// </summary>
/// <param name="x">x position of pixel</param>
/// <param name="y">y position of pixel</param>
public override void InvertPixel(int x, int y)
{
throw new NotImplementedException();
}
/// <summary>
/// Write a buffer to specific location to the current buffer
/// </summary>
/// <param name="x">x origin</param>
/// <param name="y">y origin</param>
/// <param name="buffer">buffer to write</param>
public override void WriteBuffer(int x, int y, IPixelBuffer buffer)
{
if (buffer.ColorMode == ColorMode)
{
int sourceIndex, destinationIndex;
int length = buffer.Width;
for (int i = 0; i < buffer.Height; i++)
{
sourceIndex = length * i;
destinationIndex = Width * (y + i) + x;
Array.Copy(buffer.Buffer, sourceIndex, Buffer, destinationIndex, length); ;
}
}
else
{ // fall back to a slow write
base.WriteBuffer(x, y, buffer);
}
}
}
}