Results 1 to 3 of 3

Thread: QBitmap color inversion

  1. #1
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QBitmap color inversion

    I have a simple bitmap image (.xpm format, though it could be any grayscale) drawn in some other tool so that black pixels are meant to be transparent and white opaque as follows

    Qt Code:
    1. /* XPM */
    2. static char *hex[]={
    3. "20 16 2 1",
    4. ". c #000000",
    5. "# c #ffffff",
    6. ".....##########.....",
    7. "....############....",
    8. "...##############...",
    9. "...##############...",
    10. "..################..",
    11. "..################..",
    12. ".##################.",
    13. "####################",
    14. "####################",
    15. ".##################.",
    16. "..################..",
    17. "..################..",
    18. "...##############...",
    19. "...##############...",
    20. "....############....",
    21. ".....##########....."};
    To copy to clipboard, switch view to plain text mode 

    First I load the image as QBitmap, then draw to another QPixmap

    Qt Code:
    1. QBitmap bitmap("mono.xpm");
    2.  
    3. QPixmap pixmap(bitmap.size());
    4. pixmap.fill(Qt::green);
    5. QPainter painter(&pixmap);
    6. painter.setPen(Qt::red); // 1-bits in bitmap red
    7. painter.setBackgroundMode(Qt::TransparentMode); // 0-bits in bitmap transparent
    8. painter.drawPixmap(0,0,bitmap);
    To copy to clipboard, switch view to plain text mode 

    Now when I draw the pixmap later, colors are inverted so that what originally was "white" in loaded bitmap is green and "black" is red -- contrary to what I expected.

    Now I did some source reading and found out that function QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags) defined in src/gui/image/qbitmap.cpp (Qt 4.5.2) actually does invert the colors and as far as I can tell is the reason behind this behaviour

    Qt Code:
    1. --snip
    2. // make sure image.color(0) == Qt::color0 (white)
    3. // and image.color(1) == Qt::color1 (black)
    4. const QRgb c0 = QColor(Qt::black).rgb();
    5. const QRgb c1 = QColor(Qt::white).rgb();
    6. if (img.color(0) == c0 && img.color(1) == c1) {
    7. img.invertPixels();
    8. img.setColor(0, c1);
    9. img.setColor(1, c0);
    10. }
    11. --snip>
    To copy to clipboard, switch view to plain text mode 

    So I know of the reason but not of WHY it does that... Is this maybe some axiomatic idea that "white" pixels represent transparent areas in mono images and "black" opaque? I don't do much image processing... but somehow it doesn't seem "right".

    Anyone who might have some clarification to this? Thanks!

  2. #2
    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: QBitmap color inversion

    In general we (humans) consider white to be the natural colour of paper and black to be the "ink" (opaque colour). QBitmap stores bits. Value 0 represents "empty" and value 1 represents "opaque". Black corresponds to 0 (#000000), white corresponds to 1 (#FFFFFF) so if the engine receives an "inverted" picture, it inverts it back so that you can see your "white is paper" despite the bit values really used by QBitmap. In other words the conversion tells the bitmap that although it sees black, it should show white and the other way round.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    jkv (20th July 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QBitmap color inversion

    Thanks! Thinking it as paper/ink makes a lot of sense. And me fool thought 1 is a 1 always

Similar Threads

  1. QBitmap does not scale correctly with QPainter
    By macaco in forum Qt Programming
    Replies: 0
    Last Post: 8th August 2008, 11:40
  2. reading writing qimage / qbitmap to and from memory
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 11:28
  3. how can QPixmap transformation to QBitmap faster
    By duduqq in forum Qt Programming
    Replies: 4
    Last Post: 4th May 2008, 04:44
  4. Does QDrag support QBitmap?
    By alfa_wu in forum Qt Programming
    Replies: 8
    Last Post: 9th May 2007, 02:56

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.