The Allegro Wiki is migrating to github at https://github.com/liballeg/allegro_wiki/wiki

Alpha channel

From Allegro Wiki
Jump to: navigation, search

This article has been proposed for deletion.

  • Reason: Not really a useful article in current form, and I don't think that this is in the scope of the Allegro Wiki. An article on translucent drawing would be more appropriate.
  • Marked by: CGamesPlay 13:41, February 15, 2008 (UTC)

This article may be removed if the voting approves the decision. To discuss the deletion of this page, use Talk:Alpha channel.


What is an alpha channel?

Bitmaps are represented as an array of pixels, representing colors. 8 bit bitmaps are indexes to a palette. Palettes do not have alpha channels and so 8-bit bitmaps do not either. 15, 16 and 24 bit bitmaps use all their space (except for 1 spare bit in 15 bits) for red, green and blue, and so none of these have alpha channels.

32-bit bitmaps use 8 bits each for red, green and blue. This leaves 8 bits which may optionally be used for alpha. Programmers are allowed to interpret alpha channel whatever they like. There is no strict rule saying what it does. It is usually ignored, because 32 bit bitmaps are used for performance reasons in preference to 24 bit ones.

Drawing to the alpha channel in Allegro

First you must call set_write_alpha_blender() which enables the special alpha channel editing mode. Then you need to either

  • Use drawing_mode( DRAW_MODE_TRANS, NULL, 0, 0 ); and then draw primitives (e.g. putpixel) using colors 0-255 to a 32-bit bitmap.
  • Use draw_trans_sprite() to write an 8-bit bitmap into the alpha channel of a 32-bit bitmap.

TODO: Examples

Saving bitmaps with alpha channels

TODO: Explain which file formats available to save_bitmap() are capable of preserving the alpha channel.

BMP

Unknown status, does Allegro support 32 bit BMP yet?

PCX

An 8 bit or 24 bit format. It cannot preserve the alpha channel.

TGA

An 8, 15, 24 or 32 bit format. It will preserve the alpha channel.


Alpha in color blending

A common use of a alpha channel in color blending is the indicator of pixel transparency. In allegro, a value of 0 indicates fully transparent (invisible) pixel and a value of 255 a fully opaque pixel. The interpolation is linear so a value of 127 indicates semitransparent pixel. Function that interprets alpha channel in such way is draw_trans_sprite() when set_alpha_blender() is called.