Ooops
November 25, 2020, 05:32:16 AM

Author Topic: [bb] Any File to b64 BB data statements by Andy_A [ 1+ years ago ]  (Read 575 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Any File to b64 BB data statements
Author : Andy_A
Posted : 1+ years ago

Description : Creates a .bb file of data statements which can be posted with your source code. The created .bb file can then be run to save the encoded image or zip to disk.
Pros:
   Encodes files in a more compact format than decimal data and hex data.
   Decimal data statements use ~3.57 bytes to encode 1 byte from file.
   Hex data statements use ~2 bytes to encode 1 byte of data from file
   b64 data statements use ~1.33 bytes to encode 1 byte of data from file
Fairly fast, I encoded a 45K jpg in 3 seconds (reading the input file, encoding the data, writing data to output file)

Cons:
   Large loader code adds ~950 bytes to data
Too slow for realtime use


Code :
Code: BlitzBasic
  1. ;Any file to base64 data statements by Andy_A
  2. ;2013.01.30
  3.  
  4. AppTitle "Any file to b64 data statements"
  5.  
  6. ;==============================================================================
  7. Const sizeOfLine = 60 ;<-- change number of bytes encoded per line of data here
  8.  
  9.  
  10. Const loader = 1 ;<-- change to zero for data output ONLY, else add loader code
  11.  
  12.  
  13. BlitzPlus = 1 ;<-- Change to zero if using Blitz Classic or Blitz3D
  14.  
  15.  
  16. ;Change 'Restore' label for data statements here
  17. dataLabel$ = "imgData" ;<-- ** DO NOT **  include leading period/stop
  18. ;==============================================================================
  19.  
  20. If BlitzPlus Then
  21.         file$ = RequestFile("Load file to convert to data...","*",False)
  22.         If file$ = "" Then RuntimeError "File Load aborted..."
  23. Else
  24.         file$ = "blocks.png" ;<-- blocks from Blitzanoid, change to your file here (B3D)
  25. End If
  26.  
  27. ;Read the file to convert
  28. filein = ReadFile(file$)
  29. ;Create and fill bank with file data   
  30. size = FileSize(file$)
  31.  
  32. ;name the output file
  33. If BlitzPlus Then
  34.         datafile$ = RequestFile("Save data as...","bb",True,"dataFile.bb")
  35.         If datafile$ = "" Then RuntimeError "File save aborted."
  36. Else
  37.         datafile$ = "blocks_png.bb" ;<-- change to your output name here (B3D)
  38. End If
  39. fileOut = WriteFile(datafile$)
  40. fullLines = Floor(size/sizeOfLine )
  41. rm = size - (fullLines * sizeOfLine )
  42. q$ = Chr$(34)
  43. char$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  44. Dim k(63)
  45. For i = 0 To 63
  46.         k(i) = Asc(Mid(char$,i+1,1))   
  47. Next
  48.  
  49. st = MilliSecs()
  50. If loader = 1 Then
  51.         WriteLine(fileOut,"Restore "+dataLabel$+":Read p$,s,t:o=WriteFile(p):n=Floor(s/t):If(n*t)<s Then n=n+1")
  52.         WriteLine(fileOut,"For i=1To n:Read d$:z$=z+d64(d):For j=1To Len(z)")
  53.         WriteLine(fileOut,"WriteByte(o,Asc(Mid(z,j,1))):Next:z="+q$+q$+":Next:CloseFile o:End")
  54.         WriteLine(fileOut,"Function d64$(d$): s$="+q$+q$+":rm=0:L=Len(d):If Right(d,1)="+q$+"="+q$+"Then rm=2")
  55.         WriteLine(fileOut,"r$="+q$+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"+q$)
  56.         WriteLine(fileOut,"If Right(d,2)="+q$+"=="+q$+"Then rm=1")
  57.         WriteLine(fileOut,"If rm>0Then m=L-5Else m=L-1")
  58.         WriteLine(fileOut,"For i=1To m Step 4:w=Instr(r,Mid(d,i,1))-1:x=Instr(r,Mid(d,i+1,1))-1")
  59.         WriteLine(fileOut,"y=Instr(r,Mid(d,i+2,1))-1:z=Instr(r,Mid(d,i+3,1))-1")
  60.         WriteLine(fileOut,"a=(w Shl 2)+((x And 48)Shr 4):b=((x And 15)Shl 4)+((y And 60)Shr 2)")
  61.         WriteLine(fileOut,"c=((y And 3)Shl 6)+z:s=s+Chr(a)+Chr(b)+Chr(c):Next:If rm=1Then")
  62.         WriteLine(fileOut,"w=Instr(r,Mid(d,L-3,1))-1:x=Instr(r,Mid(d,L-2,1))-1")
  63.         WriteLine(fileOut,"a=(w Shl 2)+((x And 48)Shr 4):s=s+Chr(a):EndIf:If rm=2Then")
  64.         WriteLine(fileOut,"w=Instr(r,Mid(d,L-3,1))-1:x=Instr(r,Mid(d,L-2,1))-1")
  65.         WriteLine(fileOut,"y=Instr(r,Mid(d,L-1,1))-1:a=(w Shl 2)+((x And 48) Shr 4)")
  66.         WriteLine(fileOut,"b=((x And 15) Shl 4)+((y And 60)Shr 2):s=s+Chr(a)+Chr(b):EndIf")
  67.         WriteLine(fileOut,"Return s:End Function")
  68. End If
  69.  
  70. WriteLine(fileout,"."+dataLabel$)
  71. WriteLine(fileout,"Data"+q$+file$+q$+","+size+","+sizeOfLine)
  72. For i = 1 To size-rm-1 Step sizeOfLine
  73.         a1$ = ""
  74.         For j = 1 To sizeOfLine
  75.                 a1$ = a1$ + Chr(ReadByte(filein))
  76.         Next
  77.         WriteLine(fileOut, "Data" + q$ + e64(a1$) + q$ )
  78. Next
  79. If rm > 0 Then
  80.         a1$ = ""
  81.         For i = 1 To rm
  82.                 a1$ = a1$ + Chr(ReadByte(filein))
  83.         Next
  84.         WriteLine(fileOut, "Data" + q$ + e64(a1$) + q$)
  85. End If
  86.  
  87. CloseFile(filein)
  88. CloseFile(fileout)
  89. et = MilliSecs()-st
  90. Print "ET: "+et
  91.  
  92. WaitKey()
  93. End
  94.  
  95. Function e64$(e$)
  96.         f$ = ""
  97.         g = Len(e$)
  98.         rm = g- Floor(g/3)*3
  99.         For i = 1 To g-rm-1 Step 3
  100.                 a = Asc(Mid$(e$,i  ,1))
  101.                 b = Asc(Mid$(e$,i+1,1))
  102.                 c = Asc(Mid$(e$,i+2,1))
  103.                 w = (a And 252) Shr 2
  104.                 x = ((a And 3) Shl 4) + ( (b And 240) Shr 4)
  105.                 y = ((b And 15) Shl 2) + ( (c And 192) Shr 6)
  106.                 z = c And 63
  107.                 f$ = f$ + Chr$(k(w)) + Chr$(k(x)) +Chr$(k(y)) +Chr$(k(z))
  108.         Next
  109.         If rm = 1 Then
  110.                 a = Asc(Right(e$,1))
  111.                 w = (a And 252) Shr 2
  112.                 x = (a And 3) Shl 4
  113.                 f$ = f$ + Chr(k(w)) + Chr(k(x)) + "=="
  114.         End If
  115.         If rm = 2 Then
  116.                 a = Asc(Mid(e$,g-1,1))
  117.                 b = Asc(Right(e$,1))
  118.                 w = (a And 252) Shr 2
  119.                 x = (a And 3) Shl 4
  120.                 x = x + ( (b And 240) Shr 4)
  121.                 y = (b And 15) Shl 2
  122.                 f$ = f$ + Chr(k(w))+Chr(k(x))+Chr(k(y)) + "="
  123.         End If
  124.         Return f$
  125. End Function


Comments :


Andy_A(Posted 1+ years ago)

 Here's an encoded png file (2K) ready to save to disk:
Code: [Select]
;LOADER code ================================================================
Restore imgData:Read p$,s,t:o=WriteFile(p):n=Floor(s/t):If(n*t)<s Then n=n+1
For i=1To n:Read d$:z$=z+d64(d):For j=1To Len(z)
WriteByte(o,Asc(Mid(z,j,1))):Next:z="":Next:CloseFile o;:End <- comment out the 'End' statement
Function d64$(d$): s$="":rm=0:L=Len(d):If Right(d,1)="="Then rm=2
r$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
If Right(d,2)="=="Then rm=1
If rm>0Then m=L-5Else m=L-1
For i=1To m Step 4:w=Instr(r,Mid(d,i,1))-1:x=Instr(r,Mid(d,i+1,1))-1
y=Instr(r,Mid(d,i+2,1))-1:z=Instr(r,Mid(d,i+3,1))-1
a=(w Shl 2)+((x And 48)Shr 4):b=((x And 15)Shl 4)+((y And 60)Shr 2)
c=((y And 3)Shl 6)+z:s=s+Chr(a)+Chr(b)+Chr(c):Next:If rm=1Then
w=Instr(r,Mid(d,L-3,1))-1:x=Instr(r,Mid(d,L-2,1))-1
a=(w Shl 2)+((x And 48)Shr 4):s=s+Chr(a):EndIf:If rm=2Then
w=Instr(r,Mid(d,L-3,1))-1:x=Instr(r,Mid(d,L-2,1))-1
y=Instr(r,Mid(d,L-1,1))-1:a=(w Shl 2)+((x And 48) Shr 4)
b=((x And 15) Shl 4)+((y And 60)Shr 2):s=s+Chr(a)+Chr(b):EndIf
Return s:End Function
;============================================================================

;add your display code here
Graphics 640,480,32,2
SetBuffer BackBuffer()

image = LoadImage("blocks.png")
DrawImage image,(640-ImageWidth(image))/2, (480-ImageHeight(image))/2
Flip

WaitMouse()
FreeImage image
End

;generated b64 data statements
.imgData
Data "blocks.png",2216,99
Data "iVBORw0KGgoAAAANSUhEUgAAAOAAAAAgCAIAAACEp+MCAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgA"
Data "ABdwnLpRPAAAAAlwSFlzAAALMgAACzIBQPVfaAAACBFJREFUeF7dmT9sHUUQxvdF0CBFAYmQPKM4doFEAQUSFFS0aSheR5UKpARRUkGTwkh5IoWpIqVEKLIQxZPS0aRxYQRK"
Data "XgGiQQJZwVYQBVYUSEjw8b039mS9Mzc373Yv/nMane729r7b2/3d3O5MrxoOA7a1tcne2Obnw9xc2NgI6+sNNXfVFjc3cfjJsMJ+3CTfnw+n5sLdjbDpkL89VfthcxH7fx9d"
Data "nJxsNz2gNx96c6HaCJXjAVO1xVcn7b/429XJC4VbDd0TXp4LpzbC3fXwe2P/rIXbqLO5OMZ++NOjif7jpu4/FuaOhY3tsL7dKB/WJpJh841J/zz46Ffs/2vqnmO73bPt6J7H"
Data "U7XXp/1/ffUr7O/cuWM368SJE8ePH793797W1lbjC5Da0tLSpCYAHYVgGHqx6verwWCy7/VwKg2XkkKULIQAuzas+mFk2JkwfrtffTiosH+lV+FUGi4lhSh5NizAqkfD0dfB"
Data "sPF3ofqnXz0cTPZ/93AqDZeSQpQsnA2wYXU1jE5aNj7dr94cVB9g36vOhvFpabiUFE5KFp6BDe9X4cuRZTfH/T+rwVaFfe+PKtwcS8OlpHBScmYB9mBYXQ8jw1bDeKtf3R9U"
Data "2P/Vq3AqDZeSQpTMhwXY6urqe+Z24cKFK1eurKysYH/p0i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+51SBm2N32rOIw7mvf8D5zQ8aMiOeWUAAAAASUVORK5CYII="



virtlands(Posted 1+ years ago)

 It's fascinating Base64 encoding.  Storing it in strings was a great idea.Did you know that if you include too many megabytes of "Data " statements that it crashes the IDE ?


Andy_A(Posted 1+ years ago)

 Yes, quite aware however, I would never recommend creating a file greater than 200K for personal use as there are better methods for including large files with your executable.As for posting stuff on the web to avoid 404's later on down the road, I would recommend keeping to 6K or less as a max. <mini-rant>Remember, it's very rude and inconsiderate to waste someone else's bandwidth with huge images or encoded images for that matter. <end-rant>Base 64 provided the best encoding efficiency of all the simple algo's I reviewed. There are more efficient algo's but require more code for re-assembling the original file from data statements. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal