Change pixel color of QPixmap
-
@Jakob-Clausen said in Change pixel color of QPixmap:
Why is it not working?
Don't know - add some debug output, use a debugger and step trough to see the values. Do what a software developer does in such a case.
@Christian-Ehrlicher I don't understand. How is a debugger going to help me? What do you think I should look for?
-
@Christian-Ehrlicher I don't understand. How is a debugger going to help me? What do you think I should look for?
@Jakob-Clausen said in Change pixel color of QPixmap:
What do you think I should look for?
You should look if e.g. setPixel() is called ...
-
@Jakob-Clausen said in Change pixel color of QPixmap:
What do you think I should look for?
You should look if e.g. setPixel() is called ...
@Christian-Ehrlicher It is being called. If I remove the line, then the picture looks like the original. For me setPixel() is a magic function that should replace one pixel color with another. If it does not, then I am lost.
-
So do you actually use the pixmap returned by the function?
-
Note the caveats mentioned in https://doc.qt.io/qt-5/qimage.html#setPixel :
If the image's format is either monochrome or paletted, the given index_or_rgb value must be an index in the image's color table, otherwise the parameter must be a QRgb value. If position is not a valid coordinate pair in the image, or if index_or_rgb >= colorCount() in the case of monochrome and paletted images, the result is undefined.
So, is your image maybe using a palette? There's more details here: https://doc.qt.io/qt-5/qimage.html#pixel-manipulation
-
@Christian-Ehrlicher It is being called. If I remove the line, then the picture looks like the original. For me setPixel() is a magic function that should replace one pixel color with another. If it does not, then I am lost.
@Jakob-Clausen
As @Christian-Ehrlicher has just asked --- if you think your code is changing the passed-inQPixmap& pixmap
, it is not! -
So do you actually use the pixmap returned by the function?
@Christian-Ehrlicher yes.
-
If I comment out the line setPixel(), then the picture will look like the original:
If I do as described, then it looks like this:
So it is changing the pixelvalues. But it is a little rough on the edges. -
Try to use
setPixelColor(int x, int y, const QColor &color)
. -
Try to use
setPixelColor(int x, int y, const QColor &color)
.@Christian-Ehrlicher If I do that, then it works perfectly. But setPixelColor() is introduced before 5.6, and I am forced to use 5.3.
-
@Christian-Ehrlicher If I do that, then it works perfectly. But setPixelColor() is introduced before 5.6, and I am forced to use 5.3.
@Jakob-Clausen Did you read @kkoehne 's comment? What pixel format do you have?
-
@Jakob-Clausen Did you read @kkoehne 's comment? What pixel format do you have?
@Christian-Ehrlicher QImage::Format_ARGB32_Premultiplied
-
If I comment out the line setPixel(), then the picture will look like the original:
If I do as described, then it looks like this:
So it is changing the pixelvalues. But it is a little rough on the edges.@Jakob-Clausen Perhaps you should try to force pixel format to what you need:
QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from) { QImage temp = pixmap.toImage(); qDebug() << "Pixel format is:" << temp.pixelFormat(); if(temp.pixelFormat() != QImage::Format_ARGB32) temp.convertTo(QImage::Format_ARGB32); for (int y = 0; y < temp.height(); ++y) { QRgb* s = reinterpret_cast<QRgb *>(temp.scanLine(y)); for (int x= 0; x < temp.width(); ++x, ++s) { QColor color = QColor::fromRgba(*s); uint8_t alpha = color.alpha(); to.setAlpha(alpha); from.setAlpha(alpha); if (color == from) *s = to.rgba(); } } return QPixmap::fromImage(temp); }
-
QImage::Format_ARGB32_Premultiplied is not a good idea to do the pixel stuff then... see the docs: https://doc.qt.io/qt-5/qimage.html#Format-enum
-
@Jakob-Clausen Perhaps you should try to force pixel format to what you need:
QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from) { QImage temp = pixmap.toImage(); qDebug() << "Pixel format is:" << temp.pixelFormat(); if(temp.pixelFormat() != QImage::Format_ARGB32) temp.convertTo(QImage::Format_ARGB32); for (int y = 0; y < temp.height(); ++y) { QRgb* s = reinterpret_cast<QRgb *>(temp.scanLine(y)); for (int x= 0; x < temp.width(); ++x, ++s) { QColor color = QColor::fromRgba(*s); uint8_t alpha = color.alpha(); to.setAlpha(alpha); from.setAlpha(alpha); if (color == from) *s = to.rgba(); } } return QPixmap::fromImage(temp); }
@KroMignon Thats a good idea. convertTo() is introduced in 5.13. But if I use
temp = temp.convertToFormat(QImage::Format_ARGB32);
Then it seems to work.
Thank you very much. -
J JoeCFD referenced this topic on