Page 1 of 1

'Silhouette-bleeding' for alpha'd mipmaps

Posted: 2019-05-09T08:08:07-07:00
by LDAsh
I've been working on a pretty convoluted script (typical of me) for doing what I call "silhouette bleeding", which essentially bleeds-out colour from the peripheries of an alpha-channel outward, so the texture can be mipped without the black "background" causing trouble at lower mip-map stages, which can cause artifacts.

As seen on this page (*although it's using Photoshop script):-
https://docs.unity3d.com/Manual/HOWTO-alphamaps.html

And here is my chunky-ass BAT, although it's only using 20 'runs', ideally it would be more like 100+:-

Code: Select all

::----------------------------
:: silhouette-bleeding
::----------------------------

convert input.tga -channel alpha -separate -define png:format=png24 alpha.png
convert input.tga -channel RGB -separate -define png:format=png24 color_chn.png

::----------------------------

convert color_chn-0.png color_chn-1.png color_chn-2.png -channel RGB -combine -define png:format=png24 rgb.png

::----------------------------

convert alpha.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl01.png
convert rgb.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl01.png

convert alpha_dl01.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl02.png
convert rgb_dl01.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl02.png
convert alpha_dl02.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl03.png
convert rgb_dl02.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl03.png
convert alpha_dl03.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl04.png
convert rgb_dl03.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl04.png
convert alpha_dl04.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl05.png
convert rgb_dl04.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl05.png
convert alpha_dl05.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl06.png
convert rgb_dl05.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl06.png
convert alpha_dl06.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl07.png
convert rgb_dl06.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl07.png
convert alpha_dl07.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl08.png
convert rgb_dl07.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl08.png
convert alpha_dl08.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl09.png
convert rgb_dl08.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl09.png
convert alpha_dl09.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl10.png
convert rgb_dl09.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl10.png
convert alpha_dl10.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl11.png
convert rgb_dl10.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl11.png
convert alpha_dl11.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl12.png
convert rgb_dl11.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl12.png
convert alpha_dl12.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl13.png
convert rgb_dl12.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl13.png
convert alpha_dl13.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl14.png
convert rgb_dl13.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl14.png
convert alpha_dl14.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl15.png
convert rgb_dl14.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl15.png
convert alpha_dl15.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl16.png
convert rgb_dl15.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl16.png
convert alpha_dl16.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl17.png
convert rgb_dl16.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl17.png
convert alpha_dl17.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl18.png
convert rgb_dl17.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl18.png
convert alpha_dl18.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl19.png
convert rgb_dl18.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl19.png
convert alpha_dl19.png -morphology dilate octagon:1 -gaussian-blur 1x1 alpha_dl20.png
convert rgb_dl19.png -morphology dilate octagon:1 -gaussian-blur 1x1 rgb_dl20.png

::----------------------------

convert rgb_dl20.png rgb_dl19.png alpha_dl19.png -compose over -composite cmb_dl19.png

convert cmb_dl19.png rgb_dl18.png alpha_dl18.png -compose over -composite cmb_dl18.png
convert cmb_dl18.png rgb_dl17.png alpha_dl17.png -compose over -composite cmb_dl17.png
convert cmb_dl17.png rgb_dl16.png alpha_dl16.png -compose over -composite cmb_dl16.png
convert cmb_dl16.png rgb_dl15.png alpha_dl15.png -compose over -composite cmb_dl15.png
convert cmb_dl15.png rgb_dl14.png alpha_dl14.png -compose over -composite cmb_dl14.png
convert cmb_dl14.png rgb_dl13.png alpha_dl13.png -compose over -composite cmb_dl13.png
convert cmb_dl13.png rgb_dl12.png alpha_dl12.png -compose over -composite cmb_dl12.png
convert cmb_dl12.png rgb_dl11.png alpha_dl11.png -compose over -composite cmb_dl11.png
convert cmb_dl11.png rgb_dl10.png alpha_dl10.png -compose over -composite cmb_dl10.png
convert cmb_dl10.png rgb_dl09.png alpha_dl09.png -compose over -composite cmb_dl09.png
convert cmb_dl09.png rgb_dl08.png alpha_dl08.png -compose over -composite cmb_dl08.png
convert cmb_dl08.png rgb_dl07.png alpha_dl07.png -compose over -composite cmb_dl07.png
convert cmb_dl07.png rgb_dl06.png alpha_dl06.png -compose over -composite cmb_dl06.png
convert cmb_dl06.png rgb_dl05.png alpha_dl05.png -compose over -composite cmb_dl05.png
convert cmb_dl05.png rgb_dl04.png alpha_dl04.png -compose over -composite cmb_dl04.png
convert cmb_dl04.png rgb_dl03.png alpha_dl03.png -compose over -composite cmb_dl03.png
convert cmb_dl03.png rgb_dl02.png alpha_dl02.png -compose over -composite cmb_dl02.png
convert cmb_dl02.png rgb_dl01.png alpha_dl01.png -compose over -composite cmb_dl01.png

::----------------------------

convert cmb_dl01.png rgb.png alpha.png -compose over -composite final.png

::----------------------------

convert final.png -channel RGB -separate -define png:format=png24 final_chn.png
convert final_chn-0.png final_chn-1.png final_chn-2.png alpha.png -channel RGBA -combine ouput.tga

::----------------------------

::del rgb*.png
::del alpha*.png
::del color*.png
::del final*.png
::del cmb*.png
pause
pause
pause



I was just wondering if there's a better way this could be done with ImageMagick?

I'm basically dilating and blurring both RGB and alpha over and over in the first chunk, the composing everything back in together in reverse, and finally putting the original image back in to that and restoring the original alpha. The outcome is more or less the same, so far, but it takes a damn long time and as you can see it's already chunky at just 20.

Re: 'Silhouette-bleeding' for alpha'd mipmaps

Posted: 2019-05-09T08:23:42-07:00
by snibgo
Please provide a sample input (input.rga), and the resulting output.

Your temporary files are PNG format. This is useful for debugging, but writing PNG is slow. I suggest you use .miff or .mpc.

"-gaussian-blur" is much slower that "-blur", for almost identical results.

You have many "convert" commands that write a file that is immediately read by the next command. These can be combined into a single command so there is no write and read.

Your final pair of "convert" commands can be combined, using "-compose CopyOpacity -composite".

Re: 'Silhouette-bleeding' for alpha'd mipmaps

Posted: 2019-05-09T08:42:59-07:00
by LDAsh
Here's the example:-
http://www.violae.net/temp/silhouette-bleeding.zip

I'm actually not sure how I could combine the commands, since each output from the first "BAT chunk" is used in each command in the second "chunk".

Re: 'Silhouette-bleeding' for alpha'd mipmaps

Posted: 2019-05-09T10:30:25-07:00
by snibgo
Oh my laptop, your script takes 131 seconds. Changing png to miff (with "+adjoin" where "-separate is used) take 23 seconds.


You have a block of commands:

Code: Select all

convert rgb_dl30.miff rgb_dl29.miff alpha_dl29.miff -compose over -composite cmb_dl29.miff
convert cmb_dl29.miff rgb_dl28.miff alpha_dl28.miff -compose over -composite cmb_dl28.miff
convert cmb_dl28.miff rgb_dl27.miff alpha_dl27.miff -compose over -composite cmb_dl27.miff
convert cmb_dl27.miff rgb_dl26.miff alpha_dl26.miff -compose over -composite cmb_dl26.miff
convert cmb_dl26.miff rgb_dl25.miff alpha_dl25.miff -compose over -composite cmb_dl25.miff
convert cmb_dl25.miff rgb_dl24.miff alpha_dl24.miff -compose over -composite cmb_dl24.miff
convert cmb_dl24.miff rgb_dl23.miff alpha_dl23.miff -compose over -composite cmb_dl23.miff
convert cmb_dl23.miff rgb_dl22.miff alpha_dl22.miff -compose over -composite cmb_dl22.miff
convert cmb_dl22.miff rgb_dl21.miff alpha_dl21.miff -compose over -composite cmb_dl21.miff
convert cmb_dl21.miff rgb_dl20.miff alpha_dl20.miff -compose over -composite cmb_dl20.miff
convert cmb_dl20.miff rgb_dl19.miff alpha_dl19.miff -compose over -composite cmb_dl19.miff
convert cmb_dl19.miff rgb_dl18.miff alpha_dl18.miff -compose over -composite cmb_dl18.miff
convert cmb_dl18.miff rgb_dl17.miff alpha_dl17.miff -compose over -composite cmb_dl17.miff
convert cmb_dl17.miff rgb_dl16.miff alpha_dl16.miff -compose over -composite cmb_dl16.miff
convert cmb_dl16.miff rgb_dl15.miff alpha_dl15.miff -compose over -composite cmb_dl15.miff
convert cmb_dl15.miff rgb_dl14.miff alpha_dl14.miff -compose over -composite cmb_dl14.miff
convert cmb_dl14.miff rgb_dl13.miff alpha_dl13.miff -compose over -composite cmb_dl13.miff
convert cmb_dl13.miff rgb_dl12.miff alpha_dl12.miff -compose over -composite cmb_dl12.miff
convert cmb_dl12.miff rgb_dl11.miff alpha_dl11.miff -compose over -composite cmb_dl11.miff
convert cmb_dl11.miff rgb_dl10.miff alpha_dl10.miff -compose over -composite cmb_dl10.miff
convert cmb_dl10.miff rgb_dl09.miff alpha_dl09.miff -compose over -composite cmb_dl09.miff
convert cmb_dl09.miff rgb_dl08.miff alpha_dl08.miff -compose over -composite cmb_dl08.miff
convert cmb_dl08.miff rgb_dl07.miff alpha_dl07.miff -compose over -composite cmb_dl07.miff
convert cmb_dl07.miff rgb_dl06.miff alpha_dl06.miff -compose over -composite cmb_dl06.miff
convert cmb_dl06.miff rgb_dl05.miff alpha_dl05.miff -compose over -composite cmb_dl05.miff
convert cmb_dl05.miff rgb_dl04.miff alpha_dl04.miff -compose over -composite cmb_dl04.miff
convert cmb_dl04.miff rgb_dl03.miff alpha_dl03.miff -compose over -composite cmb_dl03.miff
convert cmb_dl03.miff rgb_dl02.miff alpha_dl02.miff -compose over -composite cmb_dl02.miff
convert cmb_dl02.miff rgb_dl01.miff alpha_dl01.miff -compose over -composite cmb_dl01.miff
But you only need the final cmb_dl01.miff, so all those can be combined into:

Code: Select all

convert rgb_dl30.miff rgb_dl29.miff alpha_dl29.miff -compose over -composite ^
  rgb_dl28.miff alpha_dl28.miff -compose over -composite ^
  rgb_dl27.miff alpha_dl27.miff -compose over -composite ^
  rgb_dl26.miff alpha_dl26.miff -compose over -composite ^
  rgb_dl25.miff alpha_dl25.miff -compose over -composite ^
  rgb_dl24.miff alpha_dl24.miff -compose over -composite ^
  rgb_dl23.miff alpha_dl23.miff -compose over -composite ^
  rgb_dl22.miff alpha_dl22.miff -compose over -composite ^
  rgb_dl21.miff alpha_dl21.miff -compose over -composite ^
  rgb_dl20.miff alpha_dl20.miff -compose over -composite ^
  rgb_dl19.miff alpha_dl19.miff -compose over -composite ^
  rgb_dl18.miff alpha_dl18.miff -compose over -composite ^
  rgb_dl17.miff alpha_dl17.miff -compose over -composite ^
  rgb_dl16.miff alpha_dl16.miff -compose over -composite ^
  rgb_dl15.miff alpha_dl15.miff -compose over -composite ^
  rgb_dl14.miff alpha_dl14.miff -compose over -composite ^
  rgb_dl13.miff alpha_dl13.miff -compose over -composite ^
  rgb_dl12.miff alpha_dl12.miff -compose over -composite ^
  rgb_dl11.miff alpha_dl11.miff -compose over -composite ^
  rgb_dl10.miff alpha_dl10.miff -compose over -composite ^
  rgb_dl09.miff alpha_dl09.miff -compose over -composite ^
  rgb_dl08.miff alpha_dl08.miff -compose over -composite ^
  rgb_dl07.miff alpha_dl07.miff -compose over -composite ^
  rgb_dl06.miff alpha_dl06.miff -compose over -composite ^
  rgb_dl05.miff alpha_dl05.miff -compose over -composite ^
  rgb_dl04.miff alpha_dl04.miff -compose over -composite ^
  rgb_dl03.miff alpha_dl03.miff -compose over -composite ^
  rgb_dl02.miff alpha_dl02.miff -compose over -composite ^
  rgb_dl01.miff alpha_dl01.miff -compose over -composite cmb_dl01.miff

Overall, your script is similar to my own blurFill.bat. See Filling holes.

Re: 'Silhouette-bleeding' for alpha'd mipmaps

Posted: 2019-05-09T13:57:34-07:00
by LDAsh
Thanks for the replies. I can only use miff format for some commands because I get errors on others. Maybe something to do with using 6.9. Definitely useful advice for the future, though!

I was really hoping for some advice about some other approaches with morphology. Maybe there's some way of getting the same results without going over it 100 times. I would either get drowned out with bright pixels (dilate) or dark pixels (erode), with overdoing 1 command, but I noticed GIMP can do the same thing but has a "normalise" option. Maybe there is something along those lines. Or maybe 'convolve' is what I'm looking for, even though the documentation leads me to believe it would be even worse.

Anyway, I'm going to do some tweaks with what I have here already:-
- Eroding before dilating, because in many cases the periphery pixels are also too dark, to get in a pixel or two before dilating.
- Increasing the brightness and saturation of the first dilation to help the bleed look closer to the inner pixels.
- Eroding and blurring the alpha at the last step to bring the bleeding in little tighter, again only about 1 pixel.

One more approach I was considering is to upscale by percent, crop by percent, blur and compose back using that image also as an alpha. That works - in my mind...

Re: 'Silhouette-bleeding' for alpha'd mipmaps

Posted: 2019-05-09T17:13:01-07:00
by LDAsh
Ah, I found this for GIMP:-
http://gimpchat.com/viewtopic.php?t=3153
Still takes about as long if not longer, and still really needs the original pasted back in.

And so anyway, I got about as far as I'm going to get with this for now, if anyone is interested:-
http://www.violae.net/temp/silhouette-bleeding_bat.txt