Skip to content

Commit aed9acd

Browse files
committed
Fixing StyleCop
1 parent 8729fed commit aed9acd

File tree

9 files changed

+100
-76
lines changed

9 files changed

+100
-76
lines changed

src/ImageSharp/Formats/Qoi/QoiChunkEnum.cs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,54 @@
33

44
namespace SixLabors.ImageSharp.Formats.Qoi;
55

6+
/// <summary>
7+
/// Enum that contains the operations that encoder and decoder must process, written
8+
/// in binary to be easier to compare them in the reference
9+
/// </summary>
610
public enum QoiChunkEnum
711
{
8-
QOI_OP_RGB = 0b11111110,
9-
QOI_OP_RGBA = 0b11111111,
10-
QOI_OP_INDEX = 0b00000000,
11-
QOI_OP_DIFF = 0b01000000,
12-
QOI_OP_LUMA = 0b10000000,
13-
QOI_OP_RUN = 0b11000000
12+
/// <summary>
13+
/// Indicates that the operation is QOI_OP_RGB where the RGB values are written
14+
/// in one byte each one after this marker
15+
/// </summary>
16+
QoiOpRgb = 0b11111110,
17+
18+
/// <summary>
19+
/// Indicates that the operation is QOI_OP_RGBA where the RGBA values are written
20+
/// in one byte each one after this marker
21+
/// </summary>
22+
QoiOpRgba = 0b11111111,
23+
24+
/// <summary>
25+
/// Indicates that the operation is QOI_OP_INDEX where one byte contains a 2-bit
26+
/// marker (0b00) followed by an index on the previously seen pixels array 0..63
27+
/// </summary>
28+
QoiOpIndex = 0b00000000,
29+
30+
/// <summary>
31+
/// Indicates that the operation is QOI_OP_DIFF where one byte contains a 2-bit
32+
/// marker (0b01) followed by 2-bit differences in red, green and blue channel
33+
/// with the previous pixel with a bias of 2 (-2..1)
34+
/// </summary>
35+
QoiOpDiff = 0b01000000,
36+
37+
/// <summary>
38+
/// Indicates that the operation is QOI_OP_LUMA where one byte contains a 2-bit
39+
/// marker (0b01) followed by a 6-bits number that indicates the difference of
40+
/// the green channel with the previous pixel. Then another byte that contains
41+
/// a 4-bit number that indicates the difference of the red channel minus the
42+
/// previous difference, and another 4-bit number that indicates the difference
43+
/// of the blue channel minus the green difference
44+
/// Example: 0b10[6-bits diff green] 0b[6-bits dr-dg][6-bits db-dg]
45+
/// dr_dg = (cur_px.r - prev_px.r) - (cur_px.g - prev_px.g)
46+
/// db_dg = (cur_px.b - prev_px.b) - (cur_px.g - prev_px.g)
47+
/// </summary>
48+
QoiOpLuma = 0b10000000,
49+
50+
/// <summary>
51+
/// Indicates that the operation is QOI_OP_RUN where one byte contains a 2-bit
52+
/// marker (0b11) followed by a 6-bits number that indicates the times that the
53+
/// previous pixel is repeated
54+
/// </summary>
55+
QoiOpRun = 0b11000000
1456
}

src/ImageSharp/Formats/Qoi/QoiConstants.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ internal static class QoiConstants
1515
public static ReadOnlySpan<byte> Magic => SMagic;
1616

1717
/// <summary>
18-
/// The list of mimetypes that equate to a QOI.
18+
/// Gets the list of mimetypes that equate to a QOI.
1919
/// See https://github.com/phoboslab/qoi/issues/167
2020
/// </summary>
21-
public static readonly string[] MimeTypes = { "image/qoi", "image/x-qoi", "image/vnd.qoi" };
21+
public static string[] MimeTypes { get; } = { "image/qoi", "image/x-qoi", "image/vnd.qoi" };
2222

2323
/// <summary>
24-
/// The list of file extensions that equate to a QOI.
24+
/// Gets the list of file extensions that equate to a QOI.
2525
/// </summary>
26-
public static readonly string[] FileExtensions = { "qoi" };
26+
public static string[] FileExtensions { get; } = { "qoi" };
2727
}

src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
6161
};
6262
Image<TPixel> image = new(this.configuration, (int)this.header.Width, (int)this.header.Height, metadata);
6363
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
64-
64+
6565
this.ProcessPixels(stream, pixels);
6666

6767
return image;
@@ -145,11 +145,11 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
145145
where TPixel : unmanaged, IPixel<TPixel>
146146
{
147147
Rgba32[] previouslySeenPixels = new Rgba32[64];
148-
Rgba32 previousPixel = new(0,0,0,255);
148+
Rgba32 previousPixel = new(0, 0, 0, 255);
149149

150150
// We save the pixel to avoid loosing the fully opaque black pixel
151151
// See https://github.com/phoboslab/qoi/issues/258
152-
int pixelArrayPosition = this.GetArrayPosition(previousPixel);
152+
int pixelArrayPosition = GetArrayPosition(previousPixel);
153153
previouslySeenPixels[pixelArrayPosition] = previousPixel;
154154

155155
for (int i = 0; i < this.header.Height; i++)
@@ -159,11 +159,11 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
159159
byte operationByte = (byte)stream.ReadByte();
160160
byte[] pixelBytes;
161161
Rgba32 readPixel;
162-
TPixel pixel = new();
162+
TPixel pixel = default;
163163
switch ((QoiChunkEnum)operationByte)
164164
{
165165
// Reading one pixel with previous alpha intact
166-
case QoiChunkEnum.QOI_OP_RGB:
166+
case QoiChunkEnum.QoiOpRgb:
167167
pixelBytes = new byte[3];
168168
if (stream.Read(pixelBytes) < 3)
169169
{
@@ -172,12 +172,12 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
172172

173173
readPixel = previousPixel with { R = pixelBytes[0], G = pixelBytes[1], B = pixelBytes[2] };
174174
pixel.FromRgba32(readPixel);
175-
pixelArrayPosition = this.GetArrayPosition(readPixel);
175+
pixelArrayPosition = GetArrayPosition(readPixel);
176176
previouslySeenPixels[pixelArrayPosition] = readPixel;
177177
break;
178178

179179
// Reading one pixel with new alpha
180-
case QoiChunkEnum.QOI_OP_RGBA:
180+
case QoiChunkEnum.QoiOpRgba:
181181
pixelBytes = new byte[4];
182182
if (stream.Read(pixelBytes) < 4)
183183
{
@@ -186,21 +186,21 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
186186

187187
readPixel = new Rgba32(pixelBytes[0], pixelBytes[1], pixelBytes[2], pixelBytes[3]);
188188
pixel.FromRgba32(readPixel);
189-
pixelArrayPosition = this.GetArrayPosition(readPixel);
189+
pixelArrayPosition = GetArrayPosition(readPixel);
190190
previouslySeenPixels[pixelArrayPosition] = readPixel;
191191
break;
192192

193193
default:
194194
switch ((QoiChunkEnum)(operationByte & 0b11000000))
195195
{
196196
// Getting one pixel from previously seen pixels
197-
case QoiChunkEnum.QOI_OP_INDEX:
197+
case QoiChunkEnum.QoiOpIndex:
198198
readPixel = previouslySeenPixels[operationByte];
199199
pixel.FromRgba32(readPixel);
200200
break;
201201

202202
// Get one pixel from the difference (-2..1) of the previous pixel
203-
case QoiChunkEnum.QOI_OP_DIFF:
203+
case QoiChunkEnum.QoiOpDiff:
204204
byte redDifference = (byte)((operationByte & 0b00110000) >> 4),
205205
greenDifference = (byte)((operationByte & 0b00001100) >> 2),
206206
blueDifference = (byte)(operationByte & 0b00000011);
@@ -211,30 +211,30 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
211211
B = (byte)((previousPixel.B + (blueDifference - 2)) % 256)
212212
};
213213
pixel.FromRgba32(readPixel);
214-
pixelArrayPosition = this.GetArrayPosition(readPixel);
214+
pixelArrayPosition = GetArrayPosition(readPixel);
215215
previouslySeenPixels[pixelArrayPosition] = readPixel;
216216
break;
217217

218218
// Get green difference in 6 bits and red and blue differences
219219
// depending on the green one
220-
case QoiChunkEnum.QOI_OP_LUMA:
220+
case QoiChunkEnum.QoiOpLuma:
221221
byte diffGreen = (byte)(operationByte & 0b00111111),
222222
currentGreen = (byte)((previousPixel.G + (diffGreen - 32)) % 256),
223223
nextByte = (byte)stream.ReadByte(),
224224
diffRedDG = (byte)(nextByte >> 4),
225225
diffBlueDG = (byte)(nextByte & 0b00001111),
226-
currentRed = (byte)((diffRedDG-8 + (diffGreen - 32) + previousPixel.R)%256),
227-
currentBlue = (byte)((diffBlueDG-8 + (diffGreen - 32) + previousPixel.B)%256);
226+
currentRed = (byte)((diffRedDG - 8 + (diffGreen - 32) + previousPixel.R) % 256),
227+
currentBlue = (byte)((diffBlueDG - 8 + (diffGreen - 32) + previousPixel.B) % 256);
228228
readPixel = previousPixel with { R = currentRed, B = currentBlue, G = currentGreen };
229229
pixel.FromRgba32(readPixel);
230-
pixelArrayPosition = this.GetArrayPosition(readPixel);
230+
pixelArrayPosition = GetArrayPosition(readPixel);
231231
previouslySeenPixels[pixelArrayPosition] = readPixel;
232232
break;
233233

234234
// Repeating the previous pixel 1..63 times
235-
case QoiChunkEnum.QOI_OP_RUN:
235+
case QoiChunkEnum.QoiOpRun:
236236
byte repetitions = (byte)(operationByte & 0b00111111);
237-
if(repetitions is 62 or 63)
237+
if (repetitions is 62 or 63)
238238
{
239239
ThrowInvalidImageContentException();
240240
}
@@ -248,7 +248,8 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
248248
j = 0;
249249
i++;
250250
}
251-
pixels[j,i] = pixel;
251+
252+
pixels[j, i] = pixel;
252253
}
253254

254255
j--;
@@ -262,7 +263,7 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
262263
break;
263264
}
264265

265-
pixels[j,i] = pixel;
266+
pixels[j, i] = pixel;
266267
previousPixel = readPixel;
267268
}
268269
}
@@ -282,5 +283,5 @@ private void ProcessPixels<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
282283
}
283284
}
284285

285-
private int GetArrayPosition(Rgba32 pixel) => ((pixel.R * 3) + (pixel.G * 5) + (pixel.B * 7) + (pixel.A * 11)) % 64;
286+
private static int GetArrayPosition(Rgba32 pixel) => ((pixel.R * 3) + (pixel.G * 5) + (pixel.B * 7) + (pixel.A * 11)) % 64;
286287
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using SixLabors.ImageSharp.Advanced;
5-
64
namespace SixLabors.ImageSharp.Formats.Qoi;
75

86
/// <summary>
@@ -13,7 +11,7 @@ public class QoiEncoder : ImageEncoder
1311
/// <inheritdoc />
1412
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
1513
{
16-
QoiEncoderCore encoder = new(image.GetConfiguration(), this);
14+
QoiEncoderCore encoder = new();
1715
encoder.Encode(image, stream, cancellationToken);
1816
}
1917
}

src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Buffers.Binary;
5-
using System.Runtime.InteropServices.ComTypes;
65
using SixLabors.ImageSharp.Memory;
76
using SixLabors.ImageSharp.PixelFormats;
87

@@ -13,25 +12,11 @@ namespace SixLabors.ImageSharp.Formats.Qoi;
1312
/// </summary>
1413
public class QoiEncoderCore : IImageEncoderInternals
1514
{
16-
/// <summary>
17-
/// The global configuration.
18-
/// </summary>
19-
private Configuration configuration;
20-
21-
/// <summary>
22-
/// The encoder with options.
23-
/// </summary>
24-
private readonly QoiEncoder encoder;
25-
2615
/// <summary>
2716
/// Initializes a new instance of the <see cref="QoiEncoderCore"/> class.
2817
/// </summary>
29-
/// <param name="configuration">The configuration.</param>
30-
/// <param name="encoder">The encoder with options.</param>
31-
public QoiEncoderCore(Configuration configuration, QoiEncoder encoder)
18+
public QoiEncoderCore()
3219
{
33-
this.configuration = configuration;
34-
this.encoder = encoder;
3520
}
3621

3722
/// <inheritdoc />
@@ -67,7 +52,8 @@ private static void WriteHeader(Image image, Stream stream)
6752
stream.WriteByte((byte)qoiColorSpace);
6853
}
6954

70-
private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream) where TPixel : unmanaged, IPixel<TPixel>
55+
private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream)
56+
where TPixel : unmanaged, IPixel<TPixel>
7157
{
7258
// Start image encoding
7359
Rgba32[] previouslySeenPixels = new Rgba32[64];
@@ -76,7 +62,7 @@ private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream) wher
7662
previouslySeenPixels[pixelArrayPosition] = previousPixel;
7763

7864
Buffer2D<TPixel> pixels = image.Frames[0].PixelBuffer;
79-
Rgba32 currentRgba32 = new();
65+
Rgba32 currentRgba32 = default;
8066
for (int i = 0; i < pixels.Height; i++)
8167
{
8268
for (int j = 0; j < pixels.Width && i < pixels.Height; j++)
@@ -115,10 +101,11 @@ private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream) wher
115101

116102
currentPixel = pixels[j, i];
117103
currentPixel.ToRgba32(ref currentRgba32);
118-
} while (currentRgba32.Equals(previousPixel) && repetitions < 62);
104+
}
105+
while (currentRgba32.Equals(previousPixel) && repetitions < 62);
119106

120107
j--;
121-
stream.WriteByte((byte)((byte)QoiChunkEnum.QOI_OP_RUN | (repetitions - 1)));
108+
stream.WriteByte((byte)((byte)QoiChunkEnum.QoiOpRun | (repetitions - 1)));
122109

123110
/* If it's a QOI_OP_RUN, we don't overwrite the previous pixel since
124111
* it will be taken and compared on the next iteration
@@ -153,7 +140,7 @@ diffBlue is > -3 and < 2 &&
153140
byte dr = (byte)(diffRed + 2),
154141
dg = (byte)(diffGreen + 2),
155142
db = (byte)(diffBlue + 2),
156-
valueToWrite = (byte)((byte)QoiChunkEnum.QOI_OP_DIFF | (dr << 4) | (dg << 2) | db);
143+
valueToWrite = (byte)((byte)QoiChunkEnum.QoiOpDiff | (dr << 4) | (dg << 2) | db);
157144
stream.WriteByte(valueToWrite);
158145
}
159146
else
@@ -169,7 +156,7 @@ diffBlueGreen is > -9 and < 8 &&
169156
{
170157
byte dr_dg = (byte)(diffRedGreen + 8),
171158
db_dg = (byte)(diffBlueGreen + 8),
172-
byteToWrite1 = (byte)((byte)QoiChunkEnum.QOI_OP_LUMA | (diffGreen + 32)),
159+
byteToWrite1 = (byte)((byte)QoiChunkEnum.QoiOpLuma | (diffGreen + 32)),
173160
byteToWrite2 = (byte)((dr_dg << 4) | db_dg);
174161
stream.WriteByte(byteToWrite1);
175162
stream.WriteByte(byteToWrite2);
@@ -180,15 +167,15 @@ diffBlueGreen is > -9 and < 8 &&
180167
// If so, we do a QOI_OP_RGB
181168
if (currentRgba32.A == previousPixel.A)
182169
{
183-
stream.WriteByte((byte)QoiChunkEnum.QOI_OP_RGB);
170+
stream.WriteByte((byte)QoiChunkEnum.QoiOpRgb);
184171
stream.WriteByte(currentRgba32.R);
185172
stream.WriteByte(currentRgba32.G);
186173
stream.WriteByte(currentRgba32.B);
187174
}
188175
else
189176
{
190177
// else, we do a QOI_OP_RGBA
191-
stream.WriteByte((byte)QoiChunkEnum.QOI_OP_RGBA);
178+
stream.WriteByte((byte)QoiChunkEnum.QoiOpRgba);
192179
stream.WriteByte(currentRgba32.R);
193180
stream.WriteByte(currentRgba32.G);
194181
stream.WriteByte(currentRgba32.B);
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using SixLabors.ImageSharp.Formats.Png;
5-
64
namespace SixLabors.ImageSharp.Formats.Qoi;
75

86
/// <summary>
@@ -11,25 +9,26 @@ namespace SixLabors.ImageSharp.Formats.Qoi;
119
public sealed class QoiFormat : IImageFormat<QoiMetadata>
1210
{
1311
private QoiFormat()
14-
{ }
12+
{
13+
}
1514

1615
/// <summary>
1716
/// Gets the shared instance.
1817
/// </summary>
1918
public static QoiFormat Instance { get; } = new QoiFormat();
2019

2120
/// <inheritdoc/>
22-
public QoiMetadata CreateDefaultFormatMetadata() => new();
21+
public string DefaultMimeType => "image/qoi";
2322

2423
/// <inheritdoc/>
2524
public string Name => "QOI";
2625

27-
/// <inheritdoc/>
28-
public string DefaultMimeType => "image/qoi";
29-
3026
/// <inheritdoc/>
3127
public IEnumerable<string> MimeTypes => QoiConstants.MimeTypes;
3228

3329
/// <inheritdoc/>
3430
public IEnumerable<string> FileExtensions => QoiConstants.FileExtensions;
31+
32+
/// <inheritdoc/>
33+
public QoiMetadata CreateDefaultFormatMetadata() => new();
3534
}

0 commit comments

Comments
 (0)