Skip to content

Commit c79dd77

Browse files
committed
Adding qoi identification
I'm going to do some tests before the pull request and add all the functions
1 parent 6784acf commit c79dd77

File tree

5 files changed

+175
-3
lines changed

5 files changed

+175
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using SixLabors.ImageSharp.Formats.Qoi;
5+
using SixLabors.ImageSharp.Metadata;
6+
7+
namespace SixLabors.ImageSharp;
8+
9+
/// <summary>
10+
/// Extension methods for the <see cref="ImageMetadata"/> type.
11+
/// </summary>
12+
public static partial class MetadataExtensions
13+
{
14+
/// <summary>
15+
/// Gets the qoi format specific metadata for the image.
16+
/// </summary>
17+
/// <param name="metadata">The metadata this method extends.</param>
18+
/// <returns>The <see cref="QoiMetadata"/>.</returns>
19+
public static QoiMetadata GetQoiMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(QoiFormat.Instance);
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.ImageSharp.Formats.Qoi;
5+
internal class QoiDecoder : ImageDecoder
6+
{
7+
protected override Image<TPixel> Decode<TPixel>(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
8+
{
9+
Guard.NotNull(options, nameof(options));
10+
Guard.NotNull(stream, nameof(stream));
11+
throw new NotImplementedException();
12+
}
13+
14+
protected override Image Decode(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
15+
{
16+
Guard.NotNull(options, nameof(options));
17+
Guard.NotNull(stream, nameof(stream));
18+
throw new NotImplementedException();
19+
}
20+
21+
protected override ImageInfo Identify(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
22+
{
23+
Guard.NotNull(options, nameof(options));
24+
Guard.NotNull(stream, nameof(stream));
25+
throw new NotImplementedException();
26+
}
27+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using SixLabors.ImageSharp.IO;
5+
using SixLabors.ImageSharp.Memory;
6+
using SixLabors.ImageSharp.Metadata;
7+
using SixLabors.ImageSharp.PixelFormats;
8+
9+
namespace SixLabors.ImageSharp.Formats.Qoi;
10+
11+
internal class QoiDecoderCore : IImageDecoderInternals
12+
{
13+
/// <summary>
14+
/// The global configuration.
15+
/// </summary>
16+
private readonly Configuration configuration;
17+
18+
/// <summary>
19+
/// Used the manage memory allocations.
20+
/// </summary>
21+
private readonly MemoryAllocator memoryAllocator;
22+
23+
/// <summary>
24+
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
25+
/// </summary>
26+
private readonly bool skipMetadata;
27+
28+
/// <summary>
29+
/// The QOI header.
30+
/// </summary>
31+
private QoiHeader header;
32+
33+
public QoiDecoderCore(DecoderOptions options)
34+
{
35+
this.Options = options;
36+
this.configuration = options.Configuration;
37+
this.skipMetadata = options.SkipMetadata;
38+
this.memoryAllocator = this.configuration.MemoryAllocator;
39+
}
40+
41+
public DecoderOptions Options { get; }
42+
43+
public Size Dimensions { get; }
44+
45+
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
46+
where TPixel : unmanaged, IPixel<TPixel> => throw new NotImplementedException();
47+
48+
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
49+
{
50+
ImageMetadata metadata = new();
51+
52+
byte[] widthBytes, heightBytes;
53+
byte[] magicBytes = widthBytes = heightBytes = Array.Empty<byte>();
54+
55+
// Read magic bytes
56+
int read = stream.Read(magicBytes, 0, 4);
57+
if (read != 4 || !magicBytes.Equals(QoiConstants.Magic.ToArray()))
58+
{
59+
throw new InvalidImageContentException("The image is not a QOI image");
60+
}
61+
62+
// If it's a qoi image, read the rest of properties
63+
read = stream.Read(widthBytes, 0, 4);
64+
if (read != 4)
65+
{
66+
throw new InvalidImageContentException("The image is not a QOI image");
67+
}
68+
69+
read = stream.Read(heightBytes, 0, 4);
70+
if (read != 4)
71+
{
72+
throw new InvalidImageContentException("The image is not a QOI image");
73+
}
74+
75+
Size size = new(BitConverter.ToInt32(widthBytes), BitConverter.ToInt32(heightBytes));
76+
77+
int channels = stream.ReadByte();
78+
if (channels == -1)
79+
{
80+
throw new InvalidImageContentException("The image is not a QOI image");
81+
}
82+
83+
PixelTypeInfo pixelType = new(8 * channels);
84+
85+
int colorSpace = stream.ReadByte();
86+
if (colorSpace == -1)
87+
{
88+
throw new InvalidImageContentException("The image is not a QOI image");
89+
}
90+
91+
return new ImageInfo(pixelType, size, metadata);
92+
}
93+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using SixLabors.ImageSharp.Formats.Png;
5+
6+
namespace SixLabors.ImageSharp.Formats.Qoi;
7+
8+
/// <summary>
9+
/// Registers the image encoders, decoders and mime type detectors for the qoi format.
10+
/// </summary>
11+
public sealed class QoiFormat : IImageFormat<QoiMetadata>
12+
{
13+
private QoiFormat()
14+
{ }
15+
16+
/// <summary>
17+
/// Gets the shared instance.
18+
/// </summary>
19+
public static QoiFormat Instance { get; } = new QoiFormat();
20+
21+
/// <inheritdoc/>
22+
public QoiMetadata CreateDefaultFormatMetadata() => new();
23+
24+
/// <inheritdoc/>
25+
public string Name => "QOI";
26+
27+
/// <inheritdoc/>
28+
public string DefaultMimeType => "image/qoi";
29+
30+
/// <inheritdoc/>
31+
public IEnumerable<string> MimeTypes => QoiConstants.MimeTypes;
32+
33+
/// <inheritdoc/>
34+
public IEnumerable<string> FileExtensions => QoiConstants.FileExtensions;
35+
}

src/ImageSharp/Formats/Qoi/QoiMetadata.cs

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

4-
using System.Text;
5-
using SixLabors.ImageSharp.PixelFormats;
6-
74
namespace SixLabors.ImageSharp.Formats.Qoi;
85

96
/// <summary>

0 commit comments

Comments
 (0)