[Interest] Composition mode help
Simon Gornall
simon.gornall at mac.com
Wed Sep 4 05:55:26 CEST 2013
[I posted this on the forum, but it was suggested I try this mailing list :-]
I’m writing a driver for a graphics system so it can be ported to X, and QT seemed to help out in a lot of ways, but I can’t get my head around how to set the composition mode to get the various effects that the old system could do.
The old system can do:
Replace – All pixels from source replace corresponding pixels in destination – this I think is QPainter::CompositionMode_Source
Transparent – Only non-black (ie: 0) pixels from source will replace corresponding pixels in destination. I can’t find a corresponding mode for this.
XOR – standard XOR, I can do this with QPainter::RasterOp_SourceXorDestination
Reverse transparent – Only the black pixels (ie: 0) from the source will replace the corresponding pixels in destination. The pixels will be coloured as the current foreground colour.
I can’t really see how to do (2) and (4) without creating QImages to get masks and playing with alpha values and doing multiple blits. The old system doesn’t have alpha, so all the colours it draws effectively have alpha=1. Any help gratefully received :)
Simon
... to give you an idea of how I'm currently testing this, I'm trying:
#include "widget.h"
/******************************************************************************\
|* Handles the painting for the VDI functions
\******************************************************************************/
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
_pixmap = NULL;
_painter = NULL;
}
/******************************************************************************\
|* Paint the screen
\******************************************************************************/
void Widget::paintEvent(QPaintEvent *e)
{
if (_painter && _pixmap)
{
QPainter p(this);
p.drawPixmap(e->rect(), *_pixmap, e->rect());
}
else
{
QPainter painter(this);
painter.fillRect(rect(), Qt::black);
}
}
/******************************************************************************\
|* Handle the size changing
\******************************************************************************/
void Widget::resizeEvent(QResizeEvent *e)
{
if (_pixmap)
delete _pixmap;
if (_painter)
delete _painter;
int W = e->size().width();
int H = e->size().height();
fprintf(stderr, "resizing to %d,%d\n", W, H);
_pixmap = new QPixmap(W, H);
_painter = new QPainter(_pixmap);
// Fill with a solid background of grey
_painter->fillRect(QRect(0, 0, W, H),QBrush(QColor(128,128,128,255)));
// Create a pixmap to blit
QPixmap toBlit(50,50);
QPainter tbp(&toBlit);
QColor black(0,0,0,255);
tbp.fillRect(QRect(0,0,50,50), QBrush(black));
// Put a white square in the middle of toBlit
tbp.fillRect(QRect(10,10,20,20), QBrush(QColor(255,255,255,255)));
// Make a mask from 'toBlit' and multiply values using tbp
QBitmap mask = toBlit.createMaskFromColor(black,Qt::MaskOutColor);
mask.save("/tmp/test.pbm");
tbp.setClipRegion(QRegion(mask));
tbp.fillRect(QRect(0,0,50,50), QBrush(Qt::transparent));
toBlit.save("/tmp/test.png");
// Set the composition mode and blit
_painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
_painter->drawPixmap(30, 30, toBlit);
// redraw
update();
}
What I was trying to do with the above was case 2, where I'd only see the white square in the middle and not the black surrounds, but /tmp/test/png always has alpha=1 everywhere :(
Of course, if it's possible to do it just by setting a composition mode that'd be much better - there's lots of drawing primitives, and if I can avoid having code for each of them I'd much prefer doing that...
Cheers
Simon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130903/c1ad9de9/attachment.html>
More information about the Interest
mailing list