November 25, 2020, 05:24:00 AM

Author Topic: [bb] Pic-2-DATA utility by Kevin_ [ 1+ years ago ]  (Read 621 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Pic-2-DATA utility by Kevin_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Pic-2-DATA utility
Author : Kevin_
Posted : 1+ years ago

Description : Just a little util I knocked up ages ago that allows you to include images in your source as DATA statements.  The DATA is stored as shortened Hex values but the resulting file can still be very large.

Only use for icons/sprites etc...

Regards

Prof


Code :
Code: BlitzBasic
  1. ; Pic-2-Data by Prof - Include images in your source as DATA statements
  2. ;
  3. ; Only use for small images like sprites / icons etc.
  4. ;
  5. ; WARNING! -  Even though this program converts each pixel and stores it
  6. ; as a shortened Hex ARGB value, (i.e. $0000FF00 is stored as $FF00),
  7. ; the file size can still be very large.
  8. ;
  9. ; The program also gives you an option to save the decoder in the
  10. ; source code as well.
  11. ;
  12. ;
  13. ; Main Window area
  14. WinWidth=320:WinHeight=200
  15. WinTitle$=WinTitle$+"Pic2Data v1.4"
  16. WinX=(ClientWidth(Desktop())-WinWidth)/2
  17. WinY=(ClientHeight(Desktop())-WinHeight)/2
  18. Global Window = CreateWindow(WinTitle$,WinX,WInY,WinWidth,WinHeight,0,1)
  19.  
  20. ;Create a canvas
  21. Global canvas=CreateCanvas( 0,0,GadgetWidth(Window),GadgetHeight(Window),Window)
  22. SetGadgetLayout canvas,1,0,1,0
  23. SetBuffer CanvasBuffer(canvas)
  24. ClsColor 0,0,0:Cls
  25. Color 200,200,200:
  26. Text 75,20,"Pic2Data V1.4"
  27. Text 15,50,"Designed By The Prof (c)2003"
  28. Text 35,80,"For Blitzers Everywhere!"
  29.  
  30.  
  31. ;vars
  32. Global font=LoadFont("Arial",14,False,False,False)
  33. SetFont font
  34. Global ImageID=0
  35. Global ImageLoaded=False
  36. Global PicWidth,PicHeight
  37. Global ButtonHeight=20,ButtonWidth=60
  38.  
  39. ;Create some buttons
  40. ButtonY=1
  41. ReadMe=CreateButton("Read Me",GadgetWidth(canvas)-ButtonWidth-7,ButtonY,ButtonWidth,ButtonHeight,Canvas,1)
  42. ButtonY=ButtonY+ButtonHeight+1
  43. Load=CreateButton("Load Pic",GadgetWidth(canvas)-ButtonWidth-7,ButtonY,ButtonWidth,ButtonHeight,Canvas,1)
  44. ButtonY=ButtonY+ButtonHeight+1
  45. Save=CreateButton("Save Data",GadgetWidth(canvas)-ButtonWidth-7,ButtonY,ButtonWidth,ButtonHeight,Canvas,1)
  46. ButtonY=ButtonY+ButtonHeight+1
  47. Quit=CreateButton("- Exit -",GadgetWidth(canvas)-ButtonWidth-7,ButtonY,ButtonWidth,ButtonHeight,Canvas,1)
  48.  
  49.  
  50. ;Main loop
  51. While WaitEvent()<>$803
  52.   ID=EventID()
  53.   If ID=$401
  54.      If EventSource()=Quit Then ExitProgram
  55.      If EventSource()=ReadMe Then ReadMe
  56.      If EventSource()=Load Then LoadFile
  57.      If EventSource()=Save
  58.         If ImageLoaded=True
  59.            SaveAsHexData
  60.         Else
  61.            Notify("Please load an image first")
  62.         EndIf
  63.      EndIf
  64.   EndIf
  65. Wend
  66. FreeGadget(Window)
  67. End
  68.  
  69. ; ************************************
  70.  
  71. Function SaveAsHexData()
  72.   f$=RequestFile("Save as DATA file (.bb)","bb", True)
  73.   If f$<>""
  74.      Decoder=Confirm("Save the decoder as well?")
  75.      FileOut=WriteFile(f$) ; open a file to write to
  76.      If Decoder=True Then WriteHexDecoder(FileOut)
  77.  
  78.      ; Write out width & height of the image first!
  79.      WriteLine(FileOut,"Data "+Str(PicWidth)+","+Str(PicHeight))
  80.  
  81.      ; Now encode the image & write to the file
  82.      CurrentLineLength=0:MaxLineLength=10
  83.      TempLine$=""
  84.      For y=0 To PicHeight-1
  85.          For x=0 To PicWidth-1
  86.              GetColor(x,y)
  87.              r=ColorRed():g=ColorGreen():b=ColorBlue()
  88.              rgb=GetRGB(r,g,b)
  89.              TempRGB$=Str(Hex$(rgb))
  90.              RealRGB$="$"+StripLeftZerosFromHexString(TempRGB$)
  91.              
  92.              If CurrentLineLength=0
  93.                 TempLine$="Data "
  94.              EndIf
  95.              TempLine$=TempLine$+RealRGB$
  96.              CurrentLineLength=CurrentLineLength+1
  97.              If CurrentLineLength<MaxLineLength
  98.                 TempLine$=TempLine$+"," ; add a comma to the last blue
  99.              EndIf
  100.              If CurrentLineLength=MaxLineLength
  101.                 WriteLine(FileOut,TempLine$)
  102.                 CurrentLineLength=0:TempLine$=""
  103.              EndIf
  104.          Next
  105.      Next
  106.      If CurrentLineLength>0 ; write out the last line
  107.         L=Len(TempLine$)
  108.         TempLine$=Mid$(TempLine$,1,L-1); get rid of last comma
  109.         WriteLine(FileOut,TempLine$)
  110.      EndIf
  111.      CloseFile(FileOut)
  112.   EndIf
  113. End Function
  114.  
  115. ; *****************************************
  116.  
  117. Function GetRGB(red,green,blue)
  118.         Return blue Or (green Shl 8) Or (red Shl 16)
  119. End Function
  120.  
  121. ; *****************************************
  122.  
  123. Function StripLeftZerosFromHexString$(v$)
  124.   l=Len(v$):t$="":pos=1
  125.  
  126.   ; Go through the string until not a zero
  127.   c$=Mid$(v$,Pos,1)
  128.   While c$="0" And Pos<L
  129.     Pos=Pos+1
  130.     c$=Mid$(v$,Pos,1)
  131.   Wend
  132.  
  133.   ; Now create the new string from this position
  134.   For P=Pos To L
  135.      c$=Mid$(v$,P,1)
  136.      t$=t$+c$
  137.   Next
  138.   Return t$
  139. End Function
  140.  
  141. ; *****************************************
  142.  
  143. Function LoadFile()
  144.   f$=RequestFile$("Load an image file (bmp,jpg,png)","bmp,jpg,png")
  145.   If f$<>""
  146.      Cls:ImageID=LoadImage(f$)
  147.      PicWidth=ImageWidth(ImageID)
  148.      PicHeight=ImageHeight(ImageID)
  149.      Viewport 0,0,GadgetWidth(Canvas)-ButtonWidth-10,GadgetHeight(Canvas)-35
  150.      DrawBlock ImageID,0,0:Color 200,200,200
  151.      Viewport 0,0,GadgetWidth(Window),GadgetHeight(Window)
  152.  
  153.      If PicWidth>(GadgetWidth(Canvas)-ButtonWidth-10)
  154.         PicWidth=(GadgetWidth(Canvas)-ButtonWidth-10)
  155.      EndIf
  156.      If PicHeight>GadgetHeight(Canvas)-35
  157.         PicHeight=GadgetHeight(Canvas)-35
  158.      EndIf
  159.  
  160.      Pixels=PicWidth*PicHeight
  161.      SizeOriginal=FileSize(f$)
  162.  
  163.      Text GadgetWidth(canvas)-ButtonWidth-7,85,"Image Size"
  164.      Text GadgetWidth(canvas)-ButtonWidth-7,95,Str(PicWidth)+" x "+Str(PicHeight)
  165.      Text GadgetWidth(canvas)-ButtonWidth-7,115,"File Size"
  166.      Text GadgetWidth(canvas)-ButtonWidth-7,125,Str(SizeOriginal)
  167.      
  168.      ImageLoaded=True
  169.   EndIf
  170. End Function
  171.  
  172. ; ******************************************
  173.  
  174. Function ReadMe()
  175.  t$="Pic2Data By The Prof (c)2003"+Chr$(13)+Chr$(13)
  176.  t$=t$+"Converts an image into DATA statements"+Chr$(13)
  177.  t$=t$+"so you can include them in Blitz Basic."+Chr$(13)+Chr$(13)
  178.  t$=t$+"The resulting file is VERY LARGE! Only useful for"+Chr$(13)
  179.  t$=t$+"things like sprites/icons (max 50x50 pixels)"+Chr$(13)+Chr$(13)
  180.  t$=t$+"TIP: ensure desktop is set to 32-Bit colour and"+Chr$(13)
  181.  t$=t$+"only convert images that are small in size."+Chr$(13)
  182.  Notify(t$)
  183. End Function
  184.  
  185. ; ******************************************
  186.  
  187. Function ExitProgram()
  188.   q=Confirm("Do you really want to quit?")
  189.   If q=True
  190.      FreeGadget(Window):End
  191.   EndIf
  192. End Function
  193.  
  194. ; ******************************************
  195.  
  196. Function WriteHexDecoder(FileOut)
  197.   WriteLine(FileOut,"Graphics 320,200,32,2")
  198.   WriteLine(FileOut,";")
  199.   WriteLine(FileOut,"DisplayImage(0,0)")
  200.   WriteLine(FileOut,"Waitkey():End")
  201.   WriteLine(FileOut,";")
  202.   WriteLine(FileOut,";*****************************")
  203.   WriteLine(FileOut,"Function DisplayImage(StartX,StartY)")
  204.   WriteLine(FileOut,"  Read Width,Height")
  205.   WriteLine(FileOut,"  For Y=StartY To StartY+(Height-1)")
  206.   WriteLine(FileOut,"    For X=StartX To StartX+(Width-1)")
  207.   WriteLine(FileOut,"      Read rgb")
  208.   WriteLine(FileOut,"      r=GetRed(rgb):g=GetGreen(rgb):b=GetBlue(rgb)")
  209.   WriteLine(FileOut,"      Color r,g,b:Plot x,y")
  210.   WriteLine(FileOut,"    Next")
  211.   WriteLine(FileOut,"  Next")
  212.   WriteLine(FileOut,"End Function")
  213.   WriteLine(FileOut,";")
  214.   WriteLine(FileOut,"Function GetRed(rgb)")
  215.   WriteLine(FileOut,"  Return rgb Shr 16 And %11111111")
  216.   WriteLine(FileOut,"End Function")
  217.   WriteLine(FileOut,";")
  218.   WriteLine(FileOut,"Function GetGreen(rgb)")
  219.   WriteLine(FileOut,"  Return rgb Shr 8 And %11111111")
  220.   WriteLine(FileOut,"End Function")
  221.   WriteLine(FileOut,";")
  222.   WriteLine(FileOut,"Function GetBlue(rgb)")
  223.   WriteLine(FileOut,"  Return rgb And %11111111")
  224.   WriteLine(FileOut,"End Function")
  225.   WriteLine(FileOut,";")
  226.   WriteLine(FileOut,";Use 'Restore' and a .Label if using more images.")
  227. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal