Search code examples
cimage-processinglanguage-agnosticalphafreetype

Blending text, rendered by FreeType in color and alpha


I am using FreeType to render some texts.

The surface where I want to draw the text is a bitmap image with format ARGB, pre-multiplied alpha.

The needed color of the text is also ARGB.

The rendered FT_Bitmap has format FT_PIXEL_MODE_LCD - it is as the text is rendered with white color on black background, with sub-pixel antialiasing.

So, for every pixel I have 3 numbers:

Da, Dr, Dg, Db - destination pixel ARGB (the background image).

Fr, Fg, Fb - FreeType rendered pixel (FT_Bitmap rendered with FT_RENDER_MODE_LCD)

Ca, Cr, Cg, Cb - The color of the text I want to use.

So, the question: How to properly combine these 3 numbers in order to get the result bitmap pixel.

The theoretical answers are OK and even better than code samples.


Solution

  • After some discovery, I found the right answer. It is disappointing.

    It is impossible to draw subpixel rendered graphics (including fonts) on a transparent image with RGBA format.

    In order to properly render such graphics, a format that supports separate alpha channels for every color is mandatory.

    For example 48 bit per pixes: RrGgBg where r, g and b are the alpha channels for the red, green and blue collor channels respectively.