Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
private readonly bool hasAlpha;
private Color[]? paletteColors;

private const float InvMax = 1f / 65535f;

/// <summary>
/// Initializes a new instance of the <see cref="PaletteTiffColor{TPixel}"/> class.
/// </summary>
Expand All @@ -42,8 +40,15 @@ public PaletteTiffColor(TiffBitsPerSample bitsPerSample, ushort[] colorMap, Tiff

int colorCount = 1 << this.bitsPerSample0;

// Match libtiff's legacy palette detection: use 8-bit scaling only when every RGB entry
// fits in 0-255. The runtime vectorizes this range check without allocating.
// Normalize by the selected channel range, not the observed maximum, to preserve brightness.
float colorScale = colorMap.AsSpan().ContainsAnyExceptInRange((ushort)0, (ushort)255)
? 1f / 65535f
: 1f / 255f;

// TIFF PaletteColor uses ColorMap (tag 320 / 0x0140) which is RGB-only (no alpha).
this.vectorPallete = GenerateVectorPalette(colorMap, colorCount);
this.vectorPallete = GenerateVectorPalette(colorMap, colorCount, colorScale);

// ExtraSamples (tag 338 / 0x0152) describes extra per-pixel samples stored in the image data stream.
// For PaletteColor, any alpha is per pixel (stored alongside the index), not per palette entry.
Expand All @@ -61,7 +66,7 @@ public PaletteTiffColor(TiffBitsPerSample bitsPerSample, ushort[] colorMap, Tiff
else
{
// Pre-generate pixel palette for non-alpha case for performance.
this.pixelPalette = GeneratePixelPalette(colorMap, colorCount);
this.pixelPalette = GeneratePixelPalette(colorMap, colorCount, colorScale);
}
}

Expand Down Expand Up @@ -131,7 +136,14 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
}
}

private static Vector4[] GenerateVectorPalette(ushort[] colorMap, int colorCount)
/// <summary>
/// Converts the RGB color map to normalized vectors with opaque alpha.
/// </summary>
/// <param name="colorMap">The planar RGB color map.</param>
/// <param name="colorCount">The number of palette colors.</param>
/// <param name="colorScale">The reciprocal of the detected channel range maximum.</param>
/// <returns>The normalized vector palette.</returns>
private static Vector4[] GenerateVectorPalette(ushort[] colorMap, int colorCount, float colorScale)
{
Vector4[] palette = new Vector4[colorCount];

Expand All @@ -141,16 +153,23 @@ private static Vector4[] GenerateVectorPalette(ushort[] colorMap, int colorCount

for (int i = 0; i < palette.Length; i++)
{
float r = colorMap[rOffset + i] * InvMax;
float g = colorMap[gOffset + i] * InvMax;
float b = colorMap[bOffset + i] * InvMax;
float r = colorMap[rOffset + i] * colorScale;
float g = colorMap[gOffset + i] * colorScale;
float b = colorMap[bOffset + i] * colorScale;
palette[i] = new Vector4(r, g, b, 1f);
}

return palette;
}

private static TPixel[] GeneratePixelPalette(ushort[] colorMap, int colorCount)
/// <summary>
/// Converts the RGB color map to pixels with opaque alpha.
/// </summary>
/// <param name="colorMap">The planar RGB color map.</param>
/// <param name="colorCount">The number of palette colors.</param>
/// <param name="colorScale">The reciprocal of the detected channel range maximum.</param>
/// <returns>The pixel palette.</returns>
private static TPixel[] GeneratePixelPalette(ushort[] colorMap, int colorCount, float colorScale)
{
TPixel[] palette = new TPixel[colorCount];

Expand All @@ -160,9 +179,9 @@ private static TPixel[] GeneratePixelPalette(ushort[] colorMap, int colorCount)

for (int i = 0; i < palette.Length; i++)
{
float r = colorMap[rOffset + i] * InvMax;
float g = colorMap[gOffset + i] * InvMax;
float b = colorMap[bOffset + i] * InvMax;
float r = colorMap[rOffset + i] * colorScale;
float g = colorMap[gOffset + i] * colorScale;
float b = colorMap[bOffset + i] * colorScale;
palette[i] = TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f));
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ public void TiffDecoder_CanDecode_Planar_128Bit<TPixel>(TestImageProvider<TPixel
public void TiffDecoder_CanDecode_WithPalette<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);

/// <summary>
/// Palette TIFFs with LZW compression and horizontal prediction preserve colors for both palette ranges.
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="provider">The TIFF image provider.</param>
[Theory]
[WithFile(Issue3182ColorMap16Bit, PixelTypes.Rgba64)]
[WithFile(Issue3182ColorMap8Bit, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_PaletteWithLzwAndPredictor<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);

[Theory]
[WithFile(Rgb4BitPalette, PixelTypes.Rgba32)]
[WithFile(Flower4BitPalette, PixelTypes.Rgba32)]
Expand Down
2 changes: 2 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,8 @@ public static class Tiff

public const string Issue2909 = "Tiff/Issues/Issue2909.tiff";
public const string Issue2983 = "Tiff/Issues/Issue2983.tiff";
public const string Issue3182ColorMap8Bit = "Tiff/Issues/Issue3182ColorMap8Bit.tiff";
public const string Issue3182ColorMap16Bit = "Tiff/Issues/Issue3182ColorMap16Bit.tiff";

public static readonly string[] Multiframes = [MultiframeDeflateWithPreview, MultiframeLzwPredictor /*, MultiFrameDifferentSize, MultiframeDifferentSizeTiled, MultiFrameDifferentVariants,*/
];
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Tiff/Issues/Issue3182ColorMap16Bit.tiff
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/Images/Input/Tiff/Issues/Issue3182ColorMap8Bit.tiff
Git LFS file not shown
Loading