PDA

View Full Version : How to add alpha blending



coolmib
24-04-2006, 04:26 AM
Hi..
I want to Create a tool to view a custom image file. the problem is
I don't know how to implement the alpha value into the Pixel
Here some part of my code :


type tPalleteColor = record
red : byte;
green : byte;
blue : byte;
alpha : byte;
end;

type tTilePAL = record
TileType : Char;
index : byte;
TilePal: array of array [0..255] of tPalleteColor;
end;

var

PalPub,PalA, PalC : tTilePAL;
BitMap:TBitmap;

bitmap.Canvas.Pixels[k,j] := RGB(SetPal.TilePal[indexofPal, value].blue, SetPal.TilePal[indexofPal, value].green, SetPal.TilePal[indexofPal, value].red)

anyone can give me a hint?
So far I don't want to use any 3rd party component

cronodragon
24-04-2006, 05:35 AM
You have to apply a function like this one to each component of the pixels:

Component(Destination, Source, Alpha) = Source+(Alpha*(Destination-Source))/255

That is for an alpha value from 0 to 255. If you have a floating point alpha from 0.0 to 1.0, the function would be:

Component(Destination, Source, Alpha) = Source+Alpha*(Destination-Source)

In Pascal it could be something like:

Dst.R := Src.R+(Src.A*(Dst.R-Src.R))/255;
Dst.G := Src.G+(Src.A*(Dst.G-Src.G))/255;
Dst.B := Src.B+(Src.A*(Dst.B-Src.B))/255;

Dst is the pixel in the canvas, and Src is the pixel in the image. I'm not sure if you will need some typecasts to make it work correctly.

coolmib
24-04-2006, 09:20 AM
Hi CronoDragon,
I try the method above, but it still not match with the Real Picture (color).
Maybe I'm just a noob in graphics thing and missing some important part in here.
Okay start from a simple one, If I already got the information about RGB plus Alpha value for one pixel.
Now how to put/draw all the information using canvas.pixel (ignore the Background color) ?

I try a few method below:

1. tmp.R:=Src.R+Src.A*(255-Src.R)/255;

2. tmp.R:=Src.R+Src.A*Src.R/255;

3. tmp.R:=Src.R+(Src.A Xor $FF)*(255-Src.R)/255;

4. tmp.R:=Src.R+(Src.A Xor $FF)*Src.R/255;

5. swap RGB to BGR for all method above

then using RGB command to draw the pixel.
But still can't found the correct one

Any other trick ??

cronodragon
24-04-2006, 01:14 PM
bitmap.Canvas.Pixels[k, j] := RGB(SetPal.TilePal[indexofPal, value].blue, SetPal.TilePal[indexofPal, value].green, SetPal.TilePal[indexofPal, value].red)

I see in your fisrt code you use a pallete. Maybe that's the problem. If so, you will need to make a function that approximates the final color to the result of the function.

User137
24-04-2006, 11:36 PM
These usually open up simply by testing. Are you sure your source color is ok? try plotting the src rgb on top of background and it should look the same as it was. Then same test for dest rgb and apply alpha...


SrcR:=GetRValue(bitmap.Canvas.Pixels[k,j]);
SrcG:=GetGValue(bitmap.Canvas.Pixels[k,j]);
SrcB:=GetBValue(bitmap.Canvas.Pixels[k,j]);

coolmib
25-04-2006, 02:29 AM
@Cronodragon : I create one Pic for each pallate (in array) to find the correct
index of pallete, but no one is match.

@user137 : Will try it later.

Anyway here is the screenshot maybe can help

http://nexia.delphi-id.org/screenshotmapviewer.jpg

cronodragon
25-04-2006, 02:40 AM
I create one Pic for each pallate (in array) to find the correct
index of pallete, but no one is match.

Yes, that's because you are using a small pallete of 256 colors. That's ok, but to find the more appropiate color, you will need to create a function that finds the color in the pallete that is nearest to the resulting RGB with Alpha... and it will never be the right color, just an aproximation. Another way around is that you convert every image from 8 bit (palleted) to 24 bits (RGB) or 32 bits (ARGB), in that way the resulting color of the alpha melting will be more aproximate to the right color.