' data_colormap.pb — Color map generation and manipulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL colorMap(500) AS LONG
    LOCAL mapWidth AS LONG
    LOCAL mapHeight AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL x AS LONG
    LOCAL y AS LONG
    LOCAL rVal AS LONG
    LOCAL gVal AS LONG
    LOCAL bVal AS LONG
    LOCAL idx AS LONG
    LOCAL intensity AS LONG
    LOCAL grayVal AS LONG
    LOCAL sepiaR AS LONG
    LOCAL sepiaG AS LONG
    LOCAL sepiaB AS LONG
    LOCAL avgBrightness AS LONG
    LOCAL totalBrightness AS LONG
    LOCAL numPixels AS LONG
    LOCAL iter AS LONG
    LOCAL mode AS LONG
    LOCAL pixelVal AS LONG

    mapWidth = 16
    mapHeight = 16
    numPixels = mapWidth * mapHeight

    PRINT "=== Color Map Generator ==="
    PRINT "Size: " + STR$(mapWidth) + "x" + STR$(mapHeight)

    PRINT "Generating gradient colormap..."
    FOR y = 0 TO mapHeight - 1
        FOR x = 0 TO mapWidth - 1
            rVal = x * 16
            gVal = y * 16
            bVal = 128
            IF rVal > 255 THEN rVal = 255
            IF gVal > 255 THEN gVal = 255
            idx = y * mapWidth + x
            colorMap(idx) = rVal * 65536 + gVal * 256 + bVal
        NEXT x
    NEXT y

    totalBrightness = 0
    FOR y = 0 TO mapHeight - 1
        FOR x = 0 TO mapWidth - 1
            idx = y * mapWidth + x
            pixelVal = colorMap(idx)
            rVal = pixelVal / 65536
            gVal = pixelVal / 256 - rVal * 256
            bVal = pixelVal - rVal * 65536 - gVal * 256
            intensity = (rVal + gVal + bVal) / 3
            totalBrightness = totalBrightness + intensity
        NEXT x
    NEXT y
    avgBrightness = totalBrightness / numPixels
    PRINT "Average brightness: " + STR$(avgBrightness)

    PRINT "Converting to grayscale..."
    FOR y = 0 TO mapHeight - 1
        FOR x = 0 TO mapWidth - 1
            idx = y * mapWidth + x
            pixelVal = colorMap(idx)
            rVal = pixelVal / 65536
            gVal = pixelVal / 256 - rVal * 256
            bVal = pixelVal - rVal * 65536 - gVal * 256
            grayVal = (rVal + gVal + bVal) / 3
            colorMap(idx) = grayVal * 65536 + grayVal * 256 + grayVal
        NEXT x
    NEXT y
    PRINT "Grayscale done"

    PRINT "Applying sepia tone..."
    FOR y = 0 TO mapHeight - 1
        FOR x = 0 TO mapWidth - 1
            idx = y * mapWidth + x
            pixelVal = colorMap(idx)
            grayVal = pixelVal / 65536
            sepiaR = grayVal + 40
            sepiaG = grayVal + 20
            sepiaB = grayVal
            IF sepiaR > 255 THEN sepiaR = 255
            IF sepiaG > 255 THEN sepiaG = 255
            IF sepiaB > 255 THEN sepiaB = 255
            colorMap(idx) = sepiaR * 65536 + sepiaG * 256 + sepiaB
        NEXT x
    NEXT y
    PRINT "Sepia done"

    PRINT "=== End Colormap ==="
    FUNCTION = 0
END FUNCTION
