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
Binary file not shown.
12 changes: 12 additions & 0 deletions src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public void Issues1349()
}
}

[Fact]
public void Issues1351()
{
var path = IntegrationHelpers.GetDocumentPath("IndexedCS_negative_and_high.pdf");
using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true }))
{
var page = document.GetPage(1);
Assert.NotNull(page);
Assert.NotNull(page.Text);
}
}

[Fact]
public void Issues1330()
{
Expand Down
41 changes: 37 additions & 4 deletions src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public override IColor GetColor(params double[] values)
{
return GrayColor.Black;
}

if (gray == 1)
{
return GrayColor.White;
Expand Down Expand Up @@ -219,7 +219,7 @@ public override IColor GetColor(params double[] values)
{
return RGBColor.Black;
}

if (r == 1 && g == 1 && b == 1)
{
return RGBColor.White;
Expand Down Expand Up @@ -382,10 +382,27 @@ public IndexedColorSpaceDetails(ColorSpaceDetails baseColorSpaceDetails, byte hi
BaseType = baseColorSpaceDetails.Type;
}

/// <summary>
/// Convert a colour index, which may be a real number or fall outside the valid
/// range, into a valid table index. Per ISO 32000-2 (PDF 2.0) 8.6.6.3 the value is
/// rounded to the nearest integer (0.5 rounds up) and any value outside 0..<see cref="HiVal"/>
/// is adjusted to the nearest value within that range.
/// </summary>
private byte ClampColorIndex(double value)
{
double rounded = Math.Round(value, MidpointRounding.AwayFromZero);
if (rounded <= 0)
{
return 0;
}

return rounded >= HiVal ? HiVal : (byte)rounded;
}

/// <inheritdoc/>
internal override double[] Process(params double[] values)
{
var csBytes = UnwrapIndexedColorSpaceBytes([(byte)values[0]]);
var csBytes = UnwrapIndexedColorSpaceBytes([ClampColorIndex(values[0])]);

var scaledCsBytes = new double[csBytes.Length];

Expand All @@ -407,7 +424,7 @@ public override IColor GetColor(params double[] values)

return cache.GetOrAdd(values[0], v =>
{
var csBytes = UnwrapIndexedColorSpaceBytes([(byte)v]);
var csBytes = UnwrapIndexedColorSpaceBytes([ClampColorIndex(v)]);

var scaledCsBytes = new double[csBytes.Length];

Expand All @@ -422,6 +439,22 @@ public override IColor GetColor(params double[] values)

internal Span<byte> UnwrapIndexedColorSpaceBytes(Span<byte> input)
{
// ISO 32000-2 (PDF 2.0) 8.6.6.3: an index outside 0..hival is adjusted to the
// nearest valid value. Indices arrive here as (unsigned) bytes (image samples, or
// the already-clamped content-stream index), so only the upper bound can be
// exceeded.
if (HiVal != byte.MaxValue)
{
for (int k = 0; k < input.Length; ++k)
{
ref byte c = ref input[k];
if (c > HiVal)
{
c = HiVal;
}
}
}

switch (BaseType)
{
case ColorSpace.DeviceRGB:
Expand Down
Loading