

FUNCTION center$(a$, b AS INTEGER)
            t$ = SPACE$(INT(b/2)-INT(LEN(a$)/2)) + a$ + SPACE$(INT(b/2)-INT(LEN(a$)/2))
            IF LEN(t$) > b THEN
                t$ = MID$(t$,1,b)
            ELSE
                t$ = t$ + SPACE$(b-LEN(t$))
            END IF
            center$ = t$
END FUNCTION


FUNCTION lword(a$) AS LONG
            DIM num AS LONG
            num = 0
            num = num + ASC(MID$(a$,1,1))
            num = num + ASC(MID$(a$,2,1))*256
            num = num + ASC(MID$(a$,3,1))*256*256
            num = num + ASC(MID$(a$,4,1))*256*256*256
            lword = num
END FUNCTION

FUNCTION aword(a$) AS LONG
            DIM num AS LONG
            num = 0
            num = num + ASC(MID$(a$,1,1))
            num = num + ASC(MID$(a$,2,1))*256
            aword = num
END FUNCTION

TYPE PIXELDATA
    r AS INTEGER
    g AS INTEGER
    b AS INTEGER
END TYPE



            CLS

            COLOR 14, 1
            PRINT center$("Oerg866's 24-bit BMP to DMA CRAM Value Converter V1.2", 80)

            LOCATE 1,1

            DIM ERRORLEVEL AS INTEGER

            COLOR 8,0
            PRINT ""
            PRINT ""
            PRINT "[CON] Amiga can suck it, we've got more fucken colors ;D"
            PRINT "[CON] ==="
            PRINT "[CON] Checking arguments"
            IF COMMAND$ = "" THEN errorlevel = 1
            IF COMMAND$(1) = "" THEN errorlevel = 1
            IF COMMAND$(2) = "" THEN ERRORLEVEL = 1

            IF ERRORLEVEL = 1 THEN
                COLOR 12,0
                PRINT "[CON] ERROR: Invalid command line argument(s)!"
                COLOR 8, 0
                PRINT "[CON] Usage: bmpcramdma.exe <input> <output>"
                PRINT "[CON] ===="
                PRINT "[CON] http://www.mdscene.net ... Written by Oerg866"
                PRINT "[CON] Email at oerg866@mdscene.net"
                COLOR 7, 0

            End
            END IF

            COLOR 2,0
            PRINT "[CON] Arguments seem valid."

            COLOR 8, 0
            PRINT ""

            IF DIR$(COMMAND$(1)) = "" THEN
                COLOR 12,0
                PRINT "[File I/O] ERROR: File not found: "+ COMMAND$(1)
            COLOR 7, 0
                end
            END IF


            KILL COMMAND$(2)
            OPEN COMMAND$(1) FOR BINARY AS #1
            OPEN COMMAND$(2) FOR BINARY AS #2
            a$ = SPACE$(&h40)
            GET #1, ,a$
            PRINT "[Conversion] Reading BMP Header"
            IF MID$(a$, 1, 2) <> "BM" THEN
                   COLOR 12,0
                   PRINT "[Conversion] ERROR: Invalid input file (No BMP header!)"
                   COLOR 7, 0
                   CLOSE
                   END             
            END IF
               COLOR 8, 0
            DIM imagestart AS LONG
            imagestart = lword(MID$(a$, 11, 4))
            IF imagestart = 0 OR imagestart > LOF(1) THEN
                   COLOR 12,0
                   PRINT "[Conversion] ERROR: File has an invalid BMP image start value!"
                   COLOR 7, 0
                   CLOSE
                   End          
            END IF
            COLOR 8, 0
            IF lword(MID$(a$, 19, 4)) <> &hC6 THEN
                   COLOR 14,0
                   PRINT "[Conversion] Warning: Image width is not 198 pixels!"
                   COLOR 8, 0
            END IF
            IF aword(MID$(A$, &h1D, 2)) <> 24 THEN
                   COLOR 12,0
                   PRINT "[Conversion] ERROR: File isn't a 24bpp bitmap image!"
                   COLOR 7, 0
                    CLOSE
                   END             
            END IF

            DIM yres AS LONG
            yres = lWORD(MID$(a$, &h17, 4))
            DIM t AS LONG
            t=1
            DIM r AS INTEGER
            DIM g AS INTEGER
            DIM b AS INTEGER
            DIM mdcolor AS INTEGER
            DIM bytecount AS LONG
            bytecount = lword(MID$(a$, &h23, 4))
            pic$ = SPACE$(bytecount)
            GET #1, imagestart + 1, pic$
            CLOSE #1
            DIM aPIXELS(16,yres) AS PIXELDATA
            t = 0
            DIM x AS LONG
            DIM y AS LONG
            DIM i AS LONG
            x=16
            y=1
            PRINT "[Conversion] Amount of bytes to read: " + STR$(bytecount)
            print i
            FOR i = 1 TO bytecount STEP 3
            
                    aPIXELS(x, y).r = ASC(MID$(pic$, bytecount-i-1, 1))
                    aPIXELS(x, y).b = ASC(MID$(pic$, bytecount-i-0, 1))
                    aPIXELS(x, y).g = ASC(MID$(pic$, bytecount-i+1, 1))
                x=x-1
                IF x < 1 THEN
                x=16
                y=y+1
                END IF
                t=t+1
            NEXT i
            PRINT "[Conversion] Generating RGB 3,3,3 image output of 16 x " + STR$(yres) + " pixels..."
            FOR y = 1 TO yres
              FOR x = 1 TO 16
                r = INT(aPIXELS(x, y).r*7/256)*2
                g = InT(aPIXELS(x, y).g*7/256)*2
                b = INT(aPIXELS(x, y).b*7/256)*2
                IF r>14 THEN r=14
                IF g>14 THEN g=14
                IF b>14 THEN b=14
                mdcolor = b*256 + g*16 + r
                PUT  #2,,CHR$(INT(mdcolor/256))
                PUT  #2,,CHR$(INT(mdcolor MOD 256))
              NEXT x
            NEXT y
            COLOR 10,0
            PRINT "[Conversion] Conversion finished! Output size: " + STR$(LOF(2))
            CLOSE #2

            COLOR 7, 0