qoi.bmx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ' Copyright (c) 2021 Bruce A Henderson
  2. '
  3. ' Permission is hereby granted, free of charge, to any person obtaining a copy
  4. ' of this software and associated documentation files (the "Software"), to deal
  5. ' in the Software without restriction, including without limitation the rights
  6. ' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. ' copies of the Software, and to permit persons to whom the Software is
  8. ' furnished to do so, subject to the following conditions:
  9. '
  10. ' The above copyright notice and this permission notice shall be included in
  11. ' all copies or substantial portions of the Software.
  12. '
  13. ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. ' THE SOFTWARE.
  20. '
  21. SuperStrict
  22. Rem
  23. bbdoc: Qoi Image Loading/Saving
  24. End Rem
  25. Module Image.Qoi
  26. ModuleInfo "Version: 1.00"
  27. ModuleInfo "License: MIT"
  28. ModuleInfo "Copyright: Wrapper - 2021 Bruce A Henderson"
  29. ModuleInfo "History: 1.00"
  30. ModuleInfo "History: Initial Release."
  31. Import BRL.Pixmap
  32. Import Pub.StdC
  33. Import "common.bmx"
  34. Rem
  35. bbdoc: Qoi image format files.
  36. about: See https://qoiformat.org/ for more information.
  37. End Rem
  38. Type TQoiImage
  39. Rem
  40. bbdoc: Loads Qoi image from a file as a #TPixmap.
  41. End Rem
  42. Function Load:TPixmap(file:String, channels:Int = 0)
  43. Local stream:TStream = ReadStream(file)
  44. If stream Then
  45. Local pix:TPixmap = Load(stream, channels)
  46. stream.Close()
  47. Return pix
  48. End If
  49. End Function
  50. Rem
  51. bbdoc: Loads Qoi image from stream as a #TPixmap.
  52. End Rem
  53. Function Load:TPixmap(stream:TStream, channels:Int = 0)
  54. Local desc:SQoiDesc
  55. Local data:Byte[] = LoadByteArray(stream)
  56. Local pixels:Byte Ptr = qoi_decode(data, data.Length, desc, channels)
  57. If pixels Then
  58. Local format:Int = PF_RGB888
  59. Local align:Int = 3
  60. If desc.channels = 4 Then
  61. format = PF_RGBA8888
  62. align = 4
  63. End If
  64. Local pix:TPixmap = TPixmap.Create(desc.width, desc.height, format, align)
  65. MemCopy(pix.pixels, pixels, Size_T(desc.width * desc.height * desc.channels))
  66. free_(pixels)
  67. Return pix
  68. End If
  69. End Function
  70. Rem
  71. bbdoc: Saves pixmap to a #TStream as Qoi image.
  72. End Rem
  73. Function Save(stream:TStream, pix:TPixmap)
  74. Local desc:SQoiDesc
  75. desc.width = pix.width
  76. desc.height = pix.height
  77. desc.colorspace = EQoiColorspace.LINEAR
  78. If pix.format <> PF_RGB888 And pix.format <> PF_RGBA8888 Then
  79. pix = pix.Convert(PF_STDFORMAT)
  80. End If
  81. Select pix.format
  82. Case PF_RGB888
  83. desc.channels = 3
  84. Case PF_RGBA8888
  85. desc.channels = 4
  86. End Select
  87. Local data:Byte Ptr
  88. Try
  89. Local outLen:Int
  90. data = qoi_encode(pix.pixels, desc, outLen)
  91. stream.WriteBytes(data, outLen)
  92. Finally
  93. If data Then
  94. free_(data)
  95. End If
  96. End Try
  97. End Function
  98. End Type
  99. Private
  100. Type TPixmapLoaderQoi Extends TPixmapLoader
  101. Method LoadPixmap:TPixmap( stream:TStream ) Override
  102. Return TQoiImage.Load(stream)
  103. End Method
  104. End Type
  105. New TPixmapLoaderQoi