Photoshop, GIMP and other professional Photo Editors are capable to convert one color to a alpha color. You choose a color and all same colors of pixels are goes to alpha color which is transparent. These kind of images are good to blend in front o another background of images. You can also add glamorous effects, like gloom, shadows etc. on those images.
In this post we will present to how to modify bitmap color to alpha colors. Before this we highly recommend you to read Colors In Modern C++ For Windows Development and Learn About Bitmap Operations In C++ Builder (FireMonkey) posts. These will help you get more information about ARGB color format and Bitmap Operations.
In C++ Builder, FireMonkey (FMX) framework offers great and faster operations on bitmaps. To read or write pixels of a bitmap first you must obtain its Bitmap Data (TBitmapData). Then you can get color of a pixel at a x,y coordinates as below;
1 2 3 |
TColor color = bmpData.GetPixel(x, y); |
You can also set color of a pixel at that x,y coordinate as below;
1 2 3 |
bmpData.SetPixel( x, y, claWhite); |
If you want to set to full transparent color (alpha background) , you must set color to 0x0 (actually it is 0x00000000 in ARGB format) as below
1 2 3 |
bmpData.SetPixel( x, y, 0x00); |
Now let’s see full function that converts a color in bitmap to bitmap with alpha color.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void set_color_alpha( TBitmap *bmp, TAlphaColorRec acolor) { TBitmapData bmpData; TAlphaColorRec acr; if( bmp->Map( TMapAccess::ReadWrite , bmpData)) // Lock bitmap and retrive bitmap data { for (int y=0; y<bmp->Height; y++) { for (int x=0; x<bmp->Width; x++) { acr.Color = bmpData.GetPixel(x, y); if(acr.Color==acolor.Color) bmpData.SetPixel(x,y, 0x00000000); } } bmp->Unmap(bmpData); } } |
This C++ Builder Firemonkey Console Application below reads a bitmap and converts white color pixels to alpha color pixels and saves bitmap to another file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <fmx.h> #pragma hdrstop #pragma argsused #ifdef _WIN32 #include <tchar.h> #else typedef char _TCHAR; #define _tmain main #endif #include <stdio.h> void set_color_alpha( TBitmap *bmp, TAlphaColorRec acolor) { TBitmapData bmpData; TAlphaColorRec acr; if( bmp->Map( TMapAccess::ReadWrite , bmpData)) // Lock bitmap and retrive bitmap data { for (int y=0; y<bmp->Height; y++) { for (int x=0; x<bmp->Width; x++) { acr.Color = bmpData.GetPixel(x, y); if(acr.Color==acolor.Color) bmpData.SetPixel(x,y, 0x00000000); } } bmp->Unmap(bmpData); } } int _tmain(int argc, _TCHAR* argv[]) { TBitmap *bmp = new TBitmap(); bmp->LoadFromFile("D:\\testx.bmp"); set_color_alpha(bmp, claWhite); // set all white pixels to alpha bmp->SaveToFile("D:\\test_alpha.png"); //output should be PNG to display alpha colors bmp->Free(); return 0; } |
Find out more about developing Windows apps with C++Builder on Embarcadero’s website!