|
|
|
@ -272,12 +272,19 @@ namespace SixLabors.ImageSharp.Formats.Tiff |
|
|
|
/// <typeparam name="TPixel">The pixel data.</typeparam>
|
|
|
|
/// <param name="image">The image to write to the stream.</param>
|
|
|
|
/// <param name="padding">The padding bytes for each row.</param>
|
|
|
|
/// <param name="compression">The compression to use.</param>
|
|
|
|
/// <returns>The number of bytes written.</returns>
|
|
|
|
public int WriteGray<TPixel>(Image<TPixel> image, int padding) |
|
|
|
public int WriteGray<TPixel>(Image<TPixel> image, int padding, TiffEncoderCompression compression) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
using IManagedByteBuffer row = this.AllocateRow(image.Width, 1, padding); |
|
|
|
Span<byte> rowSpan = row.GetSpan(); |
|
|
|
|
|
|
|
if (compression == TiffEncoderCompression.Deflate) |
|
|
|
{ |
|
|
|
return this.WriteGrayDeflateCompressed(image, rowSpan); |
|
|
|
} |
|
|
|
|
|
|
|
int bytesWritten = 0; |
|
|
|
for (int y = 0; y < image.Height; y++) |
|
|
|
{ |
|
|
|
@ -290,6 +297,37 @@ namespace SixLabors.ImageSharp.Formats.Tiff |
|
|
|
return bytesWritten; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Writes the image data as 8 bit gray with deflate compression to the stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="image">The image to write to the stream.</param>
|
|
|
|
/// <param name="rowSpan">A span of a pixel row.</param>
|
|
|
|
/// <returns>The number of bytes written.</returns>
|
|
|
|
private int WriteGrayDeflateCompressed<TPixel>(Image<TPixel> image, Span<byte> rowSpan) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
int bytesWritten = 0; |
|
|
|
using var memoryStream = new MemoryStream(); |
|
|
|
|
|
|
|
// TODO: move zlib compression from png to a common place?
|
|
|
|
using var deflateStream = |
|
|
|
new ZlibDeflateStream(this.memoryAllocator, memoryStream, PngCompressionLevel.Level6); // TODO: make compression level configurable
|
|
|
|
|
|
|
|
for (int y = 0; y < image.Height; y++) |
|
|
|
{ |
|
|
|
Span<TPixel> pixelRow = image.GetPixelRowSpan(y); |
|
|
|
PixelOperations<TPixel>.Instance.ToL8Bytes(this.configuration, pixelRow, rowSpan, pixelRow.Length); |
|
|
|
deflateStream.Write(rowSpan); |
|
|
|
} |
|
|
|
|
|
|
|
deflateStream.Flush(); |
|
|
|
|
|
|
|
byte[] buffer = memoryStream.ToArray(); |
|
|
|
this.output.Write(buffer); |
|
|
|
bytesWritten += buffer.Length; |
|
|
|
return bytesWritten; |
|
|
|
} |
|
|
|
|
|
|
|
private IManagedByteBuffer AllocateRow(int width, int bytesPerPixel, int padding) => this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, bytesPerPixel, padding); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|