Displaying a Transparent Image

QUESTION: I have a logo in the form of a transparent PNG image
that I would like to display in a graphics window. The image itself is
dimensioned [4,93,93]. I presume this is a 24-bit image with an alpha channel, but I cannot
work out how to display this in IDL direct graphics. (It displays correctly in IDL object graphics,
presumably because images in that system are allowed alpha channels.) Can you help?

ANSWER: You are correct that the extra dimension is an alpha channel. And you are also correct
that without some modifications you will not be able to display this image with the TV command.
However, all is not hopeless yet.

We see this image is a pixel interleaved image with an alpha channel. Images like this are a bit easier
to work with in IDL if they are band interleaved, so I converted it to a band interleaved image first.

Your first thought might be that you could just ignore the alpha channel altogether. This was my first
thought, too. But the result is unsatisfactory. Considerable detail, for example,
is missing from the image. (In the original
image from the person who asked this question, the text in the image was unreadable and blurred.)
You can see the result in the figure below.

IDL> Window, XSIZE=162, YSIZE=150
IDL> TV, image[*,*,0:2], TRUE=3

The image without an alpha channel.

In fact, the alpha channel has information in it that we would like to use. We can have
a look at the alpha channel by itself.

You see here that the alpha channel of the image is byte scaled into the range 0 to 255.
Normally, alpha channels are scaled into the range
of 0 to 1, and we will need to scale it into that range if we wish to use it later on. I do so with
the cgScaleVector command from the
Coyote Library. The alpha channel essentially tells us how
to blend the foreground image (the bird, in this case) with the background image (normally the
pixels on the display where we are going to place the bird image).

What we need to do is get a 24-bit image from the location where we want to
display the bird image. Suppose we wish to put the bird on this image with the lower-left
corner of the image at pixel location [183,338]. Then we could capture a 24-bit image
from this location (this assumes you are using a 24-bit graphics card), like this.

IDL> backgnd = TVRD(183, 338, s[0], s[1], TRUE=3)

And we just display the image normally.

IDL> TV, foregnd*alpha + (1 - alpha) * backgnd, TRUE=3, 183, 338

The result is shown below.

The image on top of another image.

Update

As of May 15th, 2009 cgImage is now written in such a way that it
can display transparent PNG files directly. Just simply pass the transparent PNG file
directly to cgImage.

Update

On October 25th, 2012, I introduced a TRANSPARENT
keyword to cgImage that will allow any
2D or true-color image to be displayed with transparency. Simply set the keyword to the
amount of transparency desired. A MISSING_VALUE can be use to select a color index (in the
case of 2D images) or a particular color (in the case of true-color images) to completely
knock out and make totally transparent.

If I wanted to create this same image in a PostScript file, I would have to
supply the background image with the cgImage command. The code to do so might
look like this. This code will produce a PostScript file, and a PNG file that
is created from the PostScript file. It is the PNG file that is shown in the figure
below the code.