Skip to content

Commit 60d8763

Browse files
committed
Adding tests and fixing bugs
Now it identifies qoi images successfully
1 parent c79dd77 commit 60d8763

File tree

17 files changed

+80
-13
lines changed

17 files changed

+80
-13
lines changed

ImageSharp.sln

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tga", "Tga", "{5DFC394F-136
647647
tests\Images\Input\Tga\targa_8bit_rle.tga = tests\Images\Input\Tga\targa_8bit_rle.tga
648648
EndProjectSection
649649
EndProject
650+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Qoi", "Qoi", "{E801B508-4935-41CD-BA85-CF11BFF55A45}"
651+
ProjectSection(SolutionItems) = preProject
652+
tests\Images\Input\Qoi\dice.qoi = tests\Images\Input\Qoi\dice.qoi
653+
tests\Images\Input\Qoi\edgecase.qoi = tests\Images\Input\Qoi\edgecase.qoi
654+
tests\Images\Input\Qoi\kodim10.qoi = tests\Images\Input\Qoi\kodim10.qoi
655+
tests\Images\Input\Qoi\kodim23.qoi = tests\Images\Input\Qoi\kodim23.qoi
656+
tests\Images\Input\Qoi\qoi_logo.qoi = tests\Images\Input\Qoi\qoi_logo.qoi
657+
tests\Images\Input\Qoi\testcard.qoi = tests\Images\Input\Qoi\testcard.qoi
658+
tests\Images\Input\Qoi\testcard_rgba.qoi = tests\Images\Input\Qoi\testcard_rgba.qoi
659+
tests\Images\Input\Qoi\wikipedia_008.qoi = tests\Images\Input\Qoi\wikipedia_008.qoi
660+
EndProjectSection
661+
EndProject
650662
Global
651663
GlobalSection(SolutionConfigurationPlatforms) = preSolution
652664
Debug|Any CPU = Debug|Any CPU
@@ -699,6 +711,7 @@ Global
699711
{FC527290-2F22-432C-B77B-6E815726B02C} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
700712
{670DD46C-82E9-499A-B2D2-00A802ED0141} = {E1C42A6F-913B-4A7B-B1A8-2BB62843B254}
701713
{5DFC394F-136F-4B76-9BCA-3BA786515EFC} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
714+
{E801B508-4935-41CD-BA85-CF11BFF55A45} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
702715
EndGlobalSection
703716
GlobalSection(ExtensibilityGlobals) = postSolution
704717
SolutionGuid = {5F8B9D1F-CD8B-4CC5-8216-D531E25BD795}

src/ImageSharp/Formats/Qoi/QoiColorSpace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public enum QoiColorSpace
1313
/// <summary>
1414
/// sRGB color space with linear alpha value
1515
/// </summary>
16-
SRGB_WITH_LINEAR_ALPHA,
16+
SrgbWithLinearAlpha,
1717

1818
/// <summary>
1919
/// All the values in the color space are linear
2020
/// </summary>
21-
ALL_CHANNELS_LINEAR
21+
AllChannelsLinear
2222
}

src/ImageSharp/Formats/Qoi/QoiConstants.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ namespace SixLabors.ImageSharp.Formats.Qoi;
77

88
internal static class QoiConstants
99
{
10-
1110
/// <summary>
1211
/// Gets the bytes that indicates the image is QOI
1312
/// </summary>
1413
public static ReadOnlySpan<byte> Magic => Encoding.UTF8.GetBytes("qoif");
1514

1615
/// <summary>
1716
/// The list of mimetypes that equate to a QOI.
18-
/// See <seealso cref="https://github.com/phoboslab/qoi/issues/167"/>
17+
/// See https://github.com/phoboslab/qoi/issues/167
1918
/// </summary>
2019
public static readonly IEnumerable<string> MimeTypes = new[] { "image/qoi", "image/x-qoi", "image/vnd.qoi" };
2120

src/ImageSharp/Formats/Qoi/QoiDecoder.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using SixLabors.ImageSharp.Formats.Png;
5+
46
namespace SixLabors.ImageSharp.Formats.Qoi;
57
internal class QoiDecoder : ImageDecoder
68
{
9+
private QoiDecoder()
10+
{
11+
}
12+
13+
public static QoiDecoder Instance { get; } = new();
14+
715
protected override Image<TPixel> Decode<TPixel>(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
816
{
917
Guard.NotNull(options, nameof(options));
@@ -22,6 +30,6 @@ protected override ImageInfo Identify(DecoderOptions options, Stream stream, Can
2230
{
2331
Guard.NotNull(options, nameof(options));
2432
Guard.NotNull(stream, nameof(stream));
25-
throw new NotImplementedException();
33+
return new QoiDecoderCore(options).Identify(options.Configuration, stream, cancellationToken);
2634
}
2735
}

src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,31 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
4949
{
5050
ImageMetadata metadata = new();
5151

52-
byte[] widthBytes, heightBytes;
53-
byte[] magicBytes = widthBytes = heightBytes = Array.Empty<byte>();
52+
byte[] magicBytes = new byte[4], widthBytes = new byte[4], heightBytes = new byte[4];
5453

5554
// Read magic bytes
56-
int read = stream.Read(magicBytes, 0, 4);
57-
if (read != 4 || !magicBytes.Equals(QoiConstants.Magic.ToArray()))
55+
int read = stream.Read(magicBytes);
56+
if (read != 4 || !magicBytes.SequenceEqual(QoiConstants.Magic.ToArray()))
5857
{
5958
throw new InvalidImageContentException("The image is not a QOI image");
6059
}
6160

6261
// If it's a qoi image, read the rest of properties
63-
read = stream.Read(widthBytes, 0, 4);
62+
read = stream.Read(widthBytes);
6463
if (read != 4)
6564
{
6665
throw new InvalidImageContentException("The image is not a QOI image");
6766
}
6867

69-
read = stream.Read(heightBytes, 0, 4);
68+
read = stream.Read(heightBytes);
7069
if (read != 4)
7170
{
7271
throw new InvalidImageContentException("The image is not a QOI image");
7372
}
7473

75-
Size size = new(BitConverter.ToInt32(widthBytes), BitConverter.ToInt32(heightBytes));
74+
widthBytes = widthBytes.Reverse().ToArray();
75+
heightBytes = heightBytes.Reverse().ToArray();
76+
Size size = new((int)BitConverter.ToUInt32(widthBytes), (int)BitConverter.ToUInt32(heightBytes));
7677

7778
int channels = stream.ReadByte();
7879
if (channels == -1)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using BenchmarkDotNet.Attributes;
5+
using SixLabors.ImageSharp.Formats;
6+
using SixLabors.ImageSharp.Formats.Qoi;
7+
using SixLabors.ImageSharp.Tests;
8+
9+
namespace SixLabors.ImageSharp.Benchmarks.Codecs.Qoi;
10+
11+
[Config(typeof(Config.ShortMultiFramework))]
12+
public class IdentifyQoi
13+
{
14+
private byte[] qoiBytes;
15+
16+
private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage);
17+
18+
[Params(TestImages.Qoi.TestCardRGBA, TestImages.Qoi.TestCard, TestImages.Qoi.QoiLogo, TestImages.Qoi.EdgeCase, TestImages.Png.Bike)]
19+
public string TestImage { get; set; }
20+
21+
[GlobalSetup]
22+
public void ReadImages() => this.qoiBytes ??= File.ReadAllBytes(this.TestImageFullPath);
23+
24+
[Benchmark]
25+
public ImageInfo Identify()
26+
{
27+
using MemoryStream memoryStream = new(this.qoiBytes);
28+
return QoiDecoder.Instance.Identify(DecoderOptions.Default, memoryStream);
29+
}
30+
}

tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,8 @@
6969
<Compile Remove="PixelBlenders\**" />
7070
<Compile Remove="Processing\Resize.cs" />
7171
</ItemGroup>
72+
<ItemGroup>
73+
<Folder Include="Codecs\Qoi\" />
74+
</ItemGroup>
7275

7376
</Project>

tests/ImageSharp.Benchmarks/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using BenchmarkDotNet.Configs;
45
using BenchmarkDotNet.Running;
56

67
namespace SixLabors.ImageSharp.Benchmarks;
@@ -15,5 +16,5 @@ public class Program
1516
/// </param>
1617
public static void Main(string[] args) => BenchmarkSwitcher
1718
.FromAssembly(typeof(Program).Assembly)
18-
.Run(args);
19+
.Run(args, new DebugInProcessConfig());
1920
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,4 +1029,16 @@ public static class Pbm
10291029
public const string RgbPlainNormalized = "Pbm/rgb_plain_normalized.ppm";
10301030
public const string RgbPlainMagick = "Pbm/rgb_plain_magick.ppm";
10311031
}
1032+
1033+
public static class Qoi
1034+
{
1035+
public const string Dice = "Qoi/dice.qoi";
1036+
public const string EdgeCase = "Qoi/edgecase.qoi";
1037+
public const string Kodim10 = "Qoi/kodim10.qoi";
1038+
public const string Kodim23 = "Qoi/kodim23.qoi";
1039+
public const string QoiLogo = "Qoi/qoi_logo.qoi";
1040+
public const string TestCard = "Qoi/testcard.qoi";
1041+
public const string TestCardRGBA = "Qoi/testcard_rgba.qoi";
1042+
public const string Wikipedia008 = "Qoi/wikipedia_008.qoi";
1043+
}
10321044
}

tests/Images/Input/Qoi/dice.qoi

507 KB
Binary file not shown.

0 commit comments

Comments
 (0)