Skip to content

Commit

Permalink
Clarify some DebugGuard messages and a little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpopow committed Feb 15, 2021
1 parent 60dcaac commit 20726c3
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public DeflateCompressor(Stream output, MemoryAllocator allocator, int width, in
: base(output, allocator, width, bitsPerPixel, predictor)
=> this.compressionLevel = compressionLevel;

/// <inheritdoc/>
public override TiffEncoderCompression Method => TiffEncoderCompression.Deflate;

/// <inheritdoc/>
public override void Initialize(int rowsPerStrip)
{
}

/// <inheritdoc/>
public override void CompressStrip(Span<byte> rows, int height)
{
this.memoryStream.Seek(0, SeekOrigin.Begin);
Expand Down Expand Up @@ -52,6 +55,7 @@ public override void CompressStrip(Span<byte> rows, int height)
#endif
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public LzwCompressor(Stream output, MemoryAllocator allocator, int width, int bi
{
}

/// <inheritdoc/>
public override TiffEncoderCompression Method => TiffEncoderCompression.Lzw;

/// <inheritdoc/>
public override void Initialize(int rowsPerStrip) => this.lzwEncoder = new TiffLzwEncoder(this.Allocator);

/// <inheritdoc/>
public override void CompressStrip(Span<byte> rows, int height)
{
if (this.Predictor == TiffPredictor.Horizontal)
Expand All @@ -31,6 +34,7 @@ public override void CompressStrip(Span<byte> rows, int height)
this.lzwEncoder.Encode(rows, this.Output);
}

/// <inheritdoc/>
protected override void Dispose(bool disposing) => this.lzwEncoder?.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public NoCompressor(Stream output)
{
}

/// <inheritdoc/>
public override TiffEncoderCompression Method => TiffEncoderCompression.None;

/// <inheritdoc/>
public override void Initialize(int rowsPerStrip)
{
}

/// <inheritdoc/>
public override void CompressStrip(Span<byte> rows, int height) => this.Output.Write(rows);

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ public PackBitsCompressor(Stream output, MemoryAllocator allocator, int width, i
{
}

/// <inheritdoc/>
public override TiffEncoderCompression Method => TiffEncoderCompression.PackBits;

/// <inheritdoc/>
public override void Initialize(int rowsPerStrip)
{
int additionalBytes = ((this.BytesPerRow + 126) / 127) + 1;
this.pixelData = this.Allocator.AllocateManagedByteBuffer(this.BytesPerRow + additionalBytes);
}

/// <inheritdoc/>
public override void CompressStrip(Span<byte> rows, int height)
{
DebugGuard.IsTrue(rows.Length % height == 0, "Invalid height");
Expand All @@ -38,6 +41,7 @@ public override void CompressStrip(Span<byte> rows, int height)
}
}

/// <inheritdoc/>
protected override void Dispose(bool disposing) => this.pixelData?.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, Spa
}
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, Spa
}
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public NoneTiffCompression()
/// <inheritdoc/>
protected override void Decompress(BufferedReadStream stream, int byteCount, Span<byte> buffer) => _ = stream.Read(buffer, 0, Math.Min(buffer.Length, byteCount));

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private static void ArrayCopyRepeat(byte value, Span<byte> destinationArray, int
}
}

/// <inheritdoc/>
protected override void Dispose(bool disposing) => this.compressedDataMemory?.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, Spa
}
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
Expand Down
17 changes: 16 additions & 1 deletion src/ImageSharp/Formats/Tiff/Compression/TiffBaseCompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,35 @@ protected TiffBaseCompression(MemoryAllocator allocator, int width, int bitsPerP
this.Width = width;
this.BitsPerPixel = bitsPerPixel;
this.Predictor = predictor;

this.BytesPerRow = ((width * bitsPerPixel) + 7) / 8;
}

/// <summary>
/// Gets the image width.
/// </summary>
public int Width { get; }

/// <summary>
/// Gets the bits per pixel.
/// </summary>
public int BitsPerPixel { get; }

/// <summary>
/// Gets the bytes per row.
/// </summary>
public int BytesPerRow { get; }

/// <summary>
/// Gets the predictor to use. Should only be used with deflate or lzw compression.
/// </summary>
public TiffPredictor Predictor { get; }

/// <summary>
/// Gets the memory allocator.
/// </summary>
protected MemoryAllocator Allocator { get; }

/// <inheritdoc />
public void Dispose()
{
if (this.isDisposed)
Expand Down
23 changes: 23 additions & 0 deletions src/ImageSharp/Formats/Tiff/Compression/TiffBaseCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,39 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Compression
{
internal abstract class TiffBaseCompressor : TiffBaseCompression
{
/// <summary>
/// Initializes a new instance of the <see cref="TiffBaseCompressor"/> class.
/// </summary>
/// <param name="output">The output stream to write the compressed image to.</param>
/// <param name="allocator">The memory allocator.</param>
/// <param name="width">The image width.</param>
/// <param name="bitsPerPixel">Bits per pixel.</param>
/// <param name="predictor">The predictor to use (should only be used with deflate or lzw compression). Defaults to none.</param>
protected TiffBaseCompressor(Stream output, MemoryAllocator allocator, int width, int bitsPerPixel, TiffPredictor predictor = TiffPredictor.None)
: base(allocator, width, bitsPerPixel, predictor)
=> this.Output = output;

/// <summary>
/// Gets the compression method to use.
/// </summary>
public abstract TiffEncoderCompression Method { get; }

/// <summary>
/// Gets the output stream to write the compressed image to.
/// </summary>
public Stream Output { get; }

/// <summary>
/// Does any initialization required for the compression.
/// </summary>
/// <param name="rowsPerStrip">The number of rows per strip.</param>
public abstract void Initialize(int rowsPerStrip);

/// <summary>
/// Compresses a strip of the image.
/// </summary>
/// <param name="rows">Image rows to compress.</param>
/// <param name="height">Image height.</param>
public abstract void CompressStrip(Span<byte> rows, int height);
}
}
21 changes: 9 additions & 12 deletions src/ImageSharp/Formats/Tiff/Compression/TiffCompressorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Buffers;
using System.IO;
using SixLabors.ImageSharp.Compression.Zlib;
using SixLabors.ImageSharp.Formats.Experimental.Tiff;
using SixLabors.ImageSharp.Formats.Experimental.Tiff.Compression.Compressors;
using SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants;
using SixLabors.ImageSharp.Memory;
Expand All @@ -26,31 +23,31 @@ public static TiffBaseCompressor Create(
switch (method)
{
case TiffEncoderCompression.None:
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "No deflate compression level is expected to be set");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");

return new NoCompressor(output);

case TiffEncoderCompression.PackBits:
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "No deflate compression level is expected to be set");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
return new PackBitsCompressor(output, allocator, width, bitsPerPixel);

case TiffEncoderCompression.Deflate:
return new DeflateCompressor(output, allocator, width, bitsPerPixel, predictor, compressionLevel);

case TiffEncoderCompression.Lzw:
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "Values must be equals");
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "No deflate compression level is expected to be set");
return new LzwCompressor(output, allocator, width, bitsPerPixel, predictor);

case TiffEncoderCompression.CcittGroup3Fax:
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "No deflate compression level is expected to be set");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
return new T4BitCompressor(output, allocator, width, bitsPerPixel, false);

case TiffEncoderCompression.ModifiedHuffman:
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(compressionLevel == DeflateCompressionLevel.DefaultCompression, "No deflate compression level is expected to be set");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
return new T4BitCompressor(output, allocator, width, bitsPerPixel, true);

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ public static TiffBaseDecompresor Create(
switch (method)
{
case TiffDecoderCompressionType.None:
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "No fax compression options are expected");
return new NoneTiffCompression();

case TiffDecoderCompressionType.PackBits:
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "No fax compression options are expected");
return new PackBitsTiffCompression(allocator);

case TiffDecoderCompressionType.Deflate:
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "Values must be equals");
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "No fax compression options are expected");
return new DeflateTiffCompression(allocator, width, bitsPerPixel, predictor);

case TiffDecoderCompressionType.Lzw:
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "Values must be equals");
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "No fax compression options are expected");
return new LzwTiffCompression(allocator, width, bitsPerPixel, predictor);

case TiffDecoderCompressionType.T4:
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
return new T4TiffCompression(allocator, faxOptions, photometricInterpretation, width);

case TiffDecoderCompressionType.HuffmanRle:
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Values must be equals");
DebugGuard.IsTrue(predictor == TiffPredictor.None, "Predictor should only be used with lzw or deflate compression");
return new ModifiedHuffmanTiffCompression(allocator, photometricInterpretation, width);

default:
Expand Down
7 changes: 5 additions & 2 deletions src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants;
using SixLabors.ImageSharp.Formats.Experimental.Tiff.Writers;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
Expand Down Expand Up @@ -58,6 +57,9 @@ internal sealed class TiffEncoderCore : IImageEncoderInternals
/// </summary>
private readonly DeflateCompressionLevel compressionLevel;

/// <summary>
/// The maximum number of bytes for a strip.
/// </summary>
private readonly int maxStripBytes;

/// <summary>
Expand Down Expand Up @@ -252,7 +254,8 @@ private void SetMode(Image image)
this.bitsPerPixel = TiffBitsPerPixel.Pixel1;
return;
}
else if (this.Mode != TiffEncodingMode.BiColor)

if (this.Mode != TiffEncodingMode.BiColor)
{
TiffThrowHelper.ThrowImageFormatException($"The {this.CompressionType} compression and {this.Mode} aren't compatible. Please use {this.CompressionType} only with {TiffEncodingMode.BiColor} or {TiffEncodingMode.Default} mode.");
}
Expand Down
1 change: 1 addition & 0 deletions src/ImageSharp/Formats/Tiff/Writers/TiffBaseColorWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public virtual void Write(TiffBaseCompressor compressor, int rowsPerStrip)
this.AddStripTags(rowsPerStrip, stripOffsets, stripByteCounts);
}

/// <inheritdoc/>
public void Dispose()
{
if (this.isDisposed)
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/Formats/Tiff/Writers/TiffBiColorWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override void EncodeStrip(int y, int height, TiffBaseCompressor compre

if (compressor.Method == TiffEncoderCompression.CcittGroup3Fax || compressor.Method == TiffEncoderCompression.ModifiedHuffman)
{
// special case for T4BitCompressor
// Special case for T4BitCompressor.
compressor.CompressStrip(pixelAsGraySpan, height);
}
else
Expand All @@ -63,7 +63,7 @@ protected override void EncodeStrip(int y, int height, TiffBaseCompressor compre
Span<byte> rows = this.bitStrip.Slice(0, bytesPerStrip);
rows.Clear();

int xx = 0;
int grayPixelIndex = 0;
for (int s = 0; s < height; s++)
{
int bitIndex = 0;
Expand All @@ -72,7 +72,7 @@ protected override void EncodeStrip(int y, int height, TiffBaseCompressor compre
for (int x = 0; x < this.Image.Width; x++)
{
int shift = 7 - bitIndex;
if (pixelAsGraySpan[xx++] == 255)
if (pixelAsGraySpan[grayPixelIndex++] == 255)
{
outputRow[byteIndex] |= (byte)(1 << shift);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal abstract class TiffCompositeColorWriter<TPixel> : TiffBaseColorWriter<T
{
private IManagedByteBuffer rowBuffer;

public TiffCompositeColorWriter(ImageFrame<TPixel> image, MemoryAllocator memoryAllocator, Configuration configuration, TiffEncoderEntriesCollector entriesCollector)
protected TiffCompositeColorWriter(ImageFrame<TPixel> image, MemoryAllocator memoryAllocator, Configuration configuration, TiffEncoderEntriesCollector entriesCollector)
: base(image, memoryAllocator, configuration, entriesCollector)
{
}
Expand All @@ -40,6 +40,7 @@ protected override void EncodeStrip(int y, int height, TiffBaseCompressor compre

protected abstract void EncodePixels(Span<TPixel> pixels, Span<byte> buffer);

/// <inheritdoc />
protected override void Dispose(bool disposing) => this.rowBuffer?.Dispose();
}
}
3 changes: 2 additions & 1 deletion src/ImageSharp/Formats/Tiff/Writers/TiffGrayWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0.

using System;
using System.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

Expand All @@ -16,8 +15,10 @@ public TiffGrayWriter(ImageFrame<TPixel> image, MemoryAllocator memoryAllocator,
{
}

/// <inheritdoc />
public override int BitsPerPixel => 8;

/// <inheritdoc />
protected override void EncodePixels(Span<TPixel> pixels, Span<byte> buffer) => PixelOperations<TPixel>.Instance.ToL8Bytes(this.Configuration, pixels, buffer, pixels.Length);
}
}
Loading

0 comments on commit 20726c3

Please sign in to comment.