mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Function shading (first part)
This commit is contained in:
@ -23,6 +23,8 @@
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
#include <execution>
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
|
||||
@ -141,6 +143,43 @@ PDFPatternPtr PDFPattern::createShadingPattern(const PDFDictionary* colorSpaceDi
|
||||
const ShadingType shadingType = static_cast<ShadingType>(loader.readIntegerFromDictionary(shadingDictionary, "ShadingType", static_cast<PDFInteger>(ShadingType::Invalid)));
|
||||
switch (shadingType)
|
||||
{
|
||||
case ShadingType::Function:
|
||||
{
|
||||
PDFFunctionShading* functionShading = new PDFFunctionShading();
|
||||
PDFPatternPtr result(functionShading);
|
||||
|
||||
std::vector<PDFReal> functionDomain = loader.readNumberArrayFromDictionary(shadingDictionary, "Domain", { 0.0, 1.0, 0.0, 1.0 });
|
||||
if (functionDomain.size() != 4)
|
||||
{
|
||||
throw PDFParserException(PDFTranslationContext::tr("Invalid function shading pattern domain. Expected 4 values, but %1 provided.").arg(functionDomain.size()));
|
||||
}
|
||||
if (functionDomain[1] < functionDomain[0] || functionDomain[3] < functionDomain[2])
|
||||
{
|
||||
throw PDFParserException(PDFTranslationContext::tr("Invalid function shading pattern domain. Invalid domain ranges."));
|
||||
}
|
||||
|
||||
QMatrix domainToTargetTransform = loader.readMatrixFromDictionary(shadingDictionary, "Matrix", QMatrix());
|
||||
|
||||
size_t colorComponentCount = colorSpace->getColorComponentCount();
|
||||
if (functions.size() > 1 && colorComponentCount != functions.size())
|
||||
{
|
||||
throw PDFParserException(PDFTranslationContext::tr("Invalid axial shading pattern color functions. Expected %1 functions, but %2 provided.").arg(int(colorComponentCount)).arg(int(functions.size())));
|
||||
}
|
||||
|
||||
// Load items for function shading
|
||||
functionShading->m_antiAlias = antialias;
|
||||
functionShading->m_backgroundColor = backgroundColor;
|
||||
functionShading->m_colorSpace = colorSpace;
|
||||
functionShading->m_boundingBox = boundingBox;
|
||||
functionShading->m_domain = QRectF(functionDomain[0], functionDomain[2], functionDomain[1] - functionDomain[0], functionDomain[3] - functionDomain[2]);
|
||||
functionShading->m_domainToTargetTransform = domainToTargetTransform;
|
||||
functionShading->m_functions = qMove(functions);
|
||||
functionShading->m_matrix = matrix;
|
||||
functionShading->m_patternGraphicState = patternGraphicState;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
case ShadingType::Axial:
|
||||
{
|
||||
PDFAxialShading* axialShading = new PDFAxialShading();
|
||||
@ -162,6 +201,12 @@ PDFPatternPtr PDFPattern::createShadingPattern(const PDFDictionary* colorSpaceDi
|
||||
throw PDFParserException(PDFTranslationContext::tr("Invalid axial shading pattern domain. Expected 2, but %1 provided.").arg(domain.size()));
|
||||
}
|
||||
|
||||
size_t colorComponentCount = colorSpace->getColorComponentCount();
|
||||
if (functions.size() > 1 && colorComponentCount != functions.size())
|
||||
{
|
||||
throw PDFParserException(PDFTranslationContext::tr("Invalid axial shading pattern color functions. Expected %1 functions, but %2 provided.").arg(int(colorComponentCount)).arg(int(functions.size())));
|
||||
}
|
||||
|
||||
// Load items for axial shading
|
||||
axialShading->m_antiAlias = antialias;
|
||||
axialShading->m_backgroundColor = backgroundColor;
|
||||
@ -190,6 +235,244 @@ PDFPatternPtr PDFPattern::createShadingPattern(const PDFDictionary* colorSpaceDi
|
||||
return PDFPatternPtr();
|
||||
}
|
||||
|
||||
ShadingType PDFFunctionShading::getShadingType() const
|
||||
{
|
||||
return ShadingType::Function;
|
||||
}
|
||||
|
||||
PDFMesh PDFFunctionShading::createMesh(const PDFMeshQualitySettings& settings) const
|
||||
{
|
||||
PDFMesh mesh;
|
||||
|
||||
QMatrix domainToDeviceSpaceMatrix = m_domainToTargetTransform * settings.userSpaceToDeviceSpaceMatrix;
|
||||
QLineF topLine(m_domain.topLeft(), m_domain.topRight());
|
||||
QLineF leftLine(m_domain.topLeft(), m_domain.bottomLeft());
|
||||
|
||||
Q_ASSERT(domainToDeviceSpaceMatrix.isInvertible());
|
||||
QMatrix deviceSpaceToDomainMatrix = domainToDeviceSpaceMatrix.inverted();
|
||||
|
||||
QLineF topLineDS = domainToDeviceSpaceMatrix.map(topLine);
|
||||
QLineF leftLineDS = domainToDeviceSpaceMatrix.map(leftLine);
|
||||
|
||||
const size_t colorComponents = m_colorSpace->getColorComponentCount();
|
||||
const PDFReal resolution = settings.preferredMeshResolution;
|
||||
|
||||
const PDFReal xSteps = qMax(std::floor(topLineDS.length() / resolution), 2.0);
|
||||
const PDFReal ySteps = qMax(std::floor(leftLineDS.length() / resolution), 2.0);
|
||||
const PDFReal xStep = 1.0 / xSteps;
|
||||
const PDFReal yStep = 1.0 / ySteps;
|
||||
|
||||
// Prepare x/y ordinates array for given resolution
|
||||
std::vector<PDFReal> xOrdinates;
|
||||
std::vector<PDFReal> yOrdinates;
|
||||
xOrdinates.reserve(xSteps + 1);
|
||||
yOrdinates.reserve(ySteps + 1);
|
||||
|
||||
for (PDFReal x = 0.0; x <= 1.0; x += xStep)
|
||||
{
|
||||
xOrdinates.push_back(x);
|
||||
}
|
||||
if (xOrdinates.back() + PDF_EPSILON >= 1.0)
|
||||
{
|
||||
xOrdinates.pop_back();
|
||||
}
|
||||
xOrdinates.push_back(1.0);
|
||||
|
||||
for (PDFReal y = 0.0; y <= 1.0; y += yStep)
|
||||
{
|
||||
yOrdinates.push_back(y);
|
||||
}
|
||||
if (yOrdinates.back() + PDF_EPSILON >= 1.0)
|
||||
{
|
||||
yOrdinates.pop_back();
|
||||
}
|
||||
yOrdinates.push_back(1.0);
|
||||
|
||||
// We have determined x/y ordinates. Now we must create result array with colors,
|
||||
// which for each x/y ordinate tells us, what color in the given position is.
|
||||
const size_t rowCount = yOrdinates.size();
|
||||
const size_t columnCount = xOrdinates.size();
|
||||
const size_t nodesCount = rowCount * columnCount;
|
||||
const size_t stride = columnCount * colorComponents;
|
||||
|
||||
std::vector<size_t> indices;
|
||||
indices.resize(nodesCount, 0);
|
||||
std::iota(indices.begin(), indices.end(), 0);
|
||||
|
||||
auto indexToRowColumn = [columnCount](size_t index) -> std::pair<size_t, size_t>
|
||||
{
|
||||
return std::make_pair(index / columnCount, index % columnCount);
|
||||
};
|
||||
|
||||
auto rowColumnToIndex = [columnCount](size_t row, size_t column) -> size_t
|
||||
{
|
||||
return row * columnCount + column;
|
||||
};
|
||||
|
||||
auto rowColumnToFirstColorComponent = [stride, colorComponents](size_t row, size_t column) -> size_t
|
||||
{
|
||||
return row * stride + column * colorComponents;
|
||||
};
|
||||
|
||||
const bool isSingleFunction = m_functions.size() == 1;
|
||||
std::vector<PDFReal> sourceColorBuffer;
|
||||
sourceColorBuffer.resize(indices.size() * colorComponents, 0.0);
|
||||
|
||||
std::vector<QPointF> gridPoints;
|
||||
gridPoints.resize(nodesCount);
|
||||
|
||||
QMutex functionErrorMutex;
|
||||
PDFFunction::FunctionResult functionError(true);
|
||||
|
||||
auto setColor = [&](size_t index)
|
||||
{
|
||||
auto [row, column] = indexToRowColumn(index);
|
||||
QPointF nodeDS = topLineDS.pointAt(xOrdinates[column]) + leftLineDS.pointAt(yOrdinates[row]) - topLineDS.p1();
|
||||
QPointF node = deviceSpaceToDomainMatrix.map(nodeDS);
|
||||
const size_t colorComponentIndex = rowColumnToFirstColorComponent(row, column);
|
||||
Q_ASSERT(colorComponentIndex <= sourceColorBuffer.size());
|
||||
|
||||
gridPoints[index] = nodeDS;
|
||||
|
||||
PDFReal* sourceColorBegin = sourceColorBuffer.data() + colorComponentIndex;
|
||||
PDFReal* sourceColorEnd = sourceColorBegin + colorComponents;
|
||||
|
||||
std::array<PDFReal, 2> uv = { node.x(), node.y() };
|
||||
|
||||
if (isSingleFunction)
|
||||
{
|
||||
PDFFunction::FunctionResult result = m_functions.front()->apply(uv.data(), uv.data() + uv.size(), sourceColorBegin, sourceColorEnd);
|
||||
if (!result)
|
||||
{
|
||||
QMutexLocker lock(&functionErrorMutex);
|
||||
if (!functionError)
|
||||
{
|
||||
functionError = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0, count = colorComponents; i < count; ++i)
|
||||
{
|
||||
PDFFunction::FunctionResult result = m_functions[i]->apply(uv.data(), uv.data() + uv.size(), sourceColorBegin + i, sourceColorBegin + i + 1);
|
||||
if (!result)
|
||||
{
|
||||
QMutexLocker lock(&functionErrorMutex);
|
||||
if (!functionError)
|
||||
{
|
||||
functionError = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::for_each(std::execution::parallel_policy(), indices.cbegin(), indices.cend(), setColor);
|
||||
|
||||
if (!functionError)
|
||||
{
|
||||
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Error occured during mesh generation of shading: %1").arg(functionError.errorMessage));
|
||||
}
|
||||
|
||||
std::vector<QRgb> colors;
|
||||
colors.resize(rowCount * columnCount, QRgb());
|
||||
|
||||
mesh.setVertices(qMove(gridPoints));
|
||||
std::vector<PDFMesh::Triangle> triangles;
|
||||
triangles.resize((rowCount - 1) * (columnCount - 1) * 2);
|
||||
|
||||
auto generateTriangle = [&](size_t index)
|
||||
{
|
||||
auto [row, column] = indexToRowColumn(index);
|
||||
if (row == 0 || column == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Q_ASSERT(index == rowColumnToIndex(row, column));
|
||||
|
||||
const size_t triangleIndex1 = ((row - 1) * (columnCount - 1) + column - 1) * 2;
|
||||
const size_t triangleIndex2 = triangleIndex1 + 1;
|
||||
const size_t v1 = rowColumnToIndex(row - 1, column - 1);
|
||||
const size_t v2 = rowColumnToIndex(row - 1, column);
|
||||
const size_t v3 = index;
|
||||
const size_t v4 = rowColumnToIndex(row, column - 1);
|
||||
std::vector<PDFReal> colorBuffer;
|
||||
colorBuffer.resize(colorComponents, 0.0);
|
||||
|
||||
auto calculateColor = [&](const PDFMesh::Triangle& triangle)
|
||||
{
|
||||
QPointF centerDS = mesh.getTriangleCenter(triangle);
|
||||
QPointF center = deviceSpaceToDomainMatrix.map(centerDS);
|
||||
|
||||
std::array<PDFReal, 2> uv = { center.x(), center.y() };
|
||||
|
||||
if (isSingleFunction)
|
||||
{
|
||||
PDFFunction::FunctionResult result = m_functions.front()->apply(uv.data(), uv.data() + uv.size(), colorBuffer.data(), colorBuffer.data() + colorBuffer.size());
|
||||
if (!result)
|
||||
{
|
||||
QMutexLocker lock(&functionErrorMutex);
|
||||
if (!functionError)
|
||||
{
|
||||
functionError = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0, count = colorComponents; i < count; ++i)
|
||||
{
|
||||
PDFFunction::FunctionResult result = m_functions[i]->apply(uv.data(), uv.data() + uv.size(), colorBuffer.data() + i, colorBuffer.data() + i + 1);
|
||||
if (!result)
|
||||
{
|
||||
QMutexLocker lock(&functionErrorMutex);
|
||||
if (!functionError)
|
||||
{
|
||||
functionError = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m_colorSpace->getColor(PDFAbstractColorSpace::convertToColor(colorBuffer));
|
||||
};
|
||||
|
||||
PDFMesh::Triangle triangle1;
|
||||
triangle1.v1 = static_cast<uint32_t>(v1);
|
||||
triangle1.v2 = static_cast<uint32_t>(v2);
|
||||
triangle1.v3 = static_cast<uint32_t>(v3);
|
||||
triangle1.color = calculateColor(triangle1).rgb();
|
||||
|
||||
PDFMesh::Triangle triangle2;
|
||||
triangle2.v1 = static_cast<uint32_t>(v3);
|
||||
triangle2.v2 = static_cast<uint32_t>(v4);
|
||||
triangle2.v3 = static_cast<uint32_t>(v1);
|
||||
triangle2.color = calculateColor(triangle2).rgb();
|
||||
|
||||
triangles[triangleIndex1] = triangle1;
|
||||
triangles[triangleIndex2] = triangle2;
|
||||
};
|
||||
std::for_each(std::execution::parallel_policy(), indices.cbegin(), indices.cend(), generateTriangle);
|
||||
mesh.setTriangles(qMove(triangles));
|
||||
|
||||
if (!functionError)
|
||||
{
|
||||
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Error occured during mesh generation of shading: %1").arg(functionError.errorMessage));
|
||||
}
|
||||
|
||||
// Create bounding path
|
||||
if (m_boundingBox.isValid())
|
||||
{
|
||||
QPainterPath boundingPath;
|
||||
boundingPath.addPolygon(settings.userSpaceToDeviceSpaceMatrix.map(m_boundingBox));
|
||||
mesh.setBoundingPath(boundingPath);
|
||||
}
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
PDFMesh PDFAxialShading::createMesh(const PDFMeshQualitySettings& settings) const
|
||||
{
|
||||
PDFMesh mesh;
|
||||
@ -271,7 +554,7 @@ PDFMesh PDFAxialShading::createMesh(const PDFMeshQualitySettings& settings) cons
|
||||
{
|
||||
for (size_t i = 0, count = colorBuffer.size(); i < count; ++i)
|
||||
{
|
||||
PDFFunction::FunctionResult result = m_functions.front()->apply(&t, &t + 1, colorBuffer.data() + i, colorBuffer.data() + i + 1);
|
||||
PDFFunction::FunctionResult result = m_functions[i]->apply(&t, &t + 1, colorBuffer.data() + i, colorBuffer.data() + i + 1);
|
||||
if (!result)
|
||||
{
|
||||
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Error occured during mesh creation of shading: %1").arg(result.errorMessage));
|
||||
@ -460,6 +743,11 @@ void PDFMesh::transform(const QMatrix& matrix)
|
||||
m_boundingPath = matrix.map(m_boundingPath);
|
||||
}
|
||||
|
||||
QPointF PDFMesh::getTriangleCenter(const PDFMesh::Triangle& triangle) const
|
||||
{
|
||||
return (m_vertices[triangle.v1] + m_vertices[triangle.v2] + m_vertices[triangle.v3]) / 3.0;
|
||||
}
|
||||
|
||||
void PDFMeshQualitySettings::initDefaultResolution()
|
||||
{
|
||||
// We will take 0.5% percent of device space meshing area as minimal resolution (it is ~1.5 mm for
|
||||
|
Reference in New Issue
Block a user