PDA

View Full Version : Saving QPixmap resutls in corrupt JPG



Pit
16th July 2010, 13:19
Hi! When I try to save a QPixmap in Windows, it results in a corrupt JPG file, black or black with white lines. But it doesn't happen in all computers, I tried in four and in two happens, in the other two not.

Saving in PNG doesn't have any errors.

Any ideas???

Thank you.

wysota
16th July 2010, 13:50
Please provide a minimal compilable example reproducing the problem.

Pit
19th July 2010, 08:58
Hi! Thanks for your reply. Here is the example that fails. Basically it loads a photo into a label and then saves it:


void MainWindow::on_savePushButton_clicked()
{
[...]

if (!photoLabel->pixmap()->save(QString("fotos/%1.jpg").arg(regLineEdit->displayText()), 0, 100))
QMessageBox::warning(this, qApp->applicationName(), "Error");

[...]
}

void MainWindow::loadPhoto()
{
QPixmap pixmap;

QString path = QString("fotos/%1.jpg").arg(cardsModel->index(mapper->currentIndex(), ViewCardsTableModel::Registration).data().toString ());

if (!pixmap.load(path))
{
QMessageBox::critical(this, qApp->applicationName(), QString("Error.").arg(cardsModel->index(mapper->currentIndex(), ViewCardsTableModel::Registration).data().toString ()));
photoLabel->setPixmap(QPixmap());
}
else
photoLabel->setPixmap(pixmap);
}

wysota
19th July 2010, 12:37
This is neither minimal nor compilable.

Pit
19th July 2010, 12:52
Sorry, it's too much code. But it's simple, just this:



QPixmap pixmap;
pixmap.load("photo.jpg");
pixmap.save("photo2.jpg");


Don't work. I get the attached image.

http://www.laproductora.com/photo.jpg

wysota
19th July 2010, 20:21
Does it happen for every jpeg image or this particular one?

Pit
19th July 2010, 21:01
For every JPEG, but if I use QImage instead QPixmap it works, so it's not because jpeg plugin (I think).

wysota
20th July 2010, 01:42
Well... saving the picture is done by the same code (QImageWriter) regardless if you use QPixmap or QImage as one is converted to the other so if it matters whether you use QPixmap or QImage, I'd say Qt is not to be blamed.

So the following doesn't work?

#include <QtGui>
int main(int argc, char **argv){
QApplication app(argc, argv);
QPixmap px;
px.load("input.jpeg");
px.save("output.jpeg");
return 0;
}

And the following does?

#include <QtGui>
int main(int argc, char **argv){
QApplication app(argc, argv);
QImage px;
px.load("input.jpeg");
px.save("output.jpeg");
return 0;
}

Please check both snippets.

Pit
20th July 2010, 09:15
That's right, I checked it and the first doesn't work, the second does.

wysota
20th July 2010, 09:29
That's right, I checked it and the first doesn't work, the second does.

This exact same code, right? What platform are you using and which version of Qt (you might want to update your QtCentre profile to avoid such questions in future)?

Pit
20th July 2010, 09:37
Yes, this exact same code. I'm using Windows XP SP3 and Qt 4.6.3 (thanks for the advice :) ). This code works in another computer with XP SP3, but it doesn't in the computer with QT installed, neither in another with XP SP3 too.

Pit
20th July 2010, 10:00
I tried this, but it failed too:


#include <QtGui>
int main(int argc, char **argv){
QApplication app(argc, argv);
QImage img;
QPixmap px;
img.load("input.jpeg");
px = QPixmap::fromImage(img);
px.save("output.jpeg");
return 0;
}

wysota
20th July 2010, 10:00
One more try:

#include <QtGui>

int main(int argc, char **argv){
QApplication app(argc, argv);
QImage img = QImageReader("input.jpeg").read(); // this is almost exactly
QPixmap px = QPixmap::fromImage(img); // what QPixmap::load() does
QLabel l;
l.setPixmap(px);
l.show();
px.save("output.jpeg");
return app.exec();
}

Is the label correct? Is output.jpeg corrupted?

Pit
20th July 2010, 10:18
The label is correct, and output.jpeg is corrupted. That happens in my program too, I have a label and I put the QPixmap there, but when I save it the jpeg is corrupted. The problem appears to be the save function :(

wysota
20th July 2010, 10:25
Save essentially does this:

QPixmap px(...);
QImage img = px.toImage();
QImageWriter writer(fiileName);
writer.write(img);

Try this piece of code:

int main(...) {
QApplication app(argc, argv);
QLabel l;
QImage img("input.jpeg");
QPixmap px = QPixmap::fromImage(img);
img = px.toImage();
px = QPixmap::fromImage(img);
l.setPixmap(px);
l.show();
img.save("output_image.jpeg", "JPEG");
px.save("output_pixmap.jpeg", "JPEG");
return app.exec();
}

Basically Qt loads and saves pixmaps and images using the same code, so essentially either both or none of them should work so if what you experience is different, there might be something wrong with your installation.

Pit
20th July 2010, 10:34
Same result. Label shows correctly, but both output_image and output_pixmap are corrupted. I will go to install QT in the computer that works, and compile from them and see the result. I'll post the results soon. Thank you :)

Pit
20th July 2010, 11:11
Hmmmm... I installed QT 4.6.3 in the computer where the program works. After install, QPixmap fails again :confused:

wysota
20th July 2010, 11:30
I think there is some software installed on your computer which causes malfunction of libjpeg or something like that.

Pit
21st July 2010, 08:59
Well... I don't know what is wrong. I tried in many computers, some of them works, some don't, so finally I'm saving in PNG format... Thanks for all anyway :)