mirror of https://github.com/SixLabors/ImageSharp
7 changed files with 184 additions and 72 deletions
@ -1,51 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System.IO; |
|
||||
using SixLabors.ImageSharp.Formats.Experimental.Tiff; |
|
||||
using SixLabors.ImageSharp.PixelFormats; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Extension methods for the <see cref="Image{TPixel}"/> type.
|
|
||||
/// </summary>
|
|
||||
public static partial class ImageExtensions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Saves the image to the given stream with the tiff format.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
||||
/// <param name="source">The image this method extends.</param>
|
|
||||
/// <param name="stream">The stream to save the image to.</param>
|
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
|
|
||||
/// <returns>
|
|
||||
/// The <see cref="Image{TPixel}"/>.
|
|
||||
/// </returns>
|
|
||||
public static Image<TPixel> SaveAsTiff<TPixel>(this Image<TPixel> source, Stream stream) |
|
||||
where TPixel : unmanaged, IPixel<TPixel> |
|
||||
{ |
|
||||
return SaveAsTiff(source, stream, null); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Saves the image to the given stream with the tiff format.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
||||
/// <param name="source">The image this method extends.</param>
|
|
||||
/// <param name="stream">The stream to save the image to.</param>
|
|
||||
/// <param name="encoder">The options for the encoder.</param>
|
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
|
|
||||
/// <returns>
|
|
||||
/// The <see cref="Image{TPixel}"/>.
|
|
||||
/// </returns>
|
|
||||
public static Image<TPixel> SaveAsTiff<TPixel>(this Image<TPixel> source, Stream stream, TiffEncoder encoder) |
|
||||
where TPixel : unmanaged, IPixel<TPixel> |
|
||||
{ |
|
||||
encoder = encoder ?? new TiffEncoder(); |
|
||||
encoder.Encode(source, stream); |
|
||||
|
|
||||
return source; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,156 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.IO; |
||||
|
using System.Threading.Tasks; |
||||
|
using SixLabors.ImageSharp.Formats; |
||||
|
using SixLabors.ImageSharp.Formats.Experimental.Tiff; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Tiff |
||||
|
{ |
||||
|
[Trait("Format", "Tiff")] |
||||
|
public class ImageExtensionsTest |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void SaveAsTiff_Path() |
||||
|
{ |
||||
|
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); |
||||
|
string file = Path.Combine(dir, "SaveAsTiff_Path.tiff"); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
image.SaveAsTiff(file); |
||||
|
} |
||||
|
|
||||
|
using (Image.Load(file, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task SaveAsTiffAsync_Path() |
||||
|
{ |
||||
|
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); |
||||
|
string file = Path.Combine(dir, "SaveAsTiffAsync_Path.tiff"); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
await image.SaveAsTiffAsync(file); |
||||
|
} |
||||
|
|
||||
|
using (Image.Load(file, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void SaveAsTiff_Path_Encoder() |
||||
|
{ |
||||
|
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); |
||||
|
string file = Path.Combine(dir, "SaveAsTiff_Path_Encoder.tiff"); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
image.SaveAsTiff(file, new TiffEncoder()); |
||||
|
} |
||||
|
|
||||
|
using (Image.Load(file, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task SaveAsTiffAsync_Path_Encoder() |
||||
|
{ |
||||
|
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); |
||||
|
string file = Path.Combine(dir, "SaveAsTiffAsync_Path_Encoder.tiff"); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
await image.SaveAsTiffAsync(file, new TiffEncoder()); |
||||
|
} |
||||
|
|
||||
|
using (Image.Load(file, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void SaveAsTiff_Stream() |
||||
|
{ |
||||
|
using var memoryStream = new MemoryStream(); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
image.SaveAsTiff(memoryStream); |
||||
|
} |
||||
|
|
||||
|
memoryStream.Position = 0; |
||||
|
|
||||
|
using (Image.Load(memoryStream, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task SaveAsTiffAsync_StreamAsync() |
||||
|
{ |
||||
|
using var memoryStream = new MemoryStream(); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
await image.SaveAsTiffAsync(memoryStream); |
||||
|
} |
||||
|
|
||||
|
memoryStream.Position = 0; |
||||
|
|
||||
|
using (Image.Load(memoryStream, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void SaveAsTiff_Stream_Encoder() |
||||
|
{ |
||||
|
using var memoryStream = new MemoryStream(); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
image.SaveAsTiff(memoryStream, new TiffEncoder()); |
||||
|
} |
||||
|
|
||||
|
memoryStream.Position = 0; |
||||
|
|
||||
|
using (Image.Load(memoryStream, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task SaveAsTiffAsync_Stream_Encoder() |
||||
|
{ |
||||
|
using var memoryStream = new MemoryStream(); |
||||
|
|
||||
|
using (var image = new Image<Rgba32>(10, 10)) |
||||
|
{ |
||||
|
await image.SaveAsTiffAsync(memoryStream, new TiffEncoder()); |
||||
|
} |
||||
|
|
||||
|
memoryStream.Position = 0; |
||||
|
|
||||
|
using (Image.Load(memoryStream, out IImageFormat mime)) |
||||
|
{ |
||||
|
Assert.Equal("image/tiff", mime.DefaultMimeType); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue