Jump to content

Removing Alpha Channel


Recommended Posts

Sure, create a new layer and fill it with white. Move that layer to the bottom. Merge Down the layer with transparency.

If you are not using the 3.0 Alpha build of Paint.net, MergeDown will not be available... use Image Flatten instead.

Link to comment
Share on other sites

Nope, won't work.

Just tested with a gradient, the value of a (0,255,0,0) px become (255,255,255,255) and so on (:

Need some Codelab stuff IMHO.

No. Way. I've just seen Bob. And... *poof!*—just like that—he disappears into the mist again. ~Helio

Link to comment
Share on other sites

The codelab script is trivial if you want to try this out...

void Render(Surface dst, Surface src, Rectangle rect)
{
   PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds);
   ColorBgra CurrentPixel;
   for(int y = rect.Top; y     {
       for (int x = rect.Left; x         {
           if (selectionRegion.IsVisible(x, y))
           {
               CurrentPixel = src[x,y];
               CurrentPixel.A = (byte)255;
               dst[x,y] = CurrentPixel;
           }
       }
   }
}

Link to comment
Share on other sites

Actually I just remembered Code Lab. Never tried it before but it was so easy. Here is my code:

void Render(Surface dst, Surface src, Rectangle rect)
{
   ColorBgra PixelColor;

   for(int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
          PixelColor = src[x, y];

          //if (PixelColor.A > 0)
             PixelColor.A = (byte)(255);

          dst[x, y] = PixelColor;
       }
   }
}

Link to comment
Share on other sites

I have a follow-on question but first a bit of background: I am trying to use images with alpha channel in a toolbar in an dev. environment where the alpha channel is ignored. (The above question was so that I could easily see the effect in PDN to adjust them when required.)

The next problem is that the transparent colour for the buttons is hard-coded with RGB values of (192,192,192 - light gray). Currently the completely transparent areas seem to be black (0,0,0). I can fill them with (0,192,192,192 - transparent light gray) but it PDN seems to keep reverting these back to (0,0,0,0 - transparent black).

How do I get the right background color?

Link to comment
Share on other sites

void Render(Surface dst, Surface src, Rectangle rect)
{
   ColorBgra PixelColor;

   for(int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
          PixelColor = src[x, y];

          if (PixelColor.A == 0)
          {
             PixelColor.A = 1;
             PixelColor.R =
             PixelColor.G =
             PixelColor.B = 192;
          }

          dst[x, y] = PixelColor;
       }
   }
}

Link to comment
Share on other sites

The reason 0,192,192,192 probably jumps over to 0,0,0,0 is one of mathematical and color space equivalence: if a color is fully transparent, then it is equivalent to all other transparent colors and the R,G,B values are irrelevant. It's transparent, what more needs to be known? Mathematically, the 0 alpha value causes this to happen: a transparent color value should never contribute to anything. A pixel that is transparent is essentially a pixel that does not exist.

I'm assuming that you're seeing 0,192,192,192 converted to 0,0,0,0 when you save as a .PNG? For .PDN it should preserve the color value but for other file types the process of exporting does a flatten operation (even for 1 layer).

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

What you say makes sense mathematically (after my brain starts working) and obviously avoids problems/inconsistencies in the code. Thanks for the explanation.

FYI I am editing and saving in .PNG in case we can use alpha channel later. The actual images are currently loaded as .BMP. But I wanted to save the background as (0,192,192,192) in .PNG to allow easy/automatic conversion to .BMP.

BoltBait's workaround of (1,192,192,192) works fine.

(Another idea would be to be able to specify the RGB value for transparent areas when saving to .BMP??)

Thanks again for all replies.

Link to comment
Share on other sites

If you really want your head to explode, you should investigate the terms premultiplied alpha and alpha compositing on Wikipedia or something. http://en.wikipedia.org/wiki/Alpha_compositing . It might take awhile to wrap your head around the concepts involved in alpha composition, but it's worth it if you want full understanding of this stuff. Paint.NET didn't have correct alpha composition in the layer rendering code until v2.6. We're still fixing this up in other areas too.

The code that saves PNG (or other single-layer, non-.PDN image types) goes through the same code path as the real-time composition and rendering code. So you will not have the ability to save 1,192,192,192 except to a PDN file.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

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...