March 01, 2021, 11:11:39 PM

Author Topic: [bb] ImageToBank, BankToImage functions by Perturbatio [ 1+ years ago ]  (Read 418 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : ImageToBank, BankToImage functions
Author : Perturbatio
Posted : 1+ years ago

Description : A simple example of loading from and storing images in banks

Code :
Code: BlitzBasic
  1. Graphics 640,480
  2. SetBuffer BackBuffer()
  3.  
  4. bBank = CreateBank(5)
  5. iImage = LoadImage("testin.png")
  6.  
  7. ImageToBank(bBank, iImage)
  8.  
  9. newImage = BankToImage(bBank)
  10. DrawImage newImage, 0,0
  11.  
  12.  
  13. Flip
  14. WaitKey()
  15.  
  16. FreeBank bBank
  17. FreeImage newImage
  18. FreeImage iImage
  19. End
  20.  
  21.  
  22. ;;;;;;;;;;;;;;;;;;;;;;;;
  23. ; Function ImageToBank ;
  24. ;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;Accepts a bank which will be resized to store the data, and an image to store,
  26. ;optional parameter is the AlphaFlag (currently irrelevant)
  27. ;Returns the error code.
  28. Function ImageToBank(bankImage, bufferImage, iAlphaFlag=0)
  29. bufOldBuffer = GraphicsBuffer();get the handle for the current buffer
  30. Error = 0; Error 0 = no error, error 1 = image size invalid
  31.  
  32. ;Check the image dimensions, if invalid then exit function with errorcode 1
  33. If (ImageWidth(bufferImage)<1) Or (ImageHeight(bufferImage)<1) Then
  34.         Error = 1
  35.         Goto EndBufferToBank
  36. EndIf
  37.  
  38. ;get the total number of bytes the image makes up
  39. SizeOfImage = ImageWidth(bufferImage)*ImageHeight(bufferImage)
  40.  
  41. ResizeBank bankImage, (SizeOfImage*4) + 9 ;(4 bytes per int for the image info + 9 extra bytes for header)
  42.  
  43. PokeInt bankImage,0,ImageWidth(bufferImage);store the width in the bank
  44. PokeInt bankImage,4,ImageHeight(bufferImage);store the height
  45.  
  46. ;store other info in byte 8 (the ninth byte)
  47. iByte8 = %00000000
  48. If iAlphaFlag Then iByte8 = iByte8 Or %00000001 ; OR the first bit to 1 if it has an alpha mask
  49.  
  50. PokeByte bankImage,8,iByte8 ;store iByte8
  51.  
  52. ;DebugLog "Byte 8: " + iByte8
  53.  
  54. iBankPointer = 9
  55. SetBuffer ImageBuffer(bufferImage)
  56. LockBuffer ImageBuffer(bufferImage)
  57. For iLoopX = 0 To ImageWidth(bufferImage)-1
  58.         For iLoopY = 0 To ImageHeight(bufferImage)-1
  59.                 PokeInt bankImage,ibankPointer, ReadPixelFast(iLoopX,iLoopY)
  60.                 iBankPointer = iBankPointer + 4;increment by 4 each time since it is an int we are poking
  61.         Next
  62. Next
  63. UnlockBuffer ImageBuffer(bufferImage)
  64. SetBuffer bufOldBuffer ; restore the buffer
  65.  
  66. .EndBufferToBank
  67. Return Error
  68. End Function
  69.  
  70.  
  71. ;;;;;;;;;;;;;;;;;;;;;;;;
  72. ; Function BankToImage ;
  73. ;;;;;;;;;;;;;;;;;;;;;;;;
  74. ; Accepts a bank that has previously had its' data set with ImageToBank
  75. ; Returns an image
  76. Function BankToImage(bankImage)
  77. bufOldBuffer = GraphicsBuffer()
  78. iWidth = PeekInt(bankImage,0)
  79. iHeight = PeekInt(bankImage,4)
  80. iiByte8 = PeekByte(bankImage,5)
  81. ;get the length of the data block
  82. SizeOfData = iWidth*iHeight
  83. imgReturn = CreateImage(iWidth,iHeight)
  84.  
  85. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  86. ;Insert code To process iByte8
  87. ; NOTE FOR ALPHA: if ((BIT0) And %00000001) = 1 then (process alpha info)
  88. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  89.  
  90. iBankPointer = 9
  91.  
  92. SetBuffer ImageBuffer(imgReturn)
  93. LockBuffer ImageBuffer(imgReturn)
  94. For iLoopX = 0 To ImageWidth(imgReturn)-1
  95.         For iLoopY = 0 To ImageHeight(imgReturn)-1
  96.                 WritePixelFast(iLoopX,iLoopY,PeekInt(bankImage,iBankPointer))
  97.                 iBankPointer = iBankPointer + 4;increment by 4 each time since it is an int we are peekin
  98.         Next
  99. Next
  100. UnlockBuffer ImageBuffer(imgReturn)
  101. SetBuffer bufOldBuffer
  102.  
  103.  
  104. Return imgReturn
  105.  
  106.  
  107. End Function
  108.  
  109.  
  110. ; The following is a description of the format in which the information is stored in the bank
  111. ; -------------------------------------------------------------------------------------------
  112. ;
  113. ; Bytes 0-3 contains the width of the image (4-byte integer) must not be less than 1
  114. ; Bytes 4-7 contains the height of the image (4-byte integer) must not be less than 1
  115. ; Bit 0 of Byte 8 describes whether the image has an alpha mask (0=no, 1=yes)
  116. ;               may contain more information at a later date
  117. ;
  118. ; NOTE FOR ALPHA: if ((BIT0) And %00000001) = 1 then (process alpha info)
  119. ;
  120. ; Bytes onward from 9 descrive the image as integers, because the length of this can be calculated
  121. ; by multiplying the width by the height, there is no need for a terminator


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal