Browse Source

Migrate TgaEncoder

pull/2269/head
James Jackson-South 4 years ago
parent
commit
b88b1815d6
  1. 20
      src/ImageSharp/Formats/Tga/ITgaEncoderOptions.cs
  2. 13
      src/ImageSharp/Formats/Tga/TgaEncoder.cs
  3. 12
      src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

20
src/ImageSharp/Formats/Tga/ITgaEncoderOptions.cs

@ -1,20 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Formats.Tga;
/// <summary>
/// Configuration options for use during tga encoding.
/// </summary>
internal interface ITgaEncoderOptions
{
/// <summary>
/// Gets the number of bits per pixel.
/// </summary>
TgaBitsPerPixel? BitsPerPixel { get; }
/// <summary>
/// Gets a value indicating whether run length compression should be used.
/// </summary>
TgaCompression Compression { get; }
}

13
src/ImageSharp/Formats/Tga/TgaEncoder.cs

@ -2,14 +2,13 @@
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tga;
/// <summary>
/// Image encoder for writing an image to a stream as a targa truevision image.
/// </summary>
public sealed class TgaEncoder : IImageEncoder, ITgaEncoderOptions
public sealed class TgaEncoder : ImageEncoder
{
/// <summary>
/// Gets or sets the number of bits per pixel.
@ -22,18 +21,16 @@ public sealed class TgaEncoder : IImageEncoder, ITgaEncoderOptions
public TgaCompression Compression { get; set; } = TgaCompression.RunLength;
/// <inheritdoc/>
public void Encode<TPixel>(Image<TPixel> image, Stream stream)
where TPixel : unmanaged, IPixel<TPixel>
public override void Encode<TPixel>(Image<TPixel> image, Stream stream)
{
var encoder = new TgaEncoderCore(this, image.GetMemoryAllocator());
TgaEncoderCore encoder = new(this, image.GetMemoryAllocator());
encoder.Encode(image, stream);
}
/// <inheritdoc/>
public Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
public override Task EncodeAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
{
var encoder = new TgaEncoderCore(this, image.GetMemoryAllocator());
TgaEncoderCore encoder = new(this, image.GetMemoryAllocator());
return encoder.EncodeAsync(image, stream, cancellationToken);
}
}

12
src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

@ -45,13 +45,13 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
/// <summary>
/// Initializes a new instance of the <see cref="TgaEncoderCore"/> class.
/// </summary>
/// <param name="options">The encoder options.</param>
/// <param name="encoder">The encoder with options.</param>
/// <param name="memoryAllocator">The memory manager.</param>
public TgaEncoderCore(ITgaEncoderOptions options, MemoryAllocator memoryAllocator)
public TgaEncoderCore(TgaEncoder encoder, MemoryAllocator memoryAllocator)
{
this.memoryAllocator = memoryAllocator;
this.bitsPerPixel = options.BitsPerPixel;
this.compression = options.Compression;
this.bitsPerPixel = encoder.BitsPerPixel;
this.compression = encoder.Compression;
}
/// <summary>
@ -105,7 +105,9 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
cMapLength: 0,
cMapDepth: 0,
xOffset: 0,
yOffset: this.compression is TgaCompression.RunLength ? (short)image.Height : (short)0, // When run length encoding is used, the origin should be top left instead of the default bottom left.
// When run length encoding is used, the origin should be top left instead of the default bottom left.
yOffset: this.compression is TgaCompression.RunLength ? (short)image.Height : (short)0,
width: (short)image.Width,
height: (short)image.Height,
pixelDepth: (byte)this.bitsPerPixel.Value,

Loading…
Cancel
Save