Qbasicnews.com

Qbasic "like" compilers/interpreters => FB Discussion & Programming Help => Topic started by: Zack on February 07, 2006, 11:51:08 PM



Title: Antialiasing
Post by: Zack on February 07, 2006, 11:51:08 PM
Can anyone point me to a standalone antialiasing function for FB? Any colour depth will do.
Or how could I make one myself?


Title: Antialiasing
Post by: axipher on February 08, 2006, 12:22:41 AM
The colors would have to be in truecolor mode which I believe is 32 and the RGB values of the colors would have to be stored in variables then modified using a counnting algorythm.  That' my opinion, or maybe put the image down using ALPHA then a 1 pixel smaller version of the picture etc. for the count or steps.  It's only a theory, maybe I just got the ball rolling up a hill, or maybe down.  It's a thinker... :???:


Title: Antialiasing
Post by: Dr_Davenstein on February 08, 2006, 12:59:36 AM
There's a way you could do it here...

http://forum.qbasicnews.com/viewtopic.php?t=11293

It's more like a blur, but it works. ;)


Title: Antialiasing
Post by: na_th_an on February 08, 2006, 07:54:57 AM
If you want antialiased lines peek in the FAQ, I posted something that may give you a start.

If you just need bluring a bitmap, the thing consists on, for each pixel, add the value of the 8 pixels that surround it and the pixel, then divide by 9.


Title: Antialiasing
Post by: yetifoot on February 08, 2006, 10:26:36 AM
Look up laplace kernel filtering.  Also look at the comp.graphics.algorithms FAQ


Title: Antialiasing
Post by: Zack on February 08, 2006, 12:18:30 PM
Quote from: "na_th_an"
If you want antialiased lines peek in the FAQ, I posted something that may give you a start.

If you just need bluring a bitmap, the thing consists on, for each pixel, add the value of the 8 pixels that surround it and the pixel, then divide by 9.
I think a blur will do. So, for instance in 8-bit colour, I add up all the values of the eight pixels that surround one pixel and divide that value by nine, and replace the original pixel with one of that colour?


Title: Antialiasing
Post by: na_th_an on February 08, 2006, 01:21:10 PM
It won't work in 8 bits mode unless the palette is a gradient. You can get funny effects, tho'.

Bluring in 8 bits is tricky. You wether use gradients in your palette and make sure the limits are not exceded OR you have to blur in truecolour and then convert/dither to 8 bits, which would be slow.

Doing it in 16 or 32 bits modes is a matter of performing the above mentioned average for each colour component R, G, B, then build the pixel and write it.


Title: Antialiasing
Post by: Zack on February 08, 2006, 03:17:26 PM
Got it, thanks.
Heh heh heh, and excuse to use highcolour mode! :D


Title: Antialiasing
Post by: Dr_Davenstein on February 08, 2006, 04:20:40 PM
So let's see what you made.  :P


Title: Antialiasing
Post by: Zack on February 08, 2006, 04:37:03 PM
I'm working on it. :)
Any ideas on how to extract a particular part of an RGB value? If I use POINT to get the RGB value of a pixel, how can I extract just the R, G or B part?


Title: Antialiasing
Post by: yetifoot on February 08, 2006, 04:46:37 PM
there are various ways, but i use this

C = RGB(64, 127, 234)

B = C AND &HFF
G = (C AND &HFF00) SHR 8
R = (C AND &HFF0000) SHR 16

Print R, G, B

Sleep


Title: Antialiasing
Post by: Zack on February 08, 2006, 04:47:41 PM
Thanks yetifoot. Bitwise operators to the rescue!


Title: Antialiasing
Post by: Zack on February 08, 2006, 05:28:44 PM
OK, I have some extremely crude code. It does blur, but it makes everything that was blurred look grey.
Code:
TYPE Pixel
    R AS INTEGER
    G AS INTEGER
    B AS INTEGER
END TYPE
DECLARE FUNCTION BlurPixel(INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER) AS Pixel
SCREENRES 300,200,24
RANDOMIZE TIMER
FOR I=1 TO 1000
    CIRCLE (INT(RND*300),INT(RND*200)),INT(RND*15),RGB(INT(RND*255),INT(RND*255),INT(RND*255))
NEXT
SLEEP
DIM AS Pixel NewPixel
FOR X=100 TO 200
    FOR Y=50 TO 150
        TL=POINT(X-1,Y-1)
        TM=POINT(X,Y-1)
        TR=POINT(X+1,Y-1)
        ML=POINT(X-1,Y)
        MM=POINT(X,Y)
        MR=POINT(X+1,Y)
        BL=POINT(X-1,Y+1)
        BM=POINT(X,Y+1)
        BR=POINT(X+1,Y+1)
        NewPixel=BlurPixel(TL,TM,TR,ML,MM,MR,BL,BM,BR)
        PSET (X,Y),RGB(NewPixel.R,NewPixel.G,NewPixel.B)
    NEXT
NEXT
SLEEP
FUNCTION BlurPixel(TL AS INTEGER,TM AS INTEGER,TR AS INTEGER,ML AS INTEGER,MM AS INTEGER,MR AS INTEGER,BL AS INTEGER,BM AS INTEGER,BR AS INTEGER) AS Pixel
        DIM AS BYTE R,G,B
        DIM AS Pixel Pixels(1 TO 9),RetPixel
        DIM AS INTEGER I,RTotal,GTotal,BTotal,RetVal
        Pixels(1).R=(TL AND &HFF0000) SHR 16
        Pixels(2).R=(TM AND &HFF0000) SHR 16
        Pixels(3).R=(TR AND &HFF0000) SHR 16
        Pixels(4).R=(TM AND &HFF0000) SHR 16
        Pixels(5).R=(MM AND &HFF0000) SHR 16
        Pixels(6).R=(MR AND &HFF0000) SHR 16
        Pixels(7).R=(BL AND &HFF0000) SHR 16
        Pixels(8).R=(BM AND &HFF0000) SHR 16
        Pixels(9).R=(BR AND &HFF0000) SHR 16
       
        Pixels(1).G=(TL AND &HFF00) SHR 8
        Pixels(2).G=(TM AND &HFF00) SHR 8
        Pixels(3).G=(TR AND &HFF00) SHR 8
        Pixels(4).G=(ML AND &HFF00) SHR 8
        Pixels(5).G=(MM AND &HFF00) SHR 8
        Pixels(6).G=(MR AND &HFF00) SHR 8
        Pixels(7).G=(ML AND &HFF00) SHR 8
        Pixels(8).G=(BM AND &HFF00) SHR 8
        Pixels(9).G=(BR AND &HFF00) SHR 8
       
        Pixels(1).B=TL AND &HFF
        Pixels(1).B=TM AND &HFF
        Pixels(1).B=TR AND &HFF
        Pixels(1).B=ML AND &HFF
        Pixels(1).B=MM AND &HFF
        Pixels(1).B=MR AND &HFF
        Pixels(1).B=BL AND &HFF
        Pixels(1).B=BM AND &HFF
        Pixels(1).B=BR AND &HFF
       
        FOR I=1 TO 9
            RetPixel.R=RetPixel.R+Pixels(I).R
            RetPixel.G=RetPixel.R+Pixels(I).G
            RetPixel.B=RetPixel.R+Pixels(I).B
        NEXT
        RetPixel.R=RetPixel.R/9
        RetPixel.G=RetPixel.G/9
        RetPixel.B=RetPixel.B/9
        RETURN RetPixel
END FUNCTION

I know, I know...But I couldn't think of an elegant way to do it! :P


Title: Antialiasing
Post by: yetifoot on February 08, 2006, 06:15:12 PM
heres some code from an old VB project of mine.  Couldn't be bothered to update it yet!

the iDATA array is :first part is for rgb then second and third are x and y

hope it helps, maybe i'll convert it at some point.

Code:
Private Sub Smooth()
  Dim X As Long, Y As Long
  Dim r As Long, G As Long, B As Long

  For Y = 2 To PicInfo.bmHeight - 1
    For X = 2 To PicInfo.bmWidth - 1
      B = CLng(iDATA(1, X, Y)) + _
        CLng(iDATA(1, X - 1, Y)) + CLng(iDATA(1, X, Y - 1)) + _
        CLng(iDATA(1, X, Y + 1)) + CLng(iDATA(1, X + 1, Y))
      B = B \ 5
      G = CLng(iDATA(2, X, Y)) + _
        CLng(iDATA(2, X - 1, Y)) + CLng(iDATA(2, X, Y - 1)) + _
        CLng(iDATA(2, X, Y + 1)) + CLng(iDATA(2, X + 1, Y))
      G = G \ 5
      r = CLng(iDATA(3, X, Y)) + _
        CLng(iDATA(3, X - 1, Y)) + CLng(iDATA(3, X, Y - 1)) + _
        CLng(iDATA(3, X, Y + 1)) + CLng(iDATA(3, X + 1, Y))
      r = r \ 5
      iDATA(1, X, Y) = B
      iDATA(2, X, Y) = G
      iDATA(3, X, Y) = r
    Next X
  Next Y
End Sub


Title: Antialiasing
Post by: Dr_Davenstein on February 08, 2006, 07:48:17 PM
I changed a few things out of habit. It's basically what I did on that landmaker algo thing. ;)

Code:
Option Explicit
Const Scr_Width As Integer = 320
Const SCR_Height As Integer = 200
Const Bits_Per_Plane As Integer = 24

Screenres Scr_Width,Scr_Height,Bits_Per_Plane
Randomize Timer

Type Pixel
    R As Integer
    G As Integer
    B As Integer
End Type

Declare Function BlurPixel(Byval X As Integer, Byval Y As Integer, Byval Strength As Integer) As Pixel

Dim As Pixel NewPixel
Dim As Integer I, X, Y

For I = 1 To 1000
    Circle (Int(Rnd*300),Int(Rnd*200)),Int(Rnd*15),Rgb(Int(Rnd*255),Int(Rnd*255),Int(Rnd*255))
Next
Sleep


For X=100 To 200
    For Y=50 To 150
        NewPixel = BlurPixel(X, Y, 1)
        Pset (X,Y),Rgb(NewPixel.R,NewPixel.G,NewPixel.B)
    Next
Next
Sleep





Function BlurPixel(Byval X As Integer, Byval Y As Integer, Byval Strength As Integer) As Pixel
    Dim As Integer ScanX, Scany, StartX, EndX, StartY, EndY, TempCol, Hits
    Dim TempRGB As Pixel
   
    If Strength <=0 Then
        TempCol = Point(X,Y)
        TempRGB.R += ((TempCol Shr 16) And &HFF)
        TempRGB.G += ((TempCol Shr 8 ) And &HFF)
        TempRGB.B += ((TempCol       ) And &HFF)
        Function = TempRGB
        Exit Function
    End If    
   
    StartY= Y-Strength
    EndY  = Y+Strength
    StartX= X-Strength
    EndX  = X+Strength
   
    If StartY< 0  Then StartY = 0
    If StartX< 0  Then StartX = 0
    If EndY>Scr_Height-1 Then EndY=Scr_Height-1
    If EndX>Scr_Width-1 Then EndX=Scr_Width-1
   
    For ScanY = StartY To EndY
        For ScanX = StartX To EndX
            TempCol = Point(ScanX,ScanY)
            TempRGB.R += ((TempCol Shr 16) And &HFF)
            TempRGB.G += ((TempCol Shr 8 ) And &HFF)
            TempRGB.B += ((TempCol       ) And &HFF)
            Hits+=1
        Next
    Next
   
    TempRgb.R\=Hits
    TempRgb.G\=Hits
    TempRgb.B\=Hits
   
    Function = TempRGB
End Function


Title: Antialiasing
Post by: Zack on February 08, 2006, 09:13:29 PM
I don't understand all of it, but it works much better than mine. :P


Title: Antialiasing
Post by: Dr_Davenstein on February 08, 2006, 09:21:21 PM
What is it that you don't understand? I'd be glad to explain... or I'm sure someone else will.  8)


Title: Antialiasing
Post by: Zack on February 09, 2006, 01:08:35 PM
Well...how it works, for one. :P  It doesn't resemble my code at all.


Title: Antialiasing
Post by: yetifoot on February 09, 2006, 04:18:26 PM
his is still the same principle, which is an average of surrounding colours, but his has strength, which is how many pixels are sampled.


Title: Antialiasing
Post by: Zack on February 09, 2006, 04:23:54 PM
Well I looked at every line of the code and I understand it now.
But why does his maintain colour while mine makes everything grey?


Title: Antialiasing
Post by: yetifoot on February 09, 2006, 06:50:58 PM
yes just had a full read through your code and i cant spot it.  i'll take a closer look.

EDIT :

B Pixels all (1)

Quote
       Pixels(1).B=TL AND &HFF
        Pixels(2).B=TM AND &HFF
        ....
        Pixels(8).B=BM AND &HFF
        Pixels(9).B=BR AND &HFF


This bit all R
Quote

        FOR I=1 TO 9
            RetPixel.R=RetPixel.R+Pixels(I).R
            RetPixel.G=RetPixel.G+Pixels(I).G
            RetPixel.B=RetPixel.B+Pixels(I).B
        NEXT


Title: Antialiasing
Post by: Zack on February 09, 2006, 10:44:53 PM
OMFG was I drunk or something...thanks yetifoot!
Fixed code. But ignore it, because Dave's is way better. :P
Code:
TYPE Pixel
    R AS INTEGER
    G AS INTEGER
    B AS INTEGER
END TYPE
DECLARE FUNCTION BlurPixel(INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER,INTEGER) AS Pixel
SCREENRES 300,200,24
RANDOMIZE TIMER
FOR I=1 TO 50
    CIRCLE (INT(RND*300),INT(RND*200)),INT(RND*15),RGB(INT(RND*255),INT(RND*255),INT(RND*255))
NEXT
SLEEP
DIM AS Pixel NewPixel
FOR X=100 TO 200
    FOR Y=50 TO 150
        TL=POINT(X-1,Y-1)
        TM=POINT(X,Y-1)
        TR=POINT(X+1,Y-1)
        ML=POINT(X-1,Y)
        MM=POINT(X,Y)
        MR=POINT(X+1,Y)
        BL=POINT(X-1,Y+1)
        BM=POINT(X,Y+1)
        BR=POINT(X+1,Y+1)
        NewPixel=BlurPixel(TL,TM,TR,ML,MM,MR,BL,BM,BR)
        PSET (X,Y),RGB(NewPixel.R,NewPixel.G,NewPixel.B)
    NEXT
NEXT
SLEEP
FUNCTION BlurPixel(TL AS INTEGER,TM AS INTEGER,TR AS INTEGER,ML AS INTEGER,MM AS INTEGER,MR AS INTEGER,BL AS INTEGER,BM AS INTEGER,BR AS INTEGER) AS Pixel
        DIM AS BYTE R,G,B
        DIM AS Pixel Pixels(1 TO 9),RetPixel
        DIM AS INTEGER I,RTotal,GTotal,BTotal,RetVal
        Pixels(1).R=(TL AND &HFF0000) SHR 16
        Pixels(2).R=(TM AND &HFF0000) SHR 16
        Pixels(3).R=(TR AND &HFF0000) SHR 16
        Pixels(4).R=(TM AND &HFF0000) SHR 16
        Pixels(5).R=(MM AND &HFF0000) SHR 16
        Pixels(6).R=(MR AND &HFF0000) SHR 16
        Pixels(7).R=(BL AND &HFF0000) SHR 16
        Pixels(8).R=(BM AND &HFF0000) SHR 16
        Pixels(9).R=(BR AND &HFF0000) SHR 16
       
        Pixels(1).G=(TL AND &HFF00) SHR 8
        Pixels(2).G=(TM AND &HFF00) SHR 8
        Pixels(3).G=(TR AND &HFF00) SHR 8
        Pixels(4).G=(ML AND &HFF00) SHR 8
        Pixels(5).G=(MM AND &HFF00) SHR 8
        Pixels(6).G=(MR AND &HFF00) SHR 8
        Pixels(7).G=(ML AND &HFF00) SHR 8
        Pixels(8).G=(BM AND &HFF00) SHR 8
        Pixels(9).G=(BR AND &HFF00) SHR 8
       
        Pixels(1).B=TL AND &HFF
        Pixels(2).B=TM AND &HFF
        Pixels(3).B=TR AND &HFF
        Pixels(4).B=ML AND &HFF
        Pixels(5).B=MM AND &HFF
        Pixels(6).B=MR AND &HFF
        Pixels(7).B=BL AND &HFF
        Pixels(8).B=BM AND &HFF
        Pixels(9).B=BR AND &HFF
       
        FOR I=1 TO 9
            RetPixel.R=RetPixel.R+Pixels(I).R
            RetPixel.G=RetPixel.G+Pixels(I).G
            RetPixel.B=RetPixel.B+Pixels(I).B
        NEXT
        RetPixel.R=RetPixel.R/9
        RetPixel.G=RetPixel.G/9
        RetPixel.B=RetPixel.B/9
        RETURN RetPixel
END FUNCTION


Title: Antialiasing
Post by: Z!re on February 10, 2006, 06:37:51 AM
Bluring and Antialiasing are two very different things.
Bluring takes average and converts each pixel to that.
Antialiasing removes jagged edges by blending sharp "corners" to become more smooth, a diagonal line for example would be made up of numerous shades of the color you really want.
Bluring that same line would just make it unfocused


Title: Antialiasing
Post by: Zack on February 10, 2006, 01:43:14 PM
I know, but blurring is adequate in most of my situations.
OK, here's the better code. It's a heck of a lot like Dave's, but it's in my coding style and I did it from memory (without copying. :P )
I also used SCREENINFO to minimize external CONSTs.
Code:
TYPE Pixel
    R AS INTEGER
    G AS INTEGER
    B AS INTEGER
END TYPE
DECLARE FUNCTION BlurPixel (INTEGER,INTEGER,INTEGER) AS Pixel
SCREENRES 800,600,24,2
WP=1
SCREENSET WP,WP XOR 1
RANDOMIZE TIMER
FOR I=1 TO 5000
    CIRCLE (INT(RND*800),INT(RND*600)),INT(RND*30),RGB(INT(RND*255),INT(RND*255),INT(RND*255))
NEXT
DIM AS Pixel BlurredPixel
FOR X=300 TO 500
    FOR Y=200 TO 400
        BlurredPixel=BlurPixel(X,Y,1)
        PSET (X,Y),RGB(BlurredPixel.R,BlurredPixel.G,BlurredPixel.B)
    NEXT
NEXT
WP=WP XOR 1
SCREENSET WP,WP XOR 1
SLEEP
FUNCTION BlurPixel (X AS INTEGER,Y AS INTEGER,Intensity AS INTEGER) AS Pixel
    DIM AS Pixel RetPixel
    DIM AS INTEGER StartX,EndX,StartY,EndY,CDepth,ScreenHeight,ScreenWidth,FX,FY,PHits
    IF Intensity<=0 THEN Intensity=1
    StartX=X-Intensity
    EndX=X+Intensity
    StartY=Y-Intensity
    EndY=Y+Intensity
    SCREENINFO ScreenWidth,ScreenHeight,CDepth
    IF CDepth=8 THEN
        RetPixel.R=-1
        RetPixel.G=-1
        RetPixel.B=-1
        RETURN RetPixel
    END IF
    IF StartX<=0 THEN StartX=0
    IF EndX>=ScreenWidth THEN EndX=ScreenWidth
    IF StartY<=0 THEN StartY=0
    IF EndY>=ScreenWidth THEN EndY=ScreenWidth
    FOR FX=StartX TO EndX
        FOR FY=StartY TO EndY
            RetPixel.R+=(POINT(FX,FY) AND &HFF0000) SHR 16
            RetPixel.G+=(POINT(FX,FY) AND &HFF00) SHR 8
            RetPixel.B+=(POINT(FX,FY) AND &HFF)
            PHits+=1
        NEXT
    NEXT
    RetPixel.R/=PHits
    RetPixel.G/=PHits
    RetPixel.B/=PHits
    RETURN RetPixel
END FUNCTION


Title: Antialiasing
Post by: Zack on February 10, 2006, 01:55:18 PM
Oh, and here's a standalone module that can be used.
Code:
'BlurPixel.Bas
'By Zachary Vernon
'Contains BlurPixel function, an essential TYPE, and the function declaration.
'Date (mm/dd/yy): 02/10/06
'Usage: BlurPixel (X,Y,I)
'       Returns a Pixel TYPE with the R, G and B value for the blurred
'       version of pixel (X,Y), with I blurring intensity. I must be > 0, otherwise it is
'       set to 1.
'       Pixel TYPE contains three variables: R, G and B, each representing
'       the red, green and blue value of a pixel.
'       IMPORTANT: Colour depth must be 16 bits or higher, otherwise BlurPixel()
'                  returns -1 for R, G and B.
TYPE Pixel
    R AS INTEGER
    G AS INTEGER
    B AS INTEGER
END TYPE
DECLARE FUNCTION BlurPixel (INTEGER,INTEGER,INTEGER) AS Pixel
FUNCTION BlurPixel (X AS INTEGER,Y AS INTEGER,Intensity AS INTEGER) AS Pixel
    DIM AS Pixel RetPixel
    DIM AS INTEGER StartX,EndX,StartY,EndY,CDepth,ScreenHeight,ScreenWidth,FX,FY,PHits
    IF Intensity<=0 THEN Intensity=1
    StartX=X-Intensity
    EndX=X+Intensity
    StartY=Y-Intensity
    EndY=Y+Intensity
    SCREENINFO ScreenWidth,ScreenHeight,CDepth
    IF CDepth=8 THEN
        RetPixel.R=-1
        RetPixel.G=-1
        RetPixel.B=-1
        RETURN RetPixel
    END IF
    IF StartX<=0 THEN StartX=0
    IF EndX>=ScreenWidth THEN EndX=ScreenWidth
    IF StartY<=0 THEN StartY=0
    IF EndY>=ScreenWidth THEN EndY=ScreenWidth
    FOR FX=StartX TO EndX
        FOR FY=StartY TO EndY
            RetPixel.R+=(POINT(FX,FY) AND &HFF0000) SHR 16
            RetPixel.G+=(POINT(FX,FY) AND &HFF00) SHR 8
            RetPixel.B+=(POINT(FX,FY) AND &HFF)
            PHits+=1
        NEXT
    NEXT
    RetPixel.R/=PHits
    RetPixel.G/=PHits
    RetPixel.B/=PHits
    RETURN RetPixel
END FUNCTION


Title: Antialiasing
Post by: yetifoot on February 10, 2006, 03:23:38 PM
looks loads better than your first one.

a little tip, that may make it faster
replace this...
Code:

    FOR FX=StartX TO EndX
        FOR FY=StartY TO EndY
            RetPixel.R+=(POINT(FX,FY) AND &HFF0000) SHR 16
            RetPixel.G+=(POINT(FX,FY) AND &HFF00) SHR 8
            RetPixel.B+=(POINT(FX,FY) AND &HFF)
            PHits+=1
        NEXT
    NEXT

With this, and hopefully it will go faster, because POINT can be slow, calling it three times with the same values could be a waste.  I haven't actually checked, but i expect this would be faster.
Code:

    Dim c As uInteger
    FOR FX=StartX TO EndX
        FOR FY=StartY TO EndY
            c = POINT(FX,FY)
            RetPixel.R+=(c AND &HFF0000) SHR 16
            RetPixel.G+=(c AND &HFF00) SHR 8
            RetPixel.B+=(c AND &HFF)
            PHits+=1
        NEXT
    NEXT


Title: Antialiasing
Post by: Zack on February 10, 2006, 04:23:28 PM
Excellent point, my friend. Let's test it...
Whoo-ee! It's three times faster that way.
Corrected code, yet again...
Code:
'BlurPixel.Bas
'By Zachary Vernon
'Contains BlurPixel function, an essential TYPE, and the function declaration.
'Date (mm/dd/yy): 02/10/06
'Usage: BlurPixel (X,Y,I)
'       Returns a Pixel TYPE with the R, G and B value for the blurred
'       version of pixel (X,Y), with I  blurring intensity. I must be > 0, otherwise it is
'       set to 1.
'       Pixel TYPE contains three variables: R, G and B, each representing
'       the red, green and blue value of a pixel.
'       IMPORTANT: Colour depth must be 16 bits or higher, otherwise BlurPixel()
'                  returns -1 for R, G and B.
TYPE Pixel
    R AS INTEGER
    G AS INTEGER
    B AS INTEGER
END TYPE
DECLARE FUNCTION BlurPixel (INTEGER,INTEGER,INTEGER) AS Pixel
FUNCTION BlurPixel (X AS INTEGER,Y AS INTEGER,Intensity AS INTEGER) AS Pixel
    DIM AS Pixel RetPixel
    DIM AS INTEGER StartX,EndX,StartY,EndY,CDepth,ScreenHeight,ScreenWidth,FX,FY,PHits,C
    IF Intensity<=0 THEN Intensity=1
    StartX=X-Intensity
    EndX=X+Intensity
    StartY=Y-Intensity
    EndY=Y+Intensity
    SCREENINFO ScreenWidth,ScreenHeight,CDepth
    IF CDepth=8 THEN
        RetPixel.R=-1
        RetPixel.G=-1
        RetPixel.B=-1
        RETURN RetPixel
    END IF
    IF StartX<=0 THEN StartX=0
    IF EndX>=ScreenWidth THEN EndX=ScreenWidth
    IF StartY<=0 THEN StartY=0
    IF EndY>=ScreenWidth THEN EndY=ScreenWidth
    FOR FX=StartX TO EndX
        FOR FY=StartY TO EndY
            C=POINT(FX,FY)
            RetPixel.R+=(C AND &HFF0000) SHR 16
            RetPixel.G+=(C AND &HFF00) SHR 8
            RetPixel.B+=(C AND &HFF)
            PHits+=1
        NEXT
    NEXT
    RetPixel.R/=PHits
    RetPixel.G/=PHits
    RetPixel.B/=PHits
    RETURN RetPixel
END FUNCTION


Title: Antialiasing
Post by: Dr_Davenstein on February 10, 2006, 07:34:11 PM
Don't forget to fix this too...

Code:
IF StartX<=0 THEN StartX=0
IF EndX>=ScreenWidth THEN EndX=ScreenWidth
IF StartY<=0 THEN StartY=0
IF EndY>=ScreenWidth THEN EndY=[b]ScreenWidth[/b]


You should also subtract one from screenwidth/screenheight. That way it doesn't try to sample pixels that are off of the screen.  Blur the whole screen and you'll see what I mean. ;)


Title: Antialiasing
Post by: Zack on February 11, 2006, 11:01:35 PM
Oy Vey, so many things!! Thanks, Doc. :D
Code:
'BlurPixel.Bas
'By Zachary Vernon
'Contains BlurPixel function, an essential TYPE, and the function declaration.
'Date (mm/dd/yy): 02/10/06
'Usage: BlurPixel (X,Y,I)
'       Returns a Pixel TYPE with the R, G and B value for the blurred
'       version of pixel (X,Y), with I  blurring intensity. I must be > 0, otherwise it is
'       set to 1.
'       Pixel TYPE contains three variables: R, G and B, each representing
'       the red, green and blue value of a pixel.
'       IMPORTANT: Colour depth must be 16 bits or higher, otherwise BlurPixel()
'                  returns -1 for R, G and B.
TYPE Pixel
    R AS INTEGER
    G AS INTEGER
    B AS INTEGER
END TYPE
DECLARE FUNCTION BlurPixel (INTEGER,INTEGER,INTEGER) AS Pixel
FUNCTION BlurPixel (X AS INTEGER,Y AS INTEGER,Intensity AS INTEGER) AS Pixel
    DIM AS Pixel RetPixel
    DIM AS INTEGER StartX,EndX,StartY,EndY,CDepth,ScreenHeight,ScreenWidth,FX,FY,PHits,C
    IF Intensity<=0 THEN Intensity=1
    StartX=X-Intensity
    EndX=X+Intensity
    StartY=Y-Intensity
    EndY=Y+Intensity
    SCREENINFO ScreenWidth,ScreenHeight,CDepth
    IF CDepth=8 THEN
        RetPixel.R=-1
        RetPixel.G=-1
        RetPixel.B=-1
        RETURN RetPixel
    END IF
    IF StartX<=0 THEN StartX=0
    IF EndX>=ScreenWidth THEN EndX=ScreenWidth - 1
    IF StartY<=0 THEN StartY=0
    IF EndY>=ScreenWidth THEN EndY=ScreenHeight - 1
    FOR FX=StartX TO EndX
        FOR FY=StartY TO EndY
            C=POINT(FX,FY)
            RetPixel.R+=(C AND &HFF0000) SHR 16
            RetPixel.G+=(C AND &HFF00) SHR 8
            RetPixel.B+=(C AND &HFF)
            PHits+=1
        NEXT
    NEXT
    RetPixel.R/=PHits
    RetPixel.G/=PHits
    RetPixel.B/=PHits
    RETURN RetPixel
END FUNCTION


Title: Antialiasing
Post by: Dr_Davenstein on February 11, 2006, 11:11:58 PM
Hmm... almost got it. Look at this line again... ;)

IF EndY>=ScreenWidth THEN EndY=ScreenHeight - 1


Title: Antialiasing
Post by: yetifoot on February 12, 2006, 02:47:13 AM
i would say that if you are using a 3x3 doodah, then you would do

For x = 1 To Width - 2

because otherwise you will attempt to read pixels that don't exist


Title: Antialiasing
Post by: Zack on February 12, 2006, 12:44:08 PM
Quote from: "Dr_Davenstein"
Hmm... almost got it. Look at this line again... ;)

IF EndY>=ScreenWidth THEN EndY=ScreenHeight - 1
SON OF A &*^$#!!!

Code:
'BlurPixel.Bas
'By Zachary Vernon
'Contains BlurPixel function, an essential TYPE, and the function declaration.
'Date (mm/dd/yy): 02/10/06
'Usage: BlurPixel (X,Y,I)
'       Returns a Pixel TYPE with the R, G and B value for the blurred
'       version of pixel (X,Y), with I  blurring intensity. I must be > 0, otherwise it is
'       set to 1.
'       Pixel TYPE contains three variables: R, G and B, each representing
'       the red, green and blue value of a pixel.
'       IMPORTANT: Colour depth must be 16 bits or higher, otherwise BlurPixel()
'                  returns -1 for R, G and B.
TYPE Pixel
    R AS INTEGER
    G AS INTEGER
    B AS INTEGER
END TYPE
DECLARE FUNCTION BlurPixel (INTEGER,INTEGER,INTEGER) AS Pixel
FUNCTION BlurPixel (X AS INTEGER,Y AS INTEGER,Intensity AS INTEGER) AS Pixel
    DIM AS Pixel RetPixel
    DIM AS INTEGER StartX,EndX,StartY,EndY,CDepth,ScreenHeight,ScreenWidth,FX,FY,PHits,C
    IF Intensity<=0 THEN Intensity=1
    StartX=X-Intensity
    EndX=X+Intensity
    StartY=Y-Intensity
    EndY=Y+Intensity
    SCREENINFO ScreenWidth,ScreenHeight,CDepth
    IF CDepth=8 THEN
        RetPixel.R=-1
        RetPixel.G=-1
        RetPixel.B=-1
        RETURN RetPixel
    END IF
    IF StartX<=0 THEN StartX=0
    IF EndX>=ScreenWidth THEN EndX=ScreenWidth - 1
    IF StartY<=0 THEN StartY=0
    IF EndY>=ScreenHeight THEN EndY=ScreenHeight - 1
    FOR FX=StartX TO EndX
        FOR FY=StartY TO EndY
            C=POINT(FX,FY)
            RetPixel.R+=(C AND &HFF0000) SHR 16
            RetPixel.G+=(C AND &HFF00) SHR 8
            RetPixel.B+=(C AND &HFF)
            PHits+=1
        NEXT
    NEXT
    RetPixel.R/=PHits
    RetPixel.G/=PHits
    RetPixel.B/=PHits
    RETURN RetPixel
END FUNCTION


Title: Antialiasing
Post by: Dr_Davenstein on February 12, 2006, 04:51:09 PM
Quote from: "Zack"
Quote from: "Dr_Davenstein"
Hmm... almost got it. Look at this line again... ;)

IF EndY>=ScreenWidth THEN EndY=ScreenHeight - 1
SON OF A &*^$#!!!


:rotfl:
Good job.