stbimageloader.bmx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. SuperStrict
  2. Rem
  3. bbdoc: Graphics/Stb Image loader
  4. about:
  5. The stb image loader module provides the ability to load different image format #pixmaps.
  6. Supported formats include, BMP, PSD, TGA, GIF, HDR, PIC and PNM
  7. End Rem
  8. Module BRL.StbImageLoader
  9. ModuleInfo "Version: 1.00"
  10. ModuleInfo "Author: Bruce A Henderson"
  11. ModuleInfo "License: zlib/libpng"
  12. ModuleInfo "Copyright: Bruce A Henderson"
  13. ModuleInfo "History: 1.00"
  14. ModuleInfo "History: Initial Release."
  15. Import Pub.StbImage
  16. Import BRL.Pixmap
  17. Import "glue.c"
  18. Extern
  19. Function bmx_stbi_load_image:Byte Ptr(cb:Object, width:Int Ptr, height:Int Ptr, channels:Int Ptr)
  20. End Extern
  21. Type TPixmapLoaderSTB Extends TPixmapLoader
  22. Method LoadPixmap:TPixmap( stream:TStream ) Override
  23. Local pixmap:TPixmap
  24. Local cb:TStbioCallbacks = New TStbioCallbacks
  25. cb.stream = stream
  26. Local width:Int, height:Int, channels:Int
  27. Local imgPtr:Byte Ptr = bmx_stbi_load_image(cb, Varptr width, Varptr height, Varptr channels)
  28. If imgPtr Then
  29. Local pf:Int
  30. Select channels
  31. Case STBI_grey
  32. pf = PF_I8
  33. Case STBI_rgb
  34. pf = PF_RGB888
  35. Case STBI_rgb_alpha
  36. pf = PF_RGBA8888
  37. Case STBI_grey_alpha
  38. pixmap = CreatePixmap( width,height,PF_RGBA8888 )
  39. Local src:Byte Ptr = imgPtr
  40. Local dst:Byte Ptr = pixmap.pixels
  41. For Local y:Int = 0 Until height
  42. For Local x:Int = 0 Until width
  43. Local a:Int=src[0]
  44. Local i:Int=src[1]
  45. dst[0] = i
  46. dst[1] = i
  47. dst[2] = i
  48. dst[3] = a
  49. src:+2
  50. dst:+4
  51. Next
  52. Next
  53. End Select
  54. If pf
  55. pixmap = CreatePixmap( width, height, pf )
  56. MemCopy(pixmap.pixels, imgPtr, Size_T(width * height * BytesPerPixel[pf]))
  57. End If
  58. stbi_image_free(imgPtr)
  59. End If
  60. Return pixmap
  61. End Method
  62. End Type
  63. Type TStbioCallbacks
  64. Field stream:TStream
  65. Method Read:Int(buffer:Byte Ptr, size:Int)
  66. Return stream.Read(buffer, size)
  67. End Method
  68. Method Skip(n:Int)
  69. stream.Seek(SEEK_CUR_, n)
  70. End Method
  71. Method Eof:Int()
  72. Return stream.Eof()
  73. End Method
  74. Function _Read:Int(cb:TStbioCallbacks, buffer:Byte Ptr, size:Int) { nomangle }
  75. Return cb.Read(buffer, size)
  76. End Function
  77. Function _Skip(cb:TStbioCallbacks, n:Int) { nomangle }
  78. cb.Skip(n)
  79. End Function
  80. Function _Eof:Int(cb:TStbioCallbacks) { nomangle }
  81. Return cb.Eof()
  82. End Function
  83. End Type
  84. New TPixmapLoaderSTB