/
BufferIndexed4.cs
229 lines (202 loc) · 7.61 KB
/
BufferIndexed4.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
using Meadow.Peripherals.Displays;
using System;
using System.Linq;
namespace Meadow.Foundation.Graphics.Buffers
{
/// <summary>
/// Represents a 4bpp pixel buffer with indexed colors
/// </summary>
public class BufferIndexed4 : PixelBufferBase
{
/// <summary>
/// Color mode of the buffer
/// </summary>
public override ColorMode ColorMode => ColorMode.Format4bppIndexed;
/// <summary>
/// The indexed colors as an 8 element array of Color values
/// </summary>
public readonly Color[] IndexedColors = new Color[8];
/// <summary>
/// Create a new BufferIndexed4 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 BufferIndexed4(int width, int height, byte[] buffer) : base(width, height, buffer) { }
/// <summary>
/// Create a new BufferIndexed4 object
/// </summary>
/// <param name="width">The width in pixels</param>
/// <param name="height">The height in pixels</param>
public BufferIndexed4(int width, int height) : base(width, height) { }
/// <summary>
/// Create a new BufferIndexed4 object
/// </summary>
public BufferIndexed4() : base() { }
/// <summary>
/// Fill buffer with a color
/// </summary>
/// <param name="color">The fill color</param>
public override void Fill(Color color)
{
byte colorValue = (byte)GetIndexForColor(color);
Buffer[0] = (byte)(colorValue | colorValue << 4);
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>
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 index = GetIndexForColor(color);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
SetPixel(x + i, y + j, index);
}
}
}
/// <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)
{ //comes back as a 4bit value
var index = GetColorIndexForPixel(x, y);
return IndexedColors[index];
}
/// <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)
{
var index = GetIndexForColor(color);
SetPixel(x, y, index);
}
/// <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="colorIndex">The color index</param>
public void SetPixel(int x, int y, int colorIndex)
{
int index = y * Width / 2 + x / 2;
if ((x % 2) == 0)
{ //even pixel - shift to the significant nibble
Buffer[index] = (byte)((Buffer[index] & 0x0f) | (colorIndex << 4));
}
else
{ //odd pixel
Buffer[index] = (byte)((Buffer[index] & 0xf0) | (colorIndex));
}
}
/// <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("InvertPixel not supported for indexed buffers");
}
/// <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 &&
x % 2 == 0 &&
buffer.Width % 2 == 0)
{
//we have a happy path
int sourceIndex, destinationIndex;
int length = buffer.Width / 2;
for (int i = 0; i < buffer.Height; i++)
{
sourceIndex = length * i;
destinationIndex = (Width * (y + i) + x) >> 2; //divide by 2
Array.Copy(buffer.Buffer, sourceIndex, Buffer, destinationIndex, length);
}
}
else
{ // fall back to a slow write
base.WriteBuffer(x, y, buffer);
}
}
/// <summary>
/// Get the pixel color index
/// </summary>
/// <param name="x">The X pixel position</param>
/// <param name="y">The Y pixel position</param>
/// <returns>The pixel color as a 4bpp gray value</returns>
public byte GetColorIndexForPixel(int x, int y)
{
int index = y * Width / 2 + x / 2;
byte color;
if ((x % 2) == 0)
{ //even pixel - shift to the significant nibble
color = (byte)((Buffer[index] & 0x0f) >> 4);
}
else
{ //odd pixel
color = (byte)((Buffer[index] & 0xf0));
}
return color;
}
Color GetClosestColor(Color color)
{
return IndexedColors[GetIndexForColor(color)];
}
int GetIndexForColor(Color color)
{
if (IndexedColors == null || IndexedColors.All(x => x == null))
{
throw new NullReferenceException("No indexed colors assigned");
}
int closestIndex = -1;
double shortestDistance = double.MaxValue;
for (int i = 0; i < IndexedColors.Length; i++)
{
double distance;
if (IndexedColors[i] != null)
{
distance = GetColorDistance(color, IndexedColors[i]);
if (distance < shortestDistance)
{
shortestDistance = distance;
closestIndex = i;
if (distance == 0) { break; } //perfect match
}
}
}
return closestIndex;
}
}
}