mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
87 changed files with 2469 additions and 577 deletions
@ -0,0 +1,21 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Common.Helpers |
||||
|
{ |
||||
|
internal readonly struct ExifResolutionValues |
||||
|
{ |
||||
|
public ExifResolutionValues(ushort resolutionUnit, double? horizontalResolution, double? verticalResolution) |
||||
|
{ |
||||
|
this.ResolutionUnit = resolutionUnit; |
||||
|
this.HorizontalResolution = horizontalResolution; |
||||
|
this.VerticalResolution = verticalResolution; |
||||
|
} |
||||
|
|
||||
|
public ushort ResolutionUnit { get; } |
||||
|
|
||||
|
public double? HorizontalResolution { get; } |
||||
|
|
||||
|
public double? VerticalResolution { get; } |
||||
|
} |
||||
|
} |
||||
@ -1,144 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Provides methods to evaluate the quality of an image.
|
|
||||
/// Ported from <see href="https://github.com/ImageMagick/ImageMagick/blob/f362c02083d27211b913c6e44794f0ac6edaf2bd/coders/jpeg.c#L855"/>
|
|
||||
/// </summary>
|
|
||||
internal static class QualityEvaluator |
|
||||
{ |
|
||||
private static readonly int[] Hash = new int[101] |
|
||||
{ |
|
||||
1020, 1015, 932, 848, 780, 735, 702, 679, 660, 645, |
|
||||
632, 623, 613, 607, 600, 594, 589, 585, 581, 571, |
|
||||
555, 542, 529, 514, 494, 474, 457, 439, 424, 410, |
|
||||
397, 386, 373, 364, 351, 341, 334, 324, 317, 309, |
|
||||
299, 294, 287, 279, 274, 267, 262, 257, 251, 247, |
|
||||
243, 237, 232, 227, 222, 217, 213, 207, 202, 198, |
|
||||
192, 188, 183, 177, 173, 168, 163, 157, 153, 148, |
|
||||
143, 139, 132, 128, 125, 119, 115, 108, 104, 99, |
|
||||
94, 90, 84, 79, 74, 70, 64, 59, 55, 49, |
|
||||
45, 40, 34, 30, 25, 20, 15, 11, 6, 4, |
|
||||
0 |
|
||||
}; |
|
||||
|
|
||||
private static readonly int[] Sums = new int[101] |
|
||||
{ |
|
||||
32640, 32635, 32266, 31495, 30665, 29804, 29146, 28599, 28104, |
|
||||
27670, 27225, 26725, 26210, 25716, 25240, 24789, 24373, 23946, |
|
||||
23572, 22846, 21801, 20842, 19949, 19121, 18386, 17651, 16998, |
|
||||
16349, 15800, 15247, 14783, 14321, 13859, 13535, 13081, 12702, |
|
||||
12423, 12056, 11779, 11513, 11135, 10955, 10676, 10392, 10208, |
|
||||
9928, 9747, 9564, 9369, 9193, 9017, 8822, 8639, 8458, |
|
||||
8270, 8084, 7896, 7710, 7527, 7347, 7156, 6977, 6788, |
|
||||
6607, 6422, 6236, 6054, 5867, 5684, 5495, 5305, 5128, |
|
||||
4945, 4751, 4638, 4442, 4248, 4065, 3888, 3698, 3509, |
|
||||
3326, 3139, 2957, 2775, 2586, 2405, 2216, 2037, 1846, |
|
||||
1666, 1483, 1297, 1109, 927, 735, 554, 375, 201, |
|
||||
128, 0 |
|
||||
}; |
|
||||
|
|
||||
private static readonly int[] Hash1 = new int[101] |
|
||||
{ |
|
||||
510, 505, 422, 380, 355, 338, 326, 318, 311, 305, |
|
||||
300, 297, 293, 291, 288, 286, 284, 283, 281, 280, |
|
||||
279, 278, 277, 273, 262, 251, 243, 233, 225, 218, |
|
||||
211, 205, 198, 193, 186, 181, 177, 172, 168, 164, |
|
||||
158, 156, 152, 148, 145, 142, 139, 136, 133, 131, |
|
||||
129, 126, 123, 120, 118, 115, 113, 110, 107, 105, |
|
||||
102, 100, 97, 94, 92, 89, 87, 83, 81, 79, |
|
||||
76, 74, 70, 68, 66, 63, 61, 57, 55, 52, |
|
||||
50, 48, 44, 42, 39, 37, 34, 31, 29, 26, |
|
||||
24, 21, 18, 16, 13, 11, 8, 6, 3, 2, |
|
||||
0 |
|
||||
}; |
|
||||
|
|
||||
private static readonly int[] Sums1 = new int[101] |
|
||||
{ |
|
||||
16320, 16315, 15946, 15277, 14655, 14073, 13623, 13230, 12859, |
|
||||
12560, 12240, 11861, 11456, 11081, 10714, 10360, 10027, 9679, |
|
||||
9368, 9056, 8680, 8331, 7995, 7668, 7376, 7084, 6823, |
|
||||
6562, 6345, 6125, 5939, 5756, 5571, 5421, 5240, 5086, |
|
||||
4976, 4829, 4719, 4616, 4463, 4393, 4280, 4166, 4092, |
|
||||
3980, 3909, 3835, 3755, 3688, 3621, 3541, 3467, 3396, |
|
||||
3323, 3247, 3170, 3096, 3021, 2952, 2874, 2804, 2727, |
|
||||
2657, 2583, 2509, 2437, 2362, 2290, 2211, 2136, 2068, |
|
||||
1996, 1915, 1858, 1773, 1692, 1620, 1552, 1477, 1398, |
|
||||
1326, 1251, 1179, 1109, 1031, 961, 884, 814, 736, |
|
||||
667, 592, 518, 441, 369, 292, 221, 151, 86, |
|
||||
64, 0 |
|
||||
}; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Returns an estimated quality of the image based on the quantization tables.
|
|
||||
/// </summary>
|
|
||||
/// <param name="quantizationTables">The quantization tables.</param>
|
|
||||
/// <returns>The <see cref="int"/>.</returns>
|
|
||||
public static int EstimateQuality(Block8x8F[] quantizationTables) |
|
||||
{ |
|
||||
int quality = 75; |
|
||||
float sum = 0; |
|
||||
|
|
||||
for (int i = 0; i < quantizationTables.Length; i++) |
|
||||
{ |
|
||||
ref Block8x8F qTable = ref quantizationTables[i]; |
|
||||
|
|
||||
if (!qTable.Equals(default)) |
|
||||
{ |
|
||||
for (int j = 0; j < Block8x8F.Size; j++) |
|
||||
{ |
|
||||
sum += qTable[j]; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
ref Block8x8F qTable0 = ref quantizationTables[0]; |
|
||||
ref Block8x8F qTable1 = ref quantizationTables[1]; |
|
||||
|
|
||||
if (!qTable0.Equals(default)) |
|
||||
{ |
|
||||
if (!qTable1.Equals(default)) |
|
||||
{ |
|
||||
quality = (int)(qTable0[2] |
|
||||
+ qTable0[53] |
|
||||
+ qTable1[0] |
|
||||
+ qTable1[Block8x8F.Size - 1]); |
|
||||
|
|
||||
for (int i = 0; i < 100; i++) |
|
||||
{ |
|
||||
if (quality < Hash[i] && sum < Sums[i]) |
|
||||
{ |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
if (((quality <= Hash[i]) && (sum <= Sums[i])) || (i >= 50)) |
|
||||
{ |
|
||||
return i + 1; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
quality = (int)(qTable0[2] + qTable0[53]); |
|
||||
|
|
||||
for (int i = 0; i < 100; i++) |
|
||||
{ |
|
||||
if (quality < Hash1[i] && sum < Sums1[i]) |
|
||||
{ |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
if (((quality <= Hash1[i]) && (sum <= Sums1[i])) || (i >= 50)) |
|
||||
{ |
|
||||
return i + 1; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return quality; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,194 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Jpeg.Components |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Provides methods and properties related to jpeg quantization.
|
||||
|
/// </summary>
|
||||
|
internal static class Quantization |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Upper bound (inclusive) for jpeg quality setting.
|
||||
|
/// </summary>
|
||||
|
public const int MaxQualityFactor = 100; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Lower bound (inclusive) for jpeg quality setting.
|
||||
|
/// </summary>
|
||||
|
public const int MinQualityFactor = 1; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Default JPEG quality for both luminance and chominance tables.
|
||||
|
/// </summary>
|
||||
|
public const int DefaultQualityFactor = 75; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Represents lowest quality setting which can be estimated with enough confidence.
|
||||
|
/// Any quality below it results in a highly compressed jpeg image
|
||||
|
/// which shouldn't use standard itu quantization tables for re-encoding.
|
||||
|
/// </summary>
|
||||
|
public const int QualityEstimationConfidenceLowerThreshold = 25; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Represents highest quality setting which can be estimated with enough confidence.
|
||||
|
/// </summary>
|
||||
|
public const int QualityEstimationConfidenceUpperThreshold = 98; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the unscaled luminance quantization table in zig-zag order. Each
|
||||
|
/// encoder copies and scales the tables according to its quality parameter.
|
||||
|
/// The values are derived from ITU section K.1 after converting from natural to
|
||||
|
/// zig-zag order.
|
||||
|
/// </summary>
|
||||
|
// The C# compiler emits this as a compile-time constant embedded in the PE file.
|
||||
|
// This is effectively compiled down to: return new ReadOnlySpan<byte>(&data, length)
|
||||
|
// More details can be found: https://github.com/dotnet/roslyn/pull/24621
|
||||
|
public static ReadOnlySpan<byte> UnscaledQuant_Luminance => new byte[] |
||||
|
{ |
||||
|
16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, |
||||
|
40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, |
||||
|
57, 51, 56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, |
||||
|
109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, |
||||
|
100, 120, 92, 101, 103, 99, |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the unscaled chrominance quantization table in zig-zag order. Each
|
||||
|
/// encoder copies and scales the tables according to its quality parameter.
|
||||
|
/// The values are derived from ITU section K.1 after converting from natural to
|
||||
|
/// zig-zag order.
|
||||
|
/// </summary>
|
||||
|
// The C# compiler emits this as a compile-time constant embedded in the PE file.
|
||||
|
// This is effectively compiled down to: return new ReadOnlySpan<byte>(&data, length)
|
||||
|
// More details can be found: https://github.com/dotnet/roslyn/pull/24621
|
||||
|
public static ReadOnlySpan<byte> UnscaledQuant_Chrominance => new byte[] |
||||
|
{ |
||||
|
17, 18, 18, 24, 21, 24, 47, 26, 26, 47, 99, 66, 56, 66, |
||||
|
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, |
||||
|
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, |
||||
|
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, |
||||
|
99, 99, 99, 99, 99, 99, 99, 99, |
||||
|
}; |
||||
|
|
||||
|
/// Ported from JPEGsnoop:
|
||||
|
/// https://github.com/ImpulseAdventure/JPEGsnoop/blob/9732ee0961f100eb69bbff4a0c47438d5997abee/source/JfifDecode.cpp#L4570-L4694
|
||||
|
/// <summary>
|
||||
|
/// Estimates jpeg quality based on quantization table in zig-zag order.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// This technically can be used with any given table but internal decoder code uses ITU spec tables:
|
||||
|
/// <see cref="UnscaledQuant_Luminance"/> and <see cref="UnscaledQuant_Chrominance"/>.
|
||||
|
/// </remarks>
|
||||
|
/// <param name="table">Input quantization table.</param>
|
||||
|
/// <param name="target">Quantization to estimate against.</param>
|
||||
|
/// <returns>Estimated quality</returns>
|
||||
|
public static int EstimateQuality(ref Block8x8F table, ReadOnlySpan<byte> target) |
||||
|
{ |
||||
|
// This method can be SIMD'ified if standard table is injected as Block8x8F.
|
||||
|
// Or when we go to full-int16 spectral code implementation and inject both tables as Block8x8.
|
||||
|
double comparePercent; |
||||
|
double sumPercent = 0; |
||||
|
|
||||
|
// Corner case - all 1's => 100 quality
|
||||
|
// It would fail to deduce using algorithm below without this check
|
||||
|
if (table.EqualsToScalar(1)) |
||||
|
{ |
||||
|
// While this is a 100% to be 100 quality, any given table can be scaled to all 1's.
|
||||
|
// According to jpeg creators, top of the line quality is 99, 100 is just a technical 'limit' which will affect result filesize drastically.
|
||||
|
// Quality=100 shouldn't be used in usual use case.
|
||||
|
return 100; |
||||
|
} |
||||
|
|
||||
|
int quality; |
||||
|
for (int i = 0; i < Block8x8F.Size; i++) |
||||
|
{ |
||||
|
float coeff = table[i]; |
||||
|
int coeffInteger = (int)coeff; |
||||
|
|
||||
|
// Coefficients are actually int16 casted to float numbers so there's no truncating error.
|
||||
|
if (coeffInteger != 0) |
||||
|
{ |
||||
|
comparePercent = 100.0 * (table[i] / target[i]); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
// No 'valid' quantization table should contain zero at any position
|
||||
|
// while this is okay to decode with, it will throw DivideByZeroException at encoding proces stage.
|
||||
|
// Not sure what to do here, we can't throw as this technically correct
|
||||
|
// but this will screw up the encoder.
|
||||
|
comparePercent = 999.99; |
||||
|
} |
||||
|
|
||||
|
sumPercent += comparePercent; |
||||
|
} |
||||
|
|
||||
|
// Perform some statistical analysis of the quality factor
|
||||
|
// to determine the likelihood of the current quantization
|
||||
|
// table being a scaled version of the "standard" tables.
|
||||
|
// If the variance is high, it is unlikely to be the case.
|
||||
|
sumPercent /= 64.0; |
||||
|
|
||||
|
// Generate the equivalent IJQ "quality" factor
|
||||
|
if (sumPercent <= 100.0) |
||||
|
{ |
||||
|
quality = (int)Math.Round((200 - sumPercent) / 2); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
quality = (int)Math.Round(5000.0 / sumPercent); |
||||
|
} |
||||
|
|
||||
|
return quality; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Estimates jpeg quality based on quantization table in zig-zag order.
|
||||
|
/// </summary>
|
||||
|
/// <param name="luminanceTable">Luminance quantization table.</param>
|
||||
|
/// <returns>Estimated quality</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static int EstimateLuminanceQuality(ref Block8x8F luminanceTable) |
||||
|
=> EstimateQuality(ref luminanceTable, UnscaledQuant_Luminance); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Estimates jpeg quality based on quantization table in zig-zag order.
|
||||
|
/// </summary>
|
||||
|
/// <param name="chrominanceTable">Chrominance quantization table.</param>
|
||||
|
/// <returns>Estimated quality</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static int EstimateChrominanceQuality(ref Block8x8F chrominanceTable) |
||||
|
=> EstimateQuality(ref chrominanceTable, UnscaledQuant_Chrominance); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
private static int QualityToScale(int quality) |
||||
|
{ |
||||
|
DebugGuard.MustBeBetweenOrEqualTo(quality, MinQualityFactor, MaxQualityFactor, nameof(quality)); |
||||
|
|
||||
|
return quality < 50 ? (5000 / quality) : (200 - (quality * 2)); |
||||
|
} |
||||
|
|
||||
|
private static Block8x8F ScaleQuantizationTable(int scale, ReadOnlySpan<byte> unscaledTable) |
||||
|
{ |
||||
|
Block8x8F table = default; |
||||
|
for (int j = 0; j < Block8x8F.Size; j++) |
||||
|
{ |
||||
|
int x = ((unscaledTable[j] * scale) + 50) / 100; |
||||
|
table[j] = Numerics.Clamp(x, 1, 255); |
||||
|
} |
||||
|
|
||||
|
return table; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static Block8x8F ScaleLuminanceTable(int quality) |
||||
|
=> ScaleQuantizationTable(scale: QualityToScale(quality), UnscaledQuant_Luminance); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static Block8x8F ScaleChrominanceTable(int quality) |
||||
|
=> ScaleQuantizationTable(scale: QualityToScale(quality), UnscaledQuant_Chrominance); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'BlackIsZero' photometric interpretation for 16-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
private readonly Configuration configuration; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BlackIsZero16TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">The configuration.</param>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public BlackIsZero16TiffColor(Configuration configuration, bool isBigEndian) |
||||
|
{ |
||||
|
this.configuration = configuration; |
||||
|
this.isBigEndian = isBigEndian; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
L16 l16 = TiffUtils.L16Default; |
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ushort intensity = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
int byteCount = pixelRow.Length * 2; |
||||
|
PixelOperations<TPixel>.Instance.FromL16Bytes( |
||||
|
this.configuration, |
||||
|
data.Slice(offset, byteCount), |
||||
|
pixelRow, |
||||
|
pixelRow.Length); |
||||
|
|
||||
|
offset += byteCount; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'BlackIsZero' photometric interpretation for 24-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class BlackIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BlackIsZero24TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public BlackIsZero24TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
byte[] buffer = new byte[4]; |
||||
|
int bufferStartIdx = this.isBigEndian ? 1 : 0; |
||||
|
|
||||
|
Span<byte> bufferSpan = buffer.AsSpan(bufferStartIdx); |
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong intensity = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong intensity = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'BlackIsZero' photometric interpretation for 32-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class BlackIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BlackIsZero32TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public BlackIsZero32TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong intensity = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong intensity = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,72 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Buffers; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 16 bit.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb16PlanarTiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public Rgb16PlanarTiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
Rgba64 rgba = TiffUtils.Rgba64Default; |
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
Span<byte> redData = data[0].GetSpan(); |
||||
|
Span<byte> greenData = data[1].GetSpan(); |
||||
|
Span<byte> blueData = data[2].GetSpan(); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToUShortBigEndian(redData.Slice(offset, 2)); |
||||
|
ulong g = TiffUtils.ConvertToUShortBigEndian(greenData.Slice(offset, 2)); |
||||
|
ulong b = TiffUtils.ConvertToUShortBigEndian(blueData.Slice(offset, 2)); |
||||
|
|
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToUShortLittleEndian(redData.Slice(offset, 2)); |
||||
|
ulong g = TiffUtils.ConvertToUShortLittleEndian(greenData.Slice(offset, 2)); |
||||
|
ulong b = TiffUtils.ConvertToUShortLittleEndian(blueData.Slice(offset, 2)); |
||||
|
|
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,82 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 24 bits for each channel.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb242424TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public Rgb242424TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
int offset = 0; |
||||
|
byte[] buffer = new byte[4]; |
||||
|
int bufferStartIdx = this.isBigEndian ? 1 : 0; |
||||
|
|
||||
|
Span<byte> bufferSpan = buffer.AsSpan(bufferStartIdx); |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
|
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,80 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Buffers; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 24 bit.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb24PlanarTiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public Rgb24PlanarTiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
byte[] buffer = new byte[4]; |
||||
|
int bufferStartIdx = this.isBigEndian ? 1 : 0; |
||||
|
|
||||
|
Span<byte> redData = data[0].GetSpan(); |
||||
|
Span<byte> greenData = data[1].GetSpan(); |
||||
|
Span<byte> blueData = data[2].GetSpan(); |
||||
|
Span<byte> bufferSpan = buffer.AsSpan(bufferStartIdx); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
redData.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
greenData.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
blueData.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
|
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
redData.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
greenData.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
blueData.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
|
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,73 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 32 bits for each channel.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb323232TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public Rgb323232TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
int offset = 0; |
||||
|
|
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
|
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
ulong g = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
ulong b = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
ulong g = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
ulong b = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,71 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Buffers; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 32 bit.
|
||||
|
/// </summary>
|
||||
|
internal class Rgb32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rgb32PlanarTiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public Rgb32PlanarTiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
Span<byte> redData = data[0].GetSpan(); |
||||
|
Span<byte> greenData = data[1].GetSpan(); |
||||
|
Span<byte> blueData = data[2].GetSpan(); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToUIntBigEndian(redData.Slice(offset, 4)); |
||||
|
ulong g = TiffUtils.ConvertToUIntBigEndian(greenData.Slice(offset, 4)); |
||||
|
ulong b = TiffUtils.ConvertToUIntBigEndian(blueData.Slice(offset, 4)); |
||||
|
|
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong r = TiffUtils.ConvertToUIntLittleEndian(redData.Slice(offset, 4)); |
||||
|
ulong g = TiffUtils.ConvertToUIntLittleEndian(greenData.Slice(offset, 4)); |
||||
|
ulong b = TiffUtils.ConvertToUIntLittleEndian(blueData.Slice(offset, 4)); |
||||
|
|
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Buffers; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The base class for planar color decoders.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
|
internal abstract class TiffBasePlanarColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Decodes source raw pixel data using the current photometric interpretation.
|
||||
|
/// </summary>
|
||||
|
/// <param name="data">The buffers to read image data from.</param>
|
||||
|
/// <param name="pixels">The image buffer to write pixels to.</param>
|
||||
|
/// <param name="left">The x-coordinate of the left-hand side of the image block.</param>
|
||||
|
/// <param name="top">The y-coordinate of the top of the image block.</param>
|
||||
|
/// <param name="width">The width of the image block.</param>
|
||||
|
/// <param name="height">The height of the image block.</param>
|
||||
|
public abstract void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'WhiteIsZero' photometric interpretation for 16-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class WhiteIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="WhiteIsZero16TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public WhiteIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
L16 l16 = TiffUtils.L16Default; |
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2))); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToUShortLittleEndian(data.Slice(offset, 2))); |
||||
|
offset += 2; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'WhiteIsZero' photometric interpretation for 24-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class WhiteIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="WhiteIsZero24TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public WhiteIsZero24TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
byte[] buffer = new byte[4]; |
||||
|
int bufferStartIdx = this.isBigEndian ? 1 : 0; |
||||
|
|
||||
|
Span<byte> bufferSpan = buffer.AsSpan(bufferStartIdx); |
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong intensity = TiffUtils.ConvertToUIntBigEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
data.Slice(offset, 3).CopyTo(bufferSpan); |
||||
|
ulong intensity = TiffUtils.ConvertToUIntLittleEndian(buffer); |
||||
|
offset += 3; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Utils; |
||||
|
using SixLabors.ImageSharp.Memory; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the 'WhiteIsZero' photometric interpretation for 32-bit grayscale images.
|
||||
|
/// </summary>
|
||||
|
internal class WhiteIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
private readonly bool isBigEndian; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="WhiteIsZero32TiffColor{TPixel}" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
|
||||
|
public WhiteIsZero32TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) |
||||
|
{ |
||||
|
// Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
|
||||
|
// we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
|
||||
|
var color = default(TPixel); |
||||
|
color.FromVector4(TiffUtils.Vector4Default); |
||||
|
|
||||
|
int offset = 0; |
||||
|
for (int y = top; y < top + height; y++) |
||||
|
{ |
||||
|
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width); |
||||
|
if (this.isBigEndian) |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong intensity = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
for (int x = 0; x < pixelRow.Length; x++) |
||||
|
{ |
||||
|
ulong intensity = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); |
||||
|
offset += 4; |
||||
|
|
||||
|
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,102 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Buffers.Binary; |
||||
|
using System.Numerics; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Tiff.Utils |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Helper methods for TIFF decoding.
|
||||
|
/// </summary>
|
||||
|
internal static class TiffUtils |
||||
|
{ |
||||
|
private const float Scale24Bit = 1.0f / 0xFFFFFF; |
||||
|
|
||||
|
private const float Scale32Bit = 1.0f / 0xFFFFFFFF; |
||||
|
|
||||
|
public static Vector4 Vector4Default { get; } = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); |
||||
|
|
||||
|
public static Rgba64 Rgba64Default { get; } = new Rgba64(0, 0, 0, 0); |
||||
|
|
||||
|
public static L16 L16Default { get; } = new L16(0); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static ushort ConvertToUShortBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16BigEndian(buffer); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static ushort ConvertToUShortLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16LittleEndian(buffer); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static uint ConvertToUIntBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32BigEndian(buffer); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static uint ConvertToUIntLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32LittleEndian(buffer); |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorFromL8<TPixel>(L8 l8, byte intensity, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
l8.PackedValue = intensity; |
||||
|
color.FromL8(l8); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorFromRgba64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48); |
||||
|
color.FromRgba64(rgba); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
var colorVector = new Vector4(r * Scale24Bit, g * Scale24Bit, b * Scale24Bit, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
var colorVector = new Vector4(r * Scale32Bit, g * Scale32Bit, b * Scale32Bit, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorFromL16<TPixel>(L16 l16, ushort intensity, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
l16.PackedValue = intensity; |
||||
|
color.FromL16(l16); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorScaleTo24Bit<TPixel>(ulong intensity, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
var colorVector = new Vector4(intensity * Scale24Bit, intensity * Scale24Bit, intensity * Scale24Bit, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ColorScaleTo32Bit<TPixel>(ulong intensity, TPixel color) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
var colorVector = new Vector4(intensity * Scale32Bit, intensity * Scale32Bit, intensity * Scale32Bit, 1.0f); |
||||
|
color.FromVector4(colorVector); |
||||
|
return color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using SixLabors.ImageSharp.Formats.Jpeg.Components; |
||||
|
using Xunit; |
||||
|
|
||||
|
using JpegQuantization = SixLabors.ImageSharp.Formats.Jpeg.Components.Quantization; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Jpg |
||||
|
{ |
||||
|
[Trait("Format", "Jpg")] |
||||
|
public class QuantizationTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void QualityEstimationFromStandardEncoderTables_Luminance() |
||||
|
{ |
||||
|
int firstIndex = JpegQuantization.QualityEstimationConfidenceLowerThreshold; |
||||
|
int lastIndex = JpegQuantization.QualityEstimationConfidenceUpperThreshold; |
||||
|
for (int quality = firstIndex; quality <= lastIndex; quality++) |
||||
|
{ |
||||
|
Block8x8F table = JpegQuantization.ScaleLuminanceTable(quality); |
||||
|
int estimatedQuality = JpegQuantization.EstimateLuminanceQuality(ref table); |
||||
|
|
||||
|
Assert.True(quality.Equals(estimatedQuality), $"Failed to estimate luminance quality for standard table at quality level {quality}"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void QualityEstimationFromStandardEncoderTables_Chrominance() |
||||
|
{ |
||||
|
int firstIndex = JpegQuantization.QualityEstimationConfidenceLowerThreshold; |
||||
|
int lastIndex = JpegQuantization.QualityEstimationConfidenceUpperThreshold; |
||||
|
for (int quality = firstIndex; quality <= lastIndex; quality++) |
||||
|
{ |
||||
|
Block8x8F table = JpegQuantization.ScaleChrominanceTable(quality); |
||||
|
int estimatedQuality = JpegQuantization.EstimateChrominanceQuality(ref table); |
||||
|
|
||||
|
Assert.True(quality.Equals(estimatedQuality), $"Failed to estimate chrominance quality for standard table at quality level {quality}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,114 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.IO; |
||||
|
|
||||
|
using SixLabors.ImageSharp.Formats; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Constants; |
||||
|
using SixLabors.ImageSharp.Metadata.Profiles.Exif; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; |
||||
|
|
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Tiff |
||||
|
{ |
||||
|
[Trait("Format", "Tiff")] |
||||
|
public abstract class TiffEncoderBaseTester |
||||
|
{ |
||||
|
protected static readonly IImageDecoder ReferenceDecoder = new MagickReferenceDecoder(); |
||||
|
|
||||
|
protected static void TestStripLength<TPixel>( |
||||
|
TestImageProvider<TPixel> provider, |
||||
|
TiffPhotometricInterpretation photometricInterpretation, |
||||
|
TiffCompression compression, |
||||
|
bool useExactComparer = true, |
||||
|
float compareTolerance = 0.01f) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
// arrange
|
||||
|
var tiffEncoder = new TiffEncoder() { PhotometricInterpretation = photometricInterpretation, Compression = compression }; |
||||
|
using Image<TPixel> input = provider.GetImage(); |
||||
|
using var memStream = new MemoryStream(); |
||||
|
TiffFrameMetadata inputMeta = input.Frames.RootFrame.Metadata.GetTiffMetadata(); |
||||
|
TiffCompression inputCompression = inputMeta.Compression ?? TiffCompression.None; |
||||
|
|
||||
|
// act
|
||||
|
input.Save(memStream, tiffEncoder); |
||||
|
|
||||
|
// assert
|
||||
|
memStream.Position = 0; |
||||
|
using var output = Image.Load<Rgba32>(memStream); |
||||
|
ExifProfile exifProfileOutput = output.Frames.RootFrame.Metadata.ExifProfile; |
||||
|
TiffFrameMetadata outputMeta = output.Frames.RootFrame.Metadata.GetTiffMetadata(); |
||||
|
ImageFrame<Rgba32> rootFrame = output.Frames.RootFrame; |
||||
|
|
||||
|
Number rowsPerStrip = exifProfileOutput.GetValue(ExifTag.RowsPerStrip) != null ? exifProfileOutput.GetValue(ExifTag.RowsPerStrip).Value : TiffConstants.RowsPerStripInfinity; |
||||
|
Assert.True(output.Height > (int)rowsPerStrip); |
||||
|
Assert.True(exifProfileOutput.GetValue(ExifTag.StripOffsets)?.Value.Length > 1); |
||||
|
Number[] stripByteCounts = exifProfileOutput.GetValue(ExifTag.StripByteCounts)?.Value; |
||||
|
Assert.NotNull(stripByteCounts); |
||||
|
Assert.True(stripByteCounts.Length > 1); |
||||
|
Assert.NotNull(outputMeta.BitsPerPixel); |
||||
|
|
||||
|
foreach (Number sz in stripByteCounts) |
||||
|
{ |
||||
|
Assert.True((uint)sz <= TiffConstants.DefaultStripSize); |
||||
|
} |
||||
|
|
||||
|
// For uncompressed more accurate test.
|
||||
|
if (compression == TiffCompression.None) |
||||
|
{ |
||||
|
for (int i = 0; i < stripByteCounts.Length - 1; i++) |
||||
|
{ |
||||
|
// The difference must be less than one row.
|
||||
|
int stripBytes = (int)stripByteCounts[i]; |
||||
|
int widthBytes = ((int)outputMeta.BitsPerPixel + 7) / 8 * rootFrame.Width; |
||||
|
|
||||
|
Assert.True((TiffConstants.DefaultStripSize - stripBytes) < widthBytes); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Compare with reference.
|
||||
|
TestTiffEncoderCore( |
||||
|
provider, |
||||
|
inputMeta.BitsPerPixel, |
||||
|
photometricInterpretation, |
||||
|
inputCompression, |
||||
|
useExactComparer: useExactComparer, |
||||
|
compareTolerance: compareTolerance); |
||||
|
} |
||||
|
|
||||
|
protected static void TestTiffEncoderCore<TPixel>( |
||||
|
TestImageProvider<TPixel> provider, |
||||
|
TiffBitsPerPixel? bitsPerPixel, |
||||
|
TiffPhotometricInterpretation photometricInterpretation, |
||||
|
TiffCompression compression = TiffCompression.None, |
||||
|
TiffPredictor predictor = TiffPredictor.None, |
||||
|
bool useExactComparer = true, |
||||
|
float compareTolerance = 0.001f, |
||||
|
IImageDecoder imageDecoder = null) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = photometricInterpretation, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = compression, |
||||
|
HorizontalPredictor = predictor |
||||
|
}; |
||||
|
|
||||
|
// Does DebugSave & load reference CompareToReferenceInput():
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
useExactComparer ? ImageComparer.Exact : ImageComparer.Tolerant(compareTolerance), |
||||
|
referenceDecoder: imageDecoder ?? ReferenceDecoder); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,179 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff; |
||||
|
using SixLabors.ImageSharp.Formats.Tiff.Constants; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
||||
|
using Xunit; |
||||
|
|
||||
|
using static SixLabors.ImageSharp.Tests.TestImages.Tiff; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Tiff |
||||
|
{ |
||||
|
[Trait("Format", "Tiff")] |
||||
|
public class TiffEncoderMultiframeTests : TiffEncoderBaseTester |
||||
|
{ |
||||
|
[Theory] |
||||
|
[WithFile(MultiframeLzwPredictor, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_Works<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(MultiframeDifferentSize, PixelTypes.Rgba32)] |
||||
|
[WithFile(MultiframeDifferentVariants, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_NotSupport<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => Assert.Throws<NotSupportedException>(() => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb)); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(MultiframeDeflateWithPreview, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_WithPreview<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit24, TiffPhotometricInterpretation.Rgb); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(TestImages.Gif.Receipt, PixelTypes.Rgb24)] |
||||
|
[WithFile(TestImages.Gif.Issues.BadDescriptorWidth, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_Convert<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit48, TiffPhotometricInterpretation.Rgb); |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(MultiframeLzwPredictor, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_RemoveFrames<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
Assert.True(image.Frames.Count > 1); |
||||
|
|
||||
|
image.Frames.RemoveFrame(0); |
||||
|
|
||||
|
TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit24; |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = TiffPhotometricInterpretation.Rgb, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = TiffCompression.Deflate |
||||
|
}; |
||||
|
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
ImageComparer.Exact); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[WithFile(TestImages.Png.Bike, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_AddFrames<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
Assert.Equal(1, image.Frames.Count); |
||||
|
|
||||
|
using var image1 = new Image<Rgba32>(image.Width, image.Height, Color.Green.ToRgba32()); |
||||
|
|
||||
|
using var image2 = new Image<Rgba32>(image.Width, image.Height, Color.Yellow.ToRgba32()); |
||||
|
|
||||
|
image.Frames.AddFrame(image1.Frames.RootFrame); |
||||
|
image.Frames.AddFrame(image2.Frames.RootFrame); |
||||
|
|
||||
|
TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit24; |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = TiffPhotometricInterpretation.Rgb, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = TiffCompression.Deflate |
||||
|
}; |
||||
|
|
||||
|
using (var ms = new System.IO.MemoryStream()) |
||||
|
{ |
||||
|
image.Save(ms, encoder); |
||||
|
|
||||
|
ms.Position = 0; |
||||
|
using var output = Image.Load<Rgba32>(ms); |
||||
|
|
||||
|
Assert.Equal(3, output.Frames.Count); |
||||
|
|
||||
|
ImageFrame<Rgba32> frame1 = output.Frames[1]; |
||||
|
ImageFrame<Rgba32> frame2 = output.Frames[2]; |
||||
|
|
||||
|
Assert.Equal(Color.Green.ToRgba32(), frame1[10, 10]); |
||||
|
Assert.Equal(Color.Yellow.ToRgba32(), frame2[10, 10]); |
||||
|
|
||||
|
Assert.Equal(TiffCompression.Deflate, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
Assert.Equal(TiffCompression.Deflate, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
|
||||
|
Assert.Equal(TiffPhotometricInterpretation.Rgb, frame1.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
Assert.Equal(TiffPhotometricInterpretation.Rgb, frame2.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
} |
||||
|
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
ImageComparer.Exact); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[WithBlankImages(100, 100, PixelTypes.Rgba32)] |
||||
|
public void TiffEncoder_EncodeMultiframe_Create<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
{ |
||||
|
using Image<TPixel> image = provider.GetImage(); |
||||
|
|
||||
|
using var image0 = new Image<Rgba32>(image.Width, image.Height, Color.Red.ToRgba32()); |
||||
|
|
||||
|
using var image1 = new Image<Rgba32>(image.Width, image.Height, Color.Green.ToRgba32()); |
||||
|
|
||||
|
using var image2 = new Image<Rgba32>(image.Width, image.Height, Color.Yellow.ToRgba32()); |
||||
|
|
||||
|
image.Frames.AddFrame(image0.Frames.RootFrame); |
||||
|
image.Frames.AddFrame(image1.Frames.RootFrame); |
||||
|
image.Frames.AddFrame(image2.Frames.RootFrame); |
||||
|
image.Frames.RemoveFrame(0); |
||||
|
|
||||
|
TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit8; |
||||
|
var encoder = new TiffEncoder |
||||
|
{ |
||||
|
PhotometricInterpretation = TiffPhotometricInterpretation.PaletteColor, |
||||
|
BitsPerPixel = bitsPerPixel, |
||||
|
Compression = TiffCompression.Lzw |
||||
|
}; |
||||
|
|
||||
|
using (var ms = new System.IO.MemoryStream()) |
||||
|
{ |
||||
|
image.Save(ms, encoder); |
||||
|
|
||||
|
ms.Position = 0; |
||||
|
using var output = Image.Load<Rgba32>(ms); |
||||
|
|
||||
|
Assert.Equal(3, output.Frames.Count); |
||||
|
|
||||
|
ImageFrame<Rgba32> frame0 = output.Frames[0]; |
||||
|
ImageFrame<Rgba32> frame1 = output.Frames[1]; |
||||
|
ImageFrame<Rgba32> frame2 = output.Frames[2]; |
||||
|
|
||||
|
Assert.Equal(Color.Red.ToRgba32(), frame0[10, 10]); |
||||
|
Assert.Equal(Color.Green.ToRgba32(), frame1[10, 10]); |
||||
|
Assert.Equal(Color.Yellow.ToRgba32(), frame2[10, 10]); |
||||
|
|
||||
|
Assert.Equal(TiffCompression.Lzw, frame0.Metadata.GetTiffMetadata().Compression); |
||||
|
Assert.Equal(TiffCompression.Lzw, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
Assert.Equal(TiffCompression.Lzw, frame1.Metadata.GetTiffMetadata().Compression); |
||||
|
|
||||
|
Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame0.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame1.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame2.Metadata.GetTiffMetadata().PhotometricInterpretation); |
||||
|
} |
||||
|
|
||||
|
image.VerifyEncoder( |
||||
|
provider, |
||||
|
"tiff", |
||||
|
bitsPerPixel, |
||||
|
encoder, |
||||
|
ImageComparer.Exact); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:56b3db3d0e146ee7fe27f8fbda4bccc1483e18104bfc747cac75a2ec03d65647 |
||||
|
size 1936782 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:d377b70cedfb9d25f1ae0244dcf2edb000540aa4a8925cce57f810f7efd0dc84 |
||||
|
size 234976 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:c3806304a5453a6ec8a6795bc77b967b9aa8593288af36bbf9802f22ee27869e |
||||
|
size 6588 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:6b5a96942ee27a2b25d3cbb8bdd05239be71f84acc4d63c95380841a8a67befd |
||||
|
size 9770 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:fe2d4e0d99bdfade966e27bd9583bce39bebb90efa8e7f768ce3cec69aa306e2 |
||||
|
size 9770 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:6677c372a449fe0324b148385cf0ebaaf33ab4563484ae89831dfeacd80d7c93 |
||||
|
size 12885 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:e37a4455e6b61e32720af99127b82aacdc907be91b8ed1d8e1a1f06d6a853211 |
||||
|
size 12885 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:d8f2c2afd8f1645717087bd2edbc3e8a46b88a54a4996c0e9350fdd652b5c382 |
||||
|
size 6588 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:435c92b453587e1943940111b66afabf70307beb0e1d65e9701fd9bb753eead2 |
||||
|
size 6588 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:514417ead3d6c5c6ca33374ef0bb6ecbe5f875a266519d4cbaa4a6b91033d243 |
||||
|
size 12778 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:64c948aa03bc4a24cd1d68bb18b5031c119936154a90f1cb1d9aaabd854c5d9b |
||||
|
size 12778 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:0951a9c2207eb6864b6a19ec8513a28a874adddb37c3c06b9fd07831372924e3 |
||||
|
size 19150 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:c6368a704b0a629239024f6fbfb30723fa317593ef36ddba05d76302530bd974 |
||||
|
size 28568 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:bbb2b4ca6d7eeee4737c6963c99ef68fb6971cf6ccee463427a8246574bc6440 |
||||
|
size 28632 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:d7b9da8ec44da84fc89aed1ad221a5eb130a1f233a1ff8a4a15b41898a0e364f |
||||
|
size 38027 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:0876580f9c5d8e13656210582137104daba137c99d55eafb5ebbfa418efa6525 |
||||
|
size 38027 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:a49cf47fdf2ea43e5cb5a473523e50222fb13ff6a66bda2e4bdd5796f66140d8 |
||||
|
size 9770 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:a49cf47fdf2ea43e5cb5a473523e50222fb13ff6a66bda2e4bdd5796f66140d8 |
||||
|
size 9770 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:46a60552a7ff37f2c16c43e030e7180872af712f5d9c9c7673e2547049af3da9 |
||||
|
size 19168 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:752452ac51ad1e836fb81267ab708ff81cf81a4c7e00daeed703f67782b563ec |
||||
|
size 28586 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:72f27af4fe177ebe47bef2af64723497d5a5f44808424bedfc2012fe4e3fc34e |
||||
|
size 28586 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:a718ae37d6d7a5bb5702cc75350f6feec3e9cdcd7e22aaa4753c7fe9c2db9aae |
||||
|
size 38035 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:2241683d74e9a52c5077870731e7bd5a7e7558c2a04fd0edf57da3a583044442 |
||||
|
size 38035 |
||||
Loading…
Reference in new issue