PDA

View Full Version : How to get colored QImage



Nadja
13th August 2012, 09:49
Hey, I want to open a .png using QFileDialog. Works fine for .txt & .png! Unfortunately the Imige is only shown in gray. Although reading the Docu and some examples i cant solve my problem. Try to build a color Table in a few ways but it doesn`t works. Using a QLabel to display the Image. Could anybody help? Sorry for my English. Isn`t the best. Nadja


void MyClass::open()
{
QString filename = QFileDialog::getOpenFileName(this,tr("Load File"),"",tr("Text Files (*.txt);;Images (*.png *.jpg)"));

if (filename != "") {
/*QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
return;}

QString contents = file.readAll().constData();
ui.Textfeld -> setPlainText(contents);
file.close();}*/

QImage image(filename);
image = image.convertToFormat(QImage::Format_Indexed8); //shows the image in gray
/*image = QImage(600, 453, QImage::Format_Indexed8);*/ // shows black and white stripes (why?)

QVector<QRgb> table(256);
for(int i = 0; i < table.size(); i++)
{table[i] = qRgb ( i,i,i );}
image.setColorTable(table);

/*QRgb i;
for ( int i=0; i<256; i++ ) //build color Table
image.setColor(i,qRgb(i,i,i));*/

//image.fill(qRgb(255,255,255));
//image.setColor( 19, qRgb(255,255,0) );
//(image.scanLine(y) + x) = 19;
/*QPixmap pixmap;
pixmap = pixmap.fromImage(image);*/

ui.Display->setPixmap(QPixmap::fromImage(image));}
}

Lykurg
13th August 2012, 09:56
qRgb ( i,i,i ) will always be gray!

mvuori
13th August 2012, 10:25
And of course the colour map needs to be created _before_ conversion and passed as an argument to the conversion. How else would Qt know to what index to map each RGB pixel using a closest match principle.

So, you need to get a ready-made colour map data from somewhere...

Nadja
13th August 2012, 10:29
Okay.....you`re right.....but how to do it otherwise.....do i have to build a loop for each Rgb value......means instead of i,i,i, to use i for red, j for green and k for blue??? Is it right to use QRgb? Did I have to use a Vector or does QT have another possibility???
And why do I get the black and white Stripes when I use: image = QImage(600, 453, QImage::Format_Indexed8???

Would be very nice if someone can help a little bit more. I`m not a Guru but it`ll be nice to get one in a few years :-p ....greetz Nadja