2

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

NegatIV
  • 47
  • 2

2 Answers2

1

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.

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
0

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;
}
NegatIV
  • 47
  • 2