QB64.org Forum

Active Forums => Programs => Topic started by: TrialAndTerror on October 29, 2019, 08:51:26 pm

Title: Convert pictures to 256-colour embedded sprites
Post by: TrialAndTerror on October 29, 2019, 08:51:26 pm
Hi, here's a piece of code I use to embed sprites in a program when I don'r want to use original PNG, JPG or BMP files.
I'm sure there's a better way to embed graphics. Anyone?

T&T

P.S. it outputs "Data.bas" containing sprite and colour data.

Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3. SCREEN _NEWIMAGE(640, 480, 32)
  4.  
  5. LOCATE 10, 18: INPUT "Afbeelding plus extensie (JPG,BMP,PNG) "; I$
  6.  
  7. IF _FILEEXISTS(I$) = 0 THEN LOCATE 22, 1: PRINT "Bestandsfout. Druk een toets...": SLEEP: SYSTEM
  8.  
  9. Stam$ = LEFT$(I$, LEN(I$) - 4)
  10.  
  11.  
  12. Afbeelding& = _LOADIMAGE(I$)
  13.  
  14. Breedte = _WIDTH(Afbeelding&)
  15. Hoogte = _HEIGHT(Afbeelding&)
  16.  
  17. Volgorde$ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@"
  18.  
  19. IF Breedte < 1 OR Hoogte < 1 OR Breedte > 320 OR Hoogte > 320 THEN LOCATE 22, 1: PRINT "Foute afmetingen. Druk een toets...": SLEEP: SYSTEM
  20.  
  21. DIM ch(0 TO 63 + 256 * 63 + 65536 * 63)
  22. DIM w&(1 TO Breedte * Hoogte)
  23. DIM v&(1 TO Breedte * Hoogte)
  24. DIM p&(32 TO Breedte * Hoogte)
  25. DIM b$(1 TO LEN(Volgorde$))
  26. DIM Kleur(0 TO Breedte - 1, 0 TO Hoogte - 1)
  27.  
  28. FOR p = 1 TO LEN(Volgorde$)
  29.     b$(p) = MID$(Volgorde$, p, 1)
  30.  
  31. LINE (320, 0)-(320, 320)
  32. LINE (0, 320)-(320, 320)
  33.  
  34. _PUTIMAGE (0, 0), Afbeelding&
  35.  
  36. FOR y = 0 TO Hoogte - 1
  37.     FOR x = 0 TO Breedte - 1
  38.         t = t + 1
  39.         w&(t) = POINT(x, y)
  40.         v&(t) = (_RED32(w&(t)) \ 4) + 256 * (_GREEN32(w&(t)) \ 4) + 65536 * (_BLUE32(w&(t)) \ 4)
  41.     NEXT
  42.  
  43. FOR j = 1 TO t
  44.     Uniek = 1
  45.     FOR m = 1 TO j - 1
  46.         IF v&(j) = v&(m) THEN Uniek = 0
  47.     NEXT
  48.     IF Uniek = 1 THEN
  49.         AantalKleuren = AantalKleuren + 1
  50.         p&(AantalKleuren + 31) = v&(j)
  51.     END IF
  52.  
  53. LOCATE 22, 1: PRINT "Aantal gedetecteerde Kleuren:"; AantalKleuren
  54.  
  55. IF AantalKleuren > LEN(Volgorde$) THEN: LOCATE 22, 1: PRINT "Sorry, maximaal"; LEN(Volgorde$); " verschillende Kleuren per afbeelding. Druk een toets...": SLEEP: SYSTEM
  56.  
  57. SCREEN _NEWIMAGE(640, 480, 256)
  58.  
  59. LINE (320, 0)-(320, 320)
  60. LINE (0, 320)-(320, 320)
  61.  
  62. OPEN Stam$ + "Data.bas" FOR OUTPUT AS #1
  63.  
  64. PRINT #1, "DATA " + LTRIM$(STR$(AantalKleuren))
  65. PRINT #1, "DATA " + LEFT$(Volgorde$, AantalKleuren)
  66.  
  67. WHILE k < AantalKleuren
  68.     k = k + 1
  69.     t$ = t$ + LTRIM$(RTRIM$(STR$(p&(k + 31))))
  70.     t$ = t$ + ","
  71. t$ = LEFT$(t$, LEN(t$) - 1)
  72. t$ = "DATA " + t$
  73. PRINT #1, t$
  74.  
  75. FOR Kleurkanaal = 32 TO 32 + AantalKleuren - 1
  76.     PALETTE Kleurkanaal, p&(Kleurkanaal)
  77.     ch(p&(Kleurkanaal)) = Kleurkanaal
  78.  
  79. FOR y = 0 TO Hoogte - 1
  80.     FOR x = 0 TO Breedte - 1
  81.         Teller = Teller + 1
  82.         Kleur(x, y) = ch(v&(Teller))
  83.         PSET (x, y), Kleur(x, y)
  84.     NEXT
  85.  
  86. FOR y = 0 TO Hoogte - 1
  87.     a$ = "DATA "
  88.     FOR x = 0 TO Breedte - 1
  89.         a$ = a$ + b$(POINT(x, y) - 31)
  90.     NEXT
  91.     PRINT #1, a$
  92. PRINT #1, "DATA $$$"
  93.  
  94.  
  95. LOCATE 22, 1: PRINT Stam$ + "Data.bas weggeschreven."
  96. LOCATE 24, 1: PRINT "Druk een toets om af te sluiten.";: SLEEP: SYSTEM
  97.  

Title: Re: Convert pictures to 256-colour embedded sprites
Post by: SMcNeill on October 29, 2019, 09:33:06 pm
If you look at my SaveImage library, the latest version has a converter to convert to 256 colors for images, as well as PNG, JPG, BMP, GIF image export ability.
Title: Re: Convert pictures to 256-colour embedded sprites
Post by: TrialAndTerror on October 29, 2019, 10:08:24 pm
I'm new to GitHub but I'll surely have a look at it, SMcNeill, thanks!
I'm specially interested in the 'embedding' part of it, also looking for
a way to embed .WAV or .OGG data in a program.

Is this the good place to ask simple questions BTW?

T&T
Title: Re: Convert pictures to 256-colour embedded sprites
Post by: SMcNeill on October 29, 2019, 11:51:09 pm
I'm new to GitHub but I'll surely have a look at it, SMcNeill, thanks!
I'm specially interested in the 'embedding' part of it, also looking for
a way to embed .WAV or .OGG data in a program.

Is this the good place to ask simple questions BTW?

T&T

Embedding images can be done with the ImageToData routines here: https://www.qb64.org/forum/index.php?topic=1633.0

If you look in the toolbox section of the forum, there’s code up which you can use to embed any data you need into your programs.

Feel free to ask any question you want; just be patient if it takes me a bit to answer them.  Halloween time is always a crazy time here on the farm, and I don’t get to visit the forums as much as I’d like until the holiday passes on by.  ;)