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
 Detect number of transparent pixels in image?
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

PeterPanino

864 Posts

Posted - Dec 04 2021 :  18:35:28  Show Profile  Reply
Is there a FAST built-in function in TImageEnView to detect how many pixels inside an image are (more or less) transparent? (So I could calculate the percentage of transparent areas in the image).

xequte

38219 Posts

Posted - Dec 10 2021 :  00:05:30  Show Profile  Reply
There is not, but the code would be as follows:

var
  px: pbyte;
  y, x, l: integer;
  alphaCount, denom: Integer;
  bmp: TIEBitmap;
begin
  bmp := ImageEnView1.IEBitmap;

  alphaCount := 0;
  if bmp.HasAlphaChannel() then
  begin
    l := IEBitmapRowLen( bmp.AlphaChannel.Width, bmp.AlphaChannel.BitCount, 8);
    for y := 0 to bmp.AlphaChannel.Height - 1 do
    begin
      px := bmp.AlphaChannel.Scanline[y];
      for x := 0 to l - 1 do
      begin
        if px^ < 255 then
          Inc( alphaCount );
        inc(px);
      end;
    end;
  end;

  denom := bmp.Width * bmp.Height;
  ShowMessage( format( 'Image has %d%% transparent pixels', [ Round( alphaCount / denom * 100 )]));
end;


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

xequte

38219 Posts

Posted - Dec 19 2021 :  22:56:42  Show Profile  Reply
In v10.3.1 there will be a new method, TImageEnProc.CalcImageTransparency

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