From 2c5cb69a64d58ad35e6e0d5b6e72bb7eeca22073 Mon Sep 17 00:00:00 2001
From: BobLd <38405645+BobLd@users.noreply.github.com>
Date: Sun, 23 Apr 2023 18:39:24 +0100
Subject: [PATCH] parallel process images in color space
---
.../Integration/ColorSpaceTests.cs | 7 +-
.../Graphics/Colors/ColorSpaceDetails.cs | 103 ++++++++++++++++--
2 files changed, 99 insertions(+), 11 deletions(-)
diff --git a/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs b/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
index cd225a80..8de9a3d2 100644
--- a/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
+++ b/src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
@@ -334,11 +334,12 @@
for (int p = 0; p < document.NumberOfPages; p++)
{
var page = document.GetPage(p + 1);
- foreach (var image in page.GetImages())
+ var images = page.GetImages().ToArray();
+ for (int i = 0; i < images.Length; i++)
{
- if (image.TryGetPng(out var png))
+ if (images[i].TryGetPng(out var png))
{
- // TODO
+ File.WriteAllBytes(Path.Combine(OutputFolder, $"Pig Production Handbook_{p + 1}_{i}.png"), png);
}
}
}
diff --git a/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs b/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
index d6c7b37c..dcefe74a 100644
--- a/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
+++ b/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
@@ -6,6 +6,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
+ using System.Threading.Tasks;
using Tokens;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Functions;
@@ -535,6 +536,89 @@
///
internal override IReadOnlyList Transform(IReadOnlyList decoded)
{
+ int outputCount = Process(Enumerable.Repeat(1.0, NumberOfColorComponents).ToArray()).Length;
+ int outputSize = (int)(decoded.Count * outputCount / (double)NumberOfColorComponents);
+ var transformed = new byte[outputSize];
+
+ Parallel.For(0, decoded.Count / NumberOfColorComponents, i =>
+ {
+ double[] comps = new double[NumberOfColorComponents];
+ for (int n = 0; n < NumberOfColorComponents; n++)
+ {
+ comps[n] = decoded[i * NumberOfColorComponents + n] / 255.0;
+ }
+
+ var colors = Process(comps);
+ for (int c = 0; c < outputCount; c++)
+ {
+ transformed[i * outputCount + c] = ConvertToByte(colors[c]);
+ }
+ });
+ return transformed;
+ }
+
+ /*
+
+ ///
+ internal override IReadOnlyList Transform(IReadOnlyList decoded)
+ {
+ int outputCount = Process(Enumerable.Repeat(1.0, NumberOfColorComponents).ToArray()).Length;
+ int outputSize = (int)(decoded.Count * outputCount / (double)NumberOfColorComponents);
+ var transformed = new byte[outputSize];
+
+ //Parallel.For(0, )
+ for (var i = 0; i < decoded.Count / NumberOfColorComponents; i++)
+ {
+ double[] comps = new double[NumberOfColorComponents];
+ for (int n = 0; n < NumberOfColorComponents; n++)
+ {
+ comps[n] = decoded[i* NumberOfColorComponents + n] / 255.0;
+ }
+
+ var colors = Process(comps);
+ for (int c = 0; c < outputCount; c++)
+ {
+ transformed[i * outputCount + c] = ConvertToByte(colors[c]);
+ }
+ }
+
+ return transformed;
+ }
+ */
+
+ /*
+ ///
+ internal override IReadOnlyList Transform(IReadOnlyList decoded)
+ {
+ int outputCount = Process(Enumerable.Repeat(1.0, NumberOfColorComponents).ToArray()).Length;
+ int outputSize = (int)(decoded.Count * outputCount / (double)NumberOfColorComponents);
+
+ var transformed = new byte[outputSize];
+ for (var i = 0; i < decoded.Count; i += NumberOfColorComponents)
+ {
+ double[] comps = new double[NumberOfColorComponents];
+ for (int n = 0; n < NumberOfColorComponents; n++)
+ {
+ comps[n] = decoded[i + n] / 255.0;
+ }
+
+ var colors = Process(comps);
+ for (int c = 0; c < outputCount; c++)
+ {
+ transformed[(i / NumberOfColorComponents) * outputCount + c] = ConvertToByte(colors[c]);
+ }
+ }
+
+ return transformed;
+ }
+ */
+
+ /*
+ ///
+ internal override IReadOnlyList Transform(IReadOnlyList decoded)
+ {
+ int outputCount = Process(Enumerable.Repeat(1.0, NumberOfColorComponents).ToArray()).Length;
+
var transformed = new List();
for (var i = 0; i < decoded.Count; i += NumberOfColorComponents)
{
@@ -545,7 +629,7 @@
}
var colors = Process(comps);
- for (int c = 0; c < colors.Length; c++)
+ for (int c = 0; c < outputCount; c++)
{
transformed.Add(ConvertToByte(colors[c]));
}
@@ -553,6 +637,7 @@
return transformed;
}
+ */
///
public override IColor GetInitializeColor()
@@ -1348,22 +1433,24 @@
{
if (Profile != null)
{
- var transformed = new List();
- for (var i = 0; i < decoded.Count; i += NumberOfColorComponents)
+ int outputCount = Process(Enumerable.Repeat(1.0, NumberOfColorComponents).ToArray()).Length;
+ int outputSize = (int)(decoded.Count * outputCount / (double)NumberOfColorComponents);
+ var transformed = new byte[outputSize];
+
+ Parallel.For(0, decoded.Count / NumberOfColorComponents, i =>
{
double[] comps = new double[NumberOfColorComponents];
for (int n = 0; n < NumberOfColorComponents; n++)
{
- comps[n] = decoded[i + n] / 255.0;
+ comps[n] = decoded[i * NumberOfColorComponents + n] / 255.0;
}
var colors = Process(comps);
- for (int c = 0; c < colors.Length; c++)
+ for (int c = 0; c < outputCount; c++)
{
- transformed.Add(ConvertToByte(colors[c]));
+ transformed[i * outputCount + c] = ConvertToByte(colors[c]);
}
- }
-
+ });
return transformed;
}