gif.bmx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ' Copyright (c) 2022 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. ' 2. Altered source versions must be plainly marked as such, and must not be
  16. ' misrepresented as being the original software.
  17. ' 3. This notice may not be removed or altered from any source distribution.
  18. '
  19. SuperStrict
  20. Rem
  21. bbdoc: Image/GIF loader
  22. about:
  23. The GIF loader module provides the ability to load GIF format #pixmaps.
  24. End Rem
  25. Module Image.GIF
  26. Import BRL.Max2D
  27. Import Image.Stb
  28. Rem
  29. bbdoc: A GIF image.
  30. End Rem
  31. Type TGifImage
  32. Field imgPtr:Byte Ptr
  33. Field delays:Int Ptr
  34. Field w:Int
  35. Field h:Int
  36. Field layers:Int
  37. Field comp:Int
  38. Function Load:TGifImage(url:Object)
  39. Local stream:TStream=ReadStream( url )
  40. If Not stream Throw New TStreamReadException
  41. Local gif:TGifImage = New TGifImage()
  42. Local cb:TStbioCallbacks = New TStbioCallbacks
  43. cb.stream = stream
  44. gif.imgPtr = bmx_stbi_load_gif(cb, Varptr gif.delays, gif.w, gif.h, gif.layers, gif.comp, 0)
  45. If Not gif.imgPtr Then
  46. Return Null
  47. End If
  48. Return gif
  49. EndFunction
  50. Method ToPixmap:TPixmap(layer:Int)
  51. Local pitch:Int = w * comp
  52. Local size:Size_T = w * h * comp
  53. If layer > layers
  54. layer = layers-1
  55. ElseIf layer < 0
  56. layer = 0
  57. EndIf
  58. Local pixmap:TPixmap = CreatePixmap( w, h, PF_RGBA8888, 4 )
  59. MemCopy( pixmap.pixels, imgPtr + pitch * h * layer, size )
  60. Return pixmap
  61. EndMethod
  62. Method ToImage:TImage(flags:Int=-1)
  63. Local pitch:Int = w * comp
  64. Local size:Size_T = w * h * comp
  65. Local image:TImage = CreateImage(w, h, layers, flags)
  66. For Local i:Int = 0 Until layers
  67. Local pixmap:TPixmap = CreatePixmap( w, h, PF_RGBA8888, 4 )
  68. MemCopy( pixmap.pixels, imgPtr + pitch * h * i, size )
  69. image.SetPixmap(i, pixmap, delays[i])
  70. Next
  71. stbi_image_free(imgPtr)
  72. bmx_stbi_free_delays(Varptr delays)
  73. Return image
  74. EndMethod
  75. Rem
  76. bbdoc: Loads one layer/frame of a GIF image as a #TPixmap.
  77. returns: A #TPixmap, or #Null if the image could not be loaded.
  78. EndRem
  79. Function LoadPixmap:TPixmap(url:Object, layer:Int=0)
  80. Local gif:TGifImage = TGifImage.Load(url)
  81. If Not gif
  82. Return Null
  83. EndIf
  84. Return gif.ToPixmap(layer)
  85. EndFunction
  86. Rem
  87. bbdoc: Loads a GIF image as a #TImage.
  88. returns: A #TImage, or #Null if the image could not be loaded.
  89. about: For animated GIF images, the #TImage will be created with the appropriate frames and frame durations.
  90. End Rem
  91. Function LoadImage:TImage(url:Object, flags:Int=-1)
  92. Local gif:TGifImage = TGifImage.Load(url)
  93. If Not gif
  94. Return Null
  95. EndIf
  96. Return gif.ToImage(flags)
  97. EndFunction
  98. Method Free()
  99. If imgPtr
  100. stbi_image_free(imgPtr)
  101. EndIf
  102. If delays
  103. bmx_stbi_free_delays(Varptr delays)
  104. EndIf
  105. EndMethod
  106. Method Delete()
  107. Free()
  108. EndMethod
  109. End Type