PDA

View Full Version : Explanation to Image Formats



sincnarf
4th July 2007, 11:54
I've already seen these ( http://doc.trolltech.com/4.2/qimage.html#image-formats and http://doc.trolltech.com/4.2/qimage.html#pixel-manipulation ) but I do not know what appropiate image conversions I should use for JPEG PNG ang BMP files.

See the attached image,
http://img247.imageshack.us/img247/8603/okmw7.th.jpg (http://img247.imageshack.us/my.php?image=okmw7.jpg)
i am trying to create a grayscaled image (right QGraphicsView) of a raw image (left QGraphicsView). The right QGraphics View is applied with the following function. After the following function is applied to the right QGraphicsView, the right QGraphicsView's image shall have been grayscaled but something is wrong, the white pixels become blue.




void MainWindow::doF3()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage tempImage(fileName);

if (tempImage.isNull()) {
QMessageBox::information(this, tr("Load Warning"),
tr("Cannot load %1.").arg(fileName));
return;
}
//loadedImage is a globally declared variable
loadedImage = tempImage;

//Here is one problem, I do not know what appropriate conversions I will do for reading JPEG PNG BMP
QImage image = tempImage.convertToFormat(QImage::Format_ARGB32_Pr emultiplied);
int pixel = 0;
int gray = 0;
int alpha = 0;
int x = 0;
int y = 0;
for (x= 0; x < image.width(); x++){
for (y = 0; y < image.height(); y++){
pixel = image.pixel(x, y);
gray = qGray(pixel);
alpha = qAlpha(pixel);

//Here is another problem, I do not know how to test if this makes the pixel grayscaled
image.setPixel(x, y, qRgba(qRed(gray), qGreen(gray), qBlue(gray), alpha));

}
}

QGraphicsScene *scene = new QGraphicsScene;

//add the scene with grayscaled image to the right QGraphicsView

graphicsViewVis->setScene(scene);

graphicsViewVis->show();
}
}

sincnarf
4th July 2007, 12:05
sorry for my noobness by the way. Also , i tested other Jpegs Bmps and Pngs and they all did not become grayscaled (at least from what I see). All the images become blue!

sincnarf
5th July 2007, 15:10
Qt Centre I am sorry for this repost,I still did not solve this problem of mine

I've already seen these ( http://doc.trolltech.com/4.2/qimage.html#image-formats and http://doc.trolltech.com/4.2/qimage.html#pixel-manipulation ) but I do not know what appropiate image conversions I should use for JPEG PNG ang BMP files.

See the attached image,
i am trying to create a grayscaled image (right QGraphicsView) of a raw image (left QGraphicsView). The right QGraphics View is applied with the following function. After the following function is applied to the right QGraphicsView, the right QGraphicsView's image shall have been grayscaled but something is wrong, the white pixels become blue.
http://img247.imageshack.us/img247/8603/okmw7.th.jpg (http://img247.imageshack.us/my.php?image=okmw7.jpg)




void MainWindow::doF3()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage tempImage(fileName);

if (tempImage.isNull()) {
QMessageBox::information(this, tr("Load Warning"),
tr("Cannot load %1.").arg(fileName));
return;
}
//loadedImage is a globally declared variable
loadedImage = tempImage;

//Here is one problem, I do not know what appropriate conversions I will do for reading JPEG PNG BMP
QImage image = tempImage.convertToFormat(QImage::Format_ARGB32_Pr emultiplied);
int pixel = 0;
int gray = 0;
int alpha = 0;
int x = 0;
int y = 0;
for (x= 0; x < image.width(); x++){
for (y = 0; y < image.height(); y++){
pixel = image.pixel(x, y);
gray = qGray(pixel);
alpha = qAlpha(pixel);

//Here is another problem, I do not know how to test if this makes the pixel grayscaled
image.setPixel(x, y, qRgba(qRed(gray), qGreen(gray), qBlue(gray), alpha));

}
}

QGraphicsScene *scene = new QGraphicsScene;

//add the scene with grayscaled image to the right QGraphicsView

graphicsViewVis->setScene(scene);

graphicsViewVis->show();
}
}


I also loaded and applied the aforementioned function on other JPEG PNG and BMP images and all of those had a dominant blue color. Anyone know how to solve this?

wysota
5th July 2007, 15:55
Why do you convert to premultiplied? It would be easiest if you first recalculated the colors using qGray() and setPixel. If that works you can convert to QImage::Format_Indexed8 (remember about setting the color lookup table). If that works you can also try to keep the 32 bit colour space (should you need it).

And don't double post.

sincnarf
5th July 2007, 17:29
Sorry for the doublepost, anyway I did what you told and this is now the function for loading a grayscaled image. The grayscaling is still not done. The displayed image after applying the functions still has a dominant blue color regardless of the image conversion type.



void MainWindow::doF3()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);

if (image.isNull()) {
QMessageBox::information(this, tr("Load Warning"),
tr("Cannot load %1.").arg(fileName));
return;
}

int pixel = 0;
int red = 0;
int green = 0;
int blue = 0;
int gray = 0;
int alpha = 0;
int x = 0;
int y = 0;
for (x = 0; x < image.width(); x++){
for (y = 0; y < image.height(); y++){
pixel = image.pixel(x, y);
red = qRed(pixel);
green = qGreen(pixel);
blue = qBlue(pixel);
gray = qGray(pixel);
alpha = qAlpha(pixel);

//This does not seem to create a grayscaled pixel
image.setPixel(x, y, qRgba(qRed(gray), qGreen(gray), qBlue(gray), alpha));

//Even this one does not seem to create a grayscaled pixel
//image.setPixel(x, y, qGray(red, green, blue));

}
}
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsPixmapItem *item = scene->addPixmap(QPixmap::fromImage(image));
graphicsViewVis->setScene(scene);
graphicsViewVis->show();
}
}


If you get the qGray of the red green blue components of a white pixel (255,255,255), the result is (39,39,39) - which is a gray Color which is correct. but what's displayed is a shade of blue instead.

Now, regardless of the image conversion type
image.setPixel(x, y, qRgba(qRed(gray), qGreen(gray), qBlue(gray), alpha));
does not seem to create a grayscaled pixel

and even "image.setPixel(x, y, qGray(red, green, blue))" does not seem to create a grayscaled pixel eventhough red=39, green=39, and blue=39.

There must be something I'm missing here because 39,39,39 is a gray color but what's displayed is a BLUE COLOR.

jacek
5th July 2007, 17:40
Try:
image.setPixel(x, y, qRgba( gray, gray, gray, alpha) );

wysota
5th July 2007, 17:45
Try this:

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>

QImage toGray(const QImage &img){
QImage newimage = img;
for(int y=0;y<img.height(); y++)
for(int x=0;x<img.width(); x++){
int g = qGray(img.pixel(x,y));
int a = qAlpha(img.pixel(x,y));
newimage.setPixel(x,y, qRgba(g, g, g, a));
}
return newimage;
}

int main(int argc, char **argv){
QApplication app(argc, argv);
QGraphicsScene s;
s.addPixmap(QPixmap::fromImage(toGray(QImage(argv[1]))));
QGraphicsView v;
v.setScene(&s);
v.show();
return app.exec();
}

sincnarf
5th July 2007, 18:23
Thanks wysota but the image displayed is not grayscaled.

I saved the codes to a folder called "toGray", and named it main.cpp. entered qmake-project, entered qmake, entered make. Then I copied a bmp, png, jpeg to the debug folder. I accessed the debug folder.

I typed "toGray qt-logo.bmp". the bmp is displayed but is not grayscaled.

I typed "toGray qt-logo.jpg". the jpg is displayed but is not grayscaled.

I typed "toGray qt-logo.png". there's a long delay in loading the png. still not grayscaled.

jacek
5th July 2007, 20:06
Could you post a screenshot?

Also note that above method won't work for images with color table. In such case you have to convert the image to ARGB32 format or treat indexed images in a special way (i.e. change the color table, not pixels).

wysota
5th July 2007, 20:08
Could you try with a different file? For example the one attached. The application won't work for 8 bit (indexed) images like qt-logo.

sincnarf
6th July 2007, 02:27
Thank you both wysota and jacek! wysota's sample program worked for the attached image kdmconfig.png!

Just as I thought I need to know what valid image conversions I should use.

@wysota, what do i need to do in order to pattern other images to the properties of kdmconfig.png. Can you give me the exact properties of that attached kdmconfig? I opened it in Adobe Photoshop CS2 and I saw that the image's mode is "RGB Color", "8 bits / channel", and bit depth = "32". What other pertinent information am I missing?

@jacek, in QImage::setColorTable ( const QVector<QRgb> colors ), what is "QVector<QRgb> colors"? Is that where I set my gray color?

Separate question: By the way how much maximum colors can QImage support? Is it 256? Thanks in advance for all the advice! Thank God I saw QtCentre.org!

wysota
6th July 2007, 07:17
what do i need to do in order to pattern other images to the properties of kdmconfig.png.
They should be True or Full Color, not indexed. With indexed images (ones that have the color lookup table) you just need to change the contents of the lookup table without touching actual pixels.


Can you give me the exact properties of that attached kdmconfig?

kdmconfig.png: PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced


in QImage::setColorTable ( const QVector<QRgb> colors ), what is "QVector<QRgb> colors"? Is that where I set my gray color?
Yes, for indexed images.


Separate question: By the way how much maximum colors can QImage support? Is it 256?
16777216 (2^24). Multiplied by 256 if you also count different alpha values which then gives 4294967296 (4G) colours.

sincnarf
6th July 2007, 16:43
hmmm. definitely got the "non-interlaced" advice. Is this how you use setColorTable method?



int gray = 0;
int pixel = 0;
image.setNumColors(256);
QVector<QRgb> colorTable(256);
for (x= 0; x < image.width(); x++){
for (y = 0; y < image.height(); y++){
pixel = image.pixel(x, y);
gray = qGray(pixel);
colorTable.append(qRgb(gray, gray, gray));
}
}
image.setColorTable(colorTable);

jacek
6th July 2007, 17:02
Is this how you use setColorTable method?
No, get the original colour table using QImage::colorTable(), replace all of the entries with grayscale equivalents and set the new colour table using QImage::setColorTable(), but only if the image has a colour table (i.e. QImage::numColors() is greater than 0).