Krita Source Code Documentation
Loading...
Searching...
No Matches
KisDitherUtil.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE project
3 *
4 * SPDX-FileCopyrightText: 2019 Carl Olsson <carl.olsson@gmail.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KisDitherUtil.h"
10
14
16 : m_thresholdMode(ThresholdMode::Pattern), m_patternValueMode(PatternValueMode::Auto)
17 , m_noiseSeed(0), m_patternUseAlpha(false), m_spread(1.0)
18{
19}
20
22{
23 m_thresholdMode = thresholdMode;
24}
25
26void KisDitherUtil::setPattern(const QString &md5sum, const QString &patternName, const PatternValueMode valueMode, KisResourcesInterfaceSP resourcesInterface)
27{
28 m_patternValueMode = valueMode;
29
30 auto source = resourcesInterface->source<KoPattern>(ResourceType::Patterns);
31 m_pattern = source.bestMatch(md5sum, "", patternName);
32
34 // Automatically pick between lightness-based and alpha-based patterns by whichever has maximum range
35
36 // FIXME QT6: remove after we derecate Qt5 (and use float all the time)
37 using float_type = decltype(std::declval<QColor>().alphaF());
38
39 float_type lightnessMin = 1.0, lightnessMax = 0.0;
40 float_type alphaMin = 1.0, alphaMax = 0.0;
41 const QImage &image = m_pattern->pattern();
42 for (int y = 0; y < image.height(); ++y) {
43 for (int x = 0; x < image.width(); ++x) {
44 const QColor pixel = image.pixelColor(x, y);
45 lightnessMin = std::min(lightnessMin, pixel.lightnessF());
46 lightnessMax = std::max(lightnessMax, pixel.lightnessF());
47 alphaMin = std::min(alphaMin, pixel.alphaF());
48 alphaMax = std::max(alphaMax, pixel.alphaF());
49 }
50 }
51 m_patternUseAlpha = (alphaMax - alphaMin > lightnessMax - lightnessMin);
52 }
53 else {
55 }
56}
57
58void KisDitherUtil::setNoiseSeed(const quint64 &noiseSeed)
59{
60 m_noiseSeed = noiseSeed;
61}
62
63void KisDitherUtil::setSpread(const qreal &spread)
64{
65 m_spread = spread;
66}
67
68qreal KisDitherUtil::threshold(const QPoint &pos)
69{
70 qreal threshold;
72 const QImage &image = m_pattern->pattern();
73 const QColor color = image.pixelColor(pos.x() % image.width(), pos.y() % image.height());
74 threshold = (m_patternUseAlpha ? color.alphaF() : color.lightnessF());
75 }
78 threshold = random.doubleRandomAt(pos.x(), pos.y());
79 }
80 else threshold = 0.5;
81
82 return 0.5 - (m_spread / 2.0) + threshold * m_spread;
83}
84
85void KisDitherUtil::setConfiguration(const KisFilterConfiguration &config, const QString &prefix)
86{
87 setThresholdMode(ThresholdMode(config.getInt(prefix + "thresholdMode")));
88 setPattern(config.getString(prefix + "md5sum"), config.getString(prefix + "pattern"), PatternValueMode(config.getInt(prefix + "patternValueMode")), config.resourcesInterface());
89 setNoiseSeed(quint64(config.getInt(prefix + "noiseSeed")));
90 setSpread(config.getDouble(prefix + "spread"));
91}
KisMagneticGraph::vertex_descriptor source(typename KisMagneticGraph::edge_descriptor e, KisMagneticGraph g)
quint64 m_noiseSeed
void setPattern(const QString &md5sum, const QString &patternName, const PatternValueMode valueMode, KisResourcesInterfaceSP resourcesInterface)
qreal threshold(const QPoint &pos)
void setConfiguration(const KisFilterConfiguration &config, const QString &prefix="")
PatternValueMode m_patternValueMode
ThresholdMode m_thresholdMode
KoPatternSP m_pattern
void setNoiseSeed(const quint64 &noiseSeed)
void setSpread(const qreal &spread)
void setThresholdMode(const ThresholdMode thresholdMode)
Write API docs here.
Definition KoPattern.h:21
const QString Patterns
KisResourcesInterfaceSP resourcesInterface
QString getString(const QString &name, const QString &def=QString()) const
int getInt(const QString &name, int def=0) const
double getDouble(const QString &name, double def=0.0) const