Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0cb1e86

Browse files
committed
Break up KisVisualColorSelectorShape::getImageMap()
- move rendering of the selector shape background into own function called renderBackground() - move conversion from KoColor data array to QImage to helper function called convertImageMap() This way it can be better specialized for optimizations.
1 parent 3cf5530 commit 0cb1e86

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

libs/widgets/KisVisualColorSelectorShape.cpp

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -174,45 +174,60 @@ bool KisVisualColorSelectorShape::imagesNeedUpdate() const {
174174
QImage KisVisualColorSelectorShape::getImageMap()
175175
{
176176
//qDebug() << this << ">>>>>>>>> getImageMap()" << m_d->imagesNeedUpdate;
177-
const KisVisualColorSelector *selector = qobject_cast<KisVisualColorSelector*>(parent());
178177

179-
if (m_d->imagesNeedUpdate == true) {
178+
if (m_d->imagesNeedUpdate) {
180179
// Fill a buffer with the right kocolors
181-
quint8 *data = new quint8[width() * height() * m_d->colorSpace->pixelSize()];
182-
quint8 *dataPtr = data;
183-
QVector4D coordinates = m_d->currentChannelValues;
184-
for (int y = 0; y < height(); y++) {
185-
for (int x=0; x < width(); x++) {
186-
QPointF newcoordinate = convertWidgetCoordinateToShapeCoordinate(QPoint(x, y));
187-
coordinates[m_d->channel1] = newcoordinate.x();
188-
if (m_d->dimension == Dimensions::twodimensional){
189-
coordinates[m_d->channel2] = newcoordinate.y();
190-
}
191-
KoColor c = selector->convertShapeCoordsToKoColor(coordinates);
192-
memcpy(dataPtr, c.data(), m_d->colorSpace->pixelSize());
193-
dataPtr += m_d->colorSpace->pixelSize();
194-
}
195-
}
196-
// Convert the buffer to a qimage
197-
if (m_d->displayRenderer) {
198-
m_d->gradient = m_d->displayRenderer->convertToQImage(m_d->colorSpace, data, width(), height());
199-
}
200-
else {
201-
m_d->gradient = m_d->colorSpace->convertToQImage(data, width(), height(), 0, KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::internalConversionFlags());
202-
}
203-
delete[] data;
204-
180+
m_d->gradient = renderBackground(m_d->currentChannelValues, m_d->colorSpace->pixelSize());
205181
m_d->imagesNeedUpdate = false;
206-
// safeguard:
207-
if (m_d->gradient.isNull())
208-
{
209-
m_d->gradient = QImage(width(), height(), QImage::Format_ARGB32);
210-
m_d->gradient.fill(Qt::black);
211-
}
212182
}
213183
return m_d->gradient;
214184
}
215185

186+
QImage KisVisualColorSelectorShape::convertImageMap(const quint8 *rawColor, quint32 size) const
187+
{
188+
Q_ASSERT(size == width()*height()*m_d->colorSpace->pixelSize());
189+
QImage image;
190+
// Convert the buffer to a qimage
191+
if (m_d->displayRenderer) {
192+
image = m_d->displayRenderer->convertToQImage(m_d->colorSpace, rawColor, width(), height());
193+
}
194+
else {
195+
image = m_d->colorSpace->convertToQImage(rawColor, width(), height(), 0, KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::internalConversionFlags());
196+
}
197+
// safeguard:
198+
if (image.isNull())
199+
{
200+
image = QImage(width(), height(), QImage::Format_ARGB32);
201+
image.fill(Qt::black);
202+
}
203+
204+
return image;
205+
}
206+
207+
QImage KisVisualColorSelectorShape::renderBackground(const QVector4D &channelValues, quint32 pixelSize) const
208+
{
209+
const KisVisualColorSelector *selector = qobject_cast<KisVisualColorSelector*>(parent());
210+
Q_ASSERT(selector);
211+
212+
quint32 imageSize = width() * height() * m_d->colorSpace->pixelSize();
213+
QScopedArrayPointer<quint8> raw(new quint8[imageSize] {});
214+
quint8 *dataPtr = raw.data();
215+
QVector4D coordinates = channelValues;
216+
for (int y = 0; y < height(); y++) {
217+
for (int x=0; x < width(); x++) {
218+
QPointF newcoordinate = convertWidgetCoordinateToShapeCoordinate(QPoint(x, y));
219+
coordinates[m_d->channel1] = newcoordinate.x();
220+
if (m_d->dimension == Dimensions::twodimensional){
221+
coordinates[m_d->channel2] = newcoordinate.y();
222+
}
223+
KoColor c = selector->convertShapeCoordsToKoColor(coordinates);
224+
memcpy(dataPtr, c.data(), pixelSize);
225+
dataPtr += pixelSize;
226+
}
227+
}
228+
return convertImageMap(raw.data(), imageSize);
229+
}
230+
216231
void KisVisualColorSelectorShape::mousePressEvent(QMouseEvent *e)
217232
{
218233
if (e->button()==Qt::LeftButton) {

libs/widgets/KisVisualColorSelectorShape.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,24 @@ public Q_SLOTS:
172172
void updateFromChangedDisplayRenderer();
173173

174174
protected:
175+
/**
176+
* @brief convertImageMap
177+
* convert image data containing raw KoColor data into a QImage
178+
* @param data must point to memory of size width()*height()*pixelSize
179+
* @param size the number of bytes to read from data, must match aforementioned cirteria
180+
* @return the converted QImage guaranteed to match the widget size (black content on failure)
181+
*/
182+
QImage convertImageMap(const quint8 *rawColor, quint32 size) const;
183+
/**
184+
* @brief renderBackground
185+
* Render the widget background visible inside the widget's mask in current color space
186+
* Rendering shall be done with the conversion functions of KisVisualColorSelector
187+
* @param data points to zero-initialized memory of size width()*height()*pixelSize
188+
* @param pixelSize the data size to transfer from KoColor::data() to data per pixel
189+
* in the current color space
190+
* @param channelValues the normalized channel values of the currently picked color
191+
*/
192+
virtual QImage renderBackground(const QVector4D &channelValues, quint32 pixelSize) const;
175193

176194
void mousePressEvent(QMouseEvent *e) override;
177195
void mouseMoveEvent(QMouseEvent *e) override;

0 commit comments

Comments
 (0)