Author Topic: BMAX - Outline function  (Read 420 times)

0 Members and 1 Guest are viewing this topic.

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 886
  • Karma: 63
    • View Profile
    • zac-interactive
BMAX - Outline function
« on: March 14, 2008 »


I just did an outline function for someone else. It could probably be optimized somehow, but it needed to be clear to the receiver what was going on and it gets the job done. Example usage is included in code.

Code: [Select]
SuperStrict

Graphics 640,480

' create a pixmap
Local tmpPix:TPixmap = CreatePixmap(64,64,PF_RGBA8888)
' clear the pixmap
ClearPixels(tmpPix)
' draw a square and X into pixmap
Local color:Int = $FF88FF00
For Local i:Int = 0 To 55
WritePixel(tmpPix,4+i,4,color)
WritePixel(tmpPix,4+i,59,color)
WritePixel(tmpPix,4,4+i,color)
WritePixel(tmpPix,59,4+i,color)
WritePixel(tmpPix,4+i,4+i,color)
WritePixel(tmpPix,59-i,4+i,color)
Next

' convert pixmap to an image
Local tmpImg:TImage = LoadImage(tmpPix,0)
' create a new image with outlines
Local tmpImg2:TImage = outline(tmpImg,2,128)

' clear the screen
Cls
' draw the images
DrawImage(tmpImg,256,256)
DrawImage(tmpImg2,256+64,256)
' show the screen
Flip
' wait for a keypress
WaitKey
' end
End

Function outline:TImage(img:TImage,thickness:Int=1,dilute:Int=0)
' create pixmap from image
Local scrPix:TPixmap = LockImage(img,0)
' get pointer to pixmap pixels
Local scrPtr:Byte Ptr = PixmapPixelPtr(scrPix,0,0)
' copy pixmap to temp pixmap
Local tmpPix:TPixmap = CopyPixmap(scrPix)
' get pointer to temp pixmap pixels
Local tmpPtr:Byte Ptr = PixmapPixelPtr(tmpPix,0,0)
' get widht and height of pixmap
Local w:Int = tmpPix.width
Local h:Int = tmpPix.height

' define local variables
Local r:Int,g:Int,b:Int
Local dx:Int,dy:Int,x:Int,y:Int
Local thick:Int

' draw outlines
For thick = thickness To 1 Step -1
For dy = -thick To thick
For dx = -thick To thick
For y = 0 To h-1
' first bounds check
If y+dy > -1 And y+dy < h Then
For x = 0 To w-1
' second bounds check
If x+dx > -1 And x+dx < w Then
r = scrPtr[x*4+y*4*w]-dilute
g = scrPtr[x*4+y*4*w+1]-dilute
b = scrPtr[x*4+y*4*w+2]-dilute
If r < 0 Then r = 0
If g < 0 Then g = 0
If b < 0 Then b = 0
' check to see if there are pixels
If r+g+b <> $00000000 Then
tmpPtr[(x+dx)*4+(y+dy)*4*w] = r
tmpPtr[(x+dx)*4+(y+dy)*4*w+1] = g
tmpPtr[(x+dx)*4+(y+dy)*4*w+2] = b
tmpPtr[(x+dx)*4+(y+dy)*4*w+3] = $FF
End If
End If
Next
End If
Next
Next
Next
dilute :/ 2
Next

' draw original ontop of outlines
For y = 0 To h-1
For x = 0 To w-1
r = scrPtr[x*4+y*4*w]
g = scrPtr[x*4+y*4*w+1]
b = scrPtr[x*4+y*4*w+2]
If r+g+b <> $00000000 Then
tmpPtr[x*4+y*4*w] = r
tmpPtr[x*4+y*4*w+1] = g
tmpPtr[x*4+y*4*w+2] = b
tmpPtr[x*4+y*4*w+3] = $FF
End If
Next
Next
' return pixmap converted to image
Return LoadImage(tmpPix,0)
EndFunction

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1343
  • Karma: 79
    • View Profile
Re: BMAX - Outline function
« Reply #1 on: March 15, 2008 »
I'd imagine that this could be usefull when making a glowing vector engine. Looks really good!

Have you thought about different methods of interpolation between the midtones? You might use a curve to describe the midtone position and distance to the second color. I would provide and example but I only know beziers and exponential curves. I'm sure logarithms might give some nice recults also. I'd imagine using logs would be really cool. Again nice job just tossing out some ideas for other looks. The artsy-fartsy side acts up sometimes.


Here is an image it did from two intersecting squiggles.
« Last Edit: March 15, 2008 by Pixel_Outlaw »
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 886
  • Karma: 63
    • View Profile
    • zac-interactive
Re: BMAX - Outline function
« Reply #2 on: March 15, 2008 »
Quote
this could be usefull when making a glowing vector engine

Actually I made this code for a friend who just started working on a small game to get back into coding. He is making a game with a vector look. He is using images, but all objects are drawn like line vector. So I made this function to make it easy to add a glowing look to the objects without having to manually draw the gradient lines.

The color selection could posibly be better using some kind of curve math instead of just dividing by 2, but for what it was made for, I think it will work. So I am probably not going to improve further on this one, but I figured that someone might use something like this as well.

[edit] nice squiggles :)