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!