Ooops
October 28, 2021, 13:33:09

Author Topic: [bb] LoadWebImage by BlitzSupport [ 1+ years ago ]  (Read 1559 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] LoadWebImage by BlitzSupport [ 1+ years ago ]
« on: June 29, 2017, 00:28:43 »
Title : LoadWebImage
Author : BlitzSupport
Posted : 1+ years ago

Description : This is pretty cool! You can load an image stored on your web server (or from any site really), and use it in your game :)

Very easily adapted for other kinds of files, of course!

Keeping up with latest BlitzGet function (progress readout while loading, etc)!

[EDITED, yikes, 10 year old post... to fix example URL!]


Code :
Code: BlitzBasic
  1. ; -----------------------------------------------------------------------------
  2. ; LoadWebImage -- uses BlitzGet Deluxe, based on Mark Sibly's HTTPGet
  3. ; -----------------------------------------------------------------------------
  4. ; james@hi-toro.com
  5. ; -----------------------------------------------------------------------------
  6.  
  7. AppTitle "LoadWebImage"
  8.  
  9. Graphics 640, 480
  10.  
  11. SetBuffer BackBuffer ()
  12.  
  13. ; -----------------------------------------------------------------------------
  14. ; Load an image from the web, straight into our game!
  15. ; -----------------------------------------------------------------------------
  16. rocket = LoadWebImage ("http://www.hi-toro.com/boing.png")
  17.  
  18. ; -----------------------------------------------------------------------------
  19. ; Check for failure
  20. ; -----------------------------------------------------------------------------
  21. If rocket = 0
  22.  
  23.         RuntimeError "Failed to load web image!": End
  24.        
  25.         ; Alternative (BETTER) failure method -- use a default local image supplied with your game...
  26.         ; rocket = LoadImage ("rocket.bmp")
  27.        
  28. EndIf
  29.  
  30. MaskImage rocket, 255, 0, 255
  31.  
  32. x = 50
  33. y = 50
  34.  
  35. ClsColor 70, 110, 190
  36.  
  37. Repeat
  38.  
  39.         Cls
  40.        
  41.         DrawImage rocket, MouseX (), MouseY ()
  42.  
  43.         Flip
  44.  
  45. Until KeyDown (1) = 1
  46.  
  47. End
  48.  
  49. Function LoadWebImage (webFile$)
  50.         If BlitzGet (webFile$, CurrentDir (), "temp_web_image.bmp")
  51.                 image = LoadImage ("temp_web_image.bmp")
  52.                 DeleteFile "temp_web_image.bmp"
  53.         EndIf
  54.         Return image
  55. End Function
  56.  
  57. Function BlitzGet (webFile$, saveDir$, saveFile$)
  58.  
  59.         ; -------------------------------------------------------------------------
  60.         ; Strip "http://" if provided
  61.         ; -------------------------------------------------------------------------
  62.         If Left (webFile$, 7) = "http://" Then webFile$ = Right (webFile$, Len (webFile$) - 7)
  63.  
  64.         ; -------------------------------------------------------------------------
  65.         ; Split into hostname and path/filename to download
  66.         ; -------------------------------------------------------------------------
  67.         slash = Instr (webFile$, "/")
  68.         If slash
  69.                 webHost$ = Left (webFile$, slash - 1)
  70.                 webFile$ = Right (webFile$, Len (webFile$) - slash + 1)
  71.         Else
  72.                 webHost$ = webFile$
  73.                 webFile$ = "/"
  74.         EndIf
  75.                
  76.         ; -------------------------------------------------------------------------
  77.         ; Add trailing slash to download dir if not given
  78.         ; -------------------------------------------------------------------------
  79.         If Right (saveDir$, 1) <> "" Then saveDir$ = saveDir$ + ""
  80.  
  81.         ; -------------------------------------------------------------------------
  82.         ; Save filename -- get from webFile$ if not provided
  83.         ; -------------------------------------------------------------------------
  84.         If saveFile$ = ""
  85.                 If webFile = "/"
  86.                         saveFile$ = "Unknown file.txt"
  87.                 Else
  88.                         For findSlash = Len (webFile$) To 1 Step - 1
  89.                                 testForSlash$ = Mid (webFile$, findSlash, 1)
  90.                                 If testForSlash$ = "/"
  91.                                         saveFile$ = Right (webFile$, Len (webFile$) - findSlash)
  92.                                         Exit
  93.                                 EndIf
  94.                         Next
  95.                         If saveFile$ = "" Then saveFile$ = "Unknown file.txt"
  96.                 EndIf
  97.         EndIf
  98.  
  99.         ; DEBUG
  100.         ; RuntimeError "Web host: " + webHost$ + Chr (10) + "Web file: " + webFile$ + Chr (10) + "Save dir: " + saveDir$ + Chr (10) + "Save file: " + saveFile$
  101.  
  102.         www = OpenTCPStream (webHost$, 80)
  103.  
  104.         If www
  105.        
  106.                 WriteLine www, "GET " + webFile$ + " HTTP/1.1" ; GET / gets default page...
  107.                 WriteLine www, "Host: " + webHost$
  108.                 WriteLine www, "User-Agent: BlitzGet Deluxe"
  109.                 WriteLine www, "Accept: */*"
  110.                 WriteLine www, ""
  111.                
  112.                 ; ---------------------------------------------------------------------
  113.                 ; Find blank line after header data, where the action begins...
  114.                 ; ---------------------------------------------------------------------
  115.                                
  116.                 Repeat
  117.                         header$ = ReadLine (www)
  118.                         If Left (header$, 16) = "Content-Length: "      ; Number of bytes to read
  119.                                 bytesToRead = Right (header$, Len (header$) - 16)
  120.                         EndIf
  121.                 Until header$ = "" Or (Eof (www))
  122.                
  123.                 If bytesToRead = 0 Then Goto skipDownLoad
  124.                
  125.                 ; ---------------------------------------------------------------------
  126.                 ; Create new file to write downloaded bytes into
  127.                 ; ---------------------------------------------------------------------
  128.                 save = WriteFile (saveDir$ + saveFile$)
  129.                 If Not save Then Goto skipDownload
  130.  
  131.                 ; ---------------------------------------------------------------------
  132.                 ; Incredibly complex download-to-file routine...
  133.                 ; ---------------------------------------------------------------------
  134.  
  135.                 For readWebFile = 1 To bytesToRead
  136.                
  137.                         If Not Eof (www) Then WriteByte save, ReadByte (www)
  138.                        
  139.                         ; Call BytesReceived with position and size every 100 bytes (slows down a LOT with smaller updates)
  140.                        
  141.                         tReadWebFile = readWebFile
  142.                         If tReadWebFile Mod 100 = 0 Then BytesReceived (readWebFile, bytesToRead)
  143.  
  144.                 Next
  145.  
  146.                 CloseFile save
  147.                
  148.                 ; Fully downloaded?
  149.                 If (readWebFile - 1) = bytesToRead
  150.                         success = 1
  151.                 EndIf
  152.                
  153.                 ; Final update (so it's not rounded to nearest 100 bytes!)
  154.                 BytesReceived (bytesToRead, bytesToRead)
  155.                
  156.                 .skipDownload
  157.                 CloseTCPStream www
  158.                
  159.         Else
  160.        
  161.                 RuntimeError "Failed to connect"
  162.                
  163.         EndIf
  164.        
  165.         Return success
  166.        
  167. End Function
  168.  
  169. ; -----------------------------------------------------------------------------
  170. ; User-defined update function, called every 100 bytes of download -- alter to suit!
  171. ; -----------------------------------------------------------------------------
  172. ; TIP: Pass a user-defined type instead, with all data (this stuff plus URL, local filename, etc)
  173. ; -----------------------------------------------------------------------------
  174. Function BytesReceived (posByte, totalBytes)
  175.         ; Example update code...
  176.         Cls
  177.         Text 20, 20, "Downloading file -- please wait..."
  178.         Text 20, 40, "Received: " + posByte + "/" + totalBytes + " bytes (" + Percent (posByte, totalBytes) + "%)"
  179.         Flip
  180. End Function
  181.  
  182. ; -----------------------------------------------------------------------------
  183. ; Handy percentage function
  184. ; -----------------------------------------------------------------------------
  185. Function Percent (part#, total#)
  186.         Return Int (100 * (part / total))
  187. End Function


Comments :


jfk EO-11110(Posted 1+ years ago)

 Edit - oops, wrong button :o)


xlsior(Posted 1+ years ago)

 Alternative to load an image from a website:<div class="quote"> img:TImage = LoadImage(LoadBank("http::blitzbasic.com/img/brllogo-thin.png")) </div>


Pineapple(Posted 1+ years ago)

 First time I found this, I thought I'd post a LoadWebImage function I made a while back that I'm actually quite proud of. It's slow, messy, and mostly uncommented, but it gets the job done. I was able to learn a lot about the .bmp file format making this and how many worthless bytes is has at the beginning of the file. Oh, and I forgot to mention that it supports only the .bmp file format. You can track loading prograss in the debuglog if you like.
Code: [Select]
Graphics 800,600,0,2
SetBuffer BackBuffer()
wwwimg=LoadWebImage("http://www.d10.karoo.net/ruby/quiz/50/duck.bmp")
Repeat
Cls
DrawImage wwwimg,0,0
If KeyDown(1) Then End
Flip
Forever

Function LoadWebImage(url$)
Local tcp,file=0,pos=7,server$=""
Local bmp=1:png=2:jpg=3:gif=4

DebugLog"Finding server..."
Repeat
pos=pos+1
found=1
If Mid(url$,pos,1)<>"/" Then found=0:server$=server$+Mid(url$,pos,1)
Until found=1
DebugLog"Server detected at: "+server


;connect
DebugLog"Connecting to server..."
tcp=OpenTCPStream(server$,80)

;ok?
If tcp Then
DebugLog"Connection to server successful."

;request file
DebugLog"Requesting file..."
WriteLine tcp,"GET "+url$
WriteLine tcp,Chr$(10)
DebugLog"File retrieved."

If ReadByte(tcp)=Asc("B") And ReadByte(tcp)=Asc("M") Then DebugLog"Image is a .bmp":file=bmp
If file=0 Then
SeekFile tcp,0
If ReadByte(tcp)=137 And ReadByte(tcp)=80 And ReadByte(tcp)=78 And ReadByte(tcp)=71 And ReadByte(tcp)=13 And ReadByte(tcp)=10 And ReadByte(tcp)=26 And ReadByte(tcp)=10 Then
DebugLog"Image is a .png":file=1
EndIf
EndIf


If file=0 Then RuntimeError"Online data is not a supported image file format."

If file=bmp Then
For worthlessjunk=1 To 16
dumpdata=ReadByte(tcp)
Next
ms=MilliSecs()
iw=ReadInt(tcp):DebugLog"Image width is: "+iw
ih=ReadInt(tcp):DebugLog"Image height is: "+ih
;make output image
img=CreateImage(iw,ih)
SetBuffer ImageBuffer(img)
worthless=ReadShort(tcp)
worthless=ReadShort(tcp)
useful=ReadShort(tcp)
If useful<>0 Then RuntimeError("Sorry, but this bmp format is not supported.")
For dump=1 To 22
garbage=ReadByte(tcp)
Next

Repeat
If KeyHit(1) Then End

For rgb=1 To 3
Select rgb
Case 1:b=ReadByte(tcp)
Case 2:g=ReadByte(tcp)
Case 3:r=ReadByte(tcp):Color r,g,b:Plot x,ih-y-1:x=x+1:If x=iw Then x=0:y=y+1
End Select
Next

Until Eof(tcp)
EndIf

;ok
SetBuffer BackBuffer():DebugLog"Download successful!":Return img

Else
DebugLog"Connection to server failed."
EndIf
End Function

WaitKey()
End


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal