View Full Version : Save greyscale image
Hi all,
I'm triyng to save a image buffer only grey into an image.
When I save to a file a get an error saying QVector<T> out of range.
Is there something I'm missing?
Thanks in advance for any answer
QImage tmp(width, height, QImage::Format_Indexed8);
memcpy(tmp.bits(), pFrame->data[0], width * height);
QString filename = QString("C:\\testscene\\frame%1.jpg").arg(durationText);
tmp.scaledToHeight(120, Qt::SmoothTransformation).save(filename);
patrik08
8th July 2008, 17:19
Try this here:
extern inline QImage GreyScale( QImage income )
{
QImage base = income.convertToFormat(QImage::Format_RGB32);
for (int y = 0; y < base.height(); ++y) {
for (int x = 0; x < base.width(); ++x) {
int pixel = base.pixel(x, y);
int gray = qGray(pixel);
int alpha = qAlpha(pixel);
base.setPixel(x, y, qRgba(gray, gray, gray, alpha));
}
}
return base;
}
Or Sepia convert: ( htmledit/src/interface.cpp )
http://www.google.com/codesearch?hl=en&lr=&q=sepia+%2BGreyScale+%2BColorize
One can also (ab)use QIcon by adding a pixmap in normal mode and requesting a pixmap in disabled mode.
wysota
8th July 2008, 20:30
I think the problem comes from the fact that you didn't define a colour map for the indexed pixmap. Indexed images contain not the colour values in their pixel data but indexes of colours in a lookup table. This table has to be filled with real colours. Otherwise if your pixel data contains colour values, you probably should use real-colour image format such as QImage::Format_RGB32. Of course you'll have to provide 32b long pixel values.
Thanks for the answers.
I tried this
for (int i=0; i<=255; i++)
{
colorTable.append(qRgb(i, i, i));
}
QImage tmp(width, height, QImage::Format_Indexed8);
tmp.setColorTable(colorTable);
memcpy(tmp.bits(), pFrame->data[0], width * height);
QString filename = QString("C:\\testscene\\frame%1.png").arg(durationText);
tmp.scaledToHeight(120, Qt::SmoothTransformation).save(filename);
Now there's no error but the resulting png is all black.
Any idea?
thanks again
wysota
9th July 2008, 10:09
What values does pFrame->data[0] contain? They should be from within range 0-255.
^NyAw^
9th July 2008, 10:43
Hi,
Changing
memcpy(tmp.bits(),pFrame->data[0], width * height);
to
memcpy(tmp.bits(),pFrame->data, width * height);
is getting you different result? How "pFrame" is defined?
I "fixed" it in this way:
QImage tmp(width, height, QImage::Format_RGB888);
uchar* curs = tmp.bits();
int cont = 0;
for(int i=0; i < width * height; i++)
{
curs[cont++] = pFrame->data[0][i];
curs[cont++] = pFrame->data[0][i];
curs[cont++] = pFrame->data[0][i];
}
//memcpy(tmp.bits(), pFrame->data[0], width * height);
tmp.scaledToHeight(120, Qt::SmoothTransformation).save(filename);
yogeshgokul
12th August 2008, 08:59
@ patrik08
Good compact function, working fine. I used this function in my app. Thank you.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.