From dc973e4dac2d2686d32548d6d4c4fb0d08b15cff Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 16 Jan 2017 16:59:54 +1100 Subject: [PATCH] Use scaled int for Ycck --- src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs index 79c40a6c84..68e031c5b1 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs @@ -958,14 +958,20 @@ namespace ImageSharp.Formats // CMY, and patch in the original K. The RGB to CMY inversion cancels // out the 'Adobe inversion' described in the applyBlack doc comment // above, so in practice, only the fourth channel (black) is inverted. - // TODO: We can speed this up further with Vector4 int ccb = cb - 128; int ccr = cr - 128; + // Speed up the algorithm by removing floating point calculation + // Scale by 1024, add .5F and truncate value. We use bit shifting to divide the result + int r0 = 1436 * ccr; // (1.402F * 1024) + .5F + int g0 = 352 * ccb; // (0.34414F * 1024) + .5F + int g1 = 731 * ccr; // (0.71414F * 1024) + .5F + int b0 = 1815 * ccb; // (1.772F * 1024) + .5F + // First convert from YCbCr to CMY - float cyan = (y + (1.402F * ccr)).Clamp(0, 255) / 255F; - float magenta = (y - (0.34414F * ccb) - (0.71414F * ccr)).Clamp(0, 255) / 255F; - float yellow = (y + (1.772F * ccb)).Clamp(0, 255) / 255F; + float cyan = (y + (r0 >> 10)).Clamp(0, 255) / 255F; + float magenta = (byte)(y - (g0 >> 10) - (g1 >> 10)).Clamp(0, 255) / 255F; + float yellow = (byte)(y + (b0 >> 10)).Clamp(0, 255) / 255F; // Get keyline float keyline = (255 - this.blackImage[xx, yy]) / 255F;