// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Threading; using ImageMagick; using ImageMagick.Formats; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs { public class MagickReferenceDecoder : IImageDecoder { private readonly bool validate; public MagickReferenceDecoder() : this(true) { } public MagickReferenceDecoder(bool validate) => this.validate = validate; public static MagickReferenceDecoder Instance { get; } = new(); public Image Decode(DecoderOptions options, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { Configuration configuration = options.Configuration; var bmpReadDefines = new BmpReadDefines { IgnoreFileSize = !this.validate }; var settings = new MagickReadSettings(); settings.SetDefines(bmpReadDefines); using var magickImageCollection = new MagickImageCollection(stream, settings); var framesList = new List>(); foreach (IMagickImage magicFrame in magickImageCollection) { var frame = new ImageFrame(configuration, magicFrame.Width, magicFrame.Height); framesList.Add(frame); MemoryGroup framePixels = frame.PixelBuffer.FastMemoryGroup; using IUnsafePixelCollection pixels = magicFrame.GetPixelsUnsafe(); if (magicFrame.Depth is 12 or 10 or 8 or 6 or 5 or 4 or 3 or 2 or 1) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); FromRgba32Bytes(configuration, data, framePixels); } else if (magicFrame.Depth is 16 or 14) { ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span bytes = MemoryMarshal.Cast(data.AsSpan()); FromRgba64Bytes(configuration, bytes, framePixels); } else { throw new InvalidOperationException(); } } return new Image(configuration, new ImageMetadata(), framesList); } public Image Decode(DecoderOptions options, Stream stream, CancellationToken cancellationToken) => this.Decode(options, stream, cancellationToken); public IImageInfo Identify(DecoderOptions options, Stream stream, CancellationToken cancellationToken) => this.Decode(options, stream, cancellationToken); private static void FromRgba32Bytes(Configuration configuration, Span rgbaBytes, IMemoryGroup destinationGroup) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { Span sourcePixels = MemoryMarshal.Cast(rgbaBytes); foreach (Memory m in destinationGroup) { Span destBuffer = m.Span; PixelOperations.Instance.FromRgba32( configuration, sourcePixels.Slice(0, destBuffer.Length), destBuffer); sourcePixels = sourcePixels.Slice(destBuffer.Length); } } private static void FromRgba64Bytes(Configuration configuration, Span rgbaBytes, IMemoryGroup destinationGroup) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { foreach (Memory m in destinationGroup) { Span destBuffer = m.Span; PixelOperations.Instance.FromRgba64Bytes( configuration, rgbaBytes, destBuffer, destBuffer.Length); rgbaBytes = rgbaBytes.Slice(destBuffer.Length * 8); } } } }