BASIMAGE - Put image files in BAS code to use with _PUTIMAGE
#1
BASIMAGE converts images to BAS code and sets it up as an image handle to use with _PUTIMAGE to display the image.  Using this program you can easily embed little images to use directly in your code, and inside your EXE programs, without extracting them to disk first.  First the program will ask you for an image to load, then a filename of the BAS code to output.

I will post the BASIMAGE program below first, and then an example program made by BASIMAGE output to show what it can do.

- Dav

Code: (Select All)
'==================
'BASIMAGE.BAS v0.22
'==================
'Coded by Dav for QB64-PE, JULY/2023

'=-=-=-=
'ABOUT :
'=-=-=-=

'BASIMAGE lets you easily put images INSIDE your QB64 compiled programs.
'It does this by loading an image, then converting the screen memory to
'BAS code that you can add to your programs.  When you run the code, it
'recreates the data to an image handle you can use with _PUTIMAGE.

'BASIMAGE will ask you for an image load, and the BAS file to create.

'=========================================================================

DEFINT A-Z
DECLARE FUNCTION E$ (B$)

PRINT
PRINT "=============="
PRINT "BASIMAGE v0.22"
PRINT "=============="
PRINT

INPUT "IMAGE File to load --> ", IN$
INPUT "BAS File to make ----> ", OUT$: IF OUT$ = "" THEN END

'Load image file to screen mode
SCREEN _LOADIMAGE(IN$, 32): SLEEP 1
DIM m AS _MEM: m = _MEMIMAGE(0)

'Grab screen data
INDATA$ = SPACE$(m.SIZE)
_MEMGET m, m.OFFSET, INDATA$
'Compress it
INDATA$ = _DEFLATE$(INDATA$)
'get screen specs
wid = _WIDTH: hih = _HEIGHT

SCREEN 0

OPEN OUT$ FOR OUTPUT AS 2
PRINT: PRINT "Converting image to BAS code...";

Q$ = CHR$(34) 'quotation mark
SCREEN _NEWIMAGE(600, 600, 32)
pic& = BASIMAGE1&
_PUTIMAGE (0, 0), pic&

PRINT #2, "'EXAMPLE USAGE OF BASIMAGE1&"
PRINT #2, "'==========================="
PRINT #2, "'SCREEN _NEWIMAGE(600, 600, 32)"
PRINT #2, "'pic& = BASIMAGE1&: _PUTIMAGE (0, 0), pic&"
PRINT #2, ""
PRINT #2, "FUNCTION BASIMAGE1& '"; IN$
PRINT #2, "v&=_NEWIMAGE("; wid; ","; hih; ",32)"
PRINT #2, "DIM m AS _MEM:m=_MEMIMAGE(v&)"
PRINT #2, "A$ = "; Q$; Q$
PRINT #2, "A$ = A$ + "; Q$;

BC& = 1

DO
    a$ = MID$(INDATA$, BC&, 3)
    BC& = BC& + 3: LL& = LL& + 4
    IF LL& = 60 THEN
        LL& = 0
        PRINT #2, E$(a$);: PRINT #2, Q$
        PRINT #2, "A$ = A$ + "; Q$;
    ELSE
        PRINT #2, E$(a$);
    END IF
    IF LEN(INDATA$) - BC& < 3 THEN
        a$ = MID$(INDATA$, LEN(INDATA$) - BC&, 1): B$ = E$(a$)
        SELECT CASE LEN(B$)
            CASE 0: a$ = Q$
            CASE 1: a$ = "%%%" + B$ + Q$
            CASE 2: a$ = "%%" + B$ + Q$
            CASE 3: a$ = "%" + B$ + Q$
        END SELECT: PRINT #2, a$;: EXIT DO
    END IF
LOOP: PRINT #2, ""

PRINT #2, "btemp$="; Q$; Q$
PRINT #2, "FOR i&=1TO LEN(A$) STEP 4:B$=MID$(A$,i&,4)"
PRINT #2, "IF INSTR(1,B$,"; Q$; "%"; Q$; ") THEN"
PRINT #2, "FOR C%=1 TO LEN(B$):F$=MID$(B$,C%,1)"
PRINT #2, "IF F$<>"; Q$; "%"; Q$; "THEN C$=C$+F$"
PRINT #2, "NEXT:B$=C$:END IF:FOR j=1 TO LEN(B$)"
PRINT #2, "IF MID$(B$,j,1)="; Q$; "#"; Q$; " THEN"
PRINT #2, "MID$(B$,j)="; Q$; "@"; Q$; ":END IF:NEXT"
PRINT #2, "FOR t%=LEN(B$) TO 1 STEP-1"
PRINT #2, "B&=B&*64+ASC(MID$(B$,t%))-48"
PRINT #2, "NEXT:X$="; Q$; Q$; ":FOR t%=1 TO LEN(B$)-1"
PRINT #2, "X$=X$+CHR$(B& AND 255):B&=B&\256"
PRINT #2, "NEXT:btemp$=btemp$+X$:NEXT"
PRINT #2, "btemp$=_INFLATE$(btemp$,m.SIZE)"
PRINT #2, "_MEMPUT m, m.OFFSET, btemp$: _MEMFREE m"
PRINT #2, "BASIMAGE1& = _COPYIMAGE(v&): _FREEIMAGE v&"
PRINT #2, "END FUNCTION"

PRINT "Done!"
PRINT UCASE$(OUT$); " saved."
END

FUNCTION E$ (B$)

    FOR T% = LEN(B$) TO 1 STEP -1
        B& = B& * 256 + ASC(MID$(B$, T%))
    NEXT

    a$ = ""
    FOR T% = 1 TO LEN(B$) + 1
        g$ = CHR$(48 + (B& AND 63)): B& = B& \ 64
        'If @ is here, replace it with #
        'To fix problem posting code in the QB64 forum.
        'It'll be restored during the decoding process.
        IF g$ = "@" THEN g$ = "#"
        a$ = a$ + g$
    NEXT: E$ = a$

END FUNCTION


And here's a sample output program made with code BASIMAGE output.  This program shows a small chess piece image which is embedded as code.

Code: (Select All)
'KING.BAS
'Example of what BASIMAGE can do

king& = BASIMAGE1& 'Load the king image

SCREEN _NEWIMAGE(600, 600, 32) 'set up a screen
_PUTIMAGE (0, 0), king& 'put it on screen

FUNCTION BASIMAGE1& 'king.png
    v& = _NEWIMAGE(150, 150, 32)
    DIM m AS _MEM: m = _MEMIMAGE(v&)
    A$ = ""
    A$ = A$ + "haIkMfSLK3346EG7GZBeZBGZBejBEjBgVbDV[#>6i\i<0a>344hoPhKGXGaK"
    A$ = A$ + "6C8>V[Fl60A0#`3ONh`100000V1_ofU?Okj[gQjH2l=W?MLh?e0a7h?6eYn6"
    A$ = A$ + "34WP_0_2f3bmZolkO5i^cb0a9h7:eFEA_:P1RG`NLiecONjh;A6k<7d[:`[W"
    A$ = A$ + "?Q_m<c2>kUhEo7eOoPfc2>k_M:L;HBb:9gMF;?LUDom4J;IUPG1EQ5Lfd?8\"
    A$ = A$ + "8F`ISC1K56o:H_8Ol\HlfQJ0N5DK:=fkd?8\Ib:QaB0f<IU`hD1KUQFK<hF`"
    A$ = A$ + "JIY>5^5TBliNH6WiZmZ<>6^703lm6Z3C]NZ6SU<OPohmgFmeQeYU?HF[9m_8"
    A$ = A$ + "llKE[iJ>h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DlK\jH`[0l:`14gG4<VG5_oD3TKP=#bA]hMT"
    A$ = A$ + "j#T_n_TWGPECnk1GE>5]IeITLD;GcEdVEWCbAZMZB]I9?o0hEP=8[4enDA_2"
    A$ = A$ + "gbOH#WRf\j0`[PMdY\`m1V3gGXC9i8e>4]Ie9h0W2gbITERJ_IAN5^U=aI>5"
    A$ = A$ + "^U]iQWWH7iD3iF\6heBLoI9[dOna=iNB=L[hkL53TSOjX#YZMSJkFADWWOFH"
    A$ = A$ + "P?A]C\[^5nECmYC7OA^7POiHb:YUkc#Z9O]9RK]?>UjjIcdnUjj5_3>EIgBM"
    A$ = A$ + "mRS9OL4DG_ZT`hWTV;HL8ZSCm\kFUb5hF;0mAP?AO4HGd71n6mAPm8[TNN\Y"
    A$ = A$ + "Z5?\n0a]6eYDGOi=`]Vf[DG7iE`[`[`[`[l2hESkEFliWajl`HmY^nd:X?2j"
    A$ = A$ + "6RgSXlX`#X?2j3d71fR2UZ^o5[3NebL:eeGN3L[Ym:eeAN5l:l:lZfkEhFKd"
    A$ = A$ + "Y`[6d[Pe3Sc#6jS0g3>e8X?2l9jS000`5lG0Z8mh%%%0"
    btemp$ = ""
    FOR i& = 1 TO LEN(A$) STEP 4: B$ = MID$(A$, i&, 4)
        IF INSTR(1, B$, "%") THEN
            FOR C% = 1 TO LEN(B$): F$ = MID$(B$, C%, 1)
                IF F$ <> "%" THEN C$ = C$ + F$
            NEXT: B$ = C$: END IF: FOR j = 1 TO LEN(B$)
            IF MID$(B$, j, 1) = "#" THEN
        MID$(B$, j) = "@": END IF: NEXT
        FOR t% = LEN(B$) TO 1 STEP -1
            B& = B& * 64 + ASC(MID$(B$, t%)) - 48
            NEXT: X$ = "": FOR t% = 1 TO LEN(B$) - 1
            X$ = X$ + CHR$(B& AND 255): B& = B& \ 256
    NEXT: btemp$ = btemp$ + X$: NEXT
    btemp$ = _INFLATE$(btemp$,m.SIZE)
    _MEMPUT m, m.OFFSET, btemp$: _MEMFREE m
    BASIMAGE1& = _COPYIMAGE(v&): _FREEIMAGE v&
END FUNCTION

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
BASIMAGE - Put image files in BAS code to use with _PUTIMAGE - by Dav - 04-26-2022, 11:23 PM



Users browsing this thread: 3 Guest(s)