Blend modes: Photoshop's Overlay?

BlitzMax Forums/BlitzMax Programming/Blend modes: Photoshop's Overlay?

therevills(Posted 2016) [#1]
Hi All,

I am trying to replicate Photoshop's Overlay blend mode.

In OpenGL the closest I've got by altering glmax2d.bmx and adding OVERLAYBLEND to driver.bmx is using:
		Case OVERLAYBLEND
			glEnable GL_BLEND
			glBlendFunc GL_DST_COLOR,GL_SRC_ALPHA
			glDisable GL_ALPHA_TEST


But its still not right, as the transparent part of an image darkens the pixels under it.

Any ideas? Also if we get the overlay blend mode to work with OpenGL I would want to try to get it working with DX too :)

I found this nice site to get the DX and GL equivalents:
http://abstractprogramming.wikia.com/wiki/Blend_Modes

OpenGL                 | Direct3D
-----------------------+----------------------
GL_ZERO                | D3DBLEND_ZERO
GL_ONE                 | D3DBLEND_ONE
GL_SRC_COLORD          | 3DBLEND_SRCCOLOR
GL_ONE_MINUS_SRC_COLOR | D3DBLEND_INVSRCCOLOR
GL_SRC_ALPHA           | D3DBLEND_SRCALPHA
GL_ONE_MINUS_SRC_ALPHA | D3DBLEND_INVSRCALPHA
GL_DST_ALPHA           | D3DBLEND_DESTALPHA
GL_ONE_MINUS_DST_ALPHA | D3DBLEND_INVDESTALPHA
GL_DST_COLOR           | D3DBLEND_DESTCOLOR
GL_ONE_MINUS_DST_COLOR | D3DBLEND_INVDESTCOLOR
GL_SRC_ALPHA_SATURATE  | D3DBLEND_SRCALPHASAT



The following example code demonstrates setting the blend mode in Direct3D9.

d3dDev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
d3dDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
The following code is the GL equivalent.

glBlendFunc(GL_SRC_ALPHA,GL_ONE);


Cheers,
Steve


col(Posted 2016) [#2]
Hey!
Maybe this would help?


GW(Posted 2016) [#3]
I've always done overlay at the pixmap level. You can definitely do it with a shader. But I don't know of any dx9 equivalent of the 'tShader' type that's floating around.

Is this helpful?
http://stackoverflow.com/questions/19459255/opengl-photoshop-overlay-blend-mode

If you find the answer, please follow up and let us know.


therevills(Posted 2016) [#4]
Thanks Dave thats a cool image! Thanks GW for the link.

After further investigation it looks like the issue might be with my image:



When I display this image on top a background using the overlay blend there actually is a dark rectangle. Using gimp to colour pick the image I can see that the transparent image directly around the circles and between them is different to the outer colour.

In GIMP and Photoshop it doesnt matter but in Blitz it doesnt display correctly :(

In GIMP it displays as:


In BMX it displays something like this:



Derron(Posted 2016) [#5]
there are multiple ways to store the alphachannel in an image: premultiplied or postmultiplied ones
Try to switch between them.

think of it as if you do store the pixels as "black with 0 opacity"...


Maybe that is the problem you are faced with.


Bye
Ron


Kryzon(Posted 2016) [#6]
I think another name for 'Overlay' is MOD2X:
https://www.opengl.org/discussion_boards/showthread.php/163372-mod2x-blend-mode

If you use that overlay blending, transparency will not be taken into consideration. You will need to replace transparent pixels with medium grey (127,127,127) so they keep not doing anything.
This can be done by creating a "fill" layer with that grey and placing your glow sprite in a layer above. Then export this combination as a new sprite.


therevills(Posted 2016) [#7]
Thanks Ron and Kryzon.

I've applied MOD2X and it looks just about right, just need to remember to set the background to grey.

And I've applied it to DX7, DX9 and DX11 without too much hassle.

d3d7max2d.bmx:
		Case OVERLAYBLEND
			device.SetRenderState D3DRS_ALPHATESTENABLE,False
			device.SetRenderState D3DRS_ALPHABLENDENABLE,True
			device.SetRenderState D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR
			device.SetRenderState D3DRS_DESTBLEND,D3DBLEND_SRCCOLOR
			device.SetRenderState D3DRS_ALPHATESTENABLE,False

d3d9max2d.bmx:
		Case OVERLAYBLEND
			_d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,False
			_d3dDev.SetRenderState D3DRS_ALPHABLENDENABLE,True
			_d3dDev.SetRenderState D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR
			_d3dDev.SetRenderState D3DRS_DESTBLEND,D3DBLEND_SRCCOLOR
			_d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,False

d3d11max2d_brl.bmx:
	'Overlay blend
	_bd.RenderTarget0_BlendEnable = True
	_bd.RenderTarget0_SrcBlend = D3D11_BLEND_DEST_COLOR
	_bd.RenderTarget0_DestBlend = D3D11_BLEND_SRC_COLOR
	If _d3d11dev.CreateBlendState(_bd,_overlayblend)<0
		WriteStdout "Cannot create overlay blend state~n"
		Return False
	EndIf



Derron(Posted 2016) [#8]
did you play wIth premultiplication and the opposite before patching module sources?

If you are not sure...try various images available on the Internet ...there will surely be samples available.

Bye
Ron


therevills(Posted 2016) [#9]
did you play wIth premultiplication and the opposite before patching module sources?


I needed to patch the modules to get the effect I was after, but if you have a better idea which still works it would be great to see!