forked from crskycode/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawDecoder.cs
More file actions
191 lines (162 loc) · 5.01 KB
/
Copy pathRawDecoder.cs
File metadata and controls
191 lines (162 loc) · 5.01 KB
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
using System;
using BCnEncoder.Shared;
namespace BCnEncoder.Decoder
{
internal interface IRawDecoder
{
ColorRgba32[] Decode(ReadOnlyMemory<byte> data, OperationContext context);
}
/// <summary>
/// A class to decode data to R components.
/// </summary>
public class RawRDecoder : IRawDecoder
{
private readonly bool redAsLuminance;
/// <summary>
/// Create a new instance of <see cref="RawRDecoder"/>.
/// </summary>
/// <param name="redAsLuminance">If the decoded component should be used as the red component or luminance.</param>
public RawRDecoder(bool redAsLuminance)
{
this.redAsLuminance = redAsLuminance;
}
/// <summary>
/// Decode the data to color components.
/// </summary>
/// <param name="data">The data to decode.</param>
/// <param name="context">The context of the current operation.</param>
/// <returns>The decoded color components.</returns>
public ColorRgba32[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
{
var output = new ColorRgba32[data.Length];
// HINT: Ignoring parallel execution since we wouldn't gain performance from it.
var span = data.Span;
for (var i = 0; i < output.Length; i++)
{
context.CancellationToken.ThrowIfCancellationRequested();
if (redAsLuminance)
{
output[i].r = span[i];
output[i].g = span[i];
output[i].b = span[i];
}
else
{
output[i].r = span[i];
output[i].g = 0;
output[i].b = 0;
}
output[i].a = 255;
}
return output;
}
}
/// <summary>
/// A class to decode data to RG components.
/// </summary>
public class RawRgDecoder : IRawDecoder
{
/// <summary>
/// Decode the data to color components.
/// </summary>
/// <param name="data">The data to decode.</param>
/// <param name="context">The context of the current operation.</param>
/// <returns>The decoded color components.</returns>
public ColorRgba32[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
{
var output = new ColorRgba32[data.Length / 2];
// HINT: Ignoring parallel execution since we wouldn't gain performance from it.
var span = data.Span;
for (var i = 0; i < output.Length; i++)
{
context.CancellationToken.ThrowIfCancellationRequested();
output[i].r = span[i * 2];
output[i].g = span[i * 2 + 1];
output[i].b = 0;
output[i].a = 255;
}
return output;
}
}
/// <summary>
/// A class to decode data to RGB components.
/// </summary>
public class RawRgbDecoder : IRawDecoder
{
/// <summary>
/// Decode the data to color components.
/// </summary>
/// <param name="data">The data to decode.</param>
/// <param name="context">The context of the current operation.</param>
/// <returns>The decoded color components.</returns>
public ColorRgba32[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
{
var output = new ColorRgba32[data.Length / 3];
// HINT: Ignoring parallel execution since we wouldn't gain performance from it.
var span = data.Span;
for (var i = 0; i < output.Length; i++)
{
context.CancellationToken.ThrowIfCancellationRequested();
output[i].r = span[i * 3];
output[i].g = span[i * 3 + 1];
output[i].b = span[i * 3 + 2];
output[i].a = 255;
}
return output;
}
}
/// <summary>
/// A class to decode data to RGBA components.
/// </summary>
public class RawRgbaDecoder : IRawDecoder
{
/// <summary>
/// Decode the data to color components.
/// </summary>
/// <param name="data">The data to decode.</param>
/// <param name="context">The context of the current operation.</param>
/// <returns>The decoded color components.</returns>
public ColorRgba32[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
{
var output = new ColorRgba32[data.Length / 4];
// HINT: Ignoring parallel execution since we wouldn't gain performance from it.
var span = data.Span;
for (var i = 0; i < output.Length; i++)
{
context.CancellationToken.ThrowIfCancellationRequested();
output[i].r = span[i * 4];
output[i].g = span[i * 4 + 1];
output[i].b = span[i * 4 + 2];
output[i].a = span[i * 4 + 3];
}
return output;
}
}
/// <summary>
/// A class to decode data to BGRA components.
/// </summary>
public class RawBgraDecoder : IRawDecoder
{
/// <summary>
/// Decode the data to color components.
/// </summary>
/// <param name="data">The data to decode.</param>
/// <param name="context">The context of the current operation.</param>
/// <returns>The decoded color components.</returns>
public ColorRgba32[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
{
var output = new ColorRgba32[data.Length / 4];
// HINT: Ignoring parallel execution since we wouldn't gain performance from it.
var span = data.Span;
for (var i = 0; i < output.Length; i++)
{
context.CancellationToken.ThrowIfCancellationRequested();
output[i].b = span[i * 4];
output[i].g = span[i * 4 + 1];
output[i].r = span[i * 4 + 2];
output[i].a = span[i * 4 + 3];
}
return output;
}
}
}