Results 1 to 14 of 14

Thread: image alpha

  1. #1
    Join Date
    Oct 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default image alpha

    hi, i'm a new qt user and i want to modify the alpha of an entire image with a slider or even a stupid button just for see if it is working, but i don't know how to manipulate this alpha channel of the image..
    I have an QImage object and i have putted in it a jpg image that has an alpha channel (image.hasAlphaChannel gives me true). but how to modify the alpha values?
    i tried to work onto the "image.colorTable" but it gives me nothing back..

    ps: i'm using the latest qt version and windows

    thank you in advance!
    waitin' for your replies!
    Marco

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: image alpha

    QImage::setAlphaChannel(const QImage & alphaChannel) ?
    Sets the alpha channel of this image to the given alphaChannel.

    If alphaChannel is an 8 bit grayscale image, the intensity values are written into this buffer directly. Otherwise, alphaChannel is converted to 32 bit and the intensity of the RGB pixel values is used.

    Note that the image will be converted to the Format_ARGB32_Premultiplied format if the function succeeds.
    J-P Nurmi

  3. #3
    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: image alpha

    I have an QImage object and i have putted in it a jpg image that has an alpha channel
    Hmm... jpeg images have alpha channels? Are you sure? Or did you add the channel after loading the image?

  4. #4
    Join Date
    Oct 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: image alpha

    Quote Originally Posted by wysota View Post
    Hmm... jpeg images have alpha channels? Are you sure? Or did you add the channel after loading the image?
    yes you're right it was a png file, and i did this code:

    Qt Code:
    1. QImage image = imageLabel->pixmap()->toImage();
    2. for(int i=0;i<image.height();i++){
    3. for(int j=0;j<image.width();j++){
    4. QColor color;
    5. color=image.pixel(i,j);
    6. color.setAlpha(alpha);
    7. image.setPixel(i,j,color.rgba());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    but it's not working well cause the resulting images have different colours and not different alpha values.. ?!
    can anyone tell me why?
    Last edited by wysota; 27th October 2006 at 16:52. Reason: changed [quote] to [code]

  5. #5
    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: image alpha

    How about:
    Qt Code:
    1. QImage yourimage;
    2. QImage alphamask = yourimage;
    3. alphamask.fill(alpha);
    4. yourimage.setAlphaChannel(alphamask);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: image alpha

    if i set an alpha value from zero to 255 and i fill the mask.. i'm no more able to see the image.. 4example if i set .fill(255) the mask is white and the image is totally covered by the white mask..

  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: image alpha

    Ok, fill() corrupts the image. Use this instead:
    Qt Code:
    1. #include <QApplication>
    2. #include <QImage>
    3. #include <QLabel>
    4. #include <QPalette>
    5. #include <QVBoxLayout>
    6. #include <QPixmap>
    7. #include <QPainter>
    8.  
    9. int main(int argc, char **argv){
    10. QApplication app(argc, argv);
    11. QWidget wgt;
    12. QPalette pal = wgt.palette();
    13. pal.setColor(QPalette::Window, Qt::red);
    14. QLabel *lab = new QLabel;
    15. l->addWidget(lab);
    16. wgt.setLayout(l);
    17. QImage image("test.jpg");
    18. QImage alpha = image;
    19. QPainter painter(&alpha);
    20. painter.fillRect(alpha.rect(), QColor(127,127,127));
    21. painter.end();
    22. image.setAlphaChannel(alpha);
    23. QPixmap pix = QPixmap::fromImage(image);
    24. lab->setPixmap(pix);
    25. wgt.show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Oct 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: image alpha

    thank you allot!!
    this is working really good!
    another question:


    if i create a png (4ex in fireworks) with an alpha (4ex 60) and then i close and re-open the image.. the program is able to recognize the fact that the image has an alpha value = 60;
    is it possible to do the same thing with qt ? 'cause now when i re-open the image with the viewer i made it reads the initial alpha value as 255

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: image alpha

    Quote Originally Posted by murko81 View Post
    if i create a png (4ex in fireworks) with an alpha (4ex 60) and then i close and re-open the image.. the program is able to recognize the fact that the image has an alpha value = 60;
    is it possible to do the same thing with qt ? 'cause now when i re-open the image with the viewer i made it reads the initial alpha value as 255
    The image you are showing is created dynamically at run time. You would have to save it back to a file to keep the changes, see QImage::save().
    J-P Nurmi

  10. #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: image alpha

    Quote Originally Posted by murko81 View Post
    if i create a png (4ex in fireworks) with an alpha (4ex 60) and then i close and re-open the image.. the program is able to recognize the fact that the image has an alpha value = 60;
    is it possible to do the same thing with qt ? 'cause now when i re-open the image with the viewer i made it reads the initial alpha value as 255
    Qt handles alpha just fine. If you feed it with an image which already has an alpha channel, it will display it correctly without the 'hack' we did here.

  11. #11
    Join Date
    Oct 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: image alpha

    of course i save the file before re-opening and of course qt handles fine the alpha channel, maybe i didn't wrote it good (i apologize4my english ).
    The fact is:
    1- I create an image with a commercial editor and i give to that image an alpha value, and i save it: myfolder/myimage.png , and then i close everything.
    2- if i reopen that image with the same commercial editor, it is able to recognize the right alpha value (not only displaying the image in the proper way but also writing it correctly in between the properties of the image)
    3-instead if i reopen the image with my self made editor, i am able to display the image correctly but into the properties the alpha value is recognized as the maximum (255)

    so the question was: how can i read the rigth alpha value? 'cause if a commercial editor is able to do that maybe i can also:-)

    thank you in advance!

  12. #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: image alpha

    Quote Originally Posted by murko81 View Post
    3-instead if i reopen the image with my self made editor, i am able to display the image correctly but into the properties the alpha value is recognized as the maximum (255)

    so the question was: how can i read the rigth alpha value?
    And how do you read the alpha value now? This question is really tricky, as alpha is not a single value but a bitmap.

  13. #13
    Join Date
    Oct 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: image alpha

    :-) yes it's not a single value but starting from the fact that in fireworks it shows me it like an unique value i thought that fireworks changes all the alpha values to the same value.. so reading one of them from one single pixel it was enough.. maybe it's not the right way to get it but this is how i did it and this is how i get back always a value = 255;

  14. #14
    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: image alpha

    Attached code shows how to do it properly and attached image shows an example result. Image on the left is the actual image whether image on the right is a bitmap of the alpha channel.
    Attached Images Attached Images
    Attached Files Attached Files

Similar Threads

  1. "sensitising" an image
    By TheKedge in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2006, 07:21
  2. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:45
  3. problem with the back ground image
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2006, 21:34
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19: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.