Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

PB_Blur : apply blur to a graphic control

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin PB_Blur : apply blur to a graphic control

    This program demonstrates applying a blur to a graphic control
    The routine is self-contained in a SUB, and requires only the
    hWin, Control ID, and number of iterations desired.
    And yes, it should also work fine with any PB Graphic Bitmap.
    Code:
    #COMPILE EXE
    #DIM ALL
    ENUM ctrls SINGULAR
        grfx = 500
        bgimage
        blur
    END ENUM
    
    TYPE bgra
        b AS BYTE
        g AS BYTE
        r AS BYTE
        a AS BYTE
    END TYPE
    
    FUNCTION PBMAIN () AS LONG
        LOCAL hWin, xitcode AS LONG
        RANDOMIZE
        DIALOG NEW PIXELS, 0, "PB_Blur", , , 300, 230, %WS_BORDER OR %WS_CAPTION OR %WS_SYSMENU TO hWin
        DIALOG SET COLOR hWin, %WHITE, %GRAY
        CONTROL ADD BUTTON, hWin, %bgimage, "New Background", 5, 5, 140, 20
        CONTROL ADD BUTTON, hWin, %blur, "Blur", 155, 5, 140, 20
        CONTROL ADD GRAPHIC, hWin, %grfx, "", 5, 30, 290, 195
        GRAPHIC ATTACH hWin, %grfx
        GRAPHIC CLEAR %BLACK
        DIALOG SHOW MODAL hWin, CALL dlgproc() TO xitcode
        FUNCTION = xitcode
    END FUNCTION
    
    CALLBACK FUNCTION dlgproc()
        SELECT CASE CB.MSG
            CASE %WM_INITDIALOG
                make_background(CB.HNDL, %grfx)
            CASE %WM_COMMAND
                SELECT CASE CB.CTL
                    CASE %bgimage
                        IF CB.CTLMSG = %BN_CLICKED THEN
                            make_background CB.HNDL, %grfx
                        END IF
                    CASE %blur
                        IF CB.CTLMSG = %BN_CLICKED THEN
                            PB_blur CB.HNDL, %grfx, RND(1, 5) 'changing the last number here changes blur iterations
                        END IF
                END SELECT
        END SELECT
    END FUNCTION
    
    SUB make_background(dlg AS DWORD, ctl AS LONG)
        LOCAL bginit, x, y, xll, xhl, yll, yhl, c_rad, fgc, bgc AS LONG
        CONTROL GET SIZE dlg, ctl TO x, y
        xll = -30: xhl = x + 30
        yll = -30: yhl = y + 30
        GRAPHIC ATTACH dlg, ctl, REDRAW
        FOR bginit = 0 TO 100
            c_rad = RND(0, 20)
            fgc = RGB(RND(0, 120), RND(0, 120), RND(0, 120))
            bgc = RGB(RND(100, 240), RND(100, 240), RND(100, 240))
            GRAPHIC BOX (RND(xll, xhl), RND(yll, yhl))-(RND(xll, xhl), RND(yll, yhl)), c_rad, fgc, bgc
        NEXT
        GRAPHIC REDRAW
    END SUB
    
    SUB PB_blur(dlg AS DWORD, ctl AS LONG, iterations AS LONG)
        ' adjusting number of iterations extends the blur by bleeding into an extended area.
        ' actually increasing the amount of blur requires extending the area averaged.
        ' the processes are similar, but distinct in their results
        LOCAL avg, x, y, xc, yc, hbmp AS LONG, xy AS LONG PTR, bufr, blr AS STRING, pxls(), blur() AS bgra
        GRAPHIC ATTACH dlg, ctl, REDRAW
        GRAPHIC GET BITS TO bufr
        xy = STRPTR(bufr)
        x = @xy[0] - 1
        y = @xy[1] - 1
        REDIM pxls(x, y) AT STRPTR(bufr) + 8
        FOR avg = 1 TO iterations
            ' Using a separate copy of the image, average from the original and store in the edited version.
            ' By updating the reference copy for each iteration, the reiteratioin bleed-thru is made possible.
            ' The amount of blur is also adjustable by weighting of the pixel being altered.
            ' For instance, you can double the r, g, and b for the pixel being altered, and add 1 to the division step.
            ' That would reduce the amount of interaction between pixel states.
            blr = bufr
            REDIM blur(x, y) AT STRPTR(blr) + 8
            pxls(0, 0).r = (blur(0, 0).r + blur(1, 1).r + blur(0, 1).r + blur(1, 0).r) \ 4
            pxls(x, y).r = (blur(x, y).r + blur(x - 1, y - 1).r + blur(x, y - 1).r + blur(x - 1, y).r) \ 4
            pxls(x, 0).r = (blur(x, 0).r + blur(x - 1, 1).r + blur(x, y + 1).r + blur(x - 1, 0).r) \ 4
            pxls(0, y).r = (blur(0, y).r + blur(1, y - 1).r + blur(1, y).r + blur(0, y - 1).r) \ 4
    
            pxls(0, 0).b = (blur(0, 0).b + blur(1, 1).b + blur(0, 1).b + blur(1, 0).b) \ 4
            pxls(x, y).b = (blur(x, y).b + blur(x - 1, y - 1).b + blur(x, y - 1).b + blur(x - 1, y).b) \ 4
            pxls(x, 0).b = (blur(x, 0).b + blur(x - 1, 1).b + blur(x, y + 1).b + blur(x - 1, 0).b) \ 4
            pxls(0, y).b = (blur(0, y).b + blur(1, y - 1).b + blur(1, y).b + blur(0, y - 1).b) \ 4
    
            pxls(0, 0).g = (blur(0, 0).g + blur(1, 1).g + blur(0, 1).g + blur(1, 0).g) \ 4
            pxls(x, y).g = (blur(x, y).g + blur(x - 1, y - 1).g + blur(x, y - 1).g + blur(x - 1, y).g) \ 4
            pxls(x, 0).g = (blur(x, 0).g + blur(x - 1, 1).g + blur(x, y + 1).g + blur(x - 1, 0).g) \ 4
            pxls(0, y).g = (blur(0, y).g + blur(1, y - 1).g + blur(1, y).g + blur(0, y - 1).g) \ 4
            FOR xc = 1 TO x - 1
                pxls(xc, 0).r = (blur(xc - 1, 0).r + blur(xc, 0).r + blur(xc + 1, 0).r + blur(xc - 1, 1).r + blur(xc, 1).r + blur(xc + 1, 1).r) \ 6
                pxls(xc, 0).g = (blur(xc - 1, 0).g + blur(xc, 0).g + blur(xc + 1, 0).g + blur(xc - 1, 1).g + blur(xc, 1).g + blur(xc + 1, 1).g) \ 6
                pxls(xc, 0).b = (blur(xc - 1, 0).b + blur(xc, 0).b + blur(xc + 1, 0).b + blur(xc - 1, 1).b + blur(xc, 1).b + blur(xc + 1, 1).b) \ 6
                pxls(xc, y).r = (blur(xc - 1, y).r + blur(xc, y).r + blur(xc + 1, y).r + blur(xc - 1, y - 1).r + blur(xc, y - 1).r + blur(xc + 1, y - 1).r) \ 6
                pxls(xc, y).g = (blur(xc - 1, y).g + blur(xc, y).g + blur(xc + 1, y).g + blur(xc - 1, y - 1).g + blur(xc, y - 1).g + blur(xc + 1, y - 1).g) \ 6
                pxls(xc, y).b = (blur(xc - 1, y).b + blur(xc, y).b + blur(xc + 1, y).b + blur(xc - 1, y - 1).b + blur(xc, y - 1).b + blur(xc + 1, y - 1).b) \ 6
            NEXT
            FOR yc = 1 TO y - 1
                pxls(x, yc).r = (blur(x - 1, yc - 1).r + blur(x - 1, yc).r + blur(x - 1, yc + 1).r + blur(x, yc - 1).r + blur(x, yc).r + blur(x, yc + 1).r) \ 6
                pxls(x, yc).g = (blur(x - 1, yc - 1).g + blur(x - 1, yc).g + blur(x - 1, yc + 1).g + blur(x, yc - 1).g + blur(x, yc).g + blur(x, yc + 1).g) \ 6
                pxls(x, yc).b = (blur(x - 1, yc - 1).b + blur(x - 1, yc).b + blur(x - 1, yc + 1).b + blur(x, yc - 1).b + blur(x, yc).b + blur(x, yc + 1).b) \ 6
                pxls(0, yc).r = (blur(0, yc - 1).r + blur(0, yc).r + blur(0, yc + 1).r + blur(1, yc - 1).r + blur(1, yc).r + blur(1, yc + 1).r) \ 6
                pxls(0, yc).g = (blur(0, yc - 1).g + blur(0, yc).g + blur(0, yc + 1).g + blur(1, yc - 1).g + blur(1, yc).g + blur(1, yc + 1).g) \ 6
                pxls(0, yc).b = (blur(0, yc - 1).b + blur(0, yc).b + blur(0, yc + 1).b + blur(1, yc - 1).b + blur(1, yc).b + blur(1, yc + 1).b) \ 6
            NEXT
            FOR xc = 1 TO x - 1
                FOR yc = 1 TO y - 1
                    pxls(xc, yc).r = (blur(xc - 1, yc - 1).r + blur(xc - 1, yc).r + blur(xc - 1, yc + 1).r + blur(xc, yc - 1).r + blur(xc, yc).r + blur(xc, yc + 1).r + blur(xc + 1, yc - 1).r + blur(xc + 1, yc).r + blur(xc + 1, yc + 1).r) \ 9
                    pxls(xc, yc).g = (blur(xc - 1, yc - 1).g + blur(xc - 1, yc).g + blur(xc - 1, yc + 1).g + blur(xc, yc - 1).g + blur(xc, yc).g + blur(xc, yc + 1).g + blur(xc + 1, yc - 1).g + blur(xc + 1, yc).g + blur(xc + 1, yc + 1).g) \ 9
                    pxls(xc, yc).b = (blur(xc - 1, yc - 1).b + blur(xc - 1, yc).b + blur(xc - 1, yc + 1).b + blur(xc, yc - 1).b + blur(xc, yc).b + blur(xc, yc + 1).b + blur(xc + 1, yc - 1).b + blur(xc + 1, yc).b + blur(xc + 1, yc + 1).b) \ 9
                NEXT
            NEXT
        NEXT
        GRAPHIC SET BITS bufr
        GRAPHIC REDRAW
    END SUB
    The world is strange and wonderful.*
    I reserve the right to be horrifically wrong.
    Please maintain a safe following distance.
    *wonderful sold separately.

  • #2
    This is an update to the blur code above.
    It is a bit more matured, allowing a greater range of blurring.
    The third, and likely last, version of this will include the ability
    to dither text onto any graphic control background. I've worked
    it out in my head. Now I just need to get it into code.
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "win32api.inc"
    ENUM ctrls SINGULAR
        grfx = 500
        bgimage
        blur
        strong
        iterations
    END ENUM
    
    TYPE bgra
        b AS BYTE
        g AS BYTE
        r AS BYTE
        a AS BYTE
    END TYPE
    
    FUNCTION PBMAIN () AS LONG
        LOCAL hWin, xitcode AS LONG
        RANDOMIZE
        DIALOG NEW PIXELS, 0, "PB_GradBlur", , , 300, 230, %WS_BORDER OR %WS_CAPTION OR %WS_SYSMENU TO hWin
        DIALOG SET COLOR hWin, %WHITE, %GRAY
        CONTROL ADD BUTTON, hWin, %bgimage, "New Background", 5, 5, 120, 20
        CONTROL ADD BUTTON, hWin, %blur, "Blur", 130, 5, 40, 20
        CONTROL ADD LABEL, hWin, %strong, "Weight: 1", 175, 6, 60, 15, %SS_SUNKEN OR %SS_NOTIFY
        CONTROL SET COLOR hWin, %strong, %YELLOW, %GRAY
        CONTROL ADD LABEL, hWin, %iterations, "Iterate: 1", 240, 6, 55, 15, %SS_SUNKEN OR %SS_NOTIFY
        CONTROL SET COLOR hWin, %iterations, %YELLOW, %GRAY
        CONTROL ADD GRAPHIC, hWin, %grfx, "", 5, 30, 290, 195
        GRAPHIC ATTACH hWin, %grfx
        GRAPHIC CLEAR %BLACK
        DIALOG SHOW MODAL hWin, CALL dlgproc() TO xitcode
        FUNCTION = xitcode
    END FUNCTION
    
    CALLBACK FUNCTION dlgproc()
        LOCAL getvalue AS STRING, wgt, itr AS LONG
        SELECT CASE CB.MSG
            CASE %WM_INITDIALOG
                make_background(CB.HNDL, %grfx)
            CASE %WM_COMMAND
                SELECT CASE CB.CTL
                    CASE %bgimage
                        IF CB.CTLMSG = %BN_CLICKED THEN
                            make_background CB.HNDL, %grfx
                        END IF
                    CASE %blur
                        IF CB.CTLMSG = %BN_CLICKED THEN
                            CONTROL GET TEXT CB.HNDL, %strong TO getvalue: wgt = VAL(MID$(getvalue, 8))
                            CONTROL GET TEXT CB.HNDL, %iterations TO getvalue: itr = VAL(MID$(getvalue, 9))
                            PB_gradblur CB.HNDL, %grfx, itr, wgt 'changing the numbers here changes blur effect
                        END IF
                    CASE %strong
                        IF CB.CTLMSG = %STN_CLICKED THEN
                            getvalue = INPUTBOX$("Enter a value from 0 to 31","Set Blur Weight", "0")
                            IF getvalue <> "" THEN CONTROL SET TEXT CB.HNDL, CB.CTL, "Weight:" + STR$(VAL(getvalue) AND 31)
                        END IF
                    CASE %iterations
                        IF CB.CTLMSG = %STN_CLICKED THEN
                            getvalue = INPUTBOX$("Enter a value from 0 to 31","Set Reiterations", "1")
                            IF getvalue <> "" THEN CONTROL SET TEXT CB.HNDL, CB.CTL, "Iterate:" + STR$((VAL(getvalue) AND 31))
                        END IF
                END SELECT
        END SELECT
    END FUNCTION
    
    SUB make_background(dlg AS DWORD, ctl AS LONG)
        LOCAL bginit, x, y, xll, xhl, yll, yhl, c_rad, fgc, bgc AS LONG
        CONTROL GET SIZE dlg, ctl TO x, y
        xll = -30: xhl = x + 30
        yll = -30: yhl = y + 30
        GRAPHIC ATTACH dlg, ctl, REDRAW
        FOR bginit = 0 TO 100
            c_rad = RND(0, 20)
            fgc = RGB(RND(0, 120), RND(0, 120), RND(0, 120))
            bgc = RGB(RND(100, 240), RND(100, 240), RND(100, 240))
            GRAPHIC BOX (RND(xll, xhl), RND(yll, yhl))-(RND(xll, xhl), RND(yll, yhl)), c_rad, fgc, bgc
        NEXT
        GRAPHIC REDRAW
    END SUB
    
    SUB PB_gradblur(dlg AS DWORD, ctl AS LONG, iterations AS LONG, strength AS LONG)
        ' adjusting number of iterations extends the blur by bleeding into an extended area.
        ' adjusting strengt of blur reduces bleedover.
        LOCAL avg, x, y, xc, yc, hbmp, weight AS LONG, xy AS LONG PTR, bufr, blr AS STRING, pxls(), blur() AS bgra
        weight = 32 - (strength AND 31)
        GRAPHIC ATTACH dlg, ctl, REDRAW
        GRAPHIC GET BITS TO bufr
        xy = STRPTR(bufr)
        x = @xy[0] - 1
        y = @xy[1] - 1
        REDIM pxls(x, y) AT STRPTR(bufr) + 8
        FOR avg = 0 TO iterations
            ' Using a separate copy of the image, average from the original and store in the edited version.
            ' By updating the reference copy for each iteration, the reiteratioin bleed-thru is made possible.
            blr = bufr
            REDIM blur(x, y) AT STRPTR(blr) + 8
            pxls(0, 0).r = weight * (blur(0, 0).r + blur(1, 1).r + blur(0, 1).r + blur(1, 0).r) \ (weight + 3)
            pxls(x, y).r = weight * (blur(x, y).r + blur(x - 1, y - 1).r + blur(x, y - 1).r + blur(x - 1, y).r) \ (weight + 3)
            pxls(x, 0).r = weight * (blur(x, 0).r + blur(x - 1, 1).r + blur(x, y + 1).r + blur(x - 1, 0).r) \ (weight + 3)
            pxls(0, y).r = weight * (blur(0, y).r + blur(1, y - 1).r + blur(1, y).r + blur(0, y - 1).r) \ (weight + 3)
    
            pxls(0, 0).b = weight * (blur(0, 0).b + blur(1, 1).b + blur(0, 1).b + blur(1, 0).b) \ (weight + 3)
            pxls(x, y).b = weight * (blur(x, y).b + blur(x - 1, y - 1).b + blur(x, y - 1).b + blur(x - 1, y).b) \ (weight + 3)
            pxls(x, 0).b = weight * (blur(x, 0).b + blur(x - 1, 1).b + blur(x, y + 1).b + blur(x - 1, 0).b) \ (weight + 3)
            pxls(0, y).b = weight * (blur(0, y).b + blur(1, y - 1).b + blur(1, y).b + blur(0, y - 1).b) \ (weight + 3)
    
            pxls(0, 0).g = weight * (blur(0, 0).g + blur(1, 1).g + blur(0, 1).g + blur(1, 0).g) \ (weight + 3)
            pxls(x, y).g = weight * (blur(x, y).g + blur(x - 1, y - 1).g + blur(x, y - 1).g + blur(x - 1, y).g) \ (weight + 3)
            pxls(x, 0).g = weight * (blur(x, 0).g + blur(x - 1, 1).g + blur(x, y + 1).g + blur(x - 1, 0).g) \ (weight + 3)
            pxls(0, y).g = weight * (blur(0, y).g + blur(1, y - 1).g + blur(1, y).g + blur(0, y - 1).g) \ (weight + 3)
            FOR xc = 1 TO x - 1
                pxls(xc, 0).r = (blur(xc - 1, 0).r + weight * blur(xc, 0).r + blur(xc + 1, 0).r + blur(xc - 1, 1).r + blur(xc, 1).r + blur(xc + 1, 1).r) \ (weight + 5)
                pxls(xc, 0).g = (blur(xc - 1, 0).g + weight * blur(xc, 0).g + blur(xc + 1, 0).g + blur(xc - 1, 1).g + blur(xc, 1).g + blur(xc + 1, 1).g) \ (weight + 5)
                pxls(xc, 0).b = (blur(xc - 1, 0).b + weight * blur(xc, 0).b + blur(xc + 1, 0).b + blur(xc - 1, 1).b + blur(xc, 1).b + blur(xc + 1, 1).b) \ (weight + 5)
                pxls(xc, y).r = (blur(xc - 1, y).r + weight * blur(xc, y).r + blur(xc + 1, y).r + blur(xc - 1, y - 1).r + blur(xc, y - 1).r + blur(xc + 1, y - 1).r) \ (weight + 5)
                pxls(xc, y).g = (blur(xc - 1, y).g + weight * blur(xc, y).g + blur(xc + 1, y).g + blur(xc - 1, y - 1).g + blur(xc, y - 1).g + blur(xc + 1, y - 1).g) \ (weight + 5)
                pxls(xc, y).b = (blur(xc - 1, y).b + weight * blur(xc, y).b + blur(xc + 1, y).b + blur(xc - 1, y - 1).b + blur(xc, y - 1).b + blur(xc + 1, y - 1).b) \ (weight + 5)
            NEXT
            FOR yc = 1 TO y - 1
                pxls(x, yc).r = (blur(x - 1, yc - 1).r + blur(x - 1, yc).r + blur(x - 1, yc + 1).r + blur(x, yc - 1).r + weight * blur(x, yc).r + blur(x, yc + 1).r) \ (weight + 5)
                pxls(x, yc).g = (blur(x - 1, yc - 1).g + blur(x - 1, yc).g + blur(x - 1, yc + 1).g + blur(x, yc - 1).g + weight * blur(x, yc).g + blur(x, yc + 1).g) \ (weight + 5)
                pxls(x, yc).b = (blur(x - 1, yc - 1).b + blur(x - 1, yc).b + blur(x - 1, yc + 1).b + blur(x, yc - 1).b + weight * blur(x, yc).b + blur(x, yc + 1).b) \ (weight + 5)
                pxls(0, yc).r = (blur(0, yc - 1).r + weight * blur(0, yc).r + blur(0, yc + 1).r + blur(1, yc - 1).r + blur(1, yc).r + blur(1, yc + 1).r) \ (weight + 5)
                pxls(0, yc).g = (blur(0, yc - 1).g + weight * blur(0, yc).g + blur(0, yc + 1).g + blur(1, yc - 1).g + blur(1, yc).g + blur(1, yc + 1).g) \ (weight + 5)
                pxls(0, yc).b = (blur(0, yc - 1).b + weight * blur(0, yc).b + blur(0, yc + 1).b + blur(1, yc - 1).b + blur(1, yc).b + blur(1, yc + 1).b) \ (weight + 5)
            NEXT
            FOR xc = 1 TO x - 1
                FOR yc = 1 TO y - 1
                    pxls(xc, yc).r = (blur(xc-1, yc-1).r + blur(xc-1, yc).r + blur(xc-1, yc+1).r + blur(xc, yc-1).r + weight * blur(xc, yc).r + blur(xc, yc+1).r + blur(xc+1, yc-1).r + blur(xc+1, yc).r + blur(xc+1, yc+1).r) \ (weight+8)
                    pxls(xc, yc).g = (blur(xc-1, yc-1).g + blur(xc-1, yc).g + blur(xc-1, yc+1).g + blur(xc, yc-1).g + weight * blur(xc, yc).g + blur(xc, yc+1).g + blur(xc+1, yc-1).g + blur(xc+1, yc).g + blur(xc+1, yc+1).g) \ (weight+8)
                    pxls(xc, yc).b = (blur(xc-1, yc-1).b + blur(xc-1, yc).b + blur(xc-1, yc+1).b + blur(xc, yc-1).b + weight * blur(xc, yc).b + blur(xc, yc+1).b + blur(xc+1, yc-1).b + blur(xc+1, yc).b + blur(xc+1, yc+1).b) \ (weight+8)
                NEXT
            NEXT
        NEXT
        GRAPHIC SET BITS bufr
        GRAPHIC REDRAW
    END SUB
    The world is strange and wonderful.*
    I reserve the right to be horrifically wrong.
    Please maintain a safe following distance.
    *wonderful sold separately.

    Comment

    Working...
    X