This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Question regarding alpha blending / anti-aliasing


Question regarding alpha blending / anti-aliasing

Postby Avatar » Sep 9, 2001 @ 5:07am

Maybe Mr. Bikker can help me here :)<br><br>I've asked this before, but since I haven't received an answer that I wanted/needed, I will ask it again...<br><br>So I've got this image coming out of a 3d rendering program. It's in a format that supports alpha-channels (TIFF, to be exact).<br><br>I want to use this image in a game environment. Using simple masks (ie logical operations) produced an ugly 'outline' of near-white or to be more exact near-render background pixels. Rendering an image w/out antialiasing produced 'jagged edge' effect.<br><br>So, following suggestions on this board (those that made some sense :p) I rendered to an image format that supports alpha-channels. Now I can open that image (in Photoshop) and view it's alpha channel in all it's glory - nice! But, uh..what now? How do I go from having an image with an alpha channel to alpha blending it w/ some other image? Should I make the a-channel  into a totally seperate image and load both as resources/BITMAP structures? If yes, what then?<br><br>Please help. If anyone can do it, I know you can, Jacco ;)
Avatar
pm Member
 
Posts: 59
Joined: Aug 16, 2001 @ 8:32pm


Re: Question regarding alpha blending / anti-aliasing

Postby Phantom » Sep 9, 2001 @ 5:50am

Stop flathering me. It won't work. :)<br><br>Anyway, about the alpha stuff: Suppose you loaded your alpha mask and your image to two equally sized buffers, and you want to plot pixel (u,v). So you fetch the alpha like this:<br><br>int a = *(alphamap + x + y * image_width);<br><br>and the pixel like this:<br><br>PIXEL p = *(bitmap + x + y * imagewidth);<br><br>Now for an alpha value of zero, you don't want to plot the pixel. For an alpha value of 255 (or whatever the resolution of the alpha values is), you want to replace the pixels that are already in your framebuffer by the pixel 'p'. All values inbetween should cause the pixel to be scaled. Scaling of one color component (in case of a 24bit image) goes like this (with 8-bit alpha mask):<br><br>red = (red * alpha) / 256;<br><br>or, more efficiently:<br><br>red = (red * alpha) >> 8;<br><br>The scaled color has to be added to the pixel that is already in the framebuffer. Together, the frame buffer pixel and the sprite pixel should never exceed '100%', so you also need to scale the pixel in the frame buffer by (255 - a).<br><br>For 16bit values, scaling of colors has been discussed in the general coding forum. It can be done quite efficiently.<br><br>BTW, Fredrik Malmer (RIP) used alpha masks in Argentum. I'm sure he or Carp can help you also.<br><br>- Jacco.
Give me some good data and
I will give you the world
User avatar
Phantom
pm Insider
 
Posts: 913
Joined: Feb 21, 2001 @ 8:14am
Location: Houten, Netherlands


Re: Question regarding alpha blending / anti-aliasing

Postby Avatar » Sep 9, 2001 @ 6:25pm

So how much different would the routines be for 16 bit bitmaps? How about something even simpler than that - 8 bit (256 colours)?
Avatar
pm Member
 
Posts: 59
Joined: Aug 16, 2001 @ 8:32pm


Re: Question regarding alpha blending / anti-aliasing

Postby Phantom » Sep 10, 2001 @ 4:23am

256 indexed color bitmaps rock for alpha blending. Good thing is that you can scale colors very easily; just create (at load time) 16 palettes with 16 intensities of the original palette. Now you can scale the color with a simple lookup with a 4-bit precision. Of course, you'll need to use the 16bit scaling code for the background, since you'll probably want to use a palette per sprite.<br><br>Scaling a color in 16bit mode (copied from the general development forum):<br><br>inline unsigned short scalecolor( unsigned short c, int mul ) <br>{ <br>unsigned int p = ((((c&0xF81F)<<11)|((c&0x7E0)>>5))*mul)>>5; <br>return (((p>>16)&0xF81F)|(p&0x7E0)); <br>}<br><br>This returns the scaled color.<br><br>- Jacco.
Give me some good data and
I will give you the world
User avatar
Phantom
pm Insider
 
Posts: 913
Joined: Feb 21, 2001 @ 8:14am
Location: Houten, Netherlands


Re: Question regarding alpha blending / anti-aliasing

Postby Phantom » Sep 10, 2001 @ 4:24am

WWWWWWWWhy does the first character of each post disappear? I'm very sure I typed '256', I verified using my back button.<br><br>Anyway, it's not a big deal.
Give me some good data and
I will give you the world
User avatar
Phantom
pm Insider
 
Posts: 913
Joined: Feb 21, 2001 @ 8:14am
Location: Houten, Netherlands


Re: Question regarding alpha blending / anti-aliasing

Postby jongjungbu » Sep 10, 2001 @ 8:47am

Only seems to happen with numbers and sometimes it doesn't even happen--such as when i wish to be demonstrative.
User avatar
jongjungbu
Not JongJongBu
 
Posts: 3112
Joined: Jun 19, 2001 @ 4:22am
Location: USA


Re: Question regarding alpha blending / anti-aliasing

Postby Avatar » Sep 10, 2001 @ 7:59pm

Thanks for the code, I'm sure it will come in handy. Here are the next couple of questions ;)<br><br>First, I'm wondering which methods to implement for alpha blending. There's the option of loading two images (sprite and its alpha), scanning the alpha, trying to create a alpha value map on the run and then use it against another bitmap (background) to produce the desired effect. This approach is the most straightforward, if not simplistic. Another way to go about this process would be to pre-process the alpha bitmap and create (before runtime) a table of locations and their alpha values. This would save time since I won't have to scan through the alpha bitmap @ runtime as well reduce the amount of memory used (one less bitmap to load). The problem is creating this table of values, I'm not sure how to go about that.<br>Second, optimisations. I've heard some really good ideas in regards to optimising the alpha-blending code. One of them mentioned doing calculations and transformation of the loaded bitmaps (eg. for sprites) before the actual rendering loop kicks in, as it only leaves you to add the current framebuffer to the background to produce the desired effect (if anyone could elaborate on that, perhaps in a more practical fashion, I would appreciate it). Also, I think it was on your (Jacco) page that I saw some ideas on how to avoid using floating point variables in the calculations to increase the speed. Finally, I've seen some code for 86x asm that performs alpha blending, and was wondering if anyone's knowledgeble enough to port it into ARM asm. I personally only dealt with 86x and Z80 asm and currently am studying MIPS, so ARM is the grey area for me...<br><br>Keep up the good comments, I really appreciate the input!
Avatar
pm Member
 
Posts: 59
Joined: Aug 16, 2001 @ 8:32pm


Return to Phantom's Forum


Sort


Forum Description

Discuss any of Phantom's projects here (Operation Nutcracker, etc.)

Moderators:

sponge, RICoder, Phantom

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum