Browse Source

Adding tests and fixing bugs

Now it identifies qoi images successfully
qoi
LuisAlfredo92 3 years ago
parent
commit
60d87635fb
No known key found for this signature in database GPG Key ID: 13A8436905993B8F
  1. 13
      ImageSharp.sln
  2. 4
      src/ImageSharp/Formats/Qoi/QoiColorSpace.cs
  3. 3
      src/ImageSharp/Formats/Qoi/QoiConstants.cs
  4. 10
      src/ImageSharp/Formats/Qoi/QoiDecoder.cs
  5. 15
      src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
  6. 30
      tests/ImageSharp.Benchmarks/Codecs/Qoi/IdentifyQoi.cs
  7. 3
      tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj
  8. 3
      tests/ImageSharp.Benchmarks/Program.cs
  9. 12
      tests/ImageSharp.Tests/TestImages.cs
  10. BIN
      tests/Images/Input/Qoi/dice.qoi
  11. BIN
      tests/Images/Input/Qoi/edgecase.qoi
  12. BIN
      tests/Images/Input/Qoi/kodim10.qoi
  13. BIN
      tests/Images/Input/Qoi/kodim23.qoi
  14. BIN
      tests/Images/Input/Qoi/qoi_logo.qoi
  15. BIN
      tests/Images/Input/Qoi/testcard.qoi
  16. BIN
      tests/Images/Input/Qoi/testcard_rgba.qoi
  17. BIN
      tests/Images/Input/Qoi/wikipedia_008.qoi

13
ImageSharp.sln

@ -647,6 +647,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tga", "Tga", "{5DFC394F-136
tests\Images\Input\Tga\targa_8bit_rle.tga = tests\Images\Input\Tga\targa_8bit_rle.tga
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Qoi", "Qoi", "{E801B508-4935-41CD-BA85-CF11BFF55A45}"
ProjectSection(SolutionItems) = preProject
tests\Images\Input\Qoi\dice.qoi = tests\Images\Input\Qoi\dice.qoi
tests\Images\Input\Qoi\edgecase.qoi = tests\Images\Input\Qoi\edgecase.qoi
tests\Images\Input\Qoi\kodim10.qoi = tests\Images\Input\Qoi\kodim10.qoi
tests\Images\Input\Qoi\kodim23.qoi = tests\Images\Input\Qoi\kodim23.qoi
tests\Images\Input\Qoi\qoi_logo.qoi = tests\Images\Input\Qoi\qoi_logo.qoi
tests\Images\Input\Qoi\testcard.qoi = tests\Images\Input\Qoi\testcard.qoi
tests\Images\Input\Qoi\testcard_rgba.qoi = tests\Images\Input\Qoi\testcard_rgba.qoi
tests\Images\Input\Qoi\wikipedia_008.qoi = tests\Images\Input\Qoi\wikipedia_008.qoi
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -699,6 +711,7 @@ Global
{FC527290-2F22-432C-B77B-6E815726B02C} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
{670DD46C-82E9-499A-B2D2-00A802ED0141} = {E1C42A6F-913B-4A7B-B1A8-2BB62843B254}
{5DFC394F-136F-4B76-9BCA-3BA786515EFC} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
{E801B508-4935-41CD-BA85-CF11BFF55A45} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5F8B9D1F-CD8B-4CC5-8216-D531E25BD795}

4
src/ImageSharp/Formats/Qoi/QoiColorSpace.cs

@ -13,10 +13,10 @@ public enum QoiColorSpace
/// <summary>
/// sRGB color space with linear alpha value
/// </summary>
SRGB_WITH_LINEAR_ALPHA,
SrgbWithLinearAlpha,
/// <summary>
/// All the values in the color space are linear
/// </summary>
ALL_CHANNELS_LINEAR
AllChannelsLinear
}

3
src/ImageSharp/Formats/Qoi/QoiConstants.cs

@ -7,7 +7,6 @@ namespace SixLabors.ImageSharp.Formats.Qoi;
internal static class QoiConstants
{
/// <summary>
/// Gets the bytes that indicates the image is QOI
/// </summary>
@ -15,7 +14,7 @@ internal static class QoiConstants
/// <summary>
/// The list of mimetypes that equate to a QOI.
/// See <seealso cref="https://github.com/phoboslab/qoi/issues/167"/>
/// See https://github.com/phoboslab/qoi/issues/167
/// </summary>
public static readonly IEnumerable<string> MimeTypes = new[] { "image/qoi", "image/x-qoi", "image/vnd.qoi" };

10
src/ImageSharp/Formats/Qoi/QoiDecoder.cs

@ -1,9 +1,17 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats.Png;
namespace SixLabors.ImageSharp.Formats.Qoi;
internal class QoiDecoder : ImageDecoder
{
private QoiDecoder()
{
}
public static QoiDecoder Instance { get; } = new();
protected override Image<TPixel> Decode<TPixel>(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
{
Guard.NotNull(options, nameof(options));
@ -22,6 +30,6 @@ internal class QoiDecoder : ImageDecoder
{
Guard.NotNull(options, nameof(options));
Guard.NotNull(stream, nameof(stream));
throw new NotImplementedException();
return new QoiDecoderCore(options).Identify(options.Configuration, stream, cancellationToken);
}
}

15
src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

@ -49,30 +49,31 @@ internal class QoiDecoderCore : IImageDecoderInternals
{
ImageMetadata metadata = new();
byte[] widthBytes, heightBytes;
byte[] magicBytes = widthBytes = heightBytes = Array.Empty<byte>();
byte[] magicBytes = new byte[4], widthBytes = new byte[4], heightBytes = new byte[4];
// Read magic bytes
int read = stream.Read(magicBytes, 0, 4);
if (read != 4 || !magicBytes.Equals(QoiConstants.Magic.ToArray()))
int read = stream.Read(magicBytes);
if (read != 4 || !magicBytes.SequenceEqual(QoiConstants.Magic.ToArray()))
{
throw new InvalidImageContentException("The image is not a QOI image");
}
// If it's a qoi image, read the rest of properties
read = stream.Read(widthBytes, 0, 4);
read = stream.Read(widthBytes);
if (read != 4)
{
throw new InvalidImageContentException("The image is not a QOI image");
}
read = stream.Read(heightBytes, 0, 4);
read = stream.Read(heightBytes);
if (read != 4)
{
throw new InvalidImageContentException("The image is not a QOI image");
}
Size size = new(BitConverter.ToInt32(widthBytes), BitConverter.ToInt32(heightBytes));
widthBytes = widthBytes.Reverse().ToArray();
heightBytes = heightBytes.Reverse().ToArray();
Size size = new((int)BitConverter.ToUInt32(widthBytes), (int)BitConverter.ToUInt32(heightBytes));
int channels = stream.ReadByte();
if (channels == -1)

30
tests/ImageSharp.Benchmarks/Codecs/Qoi/IdentifyQoi.cs

@ -0,0 +1,30 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Qoi;
using SixLabors.ImageSharp.Tests;
namespace SixLabors.ImageSharp.Benchmarks.Codecs.Qoi;
[Config(typeof(Config.ShortMultiFramework))]
public class IdentifyQoi
{
private byte[] qoiBytes;
private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage);
[Params(TestImages.Qoi.TestCardRGBA, TestImages.Qoi.TestCard, TestImages.Qoi.QoiLogo, TestImages.Qoi.EdgeCase, TestImages.Png.Bike)]
public string TestImage { get; set; }
[GlobalSetup]
public void ReadImages() => this.qoiBytes ??= File.ReadAllBytes(this.TestImageFullPath);
[Benchmark]
public ImageInfo Identify()
{
using MemoryStream memoryStream = new(this.qoiBytes);
return QoiDecoder.Instance.Identify(DecoderOptions.Default, memoryStream);
}
}

3
tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj

@ -69,5 +69,8 @@
<Compile Remove="PixelBlenders\**" />
<Compile Remove="Processing\Resize.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Codecs\Qoi\" />
</ItemGroup>
</Project>

3
tests/ImageSharp.Benchmarks/Program.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
namespace SixLabors.ImageSharp.Benchmarks;
@ -15,5 +16,5 @@ public class Program
/// </param>
public static void Main(string[] args) => BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
.Run(args);
.Run(args, new DebugInProcessConfig());
}

12
tests/ImageSharp.Tests/TestImages.cs

@ -1029,4 +1029,16 @@ public static class TestImages
public const string RgbPlainNormalized = "Pbm/rgb_plain_normalized.ppm";
public const string RgbPlainMagick = "Pbm/rgb_plain_magick.ppm";
}
public static class Qoi
{
public const string Dice = "Qoi/dice.qoi";
public const string EdgeCase = "Qoi/edgecase.qoi";
public const string Kodim10 = "Qoi/kodim10.qoi";
public const string Kodim23 = "Qoi/kodim23.qoi";
public const string QoiLogo = "Qoi/qoi_logo.qoi";
public const string TestCard = "Qoi/testcard.qoi";
public const string TestCardRGBA = "Qoi/testcard_rgba.qoi";
public const string Wikipedia008 = "Qoi/wikipedia_008.qoi";
}
}

BIN
tests/Images/Input/Qoi/dice.qoi

Binary file not shown.

BIN
tests/Images/Input/Qoi/edgecase.qoi

Binary file not shown.

BIN
tests/Images/Input/Qoi/kodim10.qoi

Binary file not shown.

BIN
tests/Images/Input/Qoi/kodim23.qoi

Binary file not shown.

BIN
tests/Images/Input/Qoi/qoi_logo.qoi

Binary file not shown.

BIN
tests/Images/Input/Qoi/testcard.qoi

Binary file not shown.

BIN
tests/Images/Input/Qoi/testcard_rgba.qoi

Binary file not shown.

BIN
tests/Images/Input/Qoi/wikipedia_008.qoi

Binary file not shown.
Loading…
Cancel
Save