Jump to content

Feature Suggestion: New Layer Blending Mode - Divide


Recommended Posts

For reasons that will become plane I want to be able to divide images. I've looked at the source and it's really easy to add, it requires a few lines of code added to UserBlendOps.Generated.H.cs

Why? Recently I wanted to generate an image with an alpha channel from two images. One image is rendered on a black background the other a white background. Determining the alpha channel is easy, you subtract the black image from the white image and invert the result. Once you have the mask to determine the original image you must multiply the black image by 255 and divide it by the mask. Then you just have to apply the alpha to that resulting image.

Insert after line 309.

#define DIVIDE(A, B, r) \
{ \
   INT_DIV(((A) * 255), (, r); \
}

Insert after line 589.

DEFINE_STANDARD_OP(Divide, DIVIDE)

I tried to make an effect to do all this but... it crashed x_x

Link to comment
Share on other sites

I realized I can do what I want by using Color Dodge (which also removes the need to negate the mask when generating the base image).

How to recover lost alpha channels when you can render on the image onto different color backgrounds

1) Create a new image with two layers.

2) render target with a white background and paste it into the top layer

3) render target with a black background and past it into the background layer

4) duplicate the background layer

5) in the top layer properties change the blending mode to Difference

6) flatten the top layer down onto the middle layer

7) Save a copy of the image as a bmp or png ("Mask.png") it's going to want to auto flatten the image, let it.

8) undo the auto flatten so you have two layers again.

9) in the top layer properties change the blending mode to Color Dodge

10) flatten the image

11) Activate the Alpha Mask effect (in Object sub-menu)

12) Set the alpha mask to your previously saved copy of the image ("Mask.png") and check the "Invert Mask" checkbox.

You are done! You have just restored the alpha channel to your target image.

Caveats:

You may notice that the pixels are a one or two shades off, this is due to rounding and can't be avoided.

Math:

In case anyone is remotely interested in this, here is the math.

Alpha = 255 + Black - White;

Orig = 255 * Black / Alpha;

14508_f97d09f9077deb36c31d749eeeee59f2

14508_21860c920acbcabdd91ee25d5269d216

14508_fce00b83a2099f92e977556c3310f62c

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.

 Share

×
×
  • Create New...