-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPGEng.View.cs
112 lines (96 loc) · 3.98 KB
/
CPGEng.View.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
/*
* Crispycat PixelGraphic Engine
* CPGEng.View.cs; View objects and functions
* (C) 2020 crispycat; https://github.com/crispycat0/CPGEng/LICENSE
* 2020/04/01
*/
using System.Windows.Media.Imaging;
namespace CPGEng {
public class View {
public readonly uint Width, Height, Stride, Density, Channels = 4;
public Buffer buffer;
/// <summary>Creates a View.</summary>
/// <param name="w">Width</param>
/// <param name="h">Height</param>
/// <param name="d">Density, defaults to 96ppi (244ppc)</param>
public View(uint w, uint h, uint d = 96) {
Width = w;
Height = h;
Density = d;
Stride = w * Channels + (w % Channels);
buffer = new Buffer(Height * Stride);
}
public uint PixelLocationInBuffer(Pixel p) {
return (uint)(p.X * Channels + p.Y * Stride);
}
public uint PixelLocationInBuffer(Pixel p, uint c, uint s) {
return (uint)(p.X * c + p.Y * s);
}
/// <summary>Get the value of a Pixel in the View.</summary>
/// <param name="p">Pixel location</param>
/// <returns>ColorInt</returns>
public ColorInt Get(Pixel p) {
return new ColorInt(
buffer.Get(PixelLocationInBuffer(p)),
buffer.Get(PixelLocationInBuffer(p) + 1),
buffer.Get(PixelLocationInBuffer(p) + 2)
);
}
/// <summary>Draws a color at the location specified.</summary>
/// <param name="p">Pixel location</param>
/// <param name="c">ColorInt color</param>
public void Draw(Pixel p, ColorInt c) {
buffer.Set(PixelLocationInBuffer(p), (byte)c.Blue);
buffer.Set(PixelLocationInBuffer(p) + 1, (byte)c.Green);
buffer.Set(PixelLocationInBuffer(p) + 2, (byte)c.Red);
buffer.Set(PixelLocationInBuffer(p) + 3, 255);
}
/// <summary>Draws a color at the locations specified.</summary>
/// <param name="p">Pixel[] locations</param>
/// <param name="c">ColorInt color</param>
public void Draw(Pixel[] p, ColorInt c) {
foreach (Pixel x in p) Draw(x, c);
}
/// <summary>Draws a color at the locations specified.</summary>
/// <param name="p">Pixel[] locations</param>
/// <param name="c">ColorInt color</param>
/// <param name="o">Pixel offset</param>
public void Draw(Pixel[] p, ColorInt c, Pixel o) {
foreach (Pixel x in p) Draw(x + o, c);
}
/// <summary>Draws a BitmapData at the locations specified.</summary>
/// <param name="p">Pixel[] locations</param>
/// <param name="b">BitmapData bitmap</param>
public void Draw(Pixel[] p, BitmapData b) {
foreach (Pixel x in p) Draw(x, new ColorInt(
b.buffer.Get(PixelLocationInBuffer(x, (uint)b.Channels, (uint)b.Stride) % b.buffer.Length),
b.buffer.Get(PixelLocationInBuffer(x, (uint)b.Channels, (uint)b.Stride) % b.buffer.Length + 1),
b.buffer.Get(PixelLocationInBuffer(x, (uint)b.Channels, (uint)b.Stride) % b.buffer.Length + 2)
));
}
/// <summary>Draws a BitmapData at the locations specified.</summary>
/// <param name="p">Pixel[] locations</param>
/// <param name="b">BitmapData bitmap</param>
/// <param name="o">Pixel offset</param>
public void Draw(Pixel[] p, BitmapData b, Pixel o) {
foreach (Pixel x in p) Draw(x + o, new ColorInt(
b.buffer.Get(PixelLocationInBuffer(x, (uint)b.Channels, (uint)b.Stride) % b.buffer.Length),
b.buffer.Get(PixelLocationInBuffer(x, (uint)b.Channels, (uint)b.Stride) % b.buffer.Length + 1),
b.buffer.Get(PixelLocationInBuffer(x, (uint)b.Channels, (uint)b.Stride) % b.buffer.Length + 2)
));
}
/// <summary>Clears the View.</summary>
public void Clear() {
buffer = new Buffer(Height * Stride);
}
/// <summary>Clears the View.</summary>
public void Clear(byte v) {
buffer = new Buffer(Height * Stride, v);
}
/// <summary>Returns a BitmapSource created from the View.</summary>
/// <returns>BitmapSource</returns>
public BitmapSource ToBitmapSource() {
return BitmapSource.Create((int)Width, (int)Height, Density, Density, System.Windows.Media.PixelFormats.Bgra32, null, buffer.Data(), (int)Stride);
}
}
}