Results 1 to 2 of 2

Thread: How it create a true alpha mask from a QImage?

  1. #1
    Join Date
    Aug 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default How it create a true alpha mask from a QImage?

    Hello, I've been trying to create a simple alpha mask from a QImage using CreateAlphaMask but for some reason I can't get the mask to work correctly. I only want the simplest mask possible, if there is any alpha at all then set the bit for that pixel in the alpha mask to be 1.

    My problem with QImage::CreateAlphaMask is that no matter what Qt::ImageConversionFlags I try to use, it seems to not detect pixels with only a slight alpha.

    Here's a list of the flags I've tried already:

    Qt Code:
    1. Image.createAlphaMask(); // Qt::AutoColor | Qt::DiffuseDither
    2. Image.createAlphaMask( Qt::ThresholdAlphaDither );
    3. Image.createAlphaMask( Qt::DiffuseAlphaDither );
    4. Image.createAlphaMask( Qt::AvoidDither );
    5. Image.createAlphaMask( Qt::NoOpaqueDetection );
    To copy to clipboard, switch view to plain text mode 

    Is Qt designed to do what I want to do? or should I loop through the pixels myself and find the true alpha mask? It seems what I need is to use "Qt::ThresholdAlphaDither" but be able to set the threshhold to 0.
    Last edited by nokkie; 3rd May 2010 at 18:26.

  2. #2
    Join Date
    Aug 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How it create a true alpha mask from a QImage?

    I just wrote my own, if anyone knows how to have qt do this internally or a better way to do this let me know, here's the code in case anyone else is having the same problem:

    Qt Code:
    1. QImage CreateAlphaMask( const QImage& srcImage )
    2. {
    3. // QImage one only creates alpha mask for alpha values greater than 128... need it to be all alpha greater than 0
    4.  
    5. int nWidth = m_PatchSetImage.width();
    6. int nHeight = m_PatchSetImage.height();
    7. int nDepth = m_PatchSetImage.depth();
    8.  
    9. QImage alphaMask( nWidth, nHeight, QImage::Format_MonoLSB );
    10. alphaMask.setColor( 0, 0xffffffff );
    11. alphaMask.setColor( 1, 0xff000000 );
    12.  
    13. // initialize everything to 0 first
    14. memset( alphaMask.bits(), 0, alphaMask.numBytes() );
    15.  
    16. switch ( nDepth )
    17. {
    18. case 32:
    19. for ( int y = 0; y < nHeight; ++y )
    20. {
    21. unsigned char* pMaskData = alphaMask.scanLine( y );
    22. unsigned int* pSrcData = (unsigned int*)srcImage.scanLine( y );
    23.  
    24. for ( int x = 0; x < nWidth; ++x )
    25. {
    26. if ( *pSrcData++ >> 24 )
    27. {
    28. pMaskData[x/8] |= 1 << (x % 8);
    29. }
    30. }
    31. }
    32. break;
    33. default:
    34. // Set mask to all 1's if not 32 bit depth
    35. memset( alphaMask.bits(), 0xFF, alphaMask.numBytes() );
    36. break;
    37. }
    38.  
    39. return alphaMask;
    40. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 5
    Last Post: 17th May 2009, 20:02
  2. Accessing alpha channel of QImage directly
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2009, 16:24
  3. QImage setPixel does not set alpha value
    By sanjayshelke in forum Qt Programming
    Replies: 7
    Last Post: 20th February 2009, 10:15
  4. How can I create a transparent image with QImage?
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 15:06
  5. [SOLVED] QImage setPixel alpha issue
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2008, 15:06

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.