/
BufferGray4.cs
193 lines (169 loc) · 6.3 KB
/
BufferGray4.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
using Meadow.Peripherals.Displays;
using System;
namespace Meadow.Foundation.Graphics.Buffers
{
/// <summary>
/// Represents a 4bpp pixel buffer
/// </summary>
public class BufferGray4 : PixelBufferBase
{
/// <summary>
/// Color mode of the buffer
/// </summary>
public override ColorMode ColorMode => ColorMode.Format4bppGray;
/// <summary>
/// Create a new BufferGray4 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 BufferGray4(int width, int height, byte[] buffer) : base(width, height, buffer) { }
/// <summary>
/// Create a new BufferGray4 object
/// </summary>
/// <param name="width">The width in pixels</param>
/// <param name="height">The height in pixels</param>
public BufferGray4(int width, int height) : base(width, height) { }
/// <summary>
/// Create a new BufferGray4 object
/// </summary>
public BufferGray4() : base() { }
/// <summary>
/// Fill buffer with a color
/// </summary>
/// <param name="color">The fill color</param>
public override void Fill(Color color)
{
Buffer[0] = (byte)(color.Color4bppGray | color.Color4bppGray << 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 bColor = color.Color4bppGray;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
SetPixel(x + i, y + j, bColor);
}
}
}
/// <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 gray = GetPixel4bpp(x, y);
return new Color(gray << 4, gray << 4, gray << 4);
}
/// <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.Color4bppGray);
}
/// <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)
{
int index = y * Width / 2 + x / 2;
if ((x % 2) == 0)
{ //even pixel - shift to the significant nibble
Buffer[index] = (byte)((Buffer[index] & 0x0f) | (gray << 4));
}
else
{ //odd pixel
Buffer[index] = (byte)((Buffer[index] & 0xf0) | (gray));
}
}
/// <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 color = GetPixel4bpp(x, y);
color = (byte)(((byte)~color) & 0x0f);
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 &&
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
/// </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 GetPixel4bpp(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;
}
}
}