Browse Source

Revert non-generic decode to use RGba32 and add comments

af/merge-core
James Jackson-South 6 years ago
parent
commit
dd571d9369
  1. 7
      src/ImageSharp/Formats/IImageDecoder.cs
  2. 9
      src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
  3. 16
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  4. 30
      src/ImageSharp/Image.FromBytes.cs
  5. 22
      src/ImageSharp/Image.FromFile.cs
  6. 6
      src/ImageSharp/Image.FromStream.cs

7
src/ImageSharp/Formats/IImageDecoder.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System.IO; using System.IO;
@ -17,17 +17,16 @@ namespace SixLabors.ImageSharp.Formats
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="configuration">The configuration for the image.</param> /// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param> /// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The decoded image of a given pixel type.</returns> /// <returns>The <see cref="Image{TPixel}"/>.</returns>
Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream) Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel>; where TPixel : struct, IPixel<TPixel>;
/// <summary> /// <summary>
/// Decodes the image from the specified stream to an <see cref="Image"/>. /// Decodes the image from the specified stream to an <see cref="Image"/>.
/// The decoder is free to choose the pixel type.
/// </summary> /// </summary>
/// <param name="configuration">The configuration for the image.</param> /// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param> /// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The decoded image of a pixel type chosen by the decoder.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
Image Decode(Configuration configuration, Stream stream); Image Decode(Configuration configuration, Stream stream);
} }
} }

9
src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

@ -30,14 +30,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// <inheritdoc /> /// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream) public Image Decode(Configuration configuration, Stream stream)
{ => this.Decode<Rgba32>(configuration, stream);
Guard.NotNull(stream, nameof(stream));
using (var decoder = new JpegDecoderCore(configuration, this))
{
return decoder.Decode(stream);
}
}
/// <inheritdoc/> /// <inheritdoc/>
public IImageInfo Identify(Configuration configuration, Stream stream) public IImageInfo Identify(Configuration configuration, Stream stream)

16
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -204,22 +204,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
return new JpegFileMarker(marker[1], stream.Position - 2, true); return new JpegFileMarker(marker[1], stream.Position - 2, true);
} }
/// <summary>
/// Decodes the image from the specified <see cref="Stream"/> and sets the data to image.
/// </summary>
/// <param name="stream">The stream, where the image should be.</param>
/// <returns>The decoded image.</returns>
public Image Decode(Stream stream)
{
this.ParseStream(stream);
this.InitExifProfile();
this.InitIccProfile();
this.InitDerivedMetadataProperties();
return this.ColorSpace == JpegColorSpace.Grayscale
? (Image)this.PostProcessIntoImage<L8>()
: this.PostProcessIntoImage<Rgb24>();
}
/// <summary> /// <summary>
/// Decodes the image from the specified <see cref="Stream"/> and sets the data to image. /// Decodes the image from the specified <see cref="Stream"/> and sets the data to image.
/// </summary> /// </summary>

30
src/ImageSharp/Image.FromBytes.cs

@ -270,7 +270,7 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="data">The byte array containing image data.</param> /// <param name="data">The byte array containing image data.</param>
/// <param name="format">The detected format.</param> /// <param name="format">The detected format.</param>
/// <returns>A new <see cref="Image"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(byte[] data, out IImageFormat format) => public static Image Load(byte[] data, out IImageFormat format) =>
Load(Configuration.Default, data, out format); Load(Configuration.Default, data, out format);
@ -279,24 +279,24 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="data">The byte array containing encoded image data.</param> /// <param name="data">The byte array containing encoded image data.</param>
/// <param name="decoder">The decoder.</param> /// <param name="decoder">The decoder.</param>
/// <returns>A new <see cref="Image"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(byte[] data, IImageDecoder decoder) => Load(Configuration.Default, data, decoder); public static Image Load(byte[] data, IImageDecoder decoder) => Load(Configuration.Default, data, decoder);
/// <summary> /// <summary>
/// Load a new instance of <see cref="Image{Rgba32}"/> from the given encoded byte array. /// Load a new instance of <see cref="Image"/> from the given encoded byte array.
/// </summary> /// </summary>
/// <param name="config">The config for the decoder.</param> /// <param name="config">The config for the decoder.</param>
/// <param name="data">The byte array containing encoded image data.</param> /// <param name="data">The byte array containing encoded image data.</param>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Configuration config, byte[] data) => Load(config, data, out _); public static Image Load(Configuration config, byte[] data) => Load(config, data, out _);
/// <summary> /// <summary>
/// Load a new instance of <see cref="Image{Rgba32}"/> from the given encoded byte array. /// Load a new instance of <see cref="Image"/> from the given encoded byte array.
/// </summary> /// </summary>
/// <param name="config">The config for the decoder.</param> /// <param name="config">The config for the decoder.</param>
/// <param name="data">The byte array containing image data.</param> /// <param name="data">The byte array containing image data.</param>
/// <param name="decoder">The decoder.</param> /// <param name="decoder">The decoder.</param>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Configuration config, byte[] data, IImageDecoder decoder) public static Image Load(Configuration config, byte[] data, IImageDecoder decoder)
{ {
using (var stream = new MemoryStream(data)) using (var stream = new MemoryStream(data))
@ -306,12 +306,12 @@ namespace SixLabors.ImageSharp
} }
/// <summary> /// <summary>
/// Load a new instance of <see cref="Image{Rgba32}"/> from the given encoded byte array. /// Load a new instance of <see cref="Image"/> from the given encoded byte array.
/// </summary> /// </summary>
/// <param name="config">The config for the decoder.</param> /// <param name="config">The config for the decoder.</param>
/// <param name="data">The byte array containing image data.</param> /// <param name="data">The byte array containing image data.</param>
/// <param name="format">The mime type of the decoded image.</param> /// <param name="format">The mime type of the decoded image.</param>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Configuration config, byte[] data, out IImageFormat format) public static Image Load(Configuration config, byte[] data, out IImageFormat format)
{ {
using (var stream = new MemoryStream(data)) using (var stream = new MemoryStream(data))
@ -321,10 +321,10 @@ namespace SixLabors.ImageSharp
} }
/// <summary> /// <summary>
/// Load a new instance of <see cref="Image{Rgba32}"/> from the given encoded byte span. /// Load a new instance of <see cref="Image"/> from the given encoded byte span.
/// </summary> /// </summary>
/// <param name="data">The byte span containing image data.</param> /// <param name="data">The byte span containing image data.</param>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(ReadOnlySpan<byte> data) => Load(Configuration.Default, data); public static Image Load(ReadOnlySpan<byte> data) => Load(Configuration.Default, data);
/// <summary> /// <summary>
@ -332,7 +332,7 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="data">The byte span containing image data.</param> /// <param name="data">The byte span containing image data.</param>
/// <param name="decoder">The decoder.</param> /// <param name="decoder">The decoder.</param>
/// <returns>A new <see cref="Image"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(ReadOnlySpan<byte> data, IImageDecoder decoder) => public static Image Load(ReadOnlySpan<byte> data, IImageDecoder decoder) =>
Load(Configuration.Default, data, decoder); Load(Configuration.Default, data, decoder);
@ -341,7 +341,7 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="data">The byte span containing image data.</param> /// <param name="data">The byte span containing image data.</param>
/// <param name="format">The detected format.</param> /// <param name="format">The detected format.</param>
/// <returns>A new <see cref="Image"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(ReadOnlySpan<byte> data, out IImageFormat format) => public static Image Load(ReadOnlySpan<byte> data, out IImageFormat format) =>
Load(Configuration.Default, data, out format); Load(Configuration.Default, data, out format);
@ -350,7 +350,7 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="config">The configuration options.</param> /// <param name="config">The configuration options.</param>
/// <param name="data">The byte span containing image data.</param> /// <param name="data">The byte span containing image data.</param>
/// <returns>A new <see cref="Image"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Configuration config, ReadOnlySpan<byte> data) => Load(config, data, out _); public static Image Load(Configuration config, ReadOnlySpan<byte> data) => Load(config, data, out _);
/// <summary> /// <summary>
@ -359,7 +359,7 @@ namespace SixLabors.ImageSharp
/// <param name="config">The Configuration.</param> /// <param name="config">The Configuration.</param>
/// <param name="data">The byte span containing image data.</param> /// <param name="data">The byte span containing image data.</param>
/// <param name="decoder">The decoder.</param> /// <param name="decoder">The decoder.</param>
/// <returns>A new <see cref="Image"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static unsafe Image Load( public static unsafe Image Load(
Configuration config, Configuration config,
ReadOnlySpan<byte> data, ReadOnlySpan<byte> data,
@ -380,7 +380,7 @@ namespace SixLabors.ImageSharp
/// <param name="config">The configuration options.</param> /// <param name="config">The configuration options.</param>
/// <param name="data">The byte span containing image data.</param> /// <param name="data">The byte span containing image data.</param>
/// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>> /// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>>
/// <returns>A new <see cref="Image"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static unsafe Image Load( public static unsafe Image Load(
Configuration config, Configuration config,
ReadOnlySpan<byte> data, ReadOnlySpan<byte> data,

22
src/ImageSharp/Image.FromFile.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
@ -39,17 +39,17 @@ namespace SixLabors.ImageSharp
} }
/// <summary> /// <summary>
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given file. /// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary> /// </summary>
/// <param name="path">The file path to the image.</param> /// <param name="path">The file path to the image.</param>
/// <exception cref="NotSupportedException"> /// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable. /// Thrown if the stream is not readable nor seekable.
/// </exception> /// </exception>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(string path) => Load(Configuration.Default, path); public static Image Load(string path) => Load(Configuration.Default, path);
/// <summary> /// <summary>
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given file. /// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary> /// </summary>
/// <param name="path">The file path to the image.</param> /// <param name="path">The file path to the image.</param>
/// <param name="format">The mime type of the decoded image.</param> /// <param name="format">The mime type of the decoded image.</param>
@ -60,18 +60,18 @@ namespace SixLabors.ImageSharp
public static Image Load(string path, out IImageFormat format) => Load(Configuration.Default, path, out format); public static Image Load(string path, out IImageFormat format) => Load(Configuration.Default, path, out format);
/// <summary> /// <summary>
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given file. /// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary> /// </summary>
/// <param name="config">The config for the decoder.</param> /// <param name="config">The config for the decoder.</param>
/// <param name="path">The file path to the image.</param> /// <param name="path">The file path to the image.</param>
/// <exception cref="NotSupportedException"> /// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable. /// Thrown if the stream is not readable nor seekable.
/// </exception> /// </exception>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Configuration config, string path) => Load(config, path, out _); public static Image Load(Configuration config, string path) => Load(config, path, out _);
/// <summary> /// <summary>
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given file. /// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary> /// </summary>
/// <param name="config">The Configuration.</param> /// <param name="config">The Configuration.</param>
/// <param name="path">The file path to the image.</param> /// <param name="path">The file path to the image.</param>
@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp
/// <exception cref="NotSupportedException"> /// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable. /// Thrown if the stream is not readable nor seekable.
/// </exception> /// </exception>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Configuration config, string path, IImageDecoder decoder) public static Image Load(Configuration config, string path, IImageDecoder decoder)
{ {
using (Stream stream = config.FileSystem.OpenRead(path)) using (Stream stream = config.FileSystem.OpenRead(path))
@ -89,14 +89,14 @@ namespace SixLabors.ImageSharp
} }
/// <summary> /// <summary>
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given file. /// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary> /// </summary>
/// <param name="path">The file path to the image.</param> /// <param name="path">The file path to the image.</param>
/// <param name="decoder">The decoder.</param> /// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException"> /// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable. /// Thrown if the stream is not readable nor seekable.
/// </exception> /// </exception>
/// <returns>A new <see cref="Image{Rgba32}"/>.</returns> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(string path, IImageDecoder decoder) => Load(Configuration.Default, path, decoder); public static Image Load(string path, IImageDecoder decoder) => Load(Configuration.Default, path, decoder);
/// <summary> /// <summary>
@ -224,4 +224,4 @@ namespace SixLabors.ImageSharp
} }
} }
} }
} }

6
src/ImageSharp/Image.FromStream.cs

@ -80,7 +80,7 @@ namespace SixLabors.ImageSharp
/// <param name="format">The format type of the decoded image.</param> /// <param name="format">The format type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception> /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Stream stream, out IImageFormat format) => Load(Configuration.Default, stream, out format); public static Image Load(Stream stream, out IImageFormat format) => Load(Configuration.Default, stream, out format);
/// <summary> /// <summary>
@ -90,7 +90,7 @@ namespace SixLabors.ImageSharp
/// <param name="stream">The stream containing image information.</param> /// <param name="stream">The stream containing image information.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception> /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Stream stream) => Load(Configuration.Default, stream); public static Image Load(Stream stream) => Load(Configuration.Default, stream);
/// <summary> /// <summary>
@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp
/// <param name="decoder">The decoder.</param> /// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception> /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>> /// <returns>The <see cref="Image"/> in <see cref="Rgba32"/> pixel format.</returns>
public static Image Load(Stream stream, IImageDecoder decoder) => Load(Configuration.Default, stream, decoder); public static Image Load(Stream stream, IImageDecoder decoder) => Load(Configuration.Default, stream, decoder);
/// <summary> /// <summary>

Loading…
Cancel
Save