stb.bmx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ' Copyright (c) 2022-2024 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. Module Image.STB
  21. ModuleInfo "Version: 1.06"
  22. ModuleInfo "Author: Sean Barrett and contributers (see stb_image.h)"
  23. ModuleInfo "License: ZLib/PNG License"
  24. ModuleInfo "Credit: Adapted for BlitzMax by Bruce A Henderson"
  25. ModuleInfo "History: 1.06"
  26. ModuleInfo "History: Update to stb_image 2.30"
  27. ModuleInfo "History: Update to stb_image_resize2 2.10"
  28. ModuleInfo "History: Fixed gif build."
  29. ModuleInfo "History: 1.05"
  30. ModuleInfo "History: Update to stb_image 2.29"
  31. ModuleInfo "History: 1.04"
  32. ModuleInfo "History: Update to stb_image 2.28"
  33. ModuleInfo "History: 1.03"
  34. ModuleInfo "History: Update to stb_image 2.27"
  35. ModuleInfo "History: 1.02"
  36. ModuleInfo "History: Update to stb_image 2.19"
  37. ModuleInfo "History: 1.01"
  38. ModuleInfo "History: Update to stb_image 2.16"
  39. ModuleInfo "History: 1.00"
  40. ModuleInfo "History: Initial Release. stb_image 2.13"
  41. Import BRL.Pixmap
  42. Import "common.bmx"
  43. ' Notes
  44. ' stbi__compute_y_16() and stbi__convert_format16() need aditional "&& defined(STBI_NO_PNM)"
  45. Private
  46. Type TPixmapLoaderSTB Extends TPixmapLoader
  47. Method LoadPixmap:TPixmap( stream:TStream ) Override
  48. Local pixmap:TPixmap
  49. Local cb:TStbioCallbacks = New TStbioCallbacks
  50. cb.stream = stream
  51. Local width:Int, height:Int, channels:Int
  52. Local imgPtr:Byte Ptr = bmx_stbi_load_image(cb, Varptr width, Varptr height, Varptr channels)
  53. If imgPtr Then
  54. Local pf:Int
  55. Select channels
  56. Case STBI_grey
  57. pf = PF_I8
  58. Case STBI_rgb
  59. pf = PF_RGB888
  60. Case STBI_rgb_alpha
  61. pf = PF_RGBA8888
  62. Case STBI_grey_alpha
  63. pixmap = CreatePixmap( width,height,PF_RGBA8888 )
  64. Local src:Byte Ptr = imgPtr
  65. Local dst:Byte Ptr = pixmap.pixels
  66. For Local y:Int = 0 Until height
  67. For Local x:Int = 0 Until width
  68. Local a:Int=src[0]
  69. Local i:Int=src[1]
  70. dst[0] = i
  71. dst[1] = i
  72. dst[2] = i
  73. dst[3] = a
  74. src:+2
  75. dst:+4
  76. Next
  77. Next
  78. End Select
  79. If pf Then
  80. pixmap = CreatePixmap( width, height, pf )
  81. MemCopy(pixmap.pixels, imgPtr, Size_T(width * height * BytesPerPixel[pf]))
  82. End If
  83. stbi_image_free(imgPtr)
  84. End If
  85. Return pixmap
  86. End Method
  87. End Type
  88. Public
  89. Type TStbioCallbacks
  90. Field stream:TStream
  91. Method Read:Int(buffer:Byte Ptr, size:Int)
  92. Return stream.Read(buffer, size)
  93. End Method
  94. Method Skip(n:Int)
  95. stream.Seek(SEEK_CUR_, n)
  96. End Method
  97. Method Eof:Int()
  98. Return stream.Eof()
  99. End Method
  100. Function _Read:Int(cb:TStbioCallbacks, buffer:Byte Ptr, size:Int) { nomangle }
  101. Return cb.Read(buffer, size)
  102. End Function
  103. Function _Skip(cb:TStbioCallbacks, n:Int) { nomangle }
  104. cb.Skip(n)
  105. End Function
  106. Function _Eof:Int(cb:TStbioCallbacks) { nomangle }
  107. Return cb.Eof()
  108. End Function
  109. End Type
  110. Private
  111. New TPixmapLoaderSTB