-
Notifications
You must be signed in to change notification settings - Fork 68
/
Buffer1bppV.cs
144 lines (129 loc) · 5.03 KB
/
Buffer1bppV.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
using Meadow.Peripherals.Displays;
using System;
namespace Meadow.Foundation.Graphics.Buffers
{
/// <summary>
/// Represents a 1bpp pixel buffer with vertical pixel packing
/// 1 byte represents 8 pixels on the y-axis
/// </summary>
public class Buffer1bppV : Buffer1bpp
{
/// <summary>
/// Creates a new Buffer1bppV object
/// </summary>
/// <param name="width">width of buffer in pixels</param>
/// <param name="height">height of buffer in pixels</param>
/// <param name="buffer">data to copy into buffer</param>
public Buffer1bppV(int width, int height, byte[] buffer) :
base(width, height, buffer)
{ }
/// <summary>
/// Creates a new Buffer1bppV object
/// </summary>
/// <param name="width">width of buffer in pixels</param>
/// <param name="height">height of buffer in pixels</param>
public Buffer1bppV(int width, int height) :
base(width, height)
{ }
/// <summary>
/// Creates a new empty Buffer1bpp object
/// </summary>
public Buffer1bppV() : base()
{ }
/// <summary>
/// Creates a new Buffer1bppV object
/// </summary>
/// <param name="width">width of buffer in pixels</param>
/// <param name="height">height of buffer in pixels</param>
/// <param name="pageSize">the display page size, this will pad the total buffer size to multiples of the page size</param>
public Buffer1bppV(int width, int height, int pageSize)
{
Width = width;
Height = height;
width = (width + 7) & ~7;
int bufferSize = (width * height) >> 3;
bufferSize += bufferSize % pageSize;
Buffer = new byte[bufferSize];
}
/// <summary>
/// Is the pixel enabled / on for a given location
/// </summary>
/// <param name="x">x location in pixels</param>
/// <param name="y">y location in pixels</param>
/// <returns>true if pixel is set / enabled</returns>
public override bool GetPixelIsEnabled(int x, int y)
{
return (Buffer[(x + y * Width) >> 3] & (0x80 >> (x % 8))) != 0;
}
/// <summary>
/// Set a pixel in the display buffer
/// </summary>
/// <param name="x">x position in pixels from left</param>
/// <param name="y">y position in pixels from top</param>
/// <param name="enabled">is pixel enabled (on)</param>
public override void SetPixel(int x, int y, bool enabled)
{
int index = (x + y * Width) >> 3;
byte bitMask = (byte)(0x80 >> (x % 8));
Buffer[index] = enabled ? (byte)(Buffer[index] | bitMask) : (byte)(Buffer[index] & ~bitMask);
}
/// <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();
}
var isColored = color.Color1bpp;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
//ToDo - optimize for full byte copies
SetPixel(x + i, y + j, isColored);
}
}
}
/// <summary>
/// Invert a 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)
{
Buffer[(x + y * Width) >> 3] ^= (byte)~(0x80 >> (x % 8));
}
/// <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 is Buffer1bppV buf1bppV)
{
for (int i = 0; i < buffer.Width; i++)
{
for (int j = 0; j < buffer.Height; j++)
{ //1 bit at a time - ToDo
SetPixel(x + i, y + j, buf1bppV.GetPixelIsEnabled(i, j));
}
}
}
else
{
base.WriteBuffer(x, y, buffer);
}
}
}
}