|
Alpha channel output |
Posted by Randy Pitz on Aug-12-2005 00:29 |
|
Hi,
I'm using the C++ edition of Chart Director. I'm trying to create an image where the background has an alpha channel so it can be blended with a background, but the bar layer uses opaque colors so they are not blended. When I use the setTransparentColor(-1), all colors end up with an alpha channel, but the alpha is always 255 (0xFF), it doesn't seem to be picking up the alpha value from the color(s) I set via setBackground, or addBarLayer. Lastly, I am generating these images to memory and using CreateDIBBitmap before displaying. I am verifying the above behavior by generating a PNG file and using a image processing application to see the pixel values.
Any help would be appreciated! |
Re: Alpha channel output |
Posted by Peter Kwan on Aug-12-2005 01:53 |
|
Hi Randy,
By default, ChartDirector does not keep alpha transparency information when outputting the chart as a PNG image file. It is because most browsers (eg. Microsoft browsers) and many image viewing devices does not support alpha transparency.
The "setTransparentColor" method is used for output single color transparency (not alpha transparency). Most browsers support this kind of transparency.
However, it is possible the chart image generated is not for viewing by browser directly, but for viewing by more professional viewers that do support alpha transparency, or for further image processing. So ChartDirector can be configured to output images with alpha transparency as well. Just do the follows:
c->setTransparentColor(-1);
In this way, the alpha channel will be maintained in the PNG output.
Note that by default, the chart image background is opaque white. You may want to change the background color when creating the chart to Chart::Transparent. For example:
XYChart *c = new XYChart(500, 300, Chart::Transparent);
Regards
Peter Kwan |
Re: Alpha channel output |
Posted by Randy Pitz on Aug-12-2005 02:44 |
|
Peter,
I have done presicely as you say to obtain the alpha channel and play with background colors of my choice. However, the alpha channel "value" produced is always 100% (or 255 or 0xFF). Whereas I'd like the alpha channel value to come from the colors that are configured to draw the various aspects of the chart, such as the bars, lines, areas, etc... The goal I am trying to achieve is to produce a chart that has a background that is able to be blended while the chart "foreground" components will not be blended. I am not intending these images to be viewed from any browser, but from a custom C++ application that I am writing.
Thanks. |
Re: Alpha channel output |
Posted by Peter Kwan on Aug-12-2005 02:59 |
|
Hi Randy,
I have just tried the attached C++ code on my ChartDirector Ver 4.0 and it works normally. I use:
c->setTransparentColor(-1);
and then the output contains true alpha channel.
I have attached both the source code and the resulting image. The background is totally transparent. The green bars are only half transparent. This is verified by using a professional image manipulation program.
Please try the attached code to see if it outputs the same bar chart. If you are using the same code, but outputs a bar chart without any alpha transparency, would you mind to verify if you are using ChartDirector Ver 4.0 or an earlier version?
Regards
Peter Kwan
|
simplebar.cpp |
---|
#include "chartdir.h"
int main(int argc, char *argv[])
{
//The data for the bar chart
double data[] = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
const char *labels[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};
//Create a XYChart object of size 250 x 250 pixels
XYChart *c = new XYChart(250, 250, Chart::Transparent);
//Set the plotarea at (30, 20) and of size 200 x 200 pixels
c->setPlotArea(30, 20, 200, 200);
//Add a bar chart layer using the given data
c->addBarLayer(DoubleArray(data, sizeof(data)/sizeof(data[0])), 0x8033ff33);
//Set the labels on the x axis.
c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));
c->setTransparentColor(-1);
//output the chart
c->makeChart("simplebar.png");
//free up resources
delete c;
return 0;
}
|
| |
Re: Alpha channel output |
Posted by Randy Pitz on Aug-12-2005 05:05 |
|
I think I realize my problem. In your example you are creating the "simplebar.png" file. I want to do the following instead:
MemBlock m = m_pChart->makeChart(BMP);
HBITMAP b = CreateDIBitmap(...);
// display the bitmap to an HDC...
And probably the BMP format is not picking up the alpha channel. Do you have an example that allows me to show the chart in this way? |
Re: Alpha channel output |
Posted by Peter Kwan on Aug-13-2005 05:19 |
|
Hi Randy,
Unluckily, ChartDirector currently does not output alpha channel in BMP, as it is not commonly used in practice.
For your case, if you want to create a chart with a transparent background (like the single color transparency used in web pages), you may not need to use alpha channel. (Alpha channel is only need if part of the image is "semi-transparent". If a pixel is either fully transparent or full opaque, then single color transparency should be OK.) May be you can consider to use the Win32 API TransparentBlk, which can treat one of the colors you specified (typically the background color) as transparent.
Regards
Peter Kwan |
Re: Alpha channel output |
Posted by Muhammad Raza on Jan-12-2012 19:19 |
|
Thanks Peter Kwan, you really saved my day
Regards,
Raza. |
|