mirror of https://github.com/SixLabors/ImageSharp
35 changed files with 283 additions and 115 deletions
@ -0,0 +1,63 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Text; |
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.Formats; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.Primitives; |
|||
|
|||
namespace SixLabors.ImageSharp.Helpers |
|||
{ |
|||
/// <summary>
|
|||
/// Extension methods over Image{TPixel}
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
|
|||
/// <summary>
|
|||
/// Gets the bounds of the image.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The Pixel format.</typeparam>
|
|||
/// <param name="source">The source image</param>
|
|||
/// <returns>Returns the bounds of the image</returns>
|
|||
public static Rectangle Bounds<TPixel>(this Image<TPixel> source) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> new Rectangle(0, 0, source.Width, source.Height); |
|||
|
|||
/// <summary>
|
|||
/// Gets the bounds of the image.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The Pixel format.</typeparam>
|
|||
/// <param name="source">The source image</param>
|
|||
/// <returns>Returns the bounds of the image</returns>
|
|||
public static Rectangle Bounds<TPixel>(this ImageFrame<TPixel> source) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> new Rectangle(0, 0, source.Width, source.Height); |
|||
|
|||
/// <summary>
|
|||
/// Gets the size of the image.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The Pixel format.</typeparam>
|
|||
/// <param name="source">The source image</param>
|
|||
/// <returns>Returns the bounds of the image</returns>
|
|||
public static Size Size<TPixel>(this Image<TPixel> source) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> new Size(source.Width, source.Height); |
|||
|
|||
/// <summary>
|
|||
/// Gets the size of the image.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The Pixel format.</typeparam>
|
|||
/// <param name="source">The source image</param>
|
|||
/// <returns>Returns the bounds of the image</returns>
|
|||
public static Size Size<TPixel>(this ImageFrame<TPixel> source) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> new Size(source.Width, source.Height); |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Threading.Tasks; |
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.Formats; |
|||
using SixLabors.ImageSharp.Memory; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <content>
|
|||
/// Adds static methods allowing the creation of new image from raw pixel data.
|
|||
/// </content>
|
|||
public static partial class ImageFrame |
|||
{ |
|||
/// <summary>
|
|||
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given byte array in <typeparamref name="TPixel"/> format.
|
|||
/// </summary>
|
|||
/// <param name="config">The config for the decoder.</param>
|
|||
/// <param name="data">The byte array containing image data.</param>
|
|||
/// <param name="width">The width of the final image.</param>
|
|||
/// <param name="height">The height of the final image.</param>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
|
|||
public static ImageFrame<TPixel> LoadPixelData<TPixel>(Span<byte> data, int width, int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> LoadPixelData(data.NonPortableCast<byte, TPixel>(), width, height); |
|||
|
|||
/// <summary>
|
|||
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> data.
|
|||
/// </summary>
|
|||
/// <param name="config">The config for the decoder.</param>
|
|||
/// <param name="data">The Span containing the image Pixel data.</param>
|
|||
/// <param name="width">The width of the final image.</param>
|
|||
/// <param name="height">The height of the final image.</param>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
|
|||
public static ImageFrame<TPixel> LoadPixelData<TPixel>(Span<TPixel> data, int width, int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
int count = width * height; |
|||
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); |
|||
|
|||
var image = new ImageFrame<TPixel>(width, height); |
|||
SpanHelper.Copy(data, image.GetPixelSpan(), count); |
|||
|
|||
return image; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue