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
 Incorrect resampling of images with alpha channel?
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

soulmonster

Germany
4 Posts

Posted - Apr 02 2012 :  03:43:15  Show Profile  Reply
When I resize a png image with transparent parts, the resizing of the
alphachannel produces grey pixels at the borders of the transparent areas.

I uploaded samples:

www.motionphotos.de/download/IE_Original_Image_WithAlpha.png
www.motionphotos.de/download/IE_Resized_with_IE.png
www.motionphotos.de/download/IE_Resized_with_Photoshop.png

In Photoshop the resizing is done correctly.

Do you have a solution, this is very important to me.

Thank you very much!


Arnd

fab

1310 Posts

Posted - Apr 03 2012 :  02:52:13  Show Profile  Reply
For performance reasons resampling algorithm takes account also of transparent areas (which are black). You could avoid the gray pixels setting transparent areas to white, for example:
ImageEnView1.IO.LoadFromFile('IE_Original_Image_WithAlpha.png');
ImageEnView1.SetAlphaRangePixelsColor(0, 0, CreateRGB(255,255,255));
ImageEnView1.Proc.Resample(240, 360, rfTriangle);
ImageEnView1.IO.SaveToFile('IE_Resized_with_IE2.png');


CreateRGB is defined in imageenproc unit.
Go to Top of Page

soulmonster

Germany
4 Posts

Posted - Apr 03 2012 :  05:02:08  Show Profile  Reply
Thank you, this works.

I don't use TImageEnView in my image processing unit. I took a look in your source code: The SetAlphaRangePixelsColor procedure manipulates pixels in the IEBitmaps, so is there any reason that SetAlphaRangePixelsColor is a proc of TImageEnView? Can I cut and paste the SetAlphaRangePixelsColor proc in my own code to manipulate my TIEBitmaps? This seems easier as changing my code to use TImageEnView.

I tried to understand why this solution works, but I can't get it...
Can you explain to me, why it works and why it is not the default way?

Thank you very much for your help!

Arnd
Go to Top of Page

fab

1310 Posts

Posted - Apr 03 2012 :  23:46:22  Show Profile  Reply
The code of SetAlphaRangePixelsColor is very simple, so it is possible to extract it from TImageEnView and apply directly to a TIEBitmap object:

procedure SetAlphaRangePixelsColor(Bitmap:TIEBitmap);
var
  col, row: integer;
  px: PRGB;
  al: pbyte;
begin
  for row := 0 to Bitmap.Height - 1 do
  begin
    px := Bitmap.Scanline[row];
    al := Bitmap.AlphaChannel.ScanLine[row];
    for col := 0 to Bitmap.Width - 1 do
    begin
      if al^ = 0 then
        px^ := CreateRGB(255, 255, 255);  // set White when alpha is 0 (transparent pixel)
      inc(px);
      inc(al);
    end;
  end;
end;


quote:
Can you explain to me, why it works and why it is not the default way?


Load the original image using TImageEnView (ImageEnView1.IO.LoadFromFile('input.png')). Now set ImageEnView.EnableAlphaChannel=false.
You will see how actually are "transparent" pixels near the border: they are "black".
Sub-resampling does an average (in the simplest algorithm) of a group of pixels, included transparent/black pixels. This is because pixels that should be white, become gray.
Go to Top of Page

soulmonster

Germany
4 Posts

Posted - Apr 22 2012 :  23:11:57  Show Profile  Reply
Thank you, this works with transparent PNGs perfectly.

It doesn't seem to work, if my Alpha Channel is in a separate file:

ImageIO.LoadFromFile(ImageFilename);
AlphaIO.LoadFromFile(AlphaChannelFilename);
IEAlphaBmp.SwitchTo(ImageIO.AttachedIEBitmap.AlphaChannel);

ImageFile is a jpg photo and the AlphaFile is a jpg greyscale mask.

Any idea?

Arnd
Go to Top of Page

fab

1310 Posts

Posted - Apr 22 2012 :  23:32:42  Show Profile  Reply
Please could you send both files for tests?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: