mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-01-31 09:35:05 +01:00
363 lines
12 KiB
C++
363 lines
12 KiB
C++
// Copyright (C) 2020 Jakub Melka
|
|
//
|
|
// This file is part of Pdf4Qt.
|
|
//
|
|
// Pdf4Qt is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Pdf4Qt is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#include "pdftransparencyrenderer.h"
|
|
#include "pdfdocument.h"
|
|
#include "pdfcms.h"
|
|
|
|
namespace pdf
|
|
{
|
|
|
|
PDFFloatBitmap::PDFFloatBitmap() :
|
|
m_width(0),
|
|
m_height(0),
|
|
m_pixelSize(0)
|
|
{
|
|
|
|
}
|
|
|
|
PDFFloatBitmap::PDFFloatBitmap(size_t width, size_t height, PDFPixelFormat format) :
|
|
m_format(format),
|
|
m_width(width),
|
|
m_height(height),
|
|
m_pixelSize(format.getChannelCount())
|
|
{
|
|
Q_ASSERT(format.isValid());
|
|
|
|
m_data.resize(format.calculateBitmapDataLength(width, height), static_cast<PDFColorComponent>(0.0f));
|
|
}
|
|
|
|
PDFColorBuffer PDFFloatBitmap::getPixel(size_t x, size_t y)
|
|
{
|
|
const size_t index = getPixelIndex(x, y);
|
|
return PDFColorBuffer(m_data.data() + index, m_pixelSize);
|
|
}
|
|
|
|
PDFColorBuffer PDFFloatBitmap::getPixels()
|
|
{
|
|
return PDFColorBuffer(m_data.data(), m_data.size());
|
|
}
|
|
|
|
const PDFColorComponent* PDFFloatBitmap::begin() const
|
|
{
|
|
return m_data.data();
|
|
}
|
|
|
|
const PDFColorComponent* PDFFloatBitmap::end() const
|
|
{
|
|
return m_data.data() + m_data.size();
|
|
}
|
|
|
|
PDFColorComponent* PDFFloatBitmap::begin()
|
|
{
|
|
return m_data.data();
|
|
}
|
|
|
|
PDFColorComponent* PDFFloatBitmap::end()
|
|
{
|
|
return m_data.data() + m_data.size();
|
|
}
|
|
|
|
void PDFFloatBitmap::makeTransparent()
|
|
{
|
|
if (m_format.hasShapeChannel())
|
|
{
|
|
fillChannel(m_format.getShapeChannelIndex(), 0.0f);
|
|
}
|
|
|
|
if (m_format.hasOpacityChannel())
|
|
{
|
|
fillChannel(m_format.getOpacityChannelIndex(), 0.0f);
|
|
}
|
|
}
|
|
|
|
void PDFFloatBitmap::makeOpaque()
|
|
{
|
|
if (m_format.hasShapeChannel())
|
|
{
|
|
fillChannel(m_format.getShapeChannelIndex(), 1.0f);
|
|
}
|
|
|
|
if (m_format.hasOpacityChannel())
|
|
{
|
|
fillChannel(m_format.getOpacityChannelIndex(), 1.0f);
|
|
}
|
|
}
|
|
|
|
size_t PDFFloatBitmap::getPixelIndex(size_t x, size_t y) const
|
|
{
|
|
return (y * m_width + x) * m_pixelSize;
|
|
}
|
|
|
|
PDFFloatBitmap PDFFloatBitmap::extractProcessColors()
|
|
{
|
|
PDFPixelFormat format = PDFPixelFormat::createFormat(m_format.getProcessColorChannelCount(), 0, false, m_format.hasProcessColorsSubtractive());
|
|
PDFFloatBitmap result(getWidth(), getHeight(), format);
|
|
|
|
for (size_t x = 0; x < getWidth(); ++x)
|
|
{
|
|
for (size_t y = 0; y < getHeight(); ++y)
|
|
{
|
|
PDFColorBuffer sourceProcessColorBuffer = getPixel(x, y);
|
|
PDFColorBuffer targetProcessColorBuffer = result.getPixel(x, y);
|
|
|
|
Q_ASSERT(sourceProcessColorBuffer.size() >= targetProcessColorBuffer.size());
|
|
std::copy(sourceProcessColorBuffer.cbegin(), std::next(sourceProcessColorBuffer.cbegin(), targetProcessColorBuffer.size()), targetProcessColorBuffer.begin());
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void PDFFloatBitmap::fillChannel(size_t channel, PDFColorComponent value)
|
|
{
|
|
// Do we have just one channel?
|
|
if (m_format.getChannelCount() == 1)
|
|
{
|
|
Q_ASSERT(channel == 0);
|
|
std::fill(m_data.begin(), m_data.end(), value);
|
|
return;
|
|
}
|
|
|
|
for (PDFColorComponent* pixel = begin(); pixel != end(); pixel += m_pixelSize)
|
|
{
|
|
pixel[channel] = value;
|
|
}
|
|
}
|
|
|
|
PDFFloatBitmapWithColorSpace::PDFFloatBitmapWithColorSpace()
|
|
{
|
|
|
|
}
|
|
|
|
PDFFloatBitmapWithColorSpace::PDFFloatBitmapWithColorSpace(size_t width, size_t height, PDFPixelFormat format, PDFColorSpacePointer blendColorSpace) :
|
|
PDFFloatBitmap(width, height, format),
|
|
m_colorSpace(blendColorSpace)
|
|
{
|
|
Q_ASSERT(!blendColorSpace || blendColorSpace->isBlendColorSpace());
|
|
}
|
|
|
|
PDFColorSpacePointer PDFFloatBitmapWithColorSpace::getColorSpace() const
|
|
{
|
|
return m_colorSpace;
|
|
}
|
|
|
|
void PDFFloatBitmapWithColorSpace::setColorSpace(const PDFColorSpacePointer& colorSpace)
|
|
{
|
|
m_colorSpace = colorSpace;
|
|
}
|
|
|
|
void PDFFloatBitmapWithColorSpace::convertToColorSpace(const PDFCMS* cms,
|
|
RenderingIntent intent,
|
|
const PDFColorSpacePointer& targetColorSpace,
|
|
PDFRenderErrorReporter* reporter)
|
|
{
|
|
Q_ASSERT(m_colorSpace);
|
|
if (m_colorSpace->equals(targetColorSpace.get()))
|
|
{
|
|
return;
|
|
}
|
|
|
|
const uint8_t targetDeviceColors = static_cast<uint8_t>(targetColorSpace->getColorComponentCount());
|
|
PDFPixelFormat newFormat = getPixelFormat();
|
|
newFormat.setProcessColors(targetDeviceColors);
|
|
newFormat.setProcessColorsSubtractive(targetDeviceColors == 4);
|
|
|
|
PDFFloatBitmap sourceProcessColors = extractProcessColors();
|
|
PDFFloatBitmap targetProcessColors(sourceProcessColors.getWidth(), sourceProcessColors.getHeight(), PDFPixelFormat::createFormat(targetDeviceColors, 0, false, newFormat.hasProcessColorsSubtractive()));
|
|
|
|
if (!PDFAbstractColorSpace::transform(m_colorSpace.data(), targetColorSpace.data(), cms, intent, sourceProcessColors.getPixels(), targetProcessColors.getPixels(), reporter))
|
|
{
|
|
reporter->reportRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Transformation between blending color spaces failed."));
|
|
}
|
|
|
|
PDFFloatBitmapWithColorSpace temporary(getWidth(), getHeight(), newFormat, targetColorSpace);
|
|
for (size_t x = 0; x < getWidth(); ++x)
|
|
{
|
|
for (size_t y = 0; y < getHeight(); ++y)
|
|
{
|
|
PDFColorBuffer sourceProcessColorBuffer = targetProcessColors.getPixel(x, y);
|
|
PDFColorBuffer sourceSpotColorAndOpacityBuffer = getPixel(x, y);
|
|
PDFColorBuffer targetBuffer = temporary.getPixel(x, y);
|
|
|
|
Q_ASSERT(sourceProcessColorBuffer.size() <= targetBuffer.size());
|
|
|
|
// Copy process colors
|
|
auto targetIt = targetBuffer.begin();
|
|
targetIt = std::copy(sourceProcessColorBuffer.cbegin(), sourceProcessColorBuffer.cend(), targetIt);
|
|
|
|
Q_ASSERT(std::distance(targetIt, targetBuffer.end()) == temporary.getPixelFormat().getSpotColorChannelCount() + temporary.getPixelFormat().getAuxiliaryChannelCount());
|
|
|
|
auto sourceIt = std::next(sourceSpotColorAndOpacityBuffer.cbegin(), temporary.getPixelFormat().getProcessColorChannelCount());
|
|
targetIt = std::copy(sourceIt, sourceSpotColorAndOpacityBuffer.cend(), targetIt);
|
|
|
|
Q_ASSERT(targetIt == targetBuffer.cend());
|
|
}
|
|
}
|
|
|
|
*this = qMove(temporary);
|
|
}
|
|
|
|
PDFTransparencyRenderer::PDFTransparencyRenderer(const PDFPage* page,
|
|
const PDFDocument* document,
|
|
const PDFFontCache* fontCache,
|
|
const PDFCMS* cms,
|
|
const PDFOptionalContentActivity* optionalContentActivity,
|
|
QMatrix pagePointToDevicePointMatrix) :
|
|
BaseClass(page, document, fontCache, cms, optionalContentActivity, pagePointToDevicePointMatrix, PDFMeshQualitySettings()),
|
|
m_active(false)
|
|
{
|
|
|
|
}
|
|
|
|
void PDFTransparencyRenderer::setDeviceColorSpace(PDFColorSpacePointer colorSpace)
|
|
{
|
|
if (!colorSpace || colorSpace->isBlendColorSpace())
|
|
{
|
|
// Set device color space only, when it is a blend color space
|
|
m_deviceColorSpace = colorSpace;
|
|
}
|
|
}
|
|
|
|
void PDFTransparencyRenderer::beginPaint()
|
|
{
|
|
Q_ASSERT(!m_active);
|
|
m_active = true;
|
|
|
|
// Create page transparency group
|
|
PDFObject pageTransparencyGroupObject = getPage()->getTransparencyGroup(&getDocument()->getStorage());
|
|
PDFTransparencyGroup transparencyGroup = parseTransparencyGroup(pageTransparencyGroupObject);
|
|
transparencyGroup.isolated = true;
|
|
|
|
m_pageTransparencyGroupGuard.reset(new PDFTransparencyGroupGuard(this, qMove(transparencyGroup)));
|
|
}
|
|
|
|
const PDFFloatBitmap& PDFTransparencyRenderer::endPaint()
|
|
{
|
|
Q_ASSERT(m_active);
|
|
m_pageTransparencyGroupGuard.reset();
|
|
m_active = false;
|
|
|
|
return *getImmediateBackdrop();
|
|
}
|
|
|
|
void PDFTransparencyRenderer::performPathPainting(const QPainterPath& path, bool stroke, bool fill, bool text, Qt::FillRule fillRule)
|
|
{
|
|
}
|
|
|
|
void PDFTransparencyRenderer::performClipping(const QPainterPath& path, Qt::FillRule fillRule)
|
|
{
|
|
}
|
|
|
|
void PDFTransparencyRenderer::performUpdateGraphicsState(const PDFPageContentProcessorState& state)
|
|
{
|
|
}
|
|
|
|
void PDFTransparencyRenderer::performSaveGraphicState(ProcessOrder order)
|
|
{
|
|
}
|
|
|
|
void PDFTransparencyRenderer::performRestoreGraphicState(ProcessOrder order)
|
|
{
|
|
}
|
|
|
|
void PDFTransparencyRenderer::performBeginTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup)
|
|
{
|
|
if (order == ProcessOrder::BeforeOperation)
|
|
{
|
|
PDFTransparencyGroupPainterData data;
|
|
data.group = transparencyGroup;
|
|
data.alphaIsShape = getGraphicState()->getAlphaIsShape();
|
|
data.alphaFill = getGraphicState()->getAlphaFilling();
|
|
data.alphaStroke = getGraphicState()->getAlphaStroking();
|
|
data.blendMode = getGraphicState()->getBlendMode();
|
|
data.blackPointCompensationMode = getGraphicState()->getBlackPointCompensationMode();
|
|
data.renderingIntent = getGraphicState()->getRenderingIntent();
|
|
data.blendColorSpace = transparencyGroup.colorSpacePointer;
|
|
|
|
if (!data.blendColorSpace)
|
|
{
|
|
data.blendColorSpace = getBlendColorSpace();
|
|
}
|
|
|
|
// Create initial backdrop, according to 11.4.8 of PDF 2.0 specification.
|
|
// If group is knockout, use initial backdrop.
|
|
PDFFloatBitmapWithColorSpace* oldBackdrop = getBackdrop();
|
|
data.initialBackdrop = *getBackdrop();
|
|
|
|
if (isTransparencyGroupIsolated())
|
|
{
|
|
// Make initial backdrop transparent
|
|
data.initialBackdrop.makeTransparent();
|
|
}
|
|
|
|
// Prepare soft mask
|
|
data.softMask = PDFFloatBitmap(oldBackdrop->getWidth(), oldBackdrop->getHeight(), PDFPixelFormat::createOpacityMask());
|
|
// TODO: Create soft mask
|
|
data.softMask.makeOpaque();
|
|
|
|
data.initialBackdrop.convertToColorSpace(getCMS(), data.renderingIntent, data.blendColorSpace, this);
|
|
data.immediateBackdrop = data.initialBackdrop;
|
|
|
|
m_transparencyGroupDataStack.emplace_back(qMove(data));
|
|
}
|
|
}
|
|
|
|
void PDFTransparencyRenderer::performEndTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup)
|
|
{
|
|
|
|
}
|
|
|
|
PDFFloatBitmapWithColorSpace* PDFTransparencyRenderer::getInitialBackdrop()
|
|
{
|
|
return &m_transparencyGroupDataStack.back().initialBackdrop;
|
|
}
|
|
|
|
PDFFloatBitmapWithColorSpace* PDFTransparencyRenderer::getImmediateBackdrop()
|
|
{
|
|
return &m_transparencyGroupDataStack.back().immediateBackdrop;
|
|
}
|
|
|
|
PDFFloatBitmapWithColorSpace* PDFTransparencyRenderer::getBackdrop()
|
|
{
|
|
if (isTransparencyGroupKnockout())
|
|
{
|
|
return getInitialBackdrop();
|
|
}
|
|
else
|
|
{
|
|
return getImmediateBackdrop();
|
|
}
|
|
}
|
|
|
|
const PDFColorSpacePointer& PDFTransparencyRenderer::getBlendColorSpace() const
|
|
{
|
|
return m_transparencyGroupDataStack.back().blendColorSpace;
|
|
}
|
|
|
|
bool PDFTransparencyRenderer::isTransparencyGroupIsolated() const
|
|
{
|
|
return m_transparencyGroupDataStack.back().group.isolated;
|
|
}
|
|
|
|
bool PDFTransparencyRenderer::isTransparencyGroupKnockout() const
|
|
{
|
|
return m_transparencyGroupDataStack.back().group.knockout;
|
|
}
|
|
|
|
} // namespace pdf
|