C++Builder Learn To Create Alpha Color Bitmap From A Bitmap By A Given Color In Modern C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn To Create Alpha Color Bitmap From A Bitmap By A Given Color In Modern C++
February 23, 2021 By Yilmaz Yoru

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 Для просмотра ссылки Войди или Зарегистрируйся and Для просмотра ссылки Войди или Зарегистрируйся 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 (Для просмотра ссылки Войди или Зарегистрируйся). Then you can get color of a pixel at a x,y coordinates as below:
C++:
TColor color = bmpData.GetPixel(x, y);
You can also set color of a pixel at that x,y coordinate as below:
C++:
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:
C++:
bmpData.SetPixel( x, y, 0x00);
Now let’s see full function that converts a color in bitmap to bitmap with alpha color.
C++:
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.
C++:
#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;
}