Author Topic: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.  (Read 2164 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
This is a companion to the BASFILE program I posted earlier. Instead of converting files to BAS code that recreates the file on disk, BASIMAGE converts images to BAS code and sets it up as an image handle to use with _PUTIMAGE.  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.

I will post the program, and them an example of what it outputs.

- Dav

BASIMAGE.BAS
Code: QB64: [Select]
  1. '==================
  2. 'BASIMAGE.BAS v0.21
  3. '==================
  4. 'Coded by Dav for QB64GL 1.4, MAY/2020
  5.  
  6. '=-=-=-=
  7. 'ABOUT :
  8. '=-=-=-=
  9.  
  10. 'BASIMAGE lets you easily put images INSIDE your QB64 compiled programs.
  11. 'It does this by loading an image, then converting the screen memory to
  12. 'BAS code that you can add to your programs.  When you run the code, it
  13. 'recreates the data to an image handle you can use with _PUTIMAGE.
  14.  
  15. 'BASIMAGE will ask you for an image load, and the BAS file to create.
  16.  
  17. '=========================================================================
  18.  
  19. DEFINT A-Z
  20.  
  21. PRINT "=============="
  22. PRINT "BASIMAGE v0.21"
  23. PRINT "=============="
  24.  
  25. INPUT "IMAGE File to load --> ", IN$
  26. INPUT "BAS File to make ----> ", OUT$: IF OUT$ = "" THEN END
  27.  
  28. 'Load image file to screen mode
  29. SCREEN _LOADIMAGE(IN$, 32): SLEEP 1
  30. DIM m AS _MEM: m = _MEMIMAGE(0)
  31.  
  32. 'Grab screen data
  33. INDATA$ = SPACE$(m.SIZE)
  34. _MEMGET m, m.OFFSET, INDATA$
  35. 'Compress it
  36. INDATA$ = _DEFLATE$(INDATA$)
  37. 'get screen specs
  38. wid = _WIDTH: hih = _HEIGHT
  39.  
  40.  
  41. PRINT: PRINT "Converting image to BAS code...";
  42.  
  43. Q$ = CHR$(34) 'quotation mark
  44. SCREEN _NEWIMAGE(600, 600, 32)
  45. pic& = BASIMAGE1&
  46. _PUTIMAGE (0, 0), pic&
  47.  
  48. PRINT #2, "'EXAMPLE USAGE OF BASIMAGE1&"
  49. PRINT #2, "'==========================="
  50. PRINT #2, "'SCREEN _NEWIMAGE(600, 600, 32)"
  51. PRINT #2, "'pic& = BASIMAGE1&: _PUTIMAGE (0, 0), pic&"
  52. PRINT #2, ""
  53. PRINT #2, "FUNCTION BASIMAGE1& '"; IN$
  54. PRINT #2, "v&=_NEWIMAGE("; wid; ","; hih; ",32)"
  55. PRINT #2, "DIM m AS _MEM:m=_MEMIMAGE(v&)"
  56. PRINT #2, "A$ = "; Q$; Q$
  57. PRINT #2, "A$ = A$ + "; Q$;
  58.  
  59. BC& = 1
  60.  
  61.     a$ = MID$(INDATA$, BC&, 3)
  62.     BC& = BC& + 3: LL& = LL& + 4
  63.     IF LL& = 60 THEN
  64.         LL& = 0
  65.         PRINT #2, E$(a$);: PRINT #2, Q$
  66.         PRINT #2, "A$ = A$ + "; Q$;
  67.     ELSE
  68.         PRINT #2, E$(a$);
  69.     END IF
  70.     IF LEN(INDATA$) - BC& < 3 THEN
  71.         a$ = MID$(INDATA$, LEN(INDATA$) - BC&, 1): B$ = E$(a$)
  72.         SELECT CASE LEN(B$)
  73.             CASE 0: a$ = Q$
  74.             CASE 1: a$ = "%%%" + B$ + Q$
  75.             CASE 2: a$ = "%%" + B$ + Q$
  76.             CASE 3: a$ = "%" + B$ + Q$
  77.         END SELECT: PRINT #2, a$;: EXIT DO
  78.     END IF
  79. LOOP: PRINT #2, ""
  80.  
  81. PRINT #2, "btemp$="; Q$; Q$
  82. PRINT #2, "FOR i&=1TO LEN(A$) STEP 4:B$=MID$(A$,i&,4)"
  83. PRINT #2, "IF INSTR(1,B$,"; Q$; "%"; Q$; ") THEN"
  84. PRINT #2, "FOR C%=1 TO LEN(B$):F$=MID$(B$,C%,1)"
  85. PRINT #2, "IF F$<>"; Q$; "%"; Q$; "THEN C$=C$+F$"
  86. PRINT #2, "NEXT:B$=C$:END IF:FOR j=1 TO LEN(B$)"
  87. PRINT #2, "IF MID$(B$,j,1)="; Q$; "#"; Q$; " THEN"
  88. PRINT #2, "MID$(B$,j)="; Q$; "@"; Q$; ":END IF:NEXT"
  89. PRINT #2, "FOR t%=LEN(B$) TO 1 STEP-1"
  90. PRINT #2, "B&=B&*64+ASC(MID$(B$,t%))-48"
  91. PRINT #2, "NEXT:X$="; Q$; Q$; ":FOR t%=1 TO LEN(B$)-1"
  92. PRINT #2, "X$=X$+CHR$(B& AND 255):B&=B&\256"
  93. PRINT #2, "NEXT:btemp$=btemp$+X$:NEXT"
  94. PRINT #2, "btemp$=_INFLATE$(btemp$)"
  95. PRINT #2, "_MEMPUT m, m.OFFSET, btemp$: _MEMFREE m"
  96. PRINT #2, "BASIMAGE1& = _COPYIMAGE(v&): _FREEIMAGE v&"
  97. PRINT #2, "END FUNCTION"
  98.  
  99. PRINT "Done!"
  100. PRINT UCASE$(OUT$); " saved."
  101.  
  102. FUNCTION E$ (B$)
  103.  
  104.     FOR T% = LEN(B$) TO 1 STEP -1
  105.         B& = B& * 256 + ASC(MID$(B$, T%))
  106.     NEXT
  107.  
  108.     a$ = ""
  109.     FOR T% = 1 TO LEN(B$) + 1
  110.         g$ = CHR$(48 + (B& AND 63)): B& = B& \ 64
  111.         'If @ is here, replace it with #
  112.         'To fix problem posting code in the QB64 forum.
  113.         'It'll be restored during the decoding process.
  114.         IF g$ = "@" THEN g$ = "#"
  115.         a$ = a$ + g$
  116.     NEXT: E$ = a$
  117.  
  118.  


KING.BAS (a King image embedded in the code to use)
Code: QB64: [Select]
  1. 'KING.BAS
  2. 'Example of what BASIMAGE can do
  3.  
  4. king& = BASIMAGE1& 'Load the king image
  5.  
  6. SCREEN _NEWIMAGE(600, 600, 32) 'set up a screen
  7. _PUTIMAGE (0, 0), king& 'put it on screen
  8.  
  9. FUNCTION BASIMAGE1& 'king.png
  10.     v& = _NEWIMAGE(150, 150, 32)
  11.     DIM m AS _MEM: m = _MEMIMAGE(v&)
  12.     A$ = ""
  13.     A$ = A$ + "haIkMfSLK3346EG7GZBeZBGZBejBEjBgVbDV[#>6i\i<0a>344hoPhKGXGaK"
  14.     A$ = A$ + "6C8>V[Fl60A0#`3ONh`100000V1_ofU?Okj[gQjH2l=W?MLh?e0a7h?6eYn6"
  15.     A$ = A$ + "34WP_0_2f3bmZolkO5i^cb0a9h7:eFEA_:P1RG`NLiecONjh;A6k<7d[:`[W"
  16.     A$ = A$ + "?Q_m<c2>kUhEo7eOoPfc2>k_M:L;HBb:9gMF;?LUDom4J;IUPG1EQ5Lfd?8\"
  17.     A$ = A$ + "8F`ISC1K56o:H_8Ol\HlfQJ0N5DK:=fkd?8\Ib:QaB0f<IU`hD1KUQFK<hF`"
  18.     A$ = A$ + "JIY>5^5TBliNH6WiZmZ<>6^703lm6Z3C]NZ6SU<OPohmgFmeQeYU?HF[9m_8"
  19.     A$ = A$ + "llKE[iJ>hF1B;;lo?j=c=N^M57cH<EK6edQf:Cjn68O27><dWXfOV31O7obP"
  20.     A$ = A$ + "h#GJ<`]<0IU4Nn7Dk5hEM2hE`>kE1Dk5edYHo51gZfN5>UA`i^EJ\7772eiC"
  21.     A$ = A$ + "XXK56SAe^bJLZ8ZcS`PNE0enbZl:ei#X;L[dHmFH<BEWoPIiFZmVijD<oc>Q"
  22.     A$ = A$ + "_n[`JDA]o<IOO1DW_P5iEFe]BS_KGYo?GAbAZm8J[ZS8[4enDA_BM>2f\KYf"
  23.     A$ = A$ + "W2l`jlCMn1f\GYN\BcGkQlkZl;OTEGV_FQEeoge;WaYl>IUH1_BN>1Z^KY`Y"
  24.     A$ = A$ + ">Vo\1Y>O0MQGUNm3[o#ii3XjNU2gj^[_jL1\K^5N5hM_jQ8AMN0f=_ZUSUEj"
  25.     A$ = A$ + "eUeGL7BF9d78\7^5N5d3N5??4M<hE`>jDOG?_g>ELN0C^V\6fk0d71c3mA0<"
  26.     A$ = A$ + "Gd71;3mA0<1aiO;Y4;^OPT7SG_`hJIN<^C=TK9?_1cf[Dk>C1NUS8iX`lVXf"
  27.     A$ = A$ + "M6R7f__EWgPA9_o5;jFi>5on:O`3of533>U_H;kGo=3mA0\2:iFVhgXSCiO<"
  28.     A$ = A$ + "XGaL5f1DXDe]EQGe9X?2^7mA0D;d71o3mA0DAR__\B:ABOP68?0eWK9_WSJ]"
  29.     A$ = A$ + "G5_gD3T30l:`7T>o9]f[n0_ZKAYGm>[1i^UdeKN[m:FKkm;Yc7Mcm:fOnjFB"
  30.     A$ = A$ + "Og:gJ_RmX_nUdmg\FkEl>UXS9iXFkE<WPM<2mZ8Zon3hEPO0_2`[0_0N5PG1"
  31.     A$ = A$ + "N1l:0_2L0FHmRbJ`Zo8M^1EiE<G>M8IU8YO`0Zc3`^iEQf=JUGakb[>WTSJi"
  32.     A$ = A$ + "kKRdeD?NE7BbA]bY:fGXjl0PG1f7l:0_2l2hE#UAeH]Gb[H<gk8b:AJkE1DW"
  33.     A$ = A$ + "?PZkEQaCZeNEL_Y5_Zo8iXFkDlK\jH`[0l:`14gG4<VG5_oD3TKP=#bA]hMT"
  34.     A$ = A$ + "j#T_n_TWGPECnk1GE>5]IeITLD;GcEdVEWCbAZMZB]I9?o0hEP=8[4enDA_2"
  35.     A$ = A$ + "gbOH#WRf\j0`[PMdY\`m1V3gGXC9i8e>4]Ie9h0W2gbITERJ_IAN5^U=aI>5"
  36.     A$ = A$ + "^U]iQWWH7iD3iF\6heBLoI9[dOna=iNB=L[hkL53TSOjX#YZMSJkFADWWOFH"
  37.     A$ = A$ + "P?A]C\[^5nECmYC7OA^7POiHb:YUkc#Z9O]9RK]?>UjjIcdnUjj5_3>EIgBM"
  38.     A$ = A$ + "mRS9OL4DG_ZT`hWTV;HL8ZSCm\kFUb5hF;0mAP?AO4HGd71n6mAPm8[TNN\Y"
  39.     A$ = A$ + "Z5?\n0a]6eYDGOi=`]Vf[DG7iE`[`[`[`[l2hESkEFliWajl`HmY^nd:X?2j"
  40.     A$ = A$ + "6RgSXlX`#X?2j3d71fR2UZ^o5[3NebL:eeGN3L[Ym:eeAN5l:l:lZfkEhFKd"
  41.     A$ = A$ + "Y`[6d[Pe3Sc#6jS0g3>e8X?2l9jS000`5lG0Z8mh%%%0"
  42.     btemp$ = ""
  43.     FOR i& = 1 TO LEN(A$) STEP 4: B$ = MID$(A$, i&, 4)
  44.         IF INSTR(1, B$, "%") THEN
  45.             FOR C% = 1 TO LEN(B$): F$ = MID$(B$, C%, 1)
  46.                 IF F$ <> "%" THEN C$ = C$ + F$
  47.             NEXT: B$ = C$: END IF: FOR j = 1 TO LEN(B$)
  48.             IF MID$(B$, j, 1) = "#" THEN
  49.         MID$(B$, j) = "@": END IF: NEXT
  50.         FOR t% = LEN(B$) TO 1 STEP -1
  51.             B& = B& * 64 + ASC(MID$(B$, t%)) - 48
  52.             NEXT: X$ = "": FOR t% = 1 TO LEN(B$) - 1
  53.             X$ = X$ + CHR$(B& AND 255): B& = B& \ 256
  54.     NEXT: btemp$ = btemp$ + X$: NEXT
  55.     btemp$ = _INFLATE$(btemp$)
  56.     _MEMPUT m, m.OFFSET, btemp$: _MEMFREE m
  57.     BASIMAGE1& = _COPYIMAGE(v&): _FREEIMAGE v&
  58.  
« Last Edit: May 17, 2020, 09:38:24 pm by Dav »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #1 on: May 18, 2020, 07:44:56 am »
Looks and works great! Thanks, Dav. I'll definitely be doing some experimenting with it. How did you come up with the idea and code to do such a complex thing as converting screen memory to text or files to executables?
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #2 on: May 18, 2020, 10:29:21 am »
Dav, I've adapted your BASIMAGE code into my GUI program that I have on my Github. Here is a screenshot showing how the program looks:
 
pic2mem demo.png

If the user selects PIC2MEM then it SHELLs the pic2mem.exe located in the start directory with the input and output files as arguments. The program automatically names the function after the filename so that you can include multiple image creation functions without having to rename them manually. Attached is the output of Big_Note.png converted to MEM:
 
Let me know what you think!
« Last Edit: May 18, 2020, 10:36:18 am by SpriggsySpriggs »
Shuwatch!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #3 on: May 20, 2020, 12:51:30 pm »
Looks good, SpriggsySpriggs.  PIC2MEM is a better name for what it does.

How did you come up with the idea and code to do such a complex thing as converting screen memory to text or files to executables?

Well. putting resource files like images in a QB64 EXE is something I have been wanting for a while, but couldn't really come up with a good idea how to put images in EXE without dumping them as files first to use.  Then I remembered something @SMcNeill posted a while back, grabbing scrn to memory and saving it to a file.  That was the inspiration I needed. 

Whenever I get stuck on something, I start checking out what other people have done, it usually makes me start thinking in a new way.  Steve's code usually does that for me. 

- Dav
« Last Edit: May 20, 2020, 01:03:46 pm by Dav »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #4 on: May 20, 2020, 04:32:12 pm »
  Then I remembered something @SMcNeill posted a while back, grabbing scrn to memory and saving it to a file.  That was the inspiration I needed. 

This might've been what you're thinking of: https://www.qb64.org/forum/index.php?topic=1633.0

Very similar to what you have here, it turns screen images into DATA statements for embedding into BAS files.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #5 on: May 20, 2020, 06:07:58 pm »
That's a cool post all right (somehow I missed that one) but the one I got was a screen grab and restore sub - it may have been on [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there].  I started playing around with that, added _DEFLATE/_INFLATE, which also helped me do the video format kit I posted earlier this month.

- DAV
« Last Edit: May 20, 2020, 06:09:03 pm by Dav »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #6 on: May 22, 2020, 03:54:15 pm »
This code is such a lifesaver, man. I'm using it so much now haha. I'm embedding tons of pictures in my programs now for stuff like InForm-based programs. My latest program that basically makes a zip file (that I've named a QBZ archive) uses 7 embedded pictures created using the program I made that uses your code. Your work is much appreciated!
 
example 1 embed.png

 
example 2 embed.png
example 3 embed.png
« Last Edit: May 22, 2020, 04:00:13 pm by SpriggsySpriggs »
Shuwatch!

FellippeHeitor

  • Guest
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #7 on: May 22, 2020, 04:17:44 pm »
Tad offtopic, but still worth mentioning: InForm will resize any image assigned to controls (buttons, menu items) according to (i) button size or (ii) font size. Depending on the image's original size, the _PUTIMAGE resize will look faulty. Maybe you could downsize the images or maybe increase the control's sizes. I'm betting your images don't look like that originally, am I right?

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #8 on: May 22, 2020, 11:09:41 pm »
Yeah they look better before using them in that way but I just haven't gotten around to shrinking the images. I'll open them in an image editor sometime and shrink them down. I'm currently living in a hotel so I don't have my normal computer to do stuff with. As soon as I get back to my big PC then I will see about making them tinier. Edit: I tried one on a website online that can shrink an image. I turned my UAC shield picture to a 16 x 16 picture and it looks great. I'll do the same to all the others soon.
« Last Edit: May 22, 2020, 11:20:24 pm by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #9 on: May 22, 2020, 11:46:18 pm »
Thank you for reminding me to do that, Fellippe. Now the program looks a lot better. I was able to find 16 x 16 PNGs online that were close enough to the ones I had and so here is how it looks with the reduced image size. (It also reduced my EXE size by about 1.2 MB!)
qbz with new icons.png
qbz with new icons2.png
« Last Edit: May 22, 2020, 11:50:11 pm by SpriggsySpriggs »
Shuwatch!

FellippeHeitor

  • Guest
Re: BASIMAGE - Converts images to BAS code to use with _PUTIMAGE.
« Reply #10 on: May 23, 2020, 12:21:21 am »
Looking good!