2
votes

I try to superpose 3 QImage but I have some warnings, for example :

QImage::pixelColor: coordinate (31,30) out of range

And the result of the blending is a black image.

Here my code :

QBrush MainWindow::blendLayer(int x, int y){
    QImage blend(m_layer1_data[x][y]->background().textureImage());
    QImage l2(m_layer2_data[x][y]->background().textureImage());
    QImage l3(m_layer3_data[x][y]->background().textureImage());

    for(int a = 0; a < blend.width(); a++){
        for(int b = 0; b < blend.height(); b++ ){
            blend.setPixelColor(a,b,blendColor(blend.pixelColor(a,b),l2.pixelColor(a,b),l3.pixelColor(a,b)));
        }
    }
    QBrush brush(blend);
    return brush;
}

QColor MainWindow::blendColor(QColor c2, QColor c1){
    QColor c3;
    c3.setRed(((c1.red()+c2.red())/2)%256);
    c3.setGreen(((c1.green()+c2.green())/2)%256);
    c3.setBlue(((c1.blue()+c2.blue())/2)%256);
    c3.setAlpha(((c1.alpha()+c2.alpha())/2)%256);
    return c3;
}

QColor MainWindow::blendColor(QColor c1, QColor c2, QColor c3){
    return blendColor(c3,blendColor(c1,c2));
}

Is there an easy way to superposing some QImage ? Thanks for your help

2
what's the size of l2 and l3?eyllanesc
Why don't you use QGraphicsPixmapItem for your images ? with setZValue you can supperpose them.Gabriel de Grimouard
what's type of m_layer1_data[x][y], m_layer2_data[x][y] and m_layer3_data[x][y]?eyllanesc
QPainter does all the blending you want, and it's fast. Your code also doesn't compile as shown. You'll have to change the alpha of the source images and then simply draw them on top of each other. Painter can change the alpha too, so all operations will be quick and accelerated using SIMD. When using the painter, the sizes of source images don't matter as long as they make sense for your application.Kuba hasn't forgotten Monica
@eyllanesc Looks like they are QBrush.Kuba hasn't forgotten Monica

2 Answers

1
votes

Like Kuba Ober mentioned in the comments, the best way is to simply use QPainter in a way like this:

//[...]
QPainter p(&myWidgetOrSurface);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.drawRect(myWidgetOrSurface.size());
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.drawImage(image1);
p.end();
p.begin(image2);
p.drawImage(surface);
p.end();
//[...]

Here is the documentation for the different blend modes supported by Qt and QPainter.

0
votes

Thanks a lot I've found a solution with QPainter

QBrush MainWindow::blendLayer(int x, int y){
    QImage blend(m_layer1_data[x][y]->background().textureImage());
    QImage l2(m_layer2_data[x][y]->background().textureImage());
    QImage l3(m_layer3_data[x][y]->background().textureImage());

    QImage im(QSize(32,32),QImage::Format_ARGB32);
    QPainter painter(&im);

    painter.drawImage(im.rect(),blend);
    painter.drawImage(im.rect(),l2);
    painter.drawImage(im.rect(),l3);

    QBrush brush(im);
    return brush;
}