Qt-interest Archive, March 2002
Weird alpha issues
Message 1 in thread
I'm writing a function that will draw an alpha
gradient on a QImage. (That is, a gradient thatt goes
from a color to transparent) However, I'm finding
some weird results. When I display the image, half of
it is solid grey, the other half is the original image
with no transparency. However, if I save the image
and view it in Gimp or another editing program, I can
see the transparency applied. For example, I drew an
image with a solid red background. When I applied the
alpha gradient and displayed the image, the left half
was grey, the right half was red. But, when I viewed
it with Gimp, I saw it gradually go from transparent
to red.
Any thoughts on what could be causing me to see this?
Is it something with my X server not being setup
properly? I'd really love any input on this one. I'm
rather stumped!!
Thanks!
SN
__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/
Message 2 in thread
Steven Nakhla, Donnerstag, 14. März 2002 16:40:
> I'm writing a function that will draw an alpha
> gradient on a QImage. (That is, a gradient thatt goes
>
> >from a color to transparent) However, I'm finding
>
> some weird results. When I display the image, half of
> it is solid grey, the other half is the original image
> with no transparency. However, if I save the image
> and view it in Gimp or another editing program, I can
> see the transparency applied. For example, I drew an
> image with a solid red background. When I applied the
> alpha gradient and displayed the image, the left half
> was grey, the right half was red. But, when I viewed
> it with Gimp, I saw it gradually go from transparent
> to red.
>
> Any thoughts on what could be causing me to see this?
> Is it something with my X server not being setup
> properly? I'd really love any input on this one. I'm
> rather stumped!!
At least on Qt-x11 (don't know about qt-embedded), converting an image with
an alpha channel to a pixmap will result in a pixmap with a mask (this
conversion also takes place, if you e.g. use QPainter::drawImage()).
If you're looking for "real" alpha blending, you can merge your image with
the background. Then you should *disable* the alpha channel in the merged
image and draw it (or convert to a pixmap...).
I don't know, whether that's the easiest solution, but it works at least.
E.g. to merge your image with a white background:
QImage im("bla.png");
QRgb rgb_b = QColor(255,255,255).rgb();
QRgb rgb;
int alpha,inv_alpha;
if(im.hasAlphaBuffer())
{
for(y=0;y<im.height();y++)
{
for(x=0;x<im.width();x++)
{
rgb = im.pixel(x,y);
inv_alpha = 255 - qAlpha(rgb);
alpha = qAlpha(rgb);
im.setPixel(x,y,
qRgb(qRed(rgb_b)*inv_alpha/255 + qRed(rgb)*alpha/255,
qGreen(rgb_b)*inv_alpha/255 + qGreen(rgb)*alpha/255,
qBlue(rgb_b)*inv_alpha/255 + qBlue(rgb)*alpha/255));
}
}
im.setAlphaBuffer(false);
}
Message 3 in thread
Thanks for the info! I'm at work now, but I'll try it
when I get home.
Another thing I was wondering about was how I could
create a QImage of a certain region of the screen.
For example, if I wanted to make a semi-transparent
QPopupMenu I would grab a QImage of the region of the
screen that my menu would cover, make it semi-opaque
by utilizing my alpha-blending functions, and then
draw the contents of my menu on top of it all. How
would I get a QImage of the contents that would be
covered by my menu? Any ideas?
SN
--- mh <crapsite@gmx.net> wrote:
> Steven Nakhla, Donnerstag, 14. März 2002 16:40:
> > I'm writing a function that will draw an alpha
> > gradient on a QImage. (That is, a gradient thatt
> goes
> >
> > >from a color to transparent) However, I'm
> finding
> >
> > some weird results. When I display the image,
> half of
> > it is solid grey, the other half is the original
> image
> > with no transparency. However, if I save the
> image
> > and view it in Gimp or another editing program, I
> can
> > see the transparency applied. For example, I drew
> an
> > image with a solid red background. When I applied
> the
> > alpha gradient and displayed the image, the left
> half
> > was grey, the right half was red. But, when I
> viewed
> > it with Gimp, I saw it gradually go from
> transparent
> > to red.
> >
> > Any thoughts on what could be causing me to see
> this?
> > Is it something with my X server not being setup
> > properly? I'd really love any input on this one.
> I'm
> > rather stumped!!
>
> At least on Qt-x11 (don't know about qt-embedded),
> converting an image with
> an alpha channel to a pixmap will result in a pixmap
> with a mask (this
> conversion also takes place, if you e.g. use
> QPainter::drawImage()).
> If you're looking for "real" alpha blending, you can
> merge your image with
> the background. Then you should *disable* the alpha
> channel in the merged
> image and draw it (or convert to a pixmap...).
> I don't know, whether that's the easiest solution,
> but it works at least.
>
> E.g. to merge your image with a white background:
> QImage im("bla.png");
> QRgb rgb_b = QColor(255,255,255).rgb();
> QRgb rgb;
> int alpha,inv_alpha;
> if(im.hasAlphaBuffer())
> {
> for(y=0;y<im.height();y++)
> {
> for(x=0;x<im.width();x++)
> {
> rgb = im.pixel(x,y);
> inv_alpha = 255 - qAlpha(rgb);
> alpha = qAlpha(rgb);
> im.setPixel(x,y,
>
> qRgb(qRed(rgb_b)*inv_alpha/255 +
> qRed(rgb)*alpha/255,
> qGreen(rgb_b)*inv_alpha/255
> + qGreen(rgb)*alpha/255,
> qBlue(rgb_b)*inv_alpha/255 +
> qBlue(rgb)*alpha/255));
> }
> }
> im.setAlphaBuffer(false);
> }
>
>
> --
> List archive and information:
http://qt-interest.trolltech.com
__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/
Message 4 in thread
Steven Nakhla, Donnerstag, 14. März 2002 19:31:
> Thanks for the info! I'm at work now, but I'll try it
> when I get home.
>
> Another thing I was wondering about was how I could
> create a QImage of a certain region of the screen.
> For example, if I wanted to make a semi-transparent
> QPopupMenu I would grab a QImage of the region of the
> screen that my menu would cover, make it semi-opaque
> by utilizing my alpha-blending functions, and then
> draw the contents of my menu on top of it all. How
> would I get a QImage of the contents that would be
> covered by my menu? Any ideas?
>
I think, you can use something like
QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId(),x,y,w,h);
As far as I know, at least "Mosfets high performance liquid style" supports
transparent menus (KDE2). Maybe you can take a look at the code to get some
inspiration.
Michael