Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Png over jpeg (water mark effect) bad quality?

Hi i have two Writablebitmap , one from jpg and another from png and use this method to mix color in a loop:

private static Color Mix(Color from, Color to, float percent)
{
    float amountFrom = 1.0f - percent;
    return Color.FromArgb(
        (byte)(from.A * amountFrom + to.A * percent),
        (byte)(from.R * amountFrom + to.R * percent),
        (byte)(from.G * amountFrom + to.G * percent),
        (byte)(from.B * amountFrom + to.B * percent));
}

My problem is in alpha channel, my watermark effect result is bad (quality)!

Result

This is the original png.

Original Pgn

This is the original jpg.

Original Jpg

Any Help?

like image 539
JoeLoco Avatar asked Nov 01 '11 20:11

JoeLoco


1 Answers

In this case you probably don't want the result to take on any alpha from the watermark, you want it to retain 100% of the opacity of the JPEG. Instead of setting the new alpha to from.A * amountFrom + to.A * percent, just use from.A.

Edit: In addition you want the percent to be adjusted according to the alpha of the PNG. Here's your sample, updated:

private static Color Mix(Color from, Color to, float percent) 
{
    float amountTo = percent * to.A / 255.0;
    float amountFrom = 1.0f - amountTo; 
    return Color.FromArgb( 
        from.A, 
        (byte)(from.R * amountFrom + to.R * amountTo), 
        (byte)(from.G * amountFrom + to.G * amountTo), 
        (byte)(from.B * amountFrom + to.B * amountTo)); 
}

I converted this code to Python and ran your sample images through it with 0.5 percent, here's the result:

enter image description here

like image 63
Mark Ransom Avatar answered Oct 11 '22 20:10

Mark Ransom