Author Topic: Recursive Image to MEM  (Read 1157 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Recursive Image to MEM
« on: June 19, 2020, 04:02:43 pm »
So @Dav , a while back, had made a program that allows you to store a picture as a MEM block in a program to use it as an image handle. I turned it into a GUI conversion program along with his BASIMAGE program. Now this is something a bit different here. I saw that @Ashish had a Github with a program/library called Iconizer with a folder chock full of good icons. I thought it would be cool to make a program that recursively runs the MEM conversion function for every PNG in a directory and write them to .MEM files. The only issue was that they didn't have transparent backgrounds and the way that Dav's code was set up, you couldn't affect the transparency. I changed it up to allow for that. Here is the code for that along with a zip folder of the original PNG files and the MEM files:
Code: QB64: [Select]
  1. DEFINT A-Z
  2. SHELL "DIR /b " + CHR$(34) + _STARTDIR$ + "\Images\*.png" + CHR$(34) + " > imagelist.txt"
  3. OPEN "imagelist.txt" FOR BINARY AS F
  4. IF LOF(F) = 0 THEN
  5.     KILL "imagelist.txt"
  6.     CHDIR "Images\"
  7.     PRINT _CWD$
  8.     DO
  9.         IF NOT EOF(F) THEN
  10.             LINE INPUT #F, image$
  11.             IN$ = image$
  12.             OUT$ = StripDirectory(image$)
  13.             OUT$ = MID$(OUT$, 1, _INSTRREV(OUT$, ".png") - 1) + ".MEM"
  14.             i& = _LOADIMAGE(IN$, 32):
  15.             SCREEN _NEWIMAGE(_WIDTH(i&), _HEIGHT(i&), 32)
  16.             _SOURCE i&
  17.             _DEST 0
  18.             _SETALPHA 0, _RGB32(85, 85, 85) TO _RGB32(255, 255, 255), i&
  19.             _PUTIMAGE
  20.             DIM m AS _MEM: m = _MEMIMAGE(0)
  21.             INDATA$ = SPACE$(m.SIZE)
  22.             _MEMGET m, m.OFFSET, INDATA$
  23.             INDATA$ = _DEFLATE$(INDATA$)
  24.             wid = _WIDTH: hih = _HEIGHT
  25.             OPEN OUT$ FOR OUTPUT AS 2
  26.             Q$ = CHR$(34) 'quotation mark
  27.             inFunc$ = LEFT$(IN$, LEN(IN$) - 4)
  28.             FOR i = 32 TO 47
  29.                 IF INSTR(inFunc$, CHR$(i)) THEN
  30.                     inFunc$ = String.Remove(inFunc$, CHR$(i))
  31.                 END IF
  32.             NEXT
  33.             FOR i = 58 TO 64
  34.                 IF INSTR(inFunc$, CHR$(i)) THEN
  35.                     inFunc$ = String.Remove(inFunc$, CHR$(i))
  36.                 END IF
  37.             NEXT
  38.             FOR i = 91 TO 96
  39.                 IF INSTR(inFunc$, CHR$(i)) THEN
  40.                     IF i <> 92 THEN
  41.                         inFunc$ = String.Remove(inFunc$, CHR$(i))
  42.                     END IF
  43.                 END IF
  44.             NEXT
  45.             PRINT #2, "$IF " + UCASE$(inFunc$) + " = UNDEFINED THEN"
  46.             PRINT #2, "$LET " + UCASE$(inFunc$) + " = TRUE"
  47.             PRINT #2, "FUNCTION __" + StripDirectory(inFunc$) + "&"
  48.             PRINT #2, "DIM v&"
  49.             PRINT #2, "DIM A$"
  50.             PRINT #2, "DIM btemp$"
  51.             PRINT #2, "DIM i&"
  52.             PRINT #2, "DIM B$"
  53.             PRINT #2, "DIM C%"
  54.             PRINT #2, "DIM F$"
  55.             PRINT #2, "DIM C$"
  56.             PRINT #2, "DIM j"
  57.             PRINT #2, "DIM t%"
  58.             PRINT #2, "DIM B&"
  59.             PRINT #2, "DIM X$"
  60.             PRINT #2, "v&=_NEWIMAGE("; wid; ","; hih; ",32)"
  61.             PRINT #2, "DIM m AS _MEM:m=_MEMIMAGE(v&)"
  62.             PRINT #2, "A$ = "; Q$; Q$
  63.             PRINT #2, "A$ = A$ + "; Q$;
  64.             BC& = 1
  65.             DO
  66.                 a$ = MID$(INDATA$, BC&, 3)
  67.                 BC& = BC& + 3: LL& = LL& + 4
  68.                 IF LL& = 60 THEN
  69.                     LL& = 0
  70.                     PRINT #2, E$(a$);: PRINT #2, Q$
  71.                     PRINT #2, "A$ = A$ + "; Q$;
  72.                 ELSE
  73.                     PRINT #2, E$(a$);
  74.                 END IF
  75.                 IF LEN(INDATA$) - BC& < 3 THEN
  76.                     a$ = MID$(INDATA$, LEN(INDATA$) - BC&, 1): B$ = E$(a$)
  77.                     SELECT CASE LEN(B$)
  78.                         CASE 0: a$ = Q$
  79.                         CASE 1: a$ = "%%%" + B$ + Q$
  80.                         CASE 2: a$ = "%%" + B$ + Q$
  81.                         CASE 3: a$ = "%" + B$ + Q$
  82.                     END SELECT: PRINT #2, a$;: EXIT DO
  83.                 END IF
  84.             LOOP: PRINT #2, ""
  85.             PRINT #2, "btemp$="; Q$; Q$
  86.             PRINT #2, "FOR i&=1TO LEN(A$) STEP 4:B$=MID$(A$,i&,4)"
  87.             PRINT #2, "IF INSTR(1,B$,"; Q$; "%"; Q$; ") THEN"
  88.             PRINT #2, "FOR C%=1 TO LEN(B$):F$=MID$(B$,C%,1)"
  89.             PRINT #2, "IF F$<>"; Q$; "%"; Q$; "THEN C$=C$+F$"
  90.             PRINT #2, "NEXT:B$=C$:END IF:FOR j=1 TO LEN(B$)"
  91.             PRINT #2, "IF MID$(B$,j,1)="; Q$; "#"; Q$; " THEN"
  92.             PRINT #2, "MID$(B$,j)="; Q$; "@"; Q$; ":END IF:NEXT"
  93.             PRINT #2, "FOR t%=LEN(B$) TO 1 STEP-1"
  94.             PRINT #2, "B&=B&*64+ASC(MID$(B$,t%))-48"
  95.             PRINT #2, "NEXT:X$="; Q$; Q$; ":FOR t%=1 TO LEN(B$)-1"
  96.             PRINT #2, "X$=X$+CHR$(B& AND 255):B&=B&\256"
  97.             PRINT #2, "NEXT:btemp$=btemp$+X$:NEXT"
  98.             PRINT #2, "btemp$=_INFLATE$(btemp$)"
  99.             PRINT #2, "_MEMPUT m, m.OFFSET, btemp$: _MEMFREE m"
  100.             PRINT #2, "__" + inFunc$ + "& = _COPYIMAGE(v&): _FREEIMAGE v&"
  101.             PRINT #2, "END FUNCTION"
  102.             PRINT #2, "$END IF"
  103.             CLOSE #2
  104.         END IF
  105.     LOOP UNTIL image$ = "" OR EOF(F)
  106. FUNCTION E$ (B$)
  107.     FOR T% = LEN(B$) TO 1 STEP -1
  108.         B& = B& * 256 + ASC(MID$(B$, T%))
  109.     NEXT
  110.     a$ = ""
  111.     FOR T% = 1 TO LEN(B$) + 1
  112.         g$ = CHR$(48 + (B& AND 63)): B& = B& \ 64
  113.         IF g$ = "@" THEN g$ = "#"
  114.         a$ = a$ + g$
  115.     NEXT: E$ = a$
  116. FUNCTION StripDirectory$ (OFile$)
  117.     DO
  118.         OFile$ = RIGHT$(OFile$, LEN(OFile$) - INSTR(OFile$, "\"))
  119.     LOOP WHILE INSTR(OFile$, "\")
  120.     StripDirectory$ = OFile$
  121. FUNCTION String.Remove$ (a AS STRING, b AS STRING)
  122.     DIM c AS STRING
  123.     c = ""
  124.     j = INSTR(a, b)
  125.     IF j > 0 THEN
  126.         r$ = LEFT$(a, j - 1) + c + String.Remove(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b)
  127.     ELSE
  128.         r$ = a
  129.     END IF
  130.     String.Remove = r$
  131.  
Shuwatch!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Recursive Image to MEM
« Reply #1 on: June 20, 2020, 12:34:13 am »
Oh cool! Nice work.
BTW, I never thought of posting Iconizer library at the forum. I thought it will be hardly useful to anyone
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials