/
BufferRgb444.cs
256 lines (227 loc) · 8.53 KB
/
BufferRgb444.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Meadow.Peripherals.Displays;
using System;
namespace Meadow.Foundation.Graphics.Buffers
{
/// <summary>
/// Represents a 12bpp color pixel buffer
/// </summary>
public class BufferRgb444 : PixelBufferBase
{
/// <summary>
/// Color mode of the buffer
/// </summary>
public override ColorMode ColorMode => ColorMode.Format12bppRgb444;
/// <summary>
/// Create a new BufferRgb444 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 BufferRgb444(int width, int height, byte[] buffer) : base(width, height, buffer) { }
/// <summary>
/// Create a new BufferRgb444 object
/// </summary>
/// <param name="width">The width in pixels</param>
/// <param name="height">The height in pixels</param>
public BufferRgb444(int width, int height) : base(width, height) { }
/// <summary>
/// Create a new BufferRgb444 object
/// </summary>
public BufferRgb444() : 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 a 12bpp value</returns>
public ushort GetPixel12bpp(int x, int y)
{
byte r, g, b;
int index;
if (x % 2 == 0)
{
index = (x + y * Width) * 3 / 2;
r = (byte)(Buffer[index] >> 4);
g = (byte)(Buffer[index] & 0x0F);
b = (byte)(Buffer[index + 1] >> 4);
}
else
{
index = ((x - 1 + y * Width) * 3 / 2) + 1;
r = (byte)(Buffer[index] & 0x0F);
g = (byte)(Buffer[index + 1] >> 4);
b = (byte)(Buffer[index + 1] & 0x0F);
}
return (ushort)(r << 8 | g << 4 | b);
}
/// <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;
if (x % 2 == 0)
{
index = (x + y * Width) * 3 / 2;
r = (byte)(Buffer[index] >> 4);
g = (byte)(Buffer[index] & 0x0F);
b = (byte)(Buffer[index + 1] >> 4);
}
else
{
index = ((x - 1 + y * Width) * 3 / 2) + 1;
r = (byte)(Buffer[index] & 0x0F);
g = (byte)(Buffer[index + 1] >> 4);
b = (byte)(Buffer[index + 1] & 0x0F);
}
r = (byte)(r * 255 / 15);
g = (byte)(g * 255 / 15);
b = (byte)(b * 255 / 15);
return new Color(r, g, b);
}
/// <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.Color12bppRgb444);
}
/// <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 packed as a 12 bpp ushort</param>
public void SetPixel(int x, int y, ushort color)
{
int index;
//one of 2 possible write patterns
if (x % 2 == 0)
{
//1st byte RRRRGGGG
//2nd byte BBBB
index = ((x + y * Width) * 3 / 2);
Buffer[index] = (byte)(color >> 4); //think this is correct - grab the r & g values
index++;
Buffer[index] = (byte)((Buffer[index] & 0x0F) | (color << 4));
}
else
{
//1st byte RRRR
//2nd byte GGGGBBBB
index = ((x - 1 + y * Width) * 3 / 2) + 1;
Buffer[index] = (byte)((Buffer[index] & 0xF0) | (color >> 8));
Buffer[++index] = (byte)color; //just the lower 8 bits
}
}
/// <summary>
/// Fill buffer with a color
/// </summary>
/// <param name="color">The fill color</param>
public override void Fill(Color color)
{
// could do a minor optimization by caching the ushort 444 value
Buffer[0] = (byte)(color.Color12bppRgb444 >> 4);
Buffer[1] = (byte)((color.Color12bppRgb444 << 4) | (color.Color12bppRgb444 >> 8));
Buffer[2] = (byte)color.Color12bppRgb444;
int arrayMidPoint = Buffer.Length / 2;
int copyLength;
for (copyLength = 3; 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();
}
//TODO optimize
var uColor = color.Color12bppRgb444;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
SetPixel(x + i, y + j, uColor);
}
}
}
/// <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)
{
byte r, g, b;
int index;
if (x % 2 == 0)
{
index = (x + y * Width) * 3 / 2;
r = (byte)(Buffer[index] >> 4);
g = (byte)(Buffer[index] & 0x0F);
b = (byte)(Buffer[index + 1] >> 4);
}
else
{
index = ((x - 1 + y * Width) * 3 / 2) + 1;
r = (byte)(Buffer[index] & 0x0F);
g = (byte)(Buffer[index + 1] >> 4);
b = (byte)(Buffer[index + 1] & 0x0F);
}
r = (byte)(~r & 0x0F);
g = (byte)(~g & 0x0F);
b = (byte)(~b & 0x0F);
//get new color
var color = (ushort)(r << 8 | g << 4 | b);
SetPixel(x, y, color);
}
/// <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 &&
Width % 2 == 0 &&
x % 2 == 0 &&
buffer.Width % 2 == 0)
{
//we have a happy path
int sourceIndex, destinationIndex;
int length = buffer.Width / 2 * 3;
for (int i = 0; i < buffer.Height; i++)
{
sourceIndex = length * i;
destinationIndex = (Width * (y + i) + x) * 3 / 2;
Array.Copy(buffer.Buffer, sourceIndex, Buffer, destinationIndex, length);
}
}
else
{ // fall back to a slow write
base.WriteBuffer(x, y, buffer);
}
}
}
}