ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Custom Transparency Handling
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

PeterPanino

901 Posts

Posted - Jun 02 2024 :  11:48:32  Show Profile  Reply
I have created the following methods for custom transparency handling:

1. HighlightSelectedColor(Color: TColor):
This method makes all pixels in a TImageEnView transparent except the passed color, effectively highlighting the passed color in the image while hiding all other colors

2. ResetColorsOpaque:
This method resets all colors in the image to opaque again

My question is: Are there built-in methods in ImageEn to simplify or optimize these methods?

procedure TForm1.HighlightSelectedColor(Color: TColor);
var
  x, y: Integer;
  IEBitmap: TIEBitmap;
  AlphaBitmap: TIEBitmap;
  RGBColor: TColor;
  PixelColor: TRGB;
  Red, Green, Blue: Byte;
  AlphaScanLine: PByteArray;
begin
  IEBitmap := ImageEnView1.IEBitmap; // Reference to the ImageEnView bitmap

  // Ensure the bitmap has an alpha channel
  if not IEBitmap.HasAlphaChannel then
    IEBitmap.AlphaChannel; // Accessing AlphaChannel property will create it if it doesn't exist

  AlphaBitmap := IEBitmap.AlphaChannel; // Reference the alpha channel bitmap

  RGBColor := ColorToRGB(Color);

  // Extract the RGB components from the selected color
  Red := GetRValue(RGBColor);
  Green := GetGValue(RGBColor);
  Blue := GetBValue(RGBColor);

  // Process each pixel to make non-selected colors transparent
  for y := 0 to IEBitmap.Height - 1 do
  begin
    AlphaScanLine := AlphaBitmap.ScanLine[y];
    for x := 0 to IEBitmap.Width - 1 do
    begin
      // Access the pixel color as TRGB
      PixelColor := IEBitmap.Pixels_ie24RGB[x, y];

      // Compare each color component
      if (PixelColor.r <> Red) or (PixelColor.g <> Green) or (PixelColor.b <> Blue) then
      begin
        AlphaScanLine[x] := 0; // Make pixel fully transparent
      end
      else
      begin
        AlphaScanLine[x] := 255; // Make pixel fully opaque
      end;
    end;
  end;

  // Ensure the alpha channel is in sync
  IEBitmap.SyncAlphaChannel;

  // Update the ImageEnView
  ImageEnView1.Update;
end;

procedure TForm1.ResetColorsOpaque;
var
  x, y: Integer;
  IEBitmap: TIEBitmap;
  AlphaBitmap: TIEBitmap;
  AlphaScanLine: PByteArray;
begin
  IEBitmap := ImageEnView1.IEBitmap; // Reference to the ImageEnView bitmap

  // Ensure the bitmap has an alpha channel
  if not IEBitmap.HasAlphaChannel then
    IEBitmap.AlphaChannel; // Accessing AlphaChannel property will create it if it doesn't exist

  AlphaBitmap := IEBitmap.AlphaChannel; // Reference the alpha channel bitmap

  // Process each pixel to make all colors fully opaque
  for y := 0 to IEBitmap.Height - 1 do
  begin
    AlphaScanLine := AlphaBitmap.ScanLine[y];
    for x := 0 to IEBitmap.Width - 1 do
    begin
      AlphaScanLine[x] := 255; // Make pixel fully opaque
    end;
  end;

  // Ensure the alpha channel is in sync
  IEBitmap.SyncAlphaChannel;

  // Update the ImageEnView
  ImageEnView1.Update;
end;

xequte

38418 Posts

Posted - Jun 02 2024 :  17:19:48  Show Profile  Reply
Hi Peter

To set the alpha based on a color use:

http://www.imageen.com/help/TIEBitmap.SetTransparentColors.html


To set the entire alpha channel use:

http://www.imageen.com/help/TIEBitmap.AlphaFill.html


Also note that TIEBitmap.AlphaChannel is itself a TIEBitmap, so any operation that you can perform on a TIEBitmap, you can also perform on the AlphaChannel (with the only reminder being that the image TIEBitmap can be any pixel format, but most commonly ie24RGB, whereas the TIEBitmap.AlphaChannel will be ie8g).

Nigel
Xequte Software
www.imageen.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: