Jump to content

Malfunction - "Paste Alpha"


Recommended Posts

Previous posted at Main Troubleshooting Forum, because it's more than a Plugin-Issue ...

Previous Post

 

Sometimes, "Paste Alpha (... from Clipboard)" creates wrong color/alpha-values!

True to the motto "A picture says more than thousands words" I've created a Image

as PDN-File.

Attached here as ZIP-File (16.5 kB, 200px × 200px).

 

Best regards,

Udo Fuerhoff

 

BoltBatsPack46 installed (same DLL included in ~43)

PasteAlpha.dll -> 4.3.5815.35469

Signature: 00004550

Time/Date stamp (UTC): 56610B9A,2015-12-04 03:42:18

 

 

Application    paint.net v4.0.17 (Final 4.17.6411.1908)
Build Date    Freitag, 21. Juli 2017    
OS    Windows 10 x64 (10.0.10586.0).NET Runtime    4.0.30319.42000

ImageDocPasteAlphaMalFunction 20170807 #02 200x200px_pdn.zip

Link to comment
Share on other sites

The image you have posted is not enough to tell what the problem is. Please state which layers should be enabled to produce the intended result, what is wrong with how it works and why it doesn't produce the result you're looking for, and which layers exhibit the problem. As it stands I see a collection of layers, some of which have Gaussian blurs and some of which have paste alpha applied to them.

Link to comment
Share on other sites

If Paste Alpha has a problem, you should need only two images to demonstrate it: the alpha image, and the image it will be pasted into. Either Paste Alpha leaves the color alone while changing the alpha to appropriate value, or it doesn't. The effect of blending the resultant layer with other layers is a Paint.NET matter, not a Paste Alpha matter. You can even specify coordinates of the alpha-pasted pixel that's wrong. It can be easily checked with the Color Picker. Either the color will be changed from before the Paste Alpha, or the alpha won't be changed to what it should be.

Link to comment
Share on other sites

The math is pretty simple.  Let's take a look at the actual source code from the Paste Alpha plugin:

 

For each pixel: (CurrentPixel holds the current source pixel's color, x and y hold the current address of the pixel we're working on)

if (img != null) // the img variable holds the image on the clipboard
{
    // get the pixel from the clipboard image based on selected location (Amount2) and zoom level (Amount3)
    float px = (float)(((Amount2.First + 1) / 2 * img.Height) + x) * (float)Amount3;
    float py = (float)(((Amount2.Second + 1) / 2 * img.Width) + y) * (float)Amount3;
    if (Amount4) // nearest neighbor calculations?
    {
        px = (float)Math.Round(px);
        py = (float)Math.Round(py);
    }
    NewPixel = img.GetBilinearSampleWrapped(px, py);
}

NewPixel = desaturateOp.Apply(NewPixel); // turn the clipboard pixel black and white

if (Amount1 == 0) // we will be using the clipboard pixel's black and white value for the alpha
{
    CurrentPixel.A = NewPixel.R; // R, G, and B are all the same for black and white pixels
}
else // we will be using the clipboard pixel's alpha value
{
    CurrentPixel.A = NewPixel.A;
}

if (Amount5) // should we invert the alpha?
{
    CurrentPixel.A = (byte)(255 - CurrentPixel.A);
}

If I've made a mistake in my calculations, please let me know where it is and I'll fix it.

 

The only problem that could come up is if there is no image on the clipboard.  In that case, it will take forever and give random results.

 

 

Link to comment
Share on other sites

Quote

BoltBait

Posted Friday at 11:06 AM

...


NewPixel = desaturateOp.Apply(NewPixel); // turn the clipboard pixel black and white

...


if (Amount1 == 0) // we will be using the clipboard pixel's black and white value for the alpha

...

Why do you do that? :roll:

I don't know what the functions/methods do, but it's equal!

I think:

"PasteAlpha" should not use other RGB-Values!?

 

Link to comment
Share on other sites

@fuerudo, I think I know your problem, which is a misunderstanding of what Paste Alpha does. Paste Alpha replaces the alphas in the image with the alphas taken from the clipboard; it doesn't combine the alphas. It doesn't care that an image alpha was originally zero. If the clipboard alpha is 255 (opaque), that's what the new alpha will be. When you pasted the alpha from the green-bar image into the red-bar layer, all the pixels along the green bar became nontransparent. The ones along the green-bar diagonal that were originally transparent showed up as white, because transparent pixels are typically white with an alpha of zero.

 

I wish there were the option to combine the alphas (which would be done by multiplying, then rescaling to the proper range).

  • Upvote 1
Link to comment
Share on other sites

2 hours ago, MJW said:

@fuerudo, I think I know your problem, which is a misunderstanding of what Paste Alpha does. Paste Alpha replaces the alphas in the image with the alphas taken from the clipboard; it doesn't combine the alphas. It doesn't care that an image alpha was originally zero. If the clipboard alpha is 255 (opaque), that's what the new alpha will be. When you pasted the alpha from the green-bar image into the red-bar layer, all the pixels along the green bar became nontransparent. The ones along the green-bar diagonal that were originally transparent showed up as white, because transparent pixels are typically white with an alpha of zero.

 

@MJW is exactly right.  This effect is called "Paste Alpha" not "Combine Alpha".  The previous alpha value of the target image is ignored and simply replaced with the alpha value from the clipboard image.

 

2 hours ago, MJW said:

I wish there were the option to combine the alphas (which would be done by multiplying, then rescaling to the proper range).

 

Give me some code and I'll make it an option.  The simplest way might be to have an option called "Use least opaque alpha" which is a simple comparison.

Link to comment
Share on other sites

I'll be happy to provide the code. I want to see if I can do it in integer arithmetic, so it may take a day or two to refresh my memory on the ins and outs of doing that and getting the best result.

 

There is another idea that's weird but more flexible: allow the option of applying blend modes to alpha. The alpha values could be copied into, say,  the R values of pixels (whose alphas are 255), the op applied, and the result used as alpha. The Clipboard would be the upper-layer pixel, so Normal would result in substitution. Perhaps such an option isn't useful enough to justify the confusion it might cause. On the other hand, it would allow both Multiply and "least opaque" (Darken).

 

That's actually a way Multiply could be done. Just copy the alphas into a pixel color byte, apply the Multiply op, and use the result. I don't know if that's easier than just using integer arithmetic on the alphas, which shouldn't be too difficult.

 

EDIT: After thinking about it, I must admit I'm not sure what the best way to combine alphas is. Perhaps it is "Use least opaque alpha." I know there have been quite a few times when I wished I had an easy way of combining alphas, but off the top of my head, I can't think of exactly what I was doing, and what would have worked best.

Link to comment
Share on other sites

42 minutes ago, MJW said:

After thinking about it, I must admit I'm not sure what the best way to combine alphas is.

 

This is exactly why I'm asking for code.

 

There are many ways to combine alpha (multiply, pick one or the other, add, subtract, etc.) all useful in the right circumstances.

Link to comment
Share on other sites

I'll try to come up with the exact situation, but my guess is that the only things that really matter are that opaque image pixels are forced to have the clipboard alpha, and that all pixels are made transparent if they're transparent in either the image or the clipboard. In which case either multiply or use-least would suffice.

Link to comment
Share on other sites

10 hours ago, MJW said:

@fuerudo, I think I know your problem, which is a misunderstanding of what Paste Alpha does. Paste Alpha replaces the alphas in the image with the alphas taken from the clipboard; it doesn't combine the alphas. It doesn't care that an image alpha was originally zero. If the clipboard alpha is 255 (opaque), that's what the new alpha will be. When you pasted the alpha from the green-bar image into the red-bar layer, all the pixels along the green bar became nontransparent. The ones along the green-bar diagonal that were originally transparent showed up as white, because transparent pixels are typically white with an alpha of zero.

 

I wish there were the option to combine the alphas (which would be done by multiplying, then rescaling to the proper range).

Thanks, but no, that isn't! My examples only for simple demonstration! I think I now what "paste alpha" should be do! ;)

Link to comment
Share on other sites

8 hours ago, BoltBait said:

 

@MJW is exactly right.  This effect is called "Paste Alpha" not "Combine Alpha".  The previous alpha value of the target image is ignored and simply replaced with the alpha value from the clipboard image.

 

 

Give me some code and I'll make it an option.  The simplest way might be to have an option called "Use least opaque alpha" which is a simple comparison.

Thats it!

But,

I expect from tools like "Paste Alpha" a true, pure "paste alpha" function, not combining of any others!

But "PasteAlpha" does more, combining (or use) other pixel-values for alpha!

I'm sorry, in my mind thats the mistake: Transparent areas of images are not "white" or "black" (or near that)!

In this Version, "pastealpha.dll" isn't usable as "true alpha mask"!

 

Best regards,

Udo Fuerhoff

Link to comment
Share on other sites

17 minutes ago, fuerudo said:

But "PasteAlpha" does more, combining (or use) other pixel-values for alpha!

 

No it doesn't.  But, that's what you're asking for.  (You are asking me to consider the alpha level of the original image when applying the alpha specified in the clipboard image--that's a "combine" function of some kind.  Currently, Paste Alpha simply REPLACES the alpha of every pixel in your original image (even completely transparent pixels) with the alpha that exists in the clipboard image without regard to the alpha level that previously existed in your original image.)

 

Link to comment
Share on other sites

2 minutes ago, BoltBait said:

 

No it doesn't.

Quote

10 hours ago, MJW said:

... because transparent pixels are typically white with an alpha of zero. ..

 

... and the code-snippet let suspect the same ...

Okay,

I accept that "pastealpha.dll" isn't  usable as true alpha mask, but, please take a note in the description!

Link to comment
Share on other sites

In your example, you've created an invalid alpha mask because you've specified an alpha value for pixels that don't exist in your target image.

 

The problem is in how you are using the plugin, not in the plugin itself.  I may be able to modify the algorithm to accommodate your misuse of the plugin--but, for that I'll need a good algorithm. 

 

The Paste Alpha plugin is quite useful, when used correctly.

 

Link to comment
Share on other sites

59 minutes ago, fuerudo said:

I accept that "pastealpha.dll" isn't  usable as true alpha mask, but, please take a note in the description!

 

You seem to have some personal definition of what a "true alpha mask" is. I have no idea what you mean by that phrase, and I doubt BoltBait does either. Why don't you tell us what you think a plugin that provides a "true alpha mask" should do? How would it be different from a plugin that sets alpha to the minimum of the image and clipboard alphas?

 

Quote

Transparent areas of images are not "white" or "black" (or near that)!

 

What does that mean? How can they not be some specific color? A pixel is transparent if and only if its alpha is zero, and any pixel whose alpha is zero has to have something in the RGB values, which will become its color if the alpha is changed. If you're saying you don't want transparent areas to be made nontransparent, fine, but that's what using the minimum or product of the alphas would do. (You can't just leave the transparent image pixels unchanged and replace all the rest with the clipboard alphas, since that will produce ugly artifacts when almost-transparent image pixels get replaced by opaque clipboard alphas.)

Link to comment
Share on other sites

@BoltBait, here is a suggestion for how the Paste Alpha Code might work.

 

Spoiler

int alpha;
if (Amount1 == 0) // we will be using the clipboard pixel's black and white value for the clipboard alpha
{
	alpha = NewPixel.R; // R, G, and B are all the same for black and white pixels
}
else // we will be using the clipboard pixel's alpha value
{
	alpha = NewPixel.A;
}

if (Amount5) // should we invert the CB alpha?
{
	alpha = 255 - alpha;
}

if (Amount6 == 1)
{
	// Minimum.
	if (alpha > CurrentPixel.A)
		alpha = CurrentPixel.A;
}
else if (Amount6 == 2)
{
	// Product. Use the R color channel for the calculation.
	CbAlpha.R = (byte)alpha;
	SrcAlpha.R = CurrentPixel.A;
	CbAlpha = multiplyOp.Apply(CbAlpha, SrcAlpha);
	alpha = CbAlpha.R;
}

CurrentPixel.A = (byte)alpha;
dst[x,y] = CurrentPixel;

 

 

Link to comment
Share on other sites

OK, I tested the code and it works well.

 

I have updated my "Paste Alpha" plugin with the new algorithm.

 

Download the updated plugin as part of my plugin pack version 4.7+ here: https://forums.getpaint.net/index.php?/topic/32048-v

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...