Browse Source

Fix encode tiff benchmarks

- Deflate and packbits are not supported by System.Drawing
- When 1d Compression is chosen, TiffPhotometricInterpretation should be WhiteIsZero
pull/2134/head
Brian Popow 4 years ago
parent
commit
5675aa82ab
  1. 39
      tests/ImageSharp.Benchmarks/Codecs/Tiff/EncodeTiff.cs

39
tests/ImageSharp.Benchmarks/Codecs/Tiff/EncodeTiff.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
@ -9,6 +10,7 @@ using SixLabors.ImageSharp.Formats.Tiff;
using SixLabors.ImageSharp.Formats.Tiff.Constants; using SixLabors.ImageSharp.Formats.Tiff.Constants;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests; using SixLabors.ImageSharp.Tests;
using SDImage = System.Drawing.Image;
namespace SixLabors.ImageSharp.Benchmarks.Codecs namespace SixLabors.ImageSharp.Benchmarks.Codecs
{ {
@ -17,11 +19,10 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
[Config(typeof(Config.ShortMultiFramework))] [Config(typeof(Config.ShortMultiFramework))]
public class EncodeTiff public class EncodeTiff
{ {
private System.Drawing.Image drawing; private Stream stream;
private SDImage drawing;
private Image<Rgba32> core; private Image<Rgba32> core;
private Configuration configuration;
private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage);
[Params(TestImages.Tiff.Calliphora_RgbUncompressed)] [Params(TestImages.Tiff.Calliphora_RgbUncompressed)]
@ -29,9 +30,11 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
[Params( [Params(
TiffCompression.None, TiffCompression.None,
TiffCompression.Deflate,
// System.Drawing does not support Deflate or PackBits
// TiffCompression.Deflate,
// TiffCompression.PackBits,
TiffCompression.Lzw, TiffCompression.Lzw,
TiffCompression.PackBits,
TiffCompression.CcittGroup3Fax, TiffCompression.CcittGroup3Fax,
TiffCompression.Ccitt1D)] TiffCompression.Ccitt1D)]
public TiffCompression Compression { get; set; } public TiffCompression Compression { get; set; }
@ -39,11 +42,12 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
[GlobalSetup] [GlobalSetup]
public void ReadImages() public void ReadImages()
{ {
if (this.core == null) if (this.stream == null)
{ {
this.configuration = new Configuration(); this.stream = File.OpenRead(this.TestImageFullPath);
this.core = Image.Load<Rgba32>(this.configuration, this.TestImageFullPath); this.core = Image.Load<Rgba32>(this.stream);
this.drawing = System.Drawing.Image.FromFile(this.TestImageFullPath); this.stream.Position = 0;
this.drawing = SDImage.FromStream(this.stream);
} }
} }
@ -70,7 +74,10 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
[Benchmark(Description = "ImageSharp Tiff")] [Benchmark(Description = "ImageSharp Tiff")]
public void TiffCore() public void TiffCore()
{ {
TiffPhotometricInterpretation photometricInterpretation = TiffPhotometricInterpretation.Rgb; TiffPhotometricInterpretation photometricInterpretation =
IsOneBitCompression(this.Compression) ?
TiffPhotometricInterpretation.WhiteIsZero :
TiffPhotometricInterpretation.Rgb;
var encoder = new TiffEncoder() { Compression = this.Compression, PhotometricInterpretation = photometricInterpretation }; var encoder = new TiffEncoder() { Compression = this.Compression, PhotometricInterpretation = photometricInterpretation };
using var memoryStream = new MemoryStream(); using var memoryStream = new MemoryStream();
@ -109,8 +116,18 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
return EncoderValue.CompressionLZW; return EncoderValue.CompressionLZW;
default: default:
throw new System.NotSupportedException(compression.ToString()); throw new NotSupportedException(compression.ToString());
}
}
public static bool IsOneBitCompression(TiffCompression compression)
{
if (compression is TiffCompression.Ccitt1D or TiffCompression.CcittGroup3Fax or TiffCompression.CcittGroup4Fax)
{
return true;
} }
return false;
} }
} }
} }

Loading…
Cancel
Save