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

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Custom Transparency Handling

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
PeterPanino Posted - Jun 02 2024 : 11:48:32
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;
1   L A T E S T    R E P L I E S    (Newest First)
xequte Posted - Jun 02 2024 : 17:19:48
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