Skip to content

Commit 4d8b127

Browse files
committed
Add CMYK support for image mask array
1 parent 21bf590 commit 4d8b127

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

UglyToad.PdfPig.Rendering.Skia/Helpers/SkiaImageExtensions.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ private static bool TryGenerate(this IPdfImage pdfImage, out SKImage skImage)
182182
throw new ArgumentException($"The size of the transformed mask array does not match number of components of 4: got {range.Length} but expected 8.");
183183
}
184184

185-
throw new NotImplementedException("Mask CMYK");
185+
// TODO - Add tests
186+
CmykToRgb(in range[0], in range[1], in range[2], in range[3], out rMin, out gMin, out bMin);
187+
CmykToRgb(in range[4], in range[5], in range[6], in range[7], out rMax, out gMax, out bMax);
186188
}
187189
else if (numberOfComponents == 3)
188190
{
@@ -219,20 +221,8 @@ private static bool TryGenerate(this IPdfImage pdfImage, out SKImage skImage)
219221
{
220222
for (int col = 0; col < width; ++col)
221223
{
222-
/*
223-
* Where CMYK in 0..1
224-
* R = 255 × (1-C) × (1-K)
225-
* G = 255 × (1-M) × (1-K)
226-
* B = 255 × (1-Y) × (1-K)
227-
*/
228-
229-
double c = (bytesPure[i++] / 255d);
230-
double m = (bytesPure[i++] / 255d);
231-
double y = (bytesPure[i++] / 255d);
232-
double k = (bytesPure[i++] / 255d);
233-
var r = (byte)(255 * (1 - c) * (1 - k));
234-
var g = (byte)(255 * (1 - m) * (1 - k));
235-
var b = (byte)(255 * (1 - y) * (1 - k));
224+
CmykToRgb(in bytesPure[i++], in bytesPure[i++], in bytesPure[i++], in bytesPure[i++],
225+
out byte r, out byte g, out byte b);
236226

237227
var start = (row * (width * bytesPerPixel)) + (col * bytesPerPixel);
238228
raster[start++] = r;
@@ -283,6 +273,24 @@ private static bool TryGenerate(this IPdfImage pdfImage, out SKImage skImage)
283273
return false;
284274
}
285275

276+
private static void CmykToRgb(in byte c, in byte m, in byte y, in byte k, out byte r, out byte g, out byte b)
277+
{
278+
/*
279+
* Where CMYK in 0..1
280+
* R = 255 × (1-C) × (1-K)
281+
* G = 255 × (1-M) × (1-K)
282+
* B = 255 × (1-Y) × (1-K)
283+
*/
284+
285+
double cD = c / 255d;
286+
double mD = m / 255d;
287+
double yD = y / 255d;
288+
double kD = k / 255d;
289+
r = (byte)(255 * (1 - cD) * (1 - kD));
290+
g = (byte)(255 * (1 - mD) * (1 - kD));
291+
b = (byte)(255 * (1 - yD) * (1 - kD));
292+
}
293+
286294
private static bool TryGetGray8Bitmap(int width, int height, ReadOnlySpan<byte> bytesPure, out SKImage? bitmap)
287295
{
288296
bitmap = null;

0 commit comments

Comments
 (0)