Skip to content

Commit

Permalink
Fix Separation color space and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Sep 29, 2024
1 parent 8ce6bcc commit 5260c4b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
37 changes: 35 additions & 2 deletions src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void SeparationColorSpaceImages()
{
var page1 = document.GetPage(1);
var images = page1.GetImages().ToArray();
var image1page1 = images.ElementAt(0);
var image1page1 = images[0];
var separationCs = image1page1.ColorSpaceDetails as SeparationColorSpaceDetails;
Assert.NotNull(separationCs);
Assert.True(separationCs.AlternateColorSpace is DeviceCmykColorSpaceDetails);
Expand All @@ -135,6 +135,39 @@ public void SeparationColorSpaceImages()
}
}

[Fact]
public void SeparationColorSpace()
{
var path = IntegrationHelpers.GetDocumentPath("MOZILLA-3136-0.pdf");

using (var document = PdfDocument.Open(path))
{
var page = document.GetPage(4);
var images = page.GetImages().ToArray();

var image4 = images[4];

var separation = image4.ColorSpaceDetails as SeparationColorSpaceDetails;
Assert.NotNull(separation);

Assert.True(image4.TryGetPng(out var png4));

File.WriteAllBytes(Path.Combine(OutputFolder, "MOZILLA-3136-0_4_separation.png"), png4);

// Green dolphin image
// "Colorized TIFF (should appear only in GWG Green separation)"
var image9 = images[9];

var indexedCs = image9.ColorSpaceDetails as IndexedColorSpaceDetails;
Assert.NotNull(indexedCs);
Assert.Equal(ColorSpace.Separation, indexedCs.BaseColorSpace.Type);

Assert.True(image9.TryGetPng(out var png9));

File.WriteAllBytes(Path.Combine(OutputFolder, "MOZILLA-3136-0_9_separation.png"), png9);
}
}

[Fact]
public void IndexedCalRgbColorSpaceImages()
{
Expand Down Expand Up @@ -236,7 +269,7 @@ public void StencilIndexedIccColorSpaceImages()
File.WriteAllBytes(Path.Combine(OutputFolder, "MOZILLA-10225-0_341_0.png"), bytes341_0);
}
}

[Fact]
public void SeparationLabColorSpace()
{
Expand Down
15 changes: 8 additions & 7 deletions src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,18 +728,19 @@ public override IColor GetColor(params double[] values)
/// <inheritdoc/>
internal override ReadOnlySpan<byte> Transform(ReadOnlySpan<byte> values)
{
var cache = new Dictionary<int, double[]>(values.Length * 3);
var transformed = new List<byte>(values.Length * 3);
for (var i = 0; i < values.Length; i += 3)
var colorCache = new Dictionary<int, double[]>(values.Length);
var transformed = new List<byte>(values.Length);

for (var i = 0; i < values.Length; ++i)
{
byte b = values[i++];
if (!cache.TryGetValue(b, out double[]? colors))
byte b = values[i];
if (!colorCache.TryGetValue(b, out double[]? colors))
{
colors = Process(b / 255.0);
cache[b] = colors;
colorCache[b] = colors;
}

for (int c = 0; c < colors.Length; c++)
for (int c = 0; c < colors.Length; ++c)
{
transformed.Add(ConvertToByte(colors[c]));
}
Expand Down

0 comments on commit 5260c4b

Please sign in to comment.