Drop shadows in canvas

I am looking for some good way to make convincing shadows.
I know you’ll have to take the Mask and blur and lighten it, then use it as the mask of a solid-black picture, which you draw with the desired offset.
Blurring seems to be difficult to do.

I know it can be done with CG but I need a cross platform way.

Anyone has luck with this?

[quote=79368:@Christoph De Vocht]I am looking for some good way to make convincing shadows.
I know you’ll have to take the Mask and blur and lighten it, then use it as the mask of a solid-black picture, which you draw with the desired offset.
Blurring seems to be difficult to do.

I know it can be done with CG but I need a cross platform way.

Anyone has luck with this?[/quote]
Could you use the ImagePlayEffects library for blurring? From what I can remember, it slow but it works. http://sourceforge.net/projects/imageplay/

Thanks … I think I can use the blur class.

Not sure how the blur speed compares with the ImagePlayEffects one, but I remember doing some test for the tilt shift effect and that one came out pretty fast. https://alwaysbusycorner.wordpress.com/2012/08/14/lets-shrink-the-world-a-tilt-shift-effect/

EDIT: Just see it’s based on the one from ImagePlayEffects :slight_smile:

Well, it seems there ImagePlayEffects blurring does not support alpha channels. So no use to me.

This method (from Alain) works kinda fast but it does not support alpha. I need the shadow to be a alpha channel.

dim p,t as Picture
dim L,w,h as Integer

w=pic.Width
h=pic.Height
p=NewPicture(w,h,32)
p.Graphics.UseOldRenderer=true
p.Graphics.DrawPicture pic,0,0

t=NewPicture(w,h,32)
t.Graphics.UseOldRenderer=true
t.Mask.Graphics.UseOldRenderer=true
t.Mask.Graphics.ForeColor=&c7F7F7F
t.Mask.Graphics.FillRect 0,0,t.Width,t.Height

for L=abs(Level) DownTo 1
t.Graphics.DrawPicture p,0,0
p.Graphics.DrawPicture t,-L,-L 'upper left

t.Graphics.DrawPicture p,0,0
p.Graphics.DrawPicture t,-L,L    'lower left

t.Graphics.DrawPicture p,0,0
p.Graphics.DrawPicture t,L,L     'lower right

t.Graphics.DrawPicture p,0,0
p.Graphics.DrawPicture t,L,-L    'upper right

next

Return p

[quote=79410:@Alain Bailleul]Not sure how the blur speed compares with the ImagePlayEffects one, but I remember doing some test for the tilt shift effect and that one came out pretty fast. https://alwaysbusycorner.wordpress.com/2012/08/14/lets-shrink-the-world-a-tilt-shift-effect/

EDIT: Just see it’s based on the one from ImagePlayEffects :-)[/quote]
Alien’s solution looks better.

He says it is pretty fast and I am not convinced the ImagePlay effects library is ‘pretty fast’.

Yes it is ‘fast’ but I need the blurring to have an alpha channel. In otter words: the shadows needs to be transparent.

Fair enough.

I’ve been messing around with a blur method… it essentially creates a matrix of scaled down images from the original and then creates a new image by iterating through the matrix pixel by pixel. Works pretty well, but get a little funky above a 30x30 matrix… but I think that depends on the size of the original too… see what you think. (might be a little hard to decipher, just ask if you need clarification…)

[code]Private Function scalePicByMatrix(p As Picture,MatrixSize As integer) As Picture
#Pragma DisableBoundsChecking
#pragma NilObjectChecking false
#Pragma DisableBackgroundTasks

dim workpic As new Picture(p.width+MatrixSize4,p.height+MatrixSize4)
dim buffers as new Picture(workpic.Width+MatrixSize-(workpic.Width mod MatrixSize),workpic.Height+MatrixSize-(workpic.Height mod MatrixSize))
dim buffergraphics As Graphics=buffers.Graphics
dim res As new Picture(workpic.Width,workpic.Height)
dim xmult As double=buffers.width/MatrixSize
dim ymult As double=buffers.height/MatrixSize

workpic.graphics.drawpicture p,MatrixSize2,MatrixSize2
for x as integer=0 to MatrixSize-1
for y as integer=0 to MatrixSize-1
buffergraphics.DrawPicture(workpic,(xmultx),(ymulty),xmult,ymult,x,y,workpic.Width-MatrixSize,workpic.Height-MatrixSize)
next
next

dim surfaces as RGBSurface=buffers.RGBSurface
dim s As RGBSurface=res.RGBSurface
for x as integer=0 to res.Width-1
for y as integer=0 to res.Height-1
s.Pixel(x,y)=surfaces.pixel( (x/MatrixSize)+(xmult*(x mod MatrixSize)) , (y/MatrixSize)+(ymult*(y mod MatrixSize) ))
next
next
workpic=new Picture(p.width,p.height)
workpic.graphics.drawpicture res,0,0,p.width,p.height,MatrixSize2,MatrixSize2
Return workpic
End Function
[/code]

probably should have called it BlurByMatrix() … eh late night coding…

There was a solution posted on the Xippets website, not sure if this will help you

https://www.boxedbyte.com/xippet-157

Yes, that is a solution for OS X but I want something that is cross platform. :slight_smile:

Have you thought about drawing the blur on the MASK, and a solid color on the Image?

Here’s a companion to the code I posted above.
This will draw an image with a dropshadow to a graphics object. Keep in mind that blur>10 will be sloooow.
(the above method was called scaleByMatrix. You’ll need to change it to BlurByMatrix)

[code]Private Sub DrawWithShadow(g As Graphics,p As Picture,picx As integer,picy As integer, shadXoff As integer,shadYoff as integer, blur As integer, fill as color=&c00000000)
dim m As new picture(p.Width,p.Height)
dim shadow As new picture(p.Width,p.Height,32)

m.Graphics.ForeColor=hsv(0,0,fill.Value)
m.Graphics.FillRect(0,0,m.Width,m.Height)
m.ApplyMask p.copymask

shadow.Graphics.ForeColor=fill
shadow.graphics.fillrect(0,0,shadow.Width,shadow.Height)
shadow.ApplyMask m
shadow=BlurPicByMatrix(shadow,blur)

g.DrawPicture shadow,picx+shadXoff,picy+shadYoff
g.DrawPicture p,picx,picy

End Sub
[/code]

Another option would be to use the BlurMBS method (it’s VERY fast) applied to the mask (picture.copymask and picture.applymask are handy for this)

I must admit, with my budget I have not YET bought the MBS plugins but I have tried it out a little and it is really cool because it is massively powerful because it has such a wide-range of functions for just about anything you would expect from a library of this sort. The developer of it might add new things to the plugin on high enough request. He is a nice guy. :wink:

which part of the codes i need to replace on Jim Mckay’s DrawWithShadow if i want to use the BlurMBS??

http://einhugur.com/Html/Libs.html Björn’s Picture Effects plugin is x-plat and has some really highly optimized effects. I think version 9.0.2 is the latest.