From 5260c4ba3ddc57019fd4cfdde2e2cbbbebb24962 Mon Sep 17 00:00:00 2001
From: BobLd <38405645+BobLd@users.noreply.github.com>
Date: Mon, 26 Aug 2024 17:13:48 +0100
Subject: [PATCH] Fix Separation color space and add tests
---
.../Integration/ColorSpaceTests.cs | 37 ++++++++++++++++++-
.../Graphics/Colors/ColorSpaceDetails.cs | 15 ++++----
2 files changed, 43 insertions(+), 9 deletions(-)
diff --git a/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs b/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
index e8a4fa694..0f70332e0 100644
--- a/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
+++ b/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
@@ -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);
@@ -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()
{
@@ -236,7 +269,7 @@ public void StencilIndexedIccColorSpaceImages()
File.WriteAllBytes(Path.Combine(OutputFolder, "MOZILLA-10225-0_341_0.png"), bytes341_0);
}
}
-
+
[Fact]
public void SeparationLabColorSpace()
{
diff --git a/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs b/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
index e034db7a9..55df8e0ad 100644
--- a/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
+++ b/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
@@ -728,18 +728,19 @@ public override IColor GetColor(params double[] values)
///
internal override ReadOnlySpan Transform(ReadOnlySpan values)
{
- var cache = new Dictionary(values.Length * 3);
- var transformed = new List(values.Length * 3);
- for (var i = 0; i < values.Length; i += 3)
+ var colorCache = new Dictionary(values.Length);
+ var transformed = new List(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]));
}