Results 1 to 14 of 14

Thread: Explanation to Image Formats

  1. #1
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Explanation to Image Formats

    I've already seen these ( http://doc.trolltech.com/4.2/qimage.html#image-formats and http://doc.trolltech.com/4.2/qimage....l-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.


    Qt Code:
    1. void MainWindow::doF3()
    2. {
    3. QString fileName = QFileDialog::getOpenFileName(this,
    4. tr("Open Image"), QDir::currentPath());
    5. if (!fileName.isEmpty()) {
    6. QImage tempImage(fileName);
    7.  
    8. if (tempImage.isNull()) {
    9. QMessageBox::information(this, tr("Load Warning"),
    10. tr("Cannot load %1.").arg(fileName));
    11. return;
    12. }
    13. //loadedImage is a globally declared variable
    14. loadedImage = tempImage;
    15.  
    16. //Here is one problem, I do not know what appropriate conversions I will do for reading JPEG PNG BMP
    17. QImage image = tempImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    18. int pixel = 0;
    19. int gray = 0;
    20. int alpha = 0;
    21. int x = 0;
    22. int y = 0;
    23. for (x= 0; x < image.width(); x++){
    24. for (y = 0; y < image.height(); y++){
    25. pixel = image.pixel(x, y);
    26. gray = qGray(pixel);
    27. alpha = qAlpha(pixel);
    28.  
    29. //Here is another problem, I do not know how to test if this makes the pixel grayscaled
    30. image.setPixel(x, y, qRgba(qRed(gray), qGreen(gray), qBlue(gray), alpha));
    31.  
    32. }
    33. }
    34.  
    35.  
    36. //add the scene with grayscaled image to the right QGraphicsView
    37.  
    38. graphicsViewVis->setScene(scene);
    39.  
    40. graphicsViewVis->show();
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    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!

  3. #3
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with Grayscaling and Appropriate Image Conversions

    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....l-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.



    Qt Code:
    1. void MainWindow::doF3()
    2. {
    3. QString fileName = QFileDialog::getOpenFileName(this,
    4. tr("Open Image"), QDir::currentPath());
    5. if (!fileName.isEmpty()) {
    6. QImage tempImage(fileName);
    7.  
    8. if (tempImage.isNull()) {
    9. QMessageBox::information(this, tr("Load Warning"),
    10. tr("Cannot load %1.").arg(fileName));
    11. return;
    12. }
    13. //loadedImage is a globally declared variable
    14. loadedImage = tempImage;
    15.  
    16. //Here is one problem, I do not know what appropriate conversions I will do for reading JPEG PNG BMP
    17. QImage image = tempImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    18. int pixel = 0;
    19. int gray = 0;
    20. int alpha = 0;
    21. int x = 0;
    22. int y = 0;
    23. for (x= 0; x < image.width(); x++){
    24. for (y = 0; y < image.height(); y++){
    25. pixel = image.pixel(x, y);
    26. gray = qGray(pixel);
    27. alpha = qAlpha(pixel);
    28.  
    29. //Here is another problem, I do not know how to test if this makes the pixel grayscaled
    30. image.setPixel(x, y, qRgba(qRed(gray), qGreen(gray), qBlue(gray), alpha));
    31.  
    32. }
    33. }
    34.  
    35.  
    36. //add the scene with grayscaled image to the right QGraphicsView
    37.  
    38. graphicsViewVis->setScene(scene);
    39.  
    40. graphicsViewVis->show();
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by sincnarf; 5th July 2007 at 16:13. Reason: Lack of Information

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with Grayscaling and Appropriate Image Conversions

    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.

  5. #5
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    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.

    Qt Code:
    1. void MainWindow::doF3()
    2. {
    3. QString fileName = QFileDialog::getOpenFileName(this,
    4. tr("Open Image"), QDir::currentPath());
    5. if (!fileName.isEmpty()) {
    6. QImage image(fileName);
    7.  
    8. if (image.isNull()) {
    9. QMessageBox::information(this, tr("Load Warning"),
    10. tr("Cannot load %1.").arg(fileName));
    11. return;
    12. }
    13.  
    14. int pixel = 0;
    15. int red = 0;
    16. int green = 0;
    17. int blue = 0;
    18. int gray = 0;
    19. int alpha = 0;
    20. int x = 0;
    21. int y = 0;
    22. for (x = 0; x < image.width(); x++){
    23. for (y = 0; y < image.height(); y++){
    24. pixel = image.pixel(x, y);
    25. red = qRed(pixel);
    26. green = qGreen(pixel);
    27. blue = qBlue(pixel);
    28. gray = qGray(pixel);
    29. alpha = qAlpha(pixel);
    30.  
    31. //This does not seem to create a grayscaled pixel
    32. image.setPixel(x, y, qRgba(qRed(gray), qGreen(gray), qBlue(gray), alpha));
    33.  
    34. //Even this one does not seem to create a grayscaled pixel
    35. //image.setPixel(x, y, qGray(red, green, blue));
    36.  
    37. }
    38. }
    39. QGraphicsPixmapItem *item = scene->addPixmap(QPixmap::fromImage(image));
    40. graphicsViewVis->setScene(scene);
    41. graphicsViewVis->show();
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    Try:
    Qt Code:
    1. image.setPixel(x, y, qRgba( gray, gray, gray, alpha) );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Explanation to Image Formats

    Try this:
    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsView>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsPixmapItem>
    5.  
    6. QImage toGray(const QImage &img){
    7. QImage newimage = img;
    8. for(int y=0;y<img.height(); y++)
    9. for(int x=0;x<img.width(); x++){
    10. int g = qGray(img.pixel(x,y));
    11. int a = qAlpha(img.pixel(x,y));
    12. newimage.setPixel(x,y, qRgba(g, g, g, a));
    13. }
    14. return newimage;
    15. }
    16.  
    17. int main(int argc, char **argv){
    18. QApplication app(argc, argv);
    19. s.addPixmap(QPixmap::fromImage(toGray(QImage(argv[1]))));
    20. v.setScene(&s);
    21. v.show();
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  8. The following user says thank you to wysota for this useful post:

    sincnarf (6th July 2007)

  9. #8
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    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.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    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).

  11. The following user says thank you to jacek for this useful post:

    sincnarf (6th July 2007)

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Explanation to Image Formats

    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.
    Attached Images Attached Images

  13. The following user says thank you to wysota for this useful post:

    sincnarf (6th July 2007)

  14. #11
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    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!

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Explanation to Image Formats

    Quote Originally Posted by sincnarf View Post
    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.

  16. The following user says thank you to wysota for this useful post:

    sincnarf (4th October 2007)

  17. #13
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    hmmm. definitely got the "non-interlaced" advice. Is this how you use setColorTable method?

    Qt Code:
    1. int gray = 0;
    2. int pixel = 0;
    3. image.setNumColors(256);
    4. QVector<QRgb> colorTable(256);
    5. for (x= 0; x < image.width(); x++){
    6. for (y = 0; y < image.height(); y++){
    7. pixel = image.pixel(x, y);
    8. gray = qGray(pixel);
    9. colorTable.append(qRgb(gray, gray, gray));
    10. }
    11. }
    12. image.setColorTable(colorTable);
    To copy to clipboard, switch view to plain text mode 

  18. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Explanation to Image Formats

    Quote Originally Posted by sincnarf View Post
    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).

  19. The following user says thank you to jacek for this useful post:

    sincnarf (4th October 2007)

Similar Threads

  1. Help needed handling image data
    By toratora in forum General Programming
    Replies: 2
    Last Post: 11th May 2007, 10:24
  2. custom widgets painting image formats
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2007, 11:33
  3. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 15:39
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 17:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 20:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.