|
| 1 | +using SixLabors.ImageSharp; |
| 2 | +using SixLabors.ImageSharp.Formats; |
| 3 | +using SixLabors.ImageSharp.Metadata; |
| 4 | +using SixLabors.ImageSharp.PixelFormats; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | + |
| 13 | +namespace NeoSolve.ImageSharp.AVIF; |
| 14 | + |
| 15 | +public class AVIFDecoder : IImageDecoder |
| 16 | +{ |
| 17 | + public static IImageDecoder Instance = new AVIFDecoder(); |
| 18 | + |
| 19 | + public Image<TPixel> Decode<TPixel>(DecoderOptions options, Stream stream) |
| 20 | + where TPixel : unmanaged, IPixel<TPixel> |
| 21 | + { |
| 22 | + return DecodeAsync<TPixel>(options, stream, CancellationToken.None).Result; |
| 23 | + } |
| 24 | + |
| 25 | + public Image Decode(DecoderOptions options, Stream stream) |
| 26 | + { |
| 27 | + return DecodeAsync(options, stream, CancellationToken.None).Result; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Decodes an AVIF image from a stream into an ImageSharp image, |
| 32 | + /// piping the data through avifdec. |
| 33 | + /// </summary> |
| 34 | + /// <typeparam name="TPixel">The pixel format.</typeparam> |
| 35 | + /// <param name="options">The decoder options.</param> |
| 36 | + /// <param name="stream">The input stream containing the AVIF data.</param> |
| 37 | + /// <param name="cancellationToken">A cancellation token.</param> |
| 38 | + /// <returns>A decoded image.</returns> |
| 39 | + public async Task<Image<TPixel>> DecodeAsync<TPixel>(DecoderOptions options, Stream stream, CancellationToken cancellationToken = default) |
| 40 | + where TPixel : unmanaged, IPixel<TPixel> |
| 41 | + { |
| 42 | + string inputFilePath = Path.GetTempFileName(); |
| 43 | + string outputFilePath = Path.GetTempFileName() + ".png"; |
| 44 | + |
| 45 | + try |
| 46 | + { |
| 47 | + using (var fileStream = new FileStream(inputFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true)) |
| 48 | + { |
| 49 | + await stream.CopyToAsync(fileStream, cancellationToken); |
| 50 | + } |
| 51 | + |
| 52 | + var arguments = new List<string> { inputFilePath, outputFilePath }; |
| 53 | + |
| 54 | + var psi = new ProcessStartInfo |
| 55 | + { |
| 56 | + FileName = Native.CAVIFDEC, |
| 57 | + Arguments = string.Join(' ', arguments), |
| 58 | + UseShellExecute = false, |
| 59 | + CreateNoWindow = true, |
| 60 | + RedirectStandardError = true |
| 61 | + }; |
| 62 | + |
| 63 | + var process = Process.Start(psi) ?? throw new InvalidOperationException("Failed to start avifdec process."); |
| 64 | + |
| 65 | + await process.WaitForExitAsync(cancellationToken); |
| 66 | + |
| 67 | + if (process.ExitCode != 0) |
| 68 | + { |
| 69 | + string error = await process.StandardError.ReadToEndAsync(); |
| 70 | + throw new InvalidOperationException($"AVIF decoding failed with exit code {process.ExitCode}. Error: {error}"); |
| 71 | + } |
| 72 | + |
| 73 | + using (var outputStream = new FileStream(outputFilePath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, true)) |
| 74 | + { |
| 75 | + return await Image.LoadAsync<TPixel>(outputStream, cancellationToken); |
| 76 | + } |
| 77 | + } |
| 78 | + finally |
| 79 | + { |
| 80 | + File.Delete(inputFilePath); |
| 81 | + File.Delete(outputFilePath); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + public async Task<Image> DecodeAsync(DecoderOptions options, Stream stream, CancellationToken cancellationToken = default) |
| 87 | + { |
| 88 | + return await DecodeAsync<Rgba32>(options, stream, cancellationToken); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + public ImageInfo Identify(DecoderOptions options, Stream stream) |
| 93 | + { |
| 94 | + return IdentifyAsync(options, stream, CancellationToken.None).Result; |
| 95 | + } |
| 96 | + |
| 97 | + public async Task<ImageInfo> IdentifyAsync(DecoderOptions options, Stream stream, CancellationToken cancellationToken = default) |
| 98 | + { |
| 99 | + string tempFilePath = Path.GetTempFileName(); |
| 100 | + |
| 101 | + try |
| 102 | + { |
| 103 | + await using (var fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true)) |
| 104 | + { |
| 105 | + await stream.CopyToAsync(fileStream, cancellationToken); |
| 106 | + } |
| 107 | + |
| 108 | + stream.Position = 0; |
| 109 | + |
| 110 | + var process = new Process |
| 111 | + { |
| 112 | + StartInfo = new ProcessStartInfo |
| 113 | + { |
| 114 | + FileName = Native.CAVIFDEC, |
| 115 | + Arguments = $"--info \"{tempFilePath}\"", |
| 116 | + RedirectStandardOutput = true, |
| 117 | + RedirectStandardError = true, |
| 118 | + UseShellExecute = false, |
| 119 | + CreateNoWindow = true |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + process.Start(); |
| 124 | + await process.WaitForExitAsync(cancellationToken); |
| 125 | + |
| 126 | + if (process.ExitCode == 0) |
| 127 | + { |
| 128 | + string output = await process.StandardOutput.ReadToEndAsync(); |
| 129 | + var avifInfo = ParseAVIFInfo(output); |
| 130 | + var imageMetadata = new ImageMetadata(); |
| 131 | + |
| 132 | + Size size = new(avifInfo.Width, avifInfo.Height); |
| 133 | + return new ImageInfo(new PixelTypeInfo(avifInfo.BitDepth), size, imageMetadata); |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + string error = await process.StandardError.ReadToEndAsync(); |
| 138 | + return null; |
| 139 | + } |
| 140 | + } |
| 141 | + finally |
| 142 | + { |
| 143 | + if (File.Exists(tempFilePath)) |
| 144 | + { |
| 145 | + File.Delete(tempFilePath); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public static AVIFInfo ParseAVIFInfo(string text) |
| 151 | + { |
| 152 | + var avifInfo = new AVIFInfo(); |
| 153 | + var reader = new StringReader(text); |
| 154 | + string line; |
| 155 | + |
| 156 | + while ((line = reader.ReadLine()) != null) |
| 157 | + { |
| 158 | + if (string.IsNullOrWhiteSpace(line) || !line.Contains(":")) |
| 159 | + { |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + var parts = line.Split(':', 2, StringSplitOptions.TrimEntries); |
| 164 | + var key = parts[0].Trim().Replace("*", "").Trim(); |
| 165 | + var value = parts[1].Trim(); |
| 166 | + |
| 167 | + switch (key) |
| 168 | + { |
| 169 | + case "Resolution": |
| 170 | + var resolutionParts = value.Split('x'); |
| 171 | + if (resolutionParts.Length == 2) |
| 172 | + { |
| 173 | + avifInfo.Width = int.Parse(resolutionParts[0].Trim()); |
| 174 | + avifInfo.Height = int.Parse(resolutionParts[1].Trim()); |
| 175 | + } |
| 176 | + break; |
| 177 | + case "Bit Depth": |
| 178 | + avifInfo.BitDepth = int.Parse(value); |
| 179 | + break; |
| 180 | + case "Format": |
| 181 | + avifInfo.Format = value; |
| 182 | + break; |
| 183 | + case "Chroma Sam. Pos": |
| 184 | + avifInfo.ChromaSamPos = int.Parse(value); |
| 185 | + break; |
| 186 | + case "Alpha": |
| 187 | + avifInfo.Alpha = value; |
| 188 | + break; |
| 189 | + case "Range": |
| 190 | + avifInfo.Range = value; |
| 191 | + break; |
| 192 | + case "Color Primaries": |
| 193 | + avifInfo.ColorPrimaries = int.Parse(value); |
| 194 | + break; |
| 195 | + case "Transfer Char.": |
| 196 | + avifInfo.TransferChar = int.Parse(value); |
| 197 | + break; |
| 198 | + case "Matrix Coeffs.": |
| 199 | + avifInfo.MatrixCoeffs = int.Parse(value); |
| 200 | + break; |
| 201 | + case "ICC Profile": |
| 202 | + avifInfo.IccProfile = value; |
| 203 | + break; |
| 204 | + case "XMP Metadata": |
| 205 | + avifInfo.XmpMetadata = value; |
| 206 | + break; |
| 207 | + case "Exif Metadata": |
| 208 | + avifInfo.ExifMetadata = value; |
| 209 | + break; |
| 210 | + case "Transformations": |
| 211 | + avifInfo.Transformations = value; |
| 212 | + break; |
| 213 | + case "Progressive": |
| 214 | + avifInfo.Progressive = value; |
| 215 | + break; |
| 216 | + case "Gain map": |
| 217 | + avifInfo.GainMap = value; |
| 218 | + break; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return avifInfo; |
| 223 | + } |
| 224 | +} |
0 commit comments