Skip to content

Commit 5260c4b

Browse files
committed
Fix Separation color space and add tests
1 parent 8ce6bcc commit 5260c4b

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void SeparationColorSpaceImages()
120120
{
121121
var page1 = document.GetPage(1);
122122
var images = page1.GetImages().ToArray();
123-
var image1page1 = images.ElementAt(0);
123+
var image1page1 = images[0];
124124
var separationCs = image1page1.ColorSpaceDetails as SeparationColorSpaceDetails;
125125
Assert.NotNull(separationCs);
126126
Assert.True(separationCs.AlternateColorSpace is DeviceCmykColorSpaceDetails);
@@ -135,6 +135,39 @@ public void SeparationColorSpaceImages()
135135
}
136136
}
137137

138+
[Fact]
139+
public void SeparationColorSpace()
140+
{
141+
var path = IntegrationHelpers.GetDocumentPath("MOZILLA-3136-0.pdf");
142+
143+
using (var document = PdfDocument.Open(path))
144+
{
145+
var page = document.GetPage(4);
146+
var images = page.GetImages().ToArray();
147+
148+
var image4 = images[4];
149+
150+
var separation = image4.ColorSpaceDetails as SeparationColorSpaceDetails;
151+
Assert.NotNull(separation);
152+
153+
Assert.True(image4.TryGetPng(out var png4));
154+
155+
File.WriteAllBytes(Path.Combine(OutputFolder, "MOZILLA-3136-0_4_separation.png"), png4);
156+
157+
// Green dolphin image
158+
// "Colorized TIFF (should appear only in GWG Green separation)"
159+
var image9 = images[9];
160+
161+
var indexedCs = image9.ColorSpaceDetails as IndexedColorSpaceDetails;
162+
Assert.NotNull(indexedCs);
163+
Assert.Equal(ColorSpace.Separation, indexedCs.BaseColorSpace.Type);
164+
165+
Assert.True(image9.TryGetPng(out var png9));
166+
167+
File.WriteAllBytes(Path.Combine(OutputFolder, "MOZILLA-3136-0_9_separation.png"), png9);
168+
}
169+
}
170+
138171
[Fact]
139172
public void IndexedCalRgbColorSpaceImages()
140173
{
@@ -236,7 +269,7 @@ public void StencilIndexedIccColorSpaceImages()
236269
File.WriteAllBytes(Path.Combine(OutputFolder, "MOZILLA-10225-0_341_0.png"), bytes341_0);
237270
}
238271
}
239-
272+
240273
[Fact]
241274
public void SeparationLabColorSpace()
242275
{

src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -728,18 +728,19 @@ public override IColor GetColor(params double[] values)
728728
/// <inheritdoc/>
729729
internal override ReadOnlySpan<byte> Transform(ReadOnlySpan<byte> values)
730730
{
731-
var cache = new Dictionary<int, double[]>(values.Length * 3);
732-
var transformed = new List<byte>(values.Length * 3);
733-
for (var i = 0; i < values.Length; i += 3)
731+
var colorCache = new Dictionary<int, double[]>(values.Length);
732+
var transformed = new List<byte>(values.Length);
733+
734+
for (var i = 0; i < values.Length; ++i)
734735
{
735-
byte b = values[i++];
736-
if (!cache.TryGetValue(b, out double[]? colors))
736+
byte b = values[i];
737+
if (!colorCache.TryGetValue(b, out double[]? colors))
737738
{
738739
colors = Process(b / 255.0);
739-
cache[b] = colors;
740+
colorCache[b] = colors;
740741
}
741742

742-
for (int c = 0; c < colors.Length; c++)
743+
for (int c = 0; c < colors.Length; ++c)
743744
{
744745
transformed.Add(ConvertToByte(colors[c]));
745746
}

0 commit comments

Comments
 (0)