' image_filters.pb — Image filter algorithms (convolution)
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL imageData(500) AS LONG
    LOCAL filteredData(500) AS LONG
    LOCAL imgWidth AS LONG
    LOCAL imgHeight AS LONG
    LOCAL kernel(30) AS LONG
    LOCAL kernelSize AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL x AS LONG
    LOCAL y AS LONG
    LOCAL kx AS LONG
    LOCAL ky AS LONG
    LOCAL sumVal AS LONG
    LOCAL pixelVal AS LONG
    LOCAL rVal AS LONG
    LOCAL gVal AS LONG
    LOCAL bVal AS LONG
    LOCAL grayVal AS LONG
    LOCAL idx AS LONG
    LOCAL kernelIdx AS LONG
    LOCAL imgIdx AS LONG
    LOCAL totalPixels AS LONG
    LOCAL filterType AS LONG
    LOCAL numFilters AS LONG
    LOCAL iter AS LONG
    LOCAL resultStr AS STRING

    imgWidth = 10
    imgHeight = 10

    PRINT "=== Image Filters ==="
    PRINT "Image: " + STR$(imgWidth) + "x" + STR$(imgHeight)

    FOR y = 0 TO imgHeight - 1
        FOR x = 0 TO imgWidth - 1
            rVal = (x * 25) MOD 256
            gVal = (y * 25) MOD 256
            bVal = ((x + y) * 10) MOD 256
            idx = y * imgWidth + x
            imageData(idx) = rVal * 65536 + gVal * 256 + bVal
        NEXT x
    NEXT y

    FOR numFilters = 1 TO 4
        filterType = numFilters

        IF filterType = 1 THEN
            kernelSize = 3
            kernel(0) = 0
            kernel(1) = -1
            kernel(2) = 0
            kernel(3) = -1
            kernel(4) = 4
            kernel(5) = -1
            kernel(6) = 0
            kernel(7) = -1
            kernel(8) = 0
            PRINT "Laplacian edge detection (3x3):"
        END IF

        IF filterType = 2 THEN
            kernelSize = 3
            kernel(0) = 1
            kernel(1) = 2
            kernel(2) = 1
            kernel(3) = 2
            kernel(4) = 4
            kernel(5) = 2
            kernel(6) = 1
            kernel(7) = 2
            kernel(8) = 1
            PRINT "Gaussian blur (3x3):"
        END IF

        IF filterType = 3 THEN
            kernelSize = 3
            kernel(0) = -1
            kernel(1) = 0
            kernel(2) = 1
            kernel(3) = -2
            kernel(4) = 0
            kernel(5) = 2
            kernel(6) = -1
            kernel(7) = 0
            kernel(8) = 1
            PRINT "Sobel horizontal edge (3x3):"
        END IF

        IF filterType = 4 THEN
            kernelSize = 3
            kernel(0) = -1
            kernel(1) = -2
            kernel(2) = -1
            kernel(3) = 0
            kernel(4) = 0
            kernel(5) = 0
            kernel(6) = 1
            kernel(7) = 2
            kernel(8) = 1
            PRINT "Sobel vertical edge (3x3):"
        END IF

        totalPixels = 0
        FOR y = 1 TO imgHeight - 2
            FOR x = 1 TO imgWidth - 2
                sumVal = 0
                kernelIdx = 0
                FOR ky = -1 TO 1
                    FOR kx = -1 TO 1
                        imgIdx = (y + ky) * imgWidth + (x + kx)
                        pixelVal = imageData(imgIdx)
                        grayVal = pixelVal / 65536
                        IF kernelIdx < kernelSize * kernelSize THEN
                            sumVal = sumVal + grayVal * kernel(kernelIdx)
                            kernelIdx = kernelIdx + 1
                        END IF
                    NEXT kx
                NEXT ky

                IF sumVal < 0 THEN sumVal = 0
                IF sumVal > 255 THEN sumVal = 255

                idx = y * imgWidth + x
                filteredData(idx) = sumVal * 65536 + sumVal * 256 + sumVal
                totalPixels = totalPixels + 1
            NEXT x
        NEXT y

        PRINT "  Pixels filtered: " + STR$(totalPixels)
    NEXT numFilters

    PRINT "Filters applied: " + STR$(numFilters - 1)
    PRINT "=== End Filters ==="
    FUNCTION = 0
END FUNCTION
