Advanced blending

This commit is contained in:
Jakub Melka 2021-01-23 19:03:12 +01:00
parent ea62e6ee32
commit 443c656a46
6 changed files with 442 additions and 0 deletions

View File

@ -80,6 +80,52 @@ bool PDFBlendModeInfo::isSupportedByQt(BlendMode mode)
}
}
bool PDFBlendModeInfo::isSeparable(BlendMode mode)
{
switch (mode)
{
case BlendMode::Normal:
case BlendMode::Multiply:
case BlendMode::Screen:
case BlendMode::Overlay:
case BlendMode::Darken:
case BlendMode::Lighten:
case BlendMode::ColorDodge:
case BlendMode::ColorBurn:
case BlendMode::HardLight:
case BlendMode::SoftLight:
case BlendMode::Difference:
case BlendMode::Exclusion:
case BlendMode::Compatible:
return true;
case BlendMode::Hue:
case BlendMode::Saturation:
case BlendMode::Color:
case BlendMode::Luminosity:
return false;
default:
Q_ASSERT(false);
return false;
}
}
bool PDFBlendModeInfo::isWhitePreserving(BlendMode mode)
{
if (!isSeparable(mode))
{
return false;
}
if (mode == BlendMode::Difference || mode == BlendMode::Exclusion)
{
return false;
}
return true;
}
QPainter::CompositionMode PDFBlendModeInfo::getCompositionModeFromBlendMode(BlendMode mode)
{
switch (mode)
@ -363,6 +409,78 @@ PDFCMYK PDFBlendFunction::blend_Luminosity(PDFCMYK Cb, PDFCMYK Cs)
return nonseparable_rgb2cmyk(blend_Luminosity(nonseparable_cmyk2rgb(Cb), nonseparable_cmyk2rgb(Cs)), Cs[3]);
}
PDFGray PDFBlendFunction::blend_Nonseparable(BlendMode mode, PDFGray Cb, PDFGray Cs)
{
switch (mode)
{
case BlendMode::Hue:
return blend_Hue(Cb, Cs);
case BlendMode::Saturation:
return blend_Saturation(Cb, Cs);
case BlendMode::Color:
return blend_Color(Cb, Cs);
case BlendMode::Luminosity:
return blend_Luminosity(Cb, Cs);
default:
Q_ASSERT(false);
break;
}
return Cs;
}
PDFRGB PDFBlendFunction::blend_Nonseparable(BlendMode mode, PDFRGB Cb, PDFRGB Cs)
{
switch (mode)
{
case BlendMode::Hue:
return blend_Hue(Cb, Cs);
case BlendMode::Saturation:
return blend_Saturation(Cb, Cs);
case BlendMode::Color:
return blend_Color(Cb, Cs);
case BlendMode::Luminosity:
return blend_Luminosity(Cb, Cs);
default:
Q_ASSERT(false);
break;
}
return Cs;
}
PDFCMYK PDFBlendFunction::blend_Nonseparable(BlendMode mode, PDFCMYK Cb, PDFCMYK Cs)
{
switch (mode)
{
case BlendMode::Hue:
return blend_Hue(Cb, Cs);
case BlendMode::Saturation:
return blend_Saturation(Cb, Cs);
case BlendMode::Color:
return blend_Color(Cb, Cs);
case BlendMode::Luminosity:
return blend_Luminosity(Cb, Cs);
default:
Q_ASSERT(false);
break;
}
return Cs;
}
PDFRGB PDFBlendFunction::nonseparable_gray2rgb(PDFGray gray)
{
return nonseparable_SetLum(PDFRGB{ 0.0f, 0.0f, 0.0f }, gray);

View File

@ -69,6 +69,12 @@ public:
/// \param mode Blend mode
static bool isSupportedByQt(BlendMode mode);
/// Returns true, if blend mode is separable
static bool isSeparable(BlendMode mode);
/// Returns true, if blend mode is white-preserving (i.e. B(1.0, 1.0) == 1.0)
static bool isWhitePreserving(BlendMode mode);
/// Returns composition mode for Qt drawing subsystem from blend mode defined
/// in PDF standard. If blend mode is not supported by Qt drawing subsystem, then default
/// composition mode is returned.
@ -160,6 +166,27 @@ public:
/// \param Cs Source color
static PDFCMYK blend_Luminosity(PDFCMYK Cb, PDFCMYK Cs);
/// Blend non-separabe. It is incorrect to call this function
/// with blend mode, which is separable.
/// \param mode Non-separable blend mode
/// \param Cb Backdrop color
/// \param Cs Source color
static PDFGray blend_Nonseparable(BlendMode mode, PDFGray Cb, PDFGray Cs);
/// Blend non-separabe. It is incorrect to call this function
/// with blend mode, which is separable.
/// \param mode Non-separable blend mode
/// \param Cb Backdrop color
/// \param Cs Source color
static PDFRGB blend_Nonseparable(BlendMode mode, PDFRGB Cb, PDFRGB Cs);
/// Blend non-separabe. It is incorrect to call this function
/// with blend mode, which is separable.
/// \param mode Non-separable blend mode
/// \param Cb Backdrop color
/// \param Cs Source color
static PDFCMYK blend_Nonseparable(BlendMode mode, PDFCMYK Cb, PDFCMYK Cs);
/// Union function
static constexpr PDFColorComponent blend_Union(PDFColorComponent b, PDFColorComponent s) { return b + s - b * s; }

View File

@ -1138,6 +1138,7 @@ bool PDFCMSGeneric::fillRGBBufferFromICC(const std::vector<float>& colors, Rende
bool PDFCMSGeneric::transformColorSpace(const PDFCMS::ColorSpaceTransformParams& params) const
{
Q_UNUSED(params);
return false;
}

View File

@ -123,6 +123,219 @@ PDFFloatBitmap PDFFloatBitmap::extractProcessColors()
return result;
}
void PDFFloatBitmap::blend(const PDFFloatBitmap& source,
PDFFloatBitmap& target,
const PDFFloatBitmap& backdrop,
const PDFFloatBitmap& initialBackdrop,
PDFFloatBitmap& softMask,
bool alphaIsShape,
PDFColorComponent constantAlpha,
BlendMode mode)
{
Q_ASSERT(source.getWidth() == target.getWidth());
Q_ASSERT(source.getHeight() == target.getHeight());
Q_ASSERT(source.getPixelFormat() == target.getPixelFormat());
Q_ASSERT(source.getWidth() == softMask.getWidth());
Q_ASSERT(source.getHeight() == softMask.getHeight());
Q_ASSERT(softMask.getPixelFormat() == PDFPixelFormat::createOpacityMask());
const size_t width = source.getWidth();
const size_t height = source.getHeight();
const PDFPixelFormat pixelFormat = source.getPixelFormat();
const uint8_t shapeChannel = pixelFormat.getShapeChannelIndex();
const uint8_t opacityChannel = pixelFormat.getOpacityChannelIndex();
const uint8_t colorChannelStart = pixelFormat.getColorChannelIndexStart();
const uint8_t colorChannelEnd = pixelFormat.getColorChannelIndexEnd();
std::vector<PDFColorComponent> B_i(source.getPixelSize(), 0.0f);
for (size_t x = 0; x < width; ++x)
{
for (size_t y = 0; y < height; ++y)
{
PDFColorBuffer sourceColor = source.getPixel(x, y);
PDFColorBuffer targetColor = target.getPixel(x, y);
PDFColorBuffer backdropColor = backdrop.getPixel(x, y);
PDFColorBuffer initialBackdropColor = initialBackdrop.getPixel(x, y);
PDFColorBuffer alphaColorBuffer = softMask.getPixel(x, y);
const PDFColorComponent softMaskValue = alphaColorBuffer[0];
const PDFColorComponent f_j_i = sourceColor[shapeChannel];
const PDFColorComponent f_m_i = alphaIsShape ? softMaskValue : 1.0f;
const PDFColorComponent f_k_i = alphaIsShape ? constantAlpha : 1.0f;
const PDFColorComponent q_m_i = !alphaIsShape ? softMaskValue : 1.0f;
const PDFColorComponent q_k_i = !alphaIsShape ? constantAlpha : 1.0f;
const PDFColorComponent f_s_i = f_j_i * f_m_i * f_k_i;
const PDFColorComponent alpha_j_i = sourceColor[opacityChannel];
const PDFColorComponent alpha_s_i = alpha_j_i * (f_m_i * q_m_i) * (f_k_i * q_k_i);
const PDFColorComponent alpha_g_b = backdropColor[opacityChannel];
const PDFColorComponent alpha_0 = initialBackdropColor[opacityChannel];
const PDFColorComponent f_g_i_1 = targetColor[shapeChannel];
const PDFColorComponent alpha_g_i_1 = targetColor[opacityChannel];
const PDFColorComponent alpha_b = backdropColor[opacityChannel];
const PDFColorComponent f_g_i = PDFBlendFunction::blend_Union(f_g_i_1, f_s_i);
const PDFColorComponent alpha_g_i = (1.0f - f_s_i) * alpha_g_i_1 + (f_s_i - alpha_s_i) * alpha_g_b + alpha_s_i;
const PDFColorComponent alpha_i_1 = PDFBlendFunction::blend_Union(alpha_0, alpha_g_i_1);
const PDFColorComponent alpha_i = PDFBlendFunction::blend_Union(alpha_0, alpha_g_i);
if (qFuzzyIsNull(alpha_g_i))
{
// If alpha_i is zero, then color is undefined
continue;
}
std::fill(B_i.begin(), B_i.end(), 0.0f);
// Calculate blended pixel
if (PDFBlendModeInfo::isSeparable(mode))
{
// Separable blend mode - process each color separately
const bool isProcessColorSubtractive = pixelFormat.hasProcessColorsSubtractive();
const bool isSpotColorSubtractive = pixelFormat.hasSpotColorsSubtractive();
if (pixelFormat.hasProcessColors())
{
if (!isProcessColorSubtractive)
{
for (uint8_t i = pixelFormat.getProcessColorChannelIndexStart(); i < pixelFormat.getProcessColorChannelIndexEnd(); ++i)
{
B_i[i] = PDFBlendFunction::blend(mode, backdropColor[i], sourceColor[i]);
}
}
else
{
for (uint8_t i = pixelFormat.getProcessColorChannelIndexStart(); i < pixelFormat.getProcessColorChannelIndexEnd(); ++i)
{
B_i[i] = 1.0f - PDFBlendFunction::blend(mode, 1.0f - backdropColor[i], 1.0f - sourceColor[i]);
}
}
}
if (pixelFormat.hasSpotColors())
{
// Blend mode for spot colors must be white-preserving,
// see 11.7.4.2 of PDF 2.0 specification
BlendMode spotBlendMode = mode;
if (!PDFBlendModeInfo::isWhitePreserving(mode))
{
spotBlendMode = BlendMode::Normal;
}
if (!isSpotColorSubtractive)
{
for (uint8_t i = pixelFormat.getSpotColorChannelIndexStart(); i < pixelFormat.getSpotColorChannelIndexEnd(); ++i)
{
B_i[i] = PDFBlendFunction::blend(spotBlendMode, backdropColor[i], sourceColor[i]);
}
}
else
{
for (uint8_t i = pixelFormat.getSpotColorChannelIndexStart(); i < pixelFormat.getSpotColorChannelIndexEnd(); ++i)
{
B_i[i] = 1.0f - PDFBlendFunction::blend(spotBlendMode, 1.0f - backdropColor[i], 1.0f - sourceColor[i]);
}
}
}
}
else
{
// Nonseparable blend mode - process colors together
if (pixelFormat.hasProcessColors())
{
switch (pixelFormat.getProcessColorChannelCount())
{
case 1:
{
// Gray
const PDFGray Cb = backdropColor[pixelFormat.getProcessColorChannelIndexStart()];
const PDFGray Cs = sourceColor[pixelFormat.getProcessColorChannelIndexStart()];
const PDFGray blended = PDFBlendFunction::blend_Nonseparable(mode, Cb, Cs);
B_i[pixelFormat.getProcessColorChannelIndexStart()] = blended;
break;
}
case 3:
{
// RGB
const PDFRGB Cb = { backdropColor[pixelFormat.getProcessColorChannelIndexStart() + 0],
backdropColor[pixelFormat.getProcessColorChannelIndexStart() + 1],
backdropColor[pixelFormat.getProcessColorChannelIndexStart() + 2] };
const PDFRGB Cs = { sourceColor[pixelFormat.getProcessColorChannelIndexStart() + 0],
sourceColor[pixelFormat.getProcessColorChannelIndexStart() + 1],
sourceColor[pixelFormat.getProcessColorChannelIndexStart() + 2] };
const PDFRGB blended = PDFBlendFunction::blend_Nonseparable(mode, Cb, Cs);
B_i[pixelFormat.getProcessColorChannelIndexStart() + 0] = blended[0];
B_i[pixelFormat.getProcessColorChannelIndexStart() + 1] = blended[1];
B_i[pixelFormat.getProcessColorChannelIndexStart() + 2] = blended[2];
break;
}
case 4:
{
// CMYK
const PDFCMYK Cb = { backdropColor[pixelFormat.getProcessColorChannelIndexStart() + 0],
backdropColor[pixelFormat.getProcessColorChannelIndexStart() + 1],
backdropColor[pixelFormat.getProcessColorChannelIndexStart() + 2],
backdropColor[pixelFormat.getProcessColorChannelIndexStart() + 3] };
const PDFCMYK Cs = { sourceColor[pixelFormat.getProcessColorChannelIndexStart() + 0],
sourceColor[pixelFormat.getProcessColorChannelIndexStart() + 1],
sourceColor[pixelFormat.getProcessColorChannelIndexStart() + 2],
sourceColor[pixelFormat.getProcessColorChannelIndexStart() + 3] };
const PDFCMYK blended = PDFBlendFunction::blend_Nonseparable(mode, Cb, Cs);
B_i[pixelFormat.getProcessColorChannelIndexStart() + 0] = blended[0];
B_i[pixelFormat.getProcessColorChannelIndexStart() + 1] = blended[1];
B_i[pixelFormat.getProcessColorChannelIndexStart() + 2] = blended[2];
B_i[pixelFormat.getProcessColorChannelIndexStart() + 3] = blended[3];
break;
}
default:
{
// This is a serious error. Blended buffer remains unchanged (zero)
Q_ASSERT(false);
break;
}
}
}
if (pixelFormat.hasSpotColors())
{
const bool isSpotColorSubtractive = pixelFormat.hasSpotColorsSubtractive();
if (!isSpotColorSubtractive)
{
for (uint8_t i = pixelFormat.getSpotColorChannelIndexStart(); i < pixelFormat.getSpotColorChannelIndexEnd(); ++i)
{
B_i[i] = PDFBlendFunction::blend(BlendMode::Normal, backdropColor[i], sourceColor[i]);
}
}
else
{
for (uint8_t i = pixelFormat.getSpotColorChannelIndexStart(); i < pixelFormat.getSpotColorChannelIndexEnd(); ++i)
{
B_i[i] = 1.0f - PDFBlendFunction::blend(BlendMode::Normal, 1.0f - backdropColor[i], 1.0f - sourceColor[i]);
}
}
}
}
for (uint8_t i = colorChannelStart; i < colorChannelEnd; ++i)
{
const PDFColorComponent C_s_i = sourceColor[i];
const PDFColorComponent C_b = backdropColor[i];
const PDFColorComponent C_i_1 = targetColor[i];
PDFColorComponent C_t = (f_s_i - alpha_s_i) * alpha_b * C_b + alpha_s_i * ((1.0f - alpha_b) * C_s_i + alpha_b * B_i[i]);
PDFColorComponent C_i = ((1.0f - f_s_i) * alpha_i_1 * C_i_1 + C_t) / alpha_i;
targetColor[i] = C_i;
}
targetColor[shapeChannel] = f_g_i;
targetColor[opacityChannel] = alpha_g_i;
}
}
}
void PDFFloatBitmap::fillChannel(size_t channel, PDFColorComponent value)
{
// Do we have just one channel?
@ -319,7 +532,58 @@ void PDFTransparencyRenderer::performBeginTransparencyGroup(ProcessOrder order,
void PDFTransparencyRenderer::performEndTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup)
{
if (order == ProcessOrder::AfterOperation)
{
// "Unblend" the initial backdrop from immediate backdrop, according to 11.4.8
removeInitialBackdrop();
PDFTransparencyGroupPainterData sourceData = qMove(m_transparencyGroupDataStack.back());
m_transparencyGroupDataStack.pop_back();
PDFTransparencyGroupPainterData& targetData = m_transparencyGroupDataStack.back();
sourceData.immediateBackdrop.convertToColorSpace(getCMS(), targetData.renderingIntent, targetData.blendColorSpace, this);
PDFFloatBitmap::blend(sourceData, targetData, *getBackdrop(), *getInitialBackdrop(), sourceData.softMask, sourceData.alphaIsShape, sourceData.alphaFill, BlendMode::Normal);
}
}
void PDFTransparencyRenderer::removeInitialBackdrop()
{
PDFFloatBitmapWithColorSpace* immediateBackdrop = getImmediateBackdrop();
PDFFloatBitmapWithColorSpace* initialBackdrop = getInitialBackdrop();
PDFPixelFormat pixelFormat = immediateBackdrop->getPixelFormat();
const uint8_t alphaChannelIndex = pixelFormat.getOpacityChannelIndex();
const uint8_t colorChannelIndexStart = pixelFormat.getColorChannelIndexStart();
const uint8_t colorChannelIndexEnd= pixelFormat.getColorChannelIndexEnd();
Q_ASSERT(alphaChannelIndex != PDFPixelFormat::INVALID_CHANNEL_INDEX);
Q_ASSERT(colorChannelIndexStart != PDFPixelFormat::INVALID_CHANNEL_INDEX);
Q_ASSERT(colorChannelIndexEnd != PDFPixelFormat::INVALID_CHANNEL_INDEX);
for (size_t x = 0; x < immediateBackdrop->getWidth(); ++x)
{
for (size_t y = 0; y < immediateBackdrop->getHeight(); ++y)
{
PDFColorBuffer initialBackdropColorBuffer = initialBackdrop->getPixel(x, y);
PDFColorBuffer immediateBackdropColorBuffer = immediateBackdrop->getPixel(x, y);
const PDFColorComponent alpha_0 = initialBackdropColorBuffer[alphaChannelIndex];
const PDFColorComponent alpha_g_n = immediateBackdropColorBuffer[alphaChannelIndex];
if (!qFuzzyIsNull(alpha_g_n))
{
for (const uint8_t i = colorChannelIndexStart; i < colorChannelIndexEnd; ++i)
{
const PDFColorComponent C_0 = initialBackdropColorBuffer[i];
const PDFColorComponent C_n = immediateBackdropColorBuffer[i];
const PDFColorComponent C = C_n + (C_n - C_0) * alpha_0 * (1.0f / alpha_g_n - 1.0f);
const PDFColorComponent C_clipped = qBound(0.0f, C, 1.0f);
immediateBackdropColorBuffer[i] = C_clipped;
}
}
}
}
}
PDFFloatBitmapWithColorSpace* PDFTransparencyRenderer::getInitialBackdrop()

View File

@ -39,6 +39,9 @@ public:
constexpr static uint8_t INVALID_CHANNEL_INDEX = 0xFF;
constexpr bool operator==(const PDFPixelFormat&) const = default;
constexpr bool operator!=(const PDFPixelFormat&) const = default;
constexpr bool hasProcessColors() const { return m_processColors > 0; }
constexpr bool hasSpotColors() const { return m_spotColors > 0; }
constexpr bool hasShapeChannel() const { return m_flags & FLAG_HAS_SHAPE_CHANNEL; }
@ -60,6 +63,8 @@ public:
constexpr uint8_t getProcessColorChannelIndexEnd() const { return hasProcessColors() ? getProcessColorChannelCount() : INVALID_CHANNEL_INDEX; }
constexpr uint8_t getSpotColorChannelIndexStart() const { return hasSpotColors() ? getProcessColorChannelCount() : INVALID_CHANNEL_INDEX; }
constexpr uint8_t getSpotColorChannelIndexEnd() const { return hasSpotColors() ? getSpotColorChannelIndexStart() + getSpotColorChannelCount() : INVALID_CHANNEL_INDEX; }
constexpr uint8_t getColorChannelIndexStart() const { return (hasProcessColors() || hasSpotColors()) ? 0 : INVALID_CHANNEL_INDEX; }
constexpr uint8_t getColorChannelIndexEnd() const { return (hasProcessColors() || hasSpotColors()) ? (m_processColors + m_spotColors) : INVALID_CHANNEL_INDEX; }
constexpr uint8_t getShapeChannelIndex() const { return hasShapeChannel() ? getProcessColorChannelCount() + getSpotColorChannelCount() : INVALID_CHANNEL_INDEX; }
constexpr uint8_t getOpacityChannelIndex() const { return hasShapeChannel() ? getProcessColorChannelCount() + getSpotColorChannelCount() + getShapeChannelCount() : INVALID_CHANNEL_INDEX; }
@ -159,6 +164,26 @@ public:
/// Extract process colors into another bitmap
PDFFloatBitmap extractProcessColors();
/// Performs bitmap blending, pixel format of source and target must be the same.
/// Blending algorithm uses the one from chapter 11.4.8 in the PDF 2.0 specification.
/// Bitmap size must be equal for all three bitmaps (source, target and soft mask).
/// \param source Source bitmap
/// \param target Target bitmap
/// \param backdrop Backdrop
/// \param initialBackdrop Initial backdrop
/// \param softMask Soft mask
/// \param alphaIsShape Both soft mask and constant alpha are shapes and not opacity?
/// \param constantAlpha Constant alpha, can mean shape or opacity
/// \param mode Blend mode
void blend(const PDFFloatBitmap& source,
PDFFloatBitmap& target,
const PDFFloatBitmap& backdrop,
const PDFFloatBitmap& initialBackdrop,
PDFFloatBitmap& softMask,
bool alphaIsShape,
PDFColorComponent constantAlpha,
BlendMode mode);
private:
void fillChannel(size_t channel, PDFColorComponent value);
@ -247,6 +272,8 @@ private:
PDFColorSpacePointer blendColorSpace;
};
void removeInitialBackdrop();
PDFFloatBitmapWithColorSpace* getInitialBackdrop();
PDFFloatBitmapWithColorSpace* getImmediateBackdrop();
PDFFloatBitmapWithColorSpace* getBackdrop();

View File

@ -509,6 +509,8 @@ public:
using value_ptr = value_type*;
using const_value_type = const value_type;
using const_value_ptr = const_value_type*;
using value_ref = value_type&;
using const_value_ref = const value_ref;
explicit inline PDFBuffer() :
m_begin(nullptr),
@ -533,6 +535,9 @@ public:
inline const_value_ptr cbegin() const { return m_begin; }
inline const_value_ptr cend() const { return m_end; }
inline value_ref operator[](size_t index) { return *(m_begin + index); }
inline const_value_ref operator[](size_t index) const { return *(m_begin + index); }
size_t size() const { return m_end - m_begin; }
private: