PDA

View Full Version : Change a black pixel to a blue pixel in an image



icon444
16th February 2012, 14:46
Hi guys,

I've wrote this piece of code that should make the black part of an image into a blue part. But the code doesn't change the black into the blue it never comes in the if test I think.

Could someone help me?


QImage background;
QImage world(1500, 768, QImage::Format_RGB32);
QSize sizeImage;
int height, width;
background.load("Background.png");
world.fill(1);

QPainter painter(&world);
sizeImage = background.size();
width = sizeImage.width();
height = sizeImage.height();

const QRgb black = 0;
const QRgb blue = 255;
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
if (background.pixel(x,y) == black) {
background.setPixel(x,y,blue);
}
}
}

painter.drawImage(0,0,background);

//adding new image to the graphicsScene
QGraphicsPixmapItem item( QPixmap::fromImage(background));
QGraphicsScene* scene = new QGraphicsScene;
scene->addItem(&item);

QGraphicsView view(scene);
view.show();

Kind regards,

KillGabio
16th February 2012, 19:35
have you define black and blue correctly? seems to be the problem if you are saying that it never enters the IF....try using Qt::black

ChrisW67
16th February 2012, 23:10
While the use of integers to initialise a QRgb looks odd it is correct language-wise. QRgb is a typedef equivalent to unsigned int.

It is not working as expected because setting the value this way has set the alpha channel to 0: the colour exists but is 100% transparent and will not match an opaque black/blue pixel. Try setting the colours using the qRgb() macro or, as KillGabio suggests, using symbolic names.